Skip to content

Commit 0f27113

Browse files
committed
Tweak #[rustc_object_lifetime_default]
* print `Empty` as it's called in code, otherwise it's unnecessarily confusing * go through the middle::ty Generics instead of the HIR ones, so we can dump the default for the implicit `Self` type parameter of traits, too * allow the attribute on more targets (it used to be allowed anywhere for the longest time but someone must've incorrectly restricted it during the migration to the new attribute parsing API)
1 parent 0f8c502 commit 0f27113

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ impl<S: Stage> SingleAttributeParser<S> for RustcObjectLifetimeDefaultParser {
4646
const PATH: &[rustc_span::Symbol] = &[sym::rustc_object_lifetime_default];
4747
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
4848
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
49-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
49+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[
50+
Allow(Target::Struct),
51+
Allow(Target::Enum),
52+
Allow(Target::Union),
53+
Allow(Target::Trait),
54+
Allow(Target::TyAlias),
55+
Allow(Target::AssocTy),
56+
]);
5057
const TEMPLATE: AttributeTemplate = template!(Word);
5158

5259
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {

compiler/rustc_passes/src/check_attr.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -649,20 +649,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
649649
/// Debugging aid for `object_lifetime_default` query.
650650
fn check_object_lifetime_default(&self, hir_id: HirId) {
651651
let tcx = self.tcx;
652-
if let Some(owner_id) = hir_id.as_owner()
653-
&& let Some(generics) = tcx.hir_get_generics(owner_id.def_id)
654-
{
655-
for p in generics.params {
656-
let hir::GenericParamKind::Type { .. } = p.kind else { continue };
657-
let default = tcx.object_lifetime_default(p.def_id);
658-
let repr = match default {
659-
ObjectLifetimeDefault::Empty => "BaseDefault".to_owned(),
660-
ObjectLifetimeDefault::Static => "'static".to_owned(),
661-
ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
662-
ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
663-
};
664-
tcx.dcx().emit_err(errors::ObjectLifetimeErr { span: p.span, repr });
665-
}
652+
let Some(owner_id) = hir_id.as_owner() else { return };
653+
for param in &tcx.generics_of(owner_id.def_id).own_params {
654+
let ty::GenericParamDefKind::Type { .. } = param.kind else { continue };
655+
let default = tcx.object_lifetime_default(param.def_id);
656+
let repr = match default {
657+
ObjectLifetimeDefault::Empty => "Empty".to_owned(),
658+
ObjectLifetimeDefault::Static => "'static".to_owned(),
659+
ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
660+
ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
661+
};
662+
tcx.dcx()
663+
.emit_err(errors::ObjectLifetimeErr { span: tcx.def_span(param.def_id), repr });
666664
}
667665
}
668666
/// Checks if `#[collapse_debuginfo]` is applied to a macro.

tests/ui/object-lifetime/object-lifetime-default.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
#[rustc_object_lifetime_default]
44
struct A<
5-
T, //~ ERROR BaseDefault
5+
T, //~ ERROR Empty
66
>(T);
77

88
#[rustc_object_lifetime_default]
99
struct B<
1010
'a,
11-
T, //~ ERROR BaseDefault
11+
T, //~ ERROR Empty
1212
>(&'a (), T);
1313

1414
#[rustc_object_lifetime_default]
@@ -47,4 +47,12 @@ struct G<
4747
U: 'a + 'b, //~ ERROR Ambiguous
4848
>(&'a T, &'b U);
4949

50+
// Check that we also dump the default for the implicit `Self` type param of traits.
51+
#[rustc_object_lifetime_default]
52+
trait H< //~ ERROR 'a
53+
'a,
54+
'b,
55+
T: 'b, //~ ERROR 'b
56+
>: 'a {}
57+
5058
fn main() {}

tests/ui/object-lifetime/object-lifetime-default.stderr

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: BaseDefault
1+
error: Empty
22
--> $DIR/object-lifetime-default.rs:5:5
33
|
44
LL | T,
55
| ^
66

7-
error: BaseDefault
7+
error: Empty
88
--> $DIR/object-lifetime-default.rs:11:5
99
|
1010
LL | T,
@@ -52,5 +52,21 @@ error: Ambiguous
5252
LL | U: 'a + 'b,
5353
| ^
5454

55-
error: aborting due to 9 previous errors
55+
error: 'a
56+
--> $DIR/object-lifetime-default.rs:52:1
57+
|
58+
LL | / trait H<
59+
LL | | 'a,
60+
LL | | 'b,
61+
LL | | T: 'b,
62+
LL | | >: 'a {}
63+
| |_____^
64+
65+
error: 'b
66+
--> $DIR/object-lifetime-default.rs:55:5
67+
|
68+
LL | T: 'b,
69+
| ^
70+
71+
error: aborting due to 11 previous errors
5672

0 commit comments

Comments
 (0)