Skip to content

Commit 87a27f9

Browse files
committed
Specify of_trait in Target::Impl.
1 parent 9cd918b commit 87a27f9

File tree

12 files changed

+187
-165
lines changed

12 files changed

+187
-165
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/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ passes_only_has_effect_on =
560560
`#[{$attr_name}]` only has an effect on {$target_name ->
561561
[function] functions
562562
[module] modules
563-
[implementation_block] implementation blocks
563+
[trait_implementation_block] trait implementation blocks
564+
[inherent_implementation_block] inherent implementation blocks
564565
*[unspecified] (unspecified--this is a compiler bug)
565566
}
566567

compiler/rustc_passes/src/check_attr.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
161161
sym::automatically_derived,
162162
*attr_span,
163163
target,
164-
Target::Impl,
164+
Target::Impl { of_trait: true },
165165
),
166166
Attribute::Parsed(
167167
AttributeKind::Stability { span, .. }
@@ -492,7 +492,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
492492
attr: &Attribute,
493493
item: Option<ItemLike<'_>>,
494494
) {
495-
if !matches!(target, Target::Impl)
495+
if !matches!(target, Target::Impl { .. })
496496
|| matches!(
497497
item,
498498
Some(ItemLike::Item(hir::Item { kind: hir::ItemKind::Impl(_impl),.. }))
@@ -596,7 +596,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
596596
Target::Fn
597597
| Target::Closure
598598
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
599-
| Target::Impl
599+
| Target::Impl { .. }
600600
| Target::Mod => return,
601601

602602
// These are "functions", but they aren't allowed because they don't
@@ -985,9 +985,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
985985
let span = meta.span();
986986
if let Some(location) = match target {
987987
Target::AssocTy => {
988-
let parent_def_id = self.tcx.hir_get_parent_item(hir_id).def_id;
989-
let containing_item = self.tcx.hir_expect_item(parent_def_id);
990-
if Target::from_item(containing_item) == Target::Impl {
988+
if let DefKind::Impl { .. } =
989+
self.tcx.def_kind(self.tcx.local_parent(hir_id.owner.def_id))
990+
{
991991
Some("type alias in implementation block")
992992
} else {
993993
None
@@ -1010,7 +1010,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
10101010
| Target::Arm
10111011
| Target::ForeignMod
10121012
| Target::Closure
1013-
| Target::Impl
1013+
| Target::Impl { .. }
10141014
| Target::WherePredicate => Some(target.name()),
10151015
Target::ExternCrate
10161016
| Target::Use
@@ -1588,7 +1588,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
15881588
let article = match target {
15891589
Target::ExternCrate
15901590
| Target::Enum
1591-
| Target::Impl
1591+
| Target::Impl { .. }
15921592
| Target::Expression
15931593
| Target::Arm
15941594
| Target::AssocConst
@@ -2272,7 +2272,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22722272
match target {
22732273
// FIXME(staged_api): There's no reason we can't support more targets here. We're just
22742274
// being conservative to begin with.
2275-
Target::Fn | Target::Impl => {}
2275+
Target::Fn | Target::Impl { .. } => {}
22762276
Target::ExternCrate
22772277
| Target::Use
22782278
| Target::Static

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)