Skip to content

Commit 8bd80ab

Browse files
authored
Rollup merge of rust-lang#99821 - cjgillot:ast-lifetimes-2, r=compiler-errors
Remove separate indexing of early-bound regions ~Based on rust-lang#99728 This PR copies some modifications from rust-lang#97839 around object lifetime defaults. These modifications allow to stop counting generic parameters during lifetime resolution, and rely on the indexing given by `rustc_typeck::collect`.
2 parents 0acb2ed + da90ec1 commit 8bd80ab

File tree

17 files changed

+180
-432
lines changed

17 files changed

+180
-432
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/find_anon_type.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
103103
// Find the index of the named region that was part of the
104104
// error. We will then search the function parameters for a bound
105105
// region at the right depth with the same index
106-
(Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
106+
(Some(rl::Region::EarlyBound(id)), ty::BrNamed(def_id, _)) => {
107107
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
108108
if id == def_id {
109109
self.found_type = Some(arg);
@@ -133,7 +133,7 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
133133
Some(
134134
rl::Region::Static
135135
| rl::Region::Free(_, _)
136-
| rl::Region::EarlyBound(_, _)
136+
| rl::Region::EarlyBound(_)
137137
| rl::Region::LateBound(_, _, _),
138138
)
139139
| None,
@@ -188,7 +188,7 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
188188
fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
189189
match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
190190
// the lifetime of the TyPath!
191-
(Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
191+
(Some(rl::Region::EarlyBound(id)), ty::BrNamed(def_id, _)) => {
192192
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
193193
if id == def_id {
194194
self.found_it = true;
@@ -209,7 +209,7 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
209209
(
210210
Some(
211211
rl::Region::Static
212-
| rl::Region::EarlyBound(_, _)
212+
| rl::Region::EarlyBound(_)
213213
| rl::Region::LateBound(_, _, _)
214214
| rl::Region::Free(_, _),
215215
)

compiler/rustc_lint/src/builtin.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -2026,13 +2026,13 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN
20262026
impl ExplicitOutlivesRequirements {
20272027
fn lifetimes_outliving_lifetime<'tcx>(
20282028
inferred_outlives: &'tcx [(ty::Predicate<'tcx>, Span)],
2029-
index: u32,
2029+
def_id: DefId,
20302030
) -> Vec<ty::Region<'tcx>> {
20312031
inferred_outlives
20322032
.iter()
20332033
.filter_map(|(pred, _)| match pred.kind().skip_binder() {
20342034
ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match *a {
2035-
ty::ReEarlyBound(ebr) if ebr.index == index => Some(b),
2035+
ty::ReEarlyBound(ebr) if ebr.def_id == def_id => Some(b),
20362036
_ => None,
20372037
},
20382038
_ => None,
@@ -2069,8 +2069,12 @@ impl ExplicitOutlivesRequirements {
20692069
.filter_map(|(i, bound)| {
20702070
if let hir::GenericBound::Outlives(lifetime) = bound {
20712071
let is_inferred = match tcx.named_region(lifetime.hir_id) {
2072-
Some(Region::EarlyBound(index, ..)) => inferred_outlives.iter().any(|r| {
2073-
if let ty::ReEarlyBound(ebr) = **r { ebr.index == index } else { false }
2072+
Some(Region::EarlyBound(def_id)) => inferred_outlives.iter().any(|r| {
2073+
if let ty::ReEarlyBound(ebr) = **r {
2074+
ebr.def_id == def_id
2075+
} else {
2076+
false
2077+
}
20742078
}),
20752079
_ => false,
20762080
};
@@ -2164,11 +2168,14 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21642168
for (i, where_predicate) in hir_generics.predicates.iter().enumerate() {
21652169
let (relevant_lifetimes, bounds, span, in_where_clause) = match where_predicate {
21662170
hir::WherePredicate::RegionPredicate(predicate) => {
2167-
if let Some(Region::EarlyBound(index, ..)) =
2171+
if let Some(Region::EarlyBound(region_def_id)) =
21682172
cx.tcx.named_region(predicate.lifetime.hir_id)
21692173
{
21702174
(
2171-
Self::lifetimes_outliving_lifetime(inferred_outlives, index),
2175+
Self::lifetimes_outliving_lifetime(
2176+
inferred_outlives,
2177+
region_def_id,
2178+
),
21722179
&predicate.bounds,
21732180
predicate.span,
21742181
predicate.in_where_clause,

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ provide! { tcx, def_id, other, cdata,
199199
codegen_fn_attrs => { table }
200200
impl_trait_ref => { table }
201201
const_param_default => { table }
202+
object_lifetime_default => { table }
202203
thir_abstract_const => { table }
203204
optimized_mir => { table }
204205
mir_for_ctfe => { table }

compiler/rustc_metadata/src/rmeta/encoder.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
10761076
record_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives);
10771077
}
10781078
}
1079+
if let DefKind::TyParam | DefKind::ConstParam = def_kind {
1080+
if let Some(default) = self.tcx.object_lifetime_default(def_id) {
1081+
record!(self.tables.object_lifetime_default[def_id] <- default);
1082+
}
1083+
}
10791084
if let DefKind::Trait | DefKind::TraitAlias = def_kind {
10801085
record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id));
10811086
}

compiler/rustc_metadata/src/rmeta/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
1616
use rustc_middle::metadata::ModChild;
1717
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1818
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
19+
use rustc_middle::middle::resolve_lifetime::ObjectLifetimeDefault;
1920
use rustc_middle::mir;
2021
use rustc_middle::ty::fast_reject::SimplifiedType;
2122
use rustc_middle::ty::query::Providers;
@@ -358,6 +359,7 @@ define_tables! {
358359
codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
359360
impl_trait_ref: Table<DefIndex, LazyValue<ty::TraitRef<'static>>>,
360361
const_param_default: Table<DefIndex, LazyValue<rustc_middle::ty::Const<'static>>>,
362+
object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,
361363
optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,
362364
mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
363365
promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,

compiler/rustc_middle/src/hir/map/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,9 @@ impl<'hir> Map<'hir> {
486486
let def_kind = self.tcx.def_kind(def_id);
487487
match def_kind {
488488
DefKind::Trait | DefKind::TraitAlias => def_id,
489-
DefKind::TyParam | DefKind::ConstParam => self.tcx.local_parent(def_id),
489+
DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
490+
self.tcx.local_parent(def_id)
491+
}
490492
_ => bug!("ty_param_owner: {:?} is a {:?} not a type parameter", def_id, def_kind),
491493
}
492494
}
@@ -495,7 +497,9 @@ impl<'hir> Map<'hir> {
495497
let def_kind = self.tcx.def_kind(def_id);
496498
match def_kind {
497499
DefKind::Trait | DefKind::TraitAlias => kw::SelfUpper,
498-
DefKind::TyParam | DefKind::ConstParam => self.tcx.item_name(def_id.to_def_id()),
500+
DefKind::LifetimeParam | DefKind::TyParam | DefKind::ConstParam => {
501+
self.tcx.item_name(def_id.to_def_id())
502+
}
499503
_ => bug!("ty_param_name: {:?} is a {:?} not a type parameter", def_id, def_kind),
500504
}
501505
}

compiler/rustc_middle/src/middle/resolve_lifetime.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_macros::HashStable;
1010
#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable, Debug, HashStable)]
1111
pub enum Region {
1212
Static,
13-
EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
13+
EarlyBound(/* lifetime decl */ DefId),
1414
LateBound(ty::DebruijnIndex, /* late-bound index */ u32, /* lifetime decl */ DefId),
1515
Free(DefId, /* lifetime decl */ DefId),
1616
}
@@ -35,7 +35,13 @@ impl<T: PartialEq> Set1<T> {
3535
}
3636
}
3737

38-
pub type ObjectLifetimeDefault = Set1<Region>;
38+
#[derive(Copy, Clone, Debug, HashStable, Encodable, Decodable)]
39+
pub enum ObjectLifetimeDefault {
40+
Empty,
41+
Static,
42+
Ambiguous,
43+
Param(DefId),
44+
}
3945

4046
/// Maps the id of each lifetime reference to the lifetime decl
4147
/// that it corresponds to.

compiler/rustc_middle/src/query/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1597,8 +1597,9 @@ rustc_queries! {
15971597
/// for each parameter if a trait object were to be passed for that parameter.
15981598
/// For example, for `struct Foo<'a, T, U>`, this would be `['static, 'static]`.
15991599
/// For `struct Foo<'a, T: 'a, U>`, this would instead be `['a, 'static]`.
1600-
query object_lifetime_defaults(_: LocalDefId) -> Option<&'tcx [ObjectLifetimeDefault]> {
1601-
desc { "looking up lifetime defaults for a region on an item" }
1600+
query object_lifetime_default(key: DefId) -> Option<ObjectLifetimeDefault> {
1601+
desc { "looking up lifetime defaults for generic parameter `{:?}`", key }
1602+
separate_provide_extern
16021603
}
16031604
query late_bound_vars_map(_: LocalDefId)
16041605
-> Option<&'tcx FxHashMap<ItemLocalId, Vec<ty::BoundVariableKind>>> {

compiler/rustc_middle/src/ty/generics.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
21
use crate::ty;
32
use crate::ty::subst::{Subst, SubstsRef};
43
use crate::ty::EarlyBinder;
@@ -13,7 +12,7 @@ use super::{EarlyBoundRegion, InstantiatedPredicates, ParamConst, ParamTy, Predi
1312
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable)]
1413
pub enum GenericParamDefKind {
1514
Lifetime,
16-
Type { has_default: bool, object_lifetime_default: ObjectLifetimeDefault, synthetic: bool },
15+
Type { has_default: bool, synthetic: bool },
1716
Const { has_default: bool },
1817
}
1918

compiler/rustc_middle/src/ty/parameterized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ trivially_parameterized_over_tcx! {
5353
crate::metadata::ModChild,
5454
crate::middle::codegen_fn_attrs::CodegenFnAttrs,
5555
crate::middle::exported_symbols::SymbolExportInfo,
56+
crate::middle::resolve_lifetime::ObjectLifetimeDefault,
5657
crate::mir::ConstQualifs,
5758
ty::Generics,
5859
ty::ImplPolarity,

compiler/rustc_passes/src/check_attr.rs

+28
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_hir::intravisit::{self, Visitor};
1616
use rustc_hir::{self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID};
1717
use rustc_hir::{MethodKind, Target};
1818
use rustc_middle::hir::nested_filter;
19+
use rustc_middle::middle::resolve_lifetime::ObjectLifetimeDefault;
1920
use rustc_middle::ty::query::Providers;
2021
use rustc_middle::ty::TyCtxt;
2122
use rustc_session::lint::builtin::{
@@ -172,6 +173,9 @@ impl CheckAttrVisitor<'_> {
172173
sym::no_implicit_prelude => {
173174
self.check_generic_attr(hir_id, attr, target, &[Target::Mod])
174175
}
176+
sym::rustc_object_lifetime_default => {
177+
self.check_object_lifetime_default(hir_id, span)
178+
}
175179
_ => {}
176180
}
177181

@@ -410,6 +414,30 @@ impl CheckAttrVisitor<'_> {
410414
}
411415
}
412416

417+
/// Debugging aid for `object_lifetime_default` query.
418+
fn check_object_lifetime_default(&self, hir_id: HirId, span: Span) {
419+
let tcx = self.tcx;
420+
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);
438+
}
439+
}
440+
413441
/// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
414442
fn check_track_caller(
415443
&self,

0 commit comments

Comments
 (0)