Skip to content

Commit b31ee55

Browse files
committed
Tweak output of #[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.
1 parent a0c12b4 commit b31ee55

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

compiler/rustc_passes/src/check_attr.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -710,20 +710,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
710710
/// Debugging aid for `object_lifetime_default` query.
711711
fn check_object_lifetime_default(&self, hir_id: HirId) {
712712
let tcx = self.tcx;
713-
if let Some(owner_id) = hir_id.as_owner()
714-
&& let Some(generics) = tcx.hir_get_generics(owner_id.def_id)
715-
{
716-
for p in generics.params {
717-
let hir::GenericParamKind::Type { .. } = p.kind else { continue };
718-
let default = tcx.object_lifetime_default(p.def_id);
719-
let repr = match default {
720-
ObjectLifetimeDefault::Empty => "BaseDefault".to_owned(),
721-
ObjectLifetimeDefault::Static => "'static".to_owned(),
722-
ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
723-
ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
724-
};
725-
tcx.dcx().emit_err(errors::ObjectLifetimeErr { span: p.span, repr });
726-
}
713+
let Some(owner_id) = hir_id.as_owner() else { return };
714+
for param in &tcx.generics_of(owner_id.def_id).own_params {
715+
let ty::GenericParamDefKind::Type { .. } = param.kind else { continue };
716+
let default = tcx.object_lifetime_default(param.def_id);
717+
let repr = match default {
718+
ObjectLifetimeDefault::Empty => "Empty".to_owned(),
719+
ObjectLifetimeDefault::Static => "'static".to_owned(),
720+
ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
721+
ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
722+
};
723+
tcx.dcx()
724+
.emit_err(errors::ObjectLifetimeErr { span: tcx.def_span(param.def_id), repr });
727725
}
728726
}
729727

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

+10-2
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

+19-3
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)