Skip to content

Commit 6635005

Browse files
committed
Specify of_trait in Target::Impl.
1 parent 0fb279b commit 6635005

File tree

11 files changed

+184
-163
lines changed

11 files changed

+184
-163
lines changed

compiler/rustc_hir/src/target.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum Target {
4141
Union,
4242
Trait,
4343
TraitAlias,
44-
Impl,
44+
Impl { of_trait: bool },
4545
Expression,
4646
Statement,
4747
Arm,
@@ -86,7 +86,7 @@ impl Target {
8686
| Target::Union
8787
| Target::Trait
8888
| Target::TraitAlias
89-
| Target::Impl
89+
| Target::Impl { .. }
9090
| Target::Expression
9191
| Target::Statement
9292
| Target::Arm
@@ -119,7 +119,7 @@ impl Target {
119119
ItemKind::Union(..) => Target::Union,
120120
ItemKind::Trait(..) => Target::Trait,
121121
ItemKind::TraitAlias(..) => Target::TraitAlias,
122-
ItemKind::Impl { .. } => Target::Impl,
122+
ItemKind::Impl(imp_) => Target::Impl { of_trait: imp_.of_trait.is_some() },
123123
}
124124
}
125125

@@ -141,7 +141,7 @@ impl Target {
141141
DefKind::Union => Target::Union,
142142
DefKind::Trait => Target::Trait,
143143
DefKind::TraitAlias => Target::TraitAlias,
144-
DefKind::Impl { .. } => Target::Impl,
144+
DefKind::Impl { of_trait } => Target::Impl { of_trait },
145145
_ => panic!("impossible case reached"),
146146
}
147147
}
@@ -196,7 +196,8 @@ impl Target {
196196
Target::Union => "union",
197197
Target::Trait => "trait",
198198
Target::TraitAlias => "trait alias",
199-
Target::Impl => "implementation block",
199+
Target::Impl { of_trait: false } => "inherent implementation block",
200+
Target::Impl { of_trait: true } => "trait implementation block",
200201
Target::Expression => "expression",
201202
Target::Statement => "statement",
202203
Target::Arm => "match arm",

compiler/rustc_passes/src/check_attr.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
338338
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
339339
}
340340
[sym::automatically_derived, ..] => {
341-
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
341+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl{of_trait:true})
342342
}
343343
[sym::proc_macro, ..] => {
344344
self.check_proc_macro(hir_id, target, ProcMacroKind::FunctionLike)
@@ -479,7 +479,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
479479
attr: &Attribute,
480480
item: Option<ItemLike<'_>>,
481481
) {
482-
if !matches!(target, Target::Impl)
482+
if !matches!(target, Target::Impl { .. })
483483
|| matches!(
484484
item,
485485
Some(ItemLike::Item(hir::Item { kind: hir::ItemKind::Impl(_impl),.. }))
@@ -583,7 +583,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
583583
Target::Fn
584584
| Target::Closure
585585
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
586-
| Target::Impl
586+
| Target::Impl { .. }
587587
| Target::Mod => return,
588588

589589
// These are "functions", but they aren't allowed because they don't
@@ -974,9 +974,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
974974
let span = meta.span();
975975
if let Some(location) = match target {
976976
Target::AssocTy => {
977-
let parent_def_id = self.tcx.hir_get_parent_item(hir_id).def_id;
978-
let containing_item = self.tcx.hir_expect_item(parent_def_id);
979-
if Target::from_item(containing_item) == Target::Impl {
977+
if let DefKind::Impl { .. } =
978+
self.tcx.def_kind(self.tcx.local_parent(hir_id.owner.def_id))
979+
{
980980
Some("type alias in implementation block")
981981
} else {
982982
None
@@ -999,7 +999,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
999999
| Target::Arm
10001000
| Target::ForeignMod
10011001
| Target::Closure
1002-
| Target::Impl
1002+
| Target::Impl { .. }
10031003
| Target::WherePredicate => Some(target.name()),
10041004
Target::ExternCrate
10051005
| Target::Use
@@ -1575,7 +1575,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15751575
let article = match target {
15761576
Target::ExternCrate
15771577
| Target::Enum
1578-
| Target::Impl
1578+
| Target::Impl { .. }
15791579
| Target::Expression
15801580
| Target::Arm
15811581
| Target::AssocConst

compiler/rustc_passes/src/lang_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ impl<'ast, 'tcx> visit::Visitor<'ast> for LanguageItemCollector<'ast, 'tcx> {
287287
ast::ItemKind::Union(..) => Target::Union,
288288
ast::ItemKind::Trait(_) => Target::Trait,
289289
ast::ItemKind::TraitAlias(..) => Target::TraitAlias,
290-
ast::ItemKind::Impl(_) => Target::Impl,
290+
ast::ItemKind::Impl(imp_) => Target::Impl { of_trait: imp_.of_trait.is_some() },
291291
ast::ItemKind::MacroDef(..) => Target::MacroDef,
292292
ast::ItemKind::MacCall(_) | ast::ItemKind::DelegationMac(_) => {
293293
unreachable!("macros should have been expanded")

tests/rustdoc-ui/check-doc-alias-attr-location.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ error: `#[doc(alias = "...")]` isn't allowed on foreign module
44
LL | #[doc(alias = "foo")]
55
| ^^^^^^^^^^^^^
66

7-
error: `#[doc(alias = "...")]` isn't allowed on implementation block
7+
error: `#[doc(alias = "...")]` isn't allowed on inherent implementation block
88
--> $DIR/check-doc-alias-attr-location.rs:10:7
99
|
1010
LL | #[doc(alias = "bar")]
1111
| ^^^^^^^^^^^^^
1212

13-
error: `#[doc(alias = "...")]` isn't allowed on implementation block
13+
error: `#[doc(alias = "...")]` isn't allowed on trait implementation block
1414
--> $DIR/check-doc-alias-attr-location.rs:16:7
1515
|
1616
LL | #[doc(alias = "foobar")]

tests/ui/attributes/positions/used.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ error: attribute must be applied to a `static` variable
2828
LL | #[used]
2929
| ^^^^^^^
3030
LL | impl Bar for Foo {}
31-
| ------------------- but this is a implementation block
31+
| ------------------- but this is a trait implementation block
3232

3333
error: attribute must be applied to a `static` variable
3434
--> $DIR/used.rs:21:5

tests/ui/feature-gates/issue-43106-gating-of-builtin-attrs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,13 @@ mod automatically_derived {
269269
#[automatically_derived] type T = S;
270270
//~^ WARN `#[automatically_derived]
271271

272+
#[automatically_derived] trait W { }
273+
//~^ WARN `#[automatically_derived]
274+
272275
#[automatically_derived] impl S { }
276+
//~^ WARN `#[automatically_derived]
277+
278+
#[automatically_derived] impl W for S { }
273279
}
274280

275281
#[no_mangle]

0 commit comments

Comments
 (0)