Skip to content

Commit bad798e

Browse files
committed
Specify of_trait in Target::Impl.
1 parent 855e0fe commit bad798e

File tree

10 files changed

+182
-161
lines changed

10 files changed

+182
-161
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
@@ -321,7 +321,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
321321
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Fn)
322322
}
323323
[sym::automatically_derived, ..] => {
324-
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl)
324+
self.check_generic_attr_unparsed(hir_id, attr, target, Target::Impl{of_trait:true})
325325
}
326326
[sym::proc_macro, ..] => {
327327
self.check_proc_macro(hir_id, target, ProcMacroKind::FunctionLike)
@@ -458,7 +458,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
458458
attr: &Attribute,
459459
item: Option<ItemLike<'_>>,
460460
) {
461-
if !matches!(target, Target::Impl)
461+
if !matches!(target, Target::Impl { .. })
462462
|| matches!(
463463
item,
464464
Some(ItemLike::Item(hir::Item { kind: hir::ItemKind::Impl(_impl),.. }))
@@ -562,7 +562,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
562562
Target::Fn
563563
| Target::Closure
564564
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
565-
| Target::Impl
565+
| Target::Impl { .. }
566566
| Target::Mod => return,
567567

568568
// These are "functions", but they aren't allowed because they don't
@@ -955,9 +955,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
955955
let span = meta.span();
956956
if let Some(location) = match target {
957957
Target::AssocTy => {
958-
let parent_def_id = self.tcx.hir_get_parent_item(hir_id).def_id;
959-
let containing_item = self.tcx.hir_expect_item(parent_def_id);
960-
if Target::from_item(containing_item) == Target::Impl {
958+
if let DefKind::Impl { .. } =
959+
self.tcx.def_kind(self.tcx.local_parent(hir_id.owner.def_id))
960+
{
961961
Some("type alias in implementation block")
962962
} else {
963963
None
@@ -980,7 +980,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
980980
| Target::Arm
981981
| Target::ForeignMod
982982
| Target::Closure
983-
| Target::Impl
983+
| Target::Impl { .. }
984984
| Target::WherePredicate => Some(target.name()),
985985
Target::ExternCrate
986986
| Target::Use
@@ -1556,7 +1556,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15561556
let article = match target {
15571557
Target::ExternCrate
15581558
| Target::Enum
1559-
| Target::Impl
1559+
| Target::Impl { .. }
15601560
| Target::Expression
15611561
| Target::Arm
15621562
| 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/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)