Skip to content

Commit 84f0c3f

Browse files
committed
Auto merge of rust-lang#101214 - cjgillot:old-no-lt, r=estebank
Do not call object_lifetime_default on lifetime params. Small cleanup to avoid unnecessary query invocations in trivial cases.
2 parents dec6894 + e716426 commit 84f0c3f

File tree

6 files changed

+116
-79
lines changed

6 files changed

+116
-79
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1162,10 +1162,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11621162
if should_encode_type(tcx, local_id, def_kind) {
11631163
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
11641164
}
1165-
if let DefKind::TyParam | DefKind::ConstParam = def_kind {
1166-
if let Some(default) = self.tcx.object_lifetime_default(def_id) {
1167-
record!(self.tables.object_lifetime_default[def_id] <- default);
1168-
}
1165+
if let DefKind::TyParam = def_kind {
1166+
let default = self.tcx.object_lifetime_default(def_id);
1167+
record!(self.tables.object_lifetime_default[def_id] <- default);
11691168
}
11701169
if let DefKind::Trait | DefKind::TraitAlias = def_kind {
11711170
record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id));

compiler/rustc_middle/src/query/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1593,12 +1593,13 @@ rustc_queries! {
15931593
query is_late_bound_map(_: LocalDefId) -> Option<&'tcx FxIndexSet<LocalDefId>> {
15941594
desc { "testing if a region is late bound" }
15951595
}
1596-
/// For a given item (like a struct), gets the default lifetimes to be used
1596+
/// For a given item's generic parameter, gets the default lifetimes to be used
15971597
/// for each parameter if a trait object were to be passed for that parameter.
1598-
/// For example, for `struct Foo<'a, T, U>`, this would be `['static, 'static]`.
1599-
/// For `struct Foo<'a, T: 'a, U>`, this would instead be `['a, 'static]`.
1600-
query object_lifetime_default(key: DefId) -> Option<ObjectLifetimeDefault> {
1601-
desc { "looking up lifetime defaults for generic parameter `{:?}`", key }
1598+
/// For example, for `T` in `struct Foo<'a, T>`, this would be `'static`.
1599+
/// For `T` in `struct Foo<'a, T: 'a>`, this would instead be `'a`.
1600+
/// This query will panic if passed something that is not a type parameter.
1601+
query object_lifetime_default(key: DefId) -> ObjectLifetimeDefault {
1602+
desc { "looking up lifetime defaults for generic parameter `{}`", tcx.def_path_str(key) }
16021603
separate_provide_extern
16031604
}
16041605
query late_bound_vars_map(_: LocalDefId)

compiler/rustc_passes/src/check_attr.rs

+14-21
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,7 @@ impl CheckAttrVisitor<'_> {
173173
sym::no_implicit_prelude => {
174174
self.check_generic_attr(hir_id, attr, target, &[Target::Mod])
175175
}
176-
sym::rustc_object_lifetime_default => {
177-
self.check_object_lifetime_default(hir_id, span)
178-
}
176+
sym::rustc_object_lifetime_default => self.check_object_lifetime_default(hir_id),
179177
_ => {}
180178
}
181179

@@ -415,26 +413,21 @@ impl CheckAttrVisitor<'_> {
415413
}
416414

417415
/// Debugging aid for `object_lifetime_default` query.
418-
fn check_object_lifetime_default(&self, hir_id: HirId, span: Span) {
416+
fn check_object_lifetime_default(&self, hir_id: HirId) {
419417
let tcx = self.tcx;
420418
if let Some(generics) = tcx.hir().get_generics(tcx.hir().local_def_id(hir_id)) {
421-
let object_lifetime_default_reprs: String = generics
422-
.params
423-
.iter()
424-
.filter_map(|p| {
425-
let param_id = tcx.hir().local_def_id(p.hir_id);
426-
let default = tcx.object_lifetime_default(param_id)?;
427-
Some(match default {
428-
ObjectLifetimeDefault::Empty => "BaseDefault".to_owned(),
429-
ObjectLifetimeDefault::Static => "'static".to_owned(),
430-
ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
431-
ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
432-
})
433-
})
434-
.collect::<Vec<String>>()
435-
.join(",");
436-
437-
tcx.sess.span_err(span, &object_lifetime_default_reprs);
419+
for p in generics.params {
420+
let hir::GenericParamKind::Type { .. } = p.kind else { continue };
421+
let param_id = tcx.hir().local_def_id(p.hir_id);
422+
let default = tcx.object_lifetime_default(param_id);
423+
let repr = match default {
424+
ObjectLifetimeDefault::Empty => "BaseDefault".to_owned(),
425+
ObjectLifetimeDefault::Static => "'static".to_owned(),
426+
ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
427+
ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
428+
};
429+
tcx.sess.span_err(p.span, &repr);
430+
}
438431
}
439432
}
440433

compiler/rustc_resolve/src/late/lifetimes.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -1148,21 +1148,18 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
11481148
}
11491149
}
11501150

1151-
fn object_lifetime_default<'tcx>(
1152-
tcx: TyCtxt<'tcx>,
1153-
param_def_id: DefId,
1154-
) -> Option<ObjectLifetimeDefault> {
1151+
fn object_lifetime_default<'tcx>(tcx: TyCtxt<'tcx>, param_def_id: DefId) -> ObjectLifetimeDefault {
1152+
debug_assert_eq!(tcx.def_kind(param_def_id), DefKind::TyParam);
11551153
let param_def_id = param_def_id.expect_local();
11561154
let parent_def_id = tcx.local_parent(param_def_id);
1157-
let generics = tcx.hir().get_generics(parent_def_id)?;
1155+
let generics = tcx.hir().get_generics(parent_def_id).unwrap();
11581156
let param_hir_id = tcx.local_def_id_to_hir_id(param_def_id);
1159-
let param = generics.params.iter().find(|p| p.hir_id == param_hir_id)?;
1157+
let param = generics.params.iter().find(|p| p.hir_id == param_hir_id).unwrap();
11601158

11611159
// Scan the bounds and where-clauses on parameters to extract bounds
11621160
// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
11631161
// for each type parameter.
11641162
match param.kind {
1165-
GenericParamKind::Lifetime { .. } => None,
11661163
GenericParamKind::Type { .. } => {
11671164
let mut set = Set1::Empty;
11681165

@@ -1181,21 +1178,17 @@ fn object_lifetime_default<'tcx>(
11811178
}
11821179
}
11831180

1184-
Some(match set {
1181+
match set {
11851182
Set1::Empty => ObjectLifetimeDefault::Empty,
11861183
Set1::One(hir::LifetimeName::Static) => ObjectLifetimeDefault::Static,
11871184
Set1::One(hir::LifetimeName::Param(param_def_id, _)) => {
11881185
ObjectLifetimeDefault::Param(param_def_id.to_def_id())
11891186
}
11901187
_ => ObjectLifetimeDefault::Ambiguous,
1191-
})
1188+
}
11921189
}
1193-
GenericParamKind::Const { .. } => {
1194-
// Generic consts don't impose any constraints.
1195-
//
1196-
// We still store a dummy value here to allow generic parameters
1197-
// in an arbitrary order.
1198-
Some(ObjectLifetimeDefault::Empty)
1190+
_ => {
1191+
bug!("object_lifetime_default_raw must only be called on a type parameter")
11991192
}
12001193
}
12011194
}
@@ -1512,7 +1505,20 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
15121505
generics
15131506
.params
15141507
.iter()
1515-
.filter_map(|param| self.tcx.object_lifetime_default(param.def_id))
1508+
.filter_map(|param| {
1509+
match self.tcx.def_kind(param.def_id) {
1510+
// Generic consts don't impose any constraints.
1511+
//
1512+
// We still store a dummy value here to allow generic parameters
1513+
// in an arbitrary order.
1514+
DefKind::ConstParam => Some(ObjectLifetimeDefault::Empty),
1515+
DefKind::TyParam => Some(self.tcx.object_lifetime_default(param.def_id)),
1516+
// We may also get a `Trait` or `TraitAlias` because of how generics `Self` parameter
1517+
// works. Ignore it because it can't have a meaningful lifetime default.
1518+
DefKind::LifetimeParam | DefKind::Trait | DefKind::TraitAlias => None,
1519+
dk => bug!("unexpected def_kind {:?}", dk),
1520+
}
1521+
})
15161522
.map(set_to_region)
15171523
.collect()
15181524
});
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,50 @@
11
#![feature(rustc_attrs)]
22

33
#[rustc_object_lifetime_default]
4-
struct A<T>(T); //~ ERROR BaseDefault
4+
struct A<
5+
T, //~ ERROR BaseDefault
6+
>(T);
57

68
#[rustc_object_lifetime_default]
7-
struct B<'a,T>(&'a (), T); //~ ERROR BaseDefault
9+
struct B<
10+
'a,
11+
T, //~ ERROR BaseDefault
12+
>(&'a (), T);
813

914
#[rustc_object_lifetime_default]
10-
struct C<'a,T:'a>(&'a T); //~ ERROR 'a
15+
struct C<
16+
'a,
17+
T: 'a, //~ ERROR 'a
18+
>(&'a T);
1119

1220
#[rustc_object_lifetime_default]
13-
struct D<'a,'b,T:'a+'b>(&'a T, &'b T); //~ ERROR Ambiguous
21+
struct D<
22+
'a,
23+
'b,
24+
T: 'a + 'b, //~ ERROR Ambiguous
25+
>(&'a T, &'b T);
1426

1527
#[rustc_object_lifetime_default]
16-
struct E<'a,'b:'a,T:'b>(&'a T, &'b T); //~ ERROR 'b
28+
struct E<
29+
'a,
30+
'b: 'a,
31+
T: 'b, //~ ERROR 'b
32+
>(&'a T, &'b T);
1733

1834
#[rustc_object_lifetime_default]
19-
struct F<'a,'b,T:'a,U:'b>(&'a T, &'b U); //~ ERROR 'a,'b
35+
struct F<
36+
'a,
37+
'b,
38+
T: 'a, //~ ERROR 'a
39+
U: 'b, //~ ERROR 'b
40+
>(&'a T, &'b U);
2041

2142
#[rustc_object_lifetime_default]
22-
struct G<'a,'b,T:'a,U:'a+'b>(&'a T, &'b U); //~ ERROR 'a,Ambiguous
23-
24-
fn main() { }
43+
struct G<
44+
'a,
45+
'b,
46+
T: 'a, //~ ERROR 'a
47+
U: 'a + 'b, //~ ERROR Ambiguous
48+
>(&'a T, &'b U);
49+
50+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
11
error: BaseDefault
2-
--> $DIR/object-lifetime-default.rs:4:1
2+
--> $DIR/object-lifetime-default.rs:5:5
33
|
4-
LL | struct A<T>(T);
5-
| ^^^^^^^^^^^^^^^
4+
LL | T,
5+
| ^
66

77
error: BaseDefault
8-
--> $DIR/object-lifetime-default.rs:7:1
8+
--> $DIR/object-lifetime-default.rs:11:5
99
|
10-
LL | struct B<'a,T>(&'a (), T);
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | T,
11+
| ^
1212

1313
error: 'a
14-
--> $DIR/object-lifetime-default.rs:10:1
14+
--> $DIR/object-lifetime-default.rs:17:5
1515
|
16-
LL | struct C<'a,T:'a>(&'a T);
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
16+
LL | T: 'a,
17+
| ^
1818

1919
error: Ambiguous
20-
--> $DIR/object-lifetime-default.rs:13:1
20+
--> $DIR/object-lifetime-default.rs:24:5
2121
|
22-
LL | struct D<'a,'b,T:'a+'b>(&'a T, &'b T);
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
LL | T: 'a + 'b,
23+
| ^
2424

2525
error: 'b
26-
--> $DIR/object-lifetime-default.rs:16:1
26+
--> $DIR/object-lifetime-default.rs:31:5
2727
|
28-
LL | struct E<'a,'b:'a,T:'b>(&'a T, &'b T);
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
28+
LL | T: 'b,
29+
| ^
3030

31-
error: 'a,'b
32-
--> $DIR/object-lifetime-default.rs:19:1
31+
error: 'a
32+
--> $DIR/object-lifetime-default.rs:38:5
33+
|
34+
LL | T: 'a,
35+
| ^
36+
37+
error: 'b
38+
--> $DIR/object-lifetime-default.rs:39:5
3339
|
34-
LL | struct F<'a,'b,T:'a,U:'b>(&'a T, &'b U);
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
40+
LL | U: 'b,
41+
| ^
3642

37-
error: 'a,Ambiguous
38-
--> $DIR/object-lifetime-default.rs:22:1
43+
error: 'a
44+
--> $DIR/object-lifetime-default.rs:46:5
45+
|
46+
LL | T: 'a,
47+
| ^
48+
49+
error: Ambiguous
50+
--> $DIR/object-lifetime-default.rs:47:5
3951
|
40-
LL | struct G<'a,'b,T:'a,U:'a+'b>(&'a T, &'b U);
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
52+
LL | U: 'a + 'b,
53+
| ^
4254

43-
error: aborting due to 7 previous errors
55+
error: aborting due to 9 previous errors
4456

0 commit comments

Comments
 (0)