Skip to content

Commit f8e6617

Browse files
authored
Rollup merge of #100029 - hdelc:master, r=cjgillot
Prevent ICE for `doc_alias` on match arm, statement, expression Fixes #99777. This is a pretty minimal fix that should be safe, since rustdoc doesn't generate documentation for match arms, statements, or expressions. I mentioned in the linked issue that the `doc_alias` target checking should probably be improved to avoid future ICEs, but as a new contributor, I'm not confident enough with the HIR types to make a larger change.
2 parents 02fcec2 + 2be0094 commit f8e6617

File tree

7 files changed

+109
-60
lines changed

7 files changed

+109
-60
lines changed

compiler/rustc_hir/src/target.rs

+45-45
Original file line numberDiff line numberDiff line change
@@ -60,51 +60,7 @@ pub enum Target {
6060

6161
impl Display for Target {
6262
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63-
write!(
64-
f,
65-
"{}",
66-
match *self {
67-
Target::ExternCrate => "extern crate",
68-
Target::Use => "use",
69-
Target::Static => "static item",
70-
Target::Const => "constant item",
71-
Target::Fn => "function",
72-
Target::Closure => "closure",
73-
Target::Mod => "module",
74-
Target::ForeignMod => "foreign module",
75-
Target::GlobalAsm => "global asm",
76-
Target::TyAlias => "type alias",
77-
Target::OpaqueTy => "opaque type",
78-
Target::Enum => "enum",
79-
Target::Variant => "enum variant",
80-
Target::Struct => "struct",
81-
Target::Field => "struct field",
82-
Target::Union => "union",
83-
Target::Trait => "trait",
84-
Target::TraitAlias => "trait alias",
85-
Target::Impl => "item",
86-
Target::Expression => "expression",
87-
Target::Statement => "statement",
88-
Target::Arm => "match arm",
89-
Target::AssocConst => "associated const",
90-
Target::Method(kind) => match kind {
91-
MethodKind::Inherent => "inherent method",
92-
MethodKind::Trait { body: false } => "required trait method",
93-
MethodKind::Trait { body: true } => "provided trait method",
94-
},
95-
Target::AssocTy => "associated type",
96-
Target::ForeignFn => "foreign function",
97-
Target::ForeignStatic => "foreign static item",
98-
Target::ForeignTy => "foreign type",
99-
Target::GenericParam(kind) => match kind {
100-
GenericParamKind::Type => "type parameter",
101-
GenericParamKind::Lifetime => "lifetime parameter",
102-
GenericParamKind::Const => "const parameter",
103-
},
104-
Target::MacroDef => "macro def",
105-
Target::Param => "function param",
106-
}
107-
)
63+
write!(f, "{}", Self::name(*self))
10864
}
10965
}
11066

@@ -185,4 +141,48 @@ impl Target {
185141
hir::GenericParamKind::Const { .. } => Target::GenericParam(GenericParamKind::Const),
186142
}
187143
}
144+
145+
pub fn name(self) -> &'static str {
146+
match self {
147+
Target::ExternCrate => "extern crate",
148+
Target::Use => "use",
149+
Target::Static => "static item",
150+
Target::Const => "constant item",
151+
Target::Fn => "function",
152+
Target::Closure => "closure",
153+
Target::Mod => "module",
154+
Target::ForeignMod => "foreign module",
155+
Target::GlobalAsm => "global asm",
156+
Target::TyAlias => "type alias",
157+
Target::OpaqueTy => "opaque type",
158+
Target::Enum => "enum",
159+
Target::Variant => "enum variant",
160+
Target::Struct => "struct",
161+
Target::Field => "struct field",
162+
Target::Union => "union",
163+
Target::Trait => "trait",
164+
Target::TraitAlias => "trait alias",
165+
Target::Impl => "implementation block",
166+
Target::Expression => "expression",
167+
Target::Statement => "statement",
168+
Target::Arm => "match arm",
169+
Target::AssocConst => "associated const",
170+
Target::Method(kind) => match kind {
171+
MethodKind::Inherent => "inherent method",
172+
MethodKind::Trait { body: false } => "required trait method",
173+
MethodKind::Trait { body: true } => "provided trait method",
174+
},
175+
Target::AssocTy => "associated type",
176+
Target::ForeignFn => "foreign function",
177+
Target::ForeignStatic => "foreign static item",
178+
Target::ForeignTy => "foreign type",
179+
Target::GenericParam(kind) => match kind {
180+
GenericParamKind::Type => "type parameter",
181+
GenericParamKind::Lifetime => "lifetime parameter",
182+
GenericParamKind::Const => "const parameter",
183+
},
184+
Target::MacroDef => "macro def",
185+
Target::Param => "function param",
186+
}
187+
}
188188
}

compiler/rustc_passes/src/check_attr.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,6 @@ impl CheckAttrVisitor<'_> {
596596

597597
let span = meta.span();
598598
if let Some(location) = match target {
599-
Target::Impl => Some("implementation block"),
600-
Target::ForeignMod => Some("extern block"),
601599
Target::AssocTy => {
602600
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
603601
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
@@ -619,7 +617,34 @@ impl CheckAttrVisitor<'_> {
619617
}
620618
// we check the validity of params elsewhere
621619
Target::Param => return false,
622-
_ => None,
620+
Target::Expression
621+
| Target::Statement
622+
| Target::Arm
623+
| Target::ForeignMod
624+
| Target::Closure
625+
| Target::Impl => Some(target.name()),
626+
Target::ExternCrate
627+
| Target::Use
628+
| Target::Static
629+
| Target::Const
630+
| Target::Fn
631+
| Target::Mod
632+
| Target::GlobalAsm
633+
| Target::TyAlias
634+
| Target::OpaqueTy
635+
| Target::Enum
636+
| Target::Variant
637+
| Target::Struct
638+
| Target::Field
639+
| Target::Union
640+
| Target::Trait
641+
| Target::TraitAlias
642+
| Target::Method(..)
643+
| Target::ForeignFn
644+
| Target::ForeignStatic
645+
| Target::ForeignTy
646+
| Target::GenericParam(..)
647+
| Target::MacroDef => None,
623648
} {
624649
tcx.sess.emit_err(errors::DocAliasBadLocation { span, attr_str, location });
625650
return false;

src/test/rustdoc-ui/check-doc-alias-attr-location.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: `#[doc(alias = "...")]` isn't allowed on extern block
1+
error: `#[doc(alias = "...")]` isn't allowed on foreign module
22
--> $DIR/check-doc-alias-attr-location.rs:7:7
33
|
44
LL | #[doc(alias = "foo")]

src/test/ui/feature-gates/issue-43106-gating-of-builtin-attrs.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ note: the lint level is defined here
212212
LL | #![warn(unused_attributes, unknown_lints)]
213213
| ^^^^^^^^^^^^^^^^^
214214

215-
warning: `#[automatically_derived]` only has an effect on items
215+
warning: `#[automatically_derived]` only has an effect on implementation blocks
216216
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:266:1
217217
|
218218
LL | #[automatically_derived]
@@ -515,25 +515,25 @@ warning: `#[path]` only has an effect on modules
515515
LL | #[path = "3800"] impl S { }
516516
| ^^^^^^^^^^^^^^^^
517517

518-
warning: `#[automatically_derived]` only has an effect on items
518+
warning: `#[automatically_derived]` only has an effect on implementation blocks
519519
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:269:17
520520
|
521521
LL | mod inner { #![automatically_derived] }
522522
| ^^^^^^^^^^^^^^^^^^^^^^^^^
523523

524-
warning: `#[automatically_derived]` only has an effect on items
524+
warning: `#[automatically_derived]` only has an effect on implementation blocks
525525
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:272:5
526526
|
527527
LL | #[automatically_derived] fn f() { }
528528
| ^^^^^^^^^^^^^^^^^^^^^^^^
529529

530-
warning: `#[automatically_derived]` only has an effect on items
530+
warning: `#[automatically_derived]` only has an effect on implementation blocks
531531
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:275:5
532532
|
533533
LL | #[automatically_derived] struct S;
534534
| ^^^^^^^^^^^^^^^^^^^^^^^^
535535

536-
warning: `#[automatically_derived]` only has an effect on items
536+
warning: `#[automatically_derived]` only has an effect on implementation blocks
537537
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:278:5
538538
|
539539
LL | #[automatically_derived] type T = S;
@@ -923,7 +923,7 @@ warning: `#[must_use]` has no effect when applied to a type alias
923923
LL | #[must_use] type T = S;
924924
| ^^^^^^^^^^^
925925

926-
warning: `#[must_use]` has no effect when applied to an item
926+
warning: `#[must_use]` has no effect when applied to an implementation block
927927
--> $DIR/issue-43106-gating-of-builtin-attrs.rs:614:5
928928
|
929929
LL | #[must_use] impl S { }

src/test/ui/lint/unused/unused_attributes-must_use.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ error: `#[must_use]` has no effect when applied to a static item
4545
LL | #[must_use]
4646
| ^^^^^^^^^^^
4747

48-
error: `#[must_use]` has no effect when applied to an item
48+
error: `#[must_use]` has no effect when applied to an implementation block
4949
--> $DIR/unused_attributes-must_use.rs:33:1
5050
|
5151
LL | #[must_use]
@@ -69,7 +69,7 @@ error: `#[must_use]` has no effect when applied to a type parameter
6969
LL | fn qux<#[must_use] T>(_: T) {}
7070
| ^^^^^^^^^^^
7171

72-
error: `#[must_use]` has no effect when applied to an item
72+
error: `#[must_use]` has no effect when applied to an implementation block
7373
--> $DIR/unused_attributes-must_use.rs:79:1
7474
|
7575
LL | #[must_use]

src/test/ui/rustdoc/check-doc-alias-attr-location.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ impl Foo for Bar {
2121
type X = i32;
2222
fn foo(#[doc(alias = "qux")] _x: u32) -> Self::X {
2323
//~^ ERROR
24-
0
24+
#[doc(alias = "stmt")] //~ ERROR
25+
let x = 0;
26+
#[doc(alias = "expr")] //~ ERROR
27+
match x {
28+
#[doc(alias = "arm")] //~ ERROR
29+
_ => 0
30+
}
2531
}
2632
}

src/test/ui/rustdoc/check-doc-alias-attr-location.stderr

+20-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: allow, cfg, cfg_attr, deny, expect, forbid, and warn are the only allowed
44
LL | fn foo(#[doc(alias = "qux")] _x: u32) -> Self::X {
55
| ^^^^^^^^^^^^^^^^^^^^^
66

7-
error: `#[doc(alias = "...")]` isn't allowed on extern block
7+
error: `#[doc(alias = "...")]` isn't allowed on foreign module
88
--> $DIR/check-doc-alias-attr-location.rs:9:7
99
|
1010
LL | #[doc(alias = "foo")]
@@ -28,5 +28,23 @@ error: `#[doc(alias = "...")]` isn't allowed on type alias in implementation blo
2828
LL | #[doc(alias = "assoc")]
2929
| ^^^^^^^^^^^^^^^
3030

31-
error: aborting due to 5 previous errors
31+
error: `#[doc(alias = "...")]` isn't allowed on statement
32+
--> $DIR/check-doc-alias-attr-location.rs:24:15
33+
|
34+
LL | #[doc(alias = "stmt")]
35+
| ^^^^^^^^^^^^^^
36+
37+
error: `#[doc(alias = "...")]` isn't allowed on expression
38+
--> $DIR/check-doc-alias-attr-location.rs:26:15
39+
|
40+
LL | #[doc(alias = "expr")]
41+
| ^^^^^^^^^^^^^^
42+
43+
error: `#[doc(alias = "...")]` isn't allowed on match arm
44+
--> $DIR/check-doc-alias-attr-location.rs:28:19
45+
|
46+
LL | #[doc(alias = "arm")]
47+
| ^^^^^^^^^^^^^
48+
49+
error: aborting due to 8 previous errors
3250

0 commit comments

Comments
 (0)