Skip to content

Commit e52131e

Browse files
committed
Use a slice for object_lifetime_defaults.
1 parent f72f15c commit e52131e

File tree

4 files changed

+74
-87
lines changed

4 files changed

+74
-87
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,9 +1490,7 @@ rustc_queries! {
14901490
/// for each parameter if a trait object were to be passed for that parameter.
14911491
/// For example, for `struct Foo<'a, T, U>`, this would be `['static, 'static]`.
14921492
/// For `struct Foo<'a, T: 'a, U>`, this would instead be `['a, 'static]`.
1493-
query object_lifetime_defaults_map(_: LocalDefId)
1494-
-> Option<Vec<ObjectLifetimeDefault>> {
1495-
storage(ArenaCacheSelector<'tcx>)
1493+
query object_lifetime_defaults(_: LocalDefId) -> Option<&'tcx [ObjectLifetimeDefault]> {
14961494
desc { "looking up lifetime defaults for a region on an item" }
14971495
}
14981496
query late_bound_vars_map(_: LocalDefId)

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::dep_graph::{DepGraph, DepKind, DepKindStruct};
55
use crate::hir::place::Place as HirPlace;
66
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
77
use crate::lint::{struct_lint_level, LintDiagnosticBuilder, LintLevelSource};
8-
use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath, ObjectLifetimeDefault};
8+
use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath};
99
use crate::middle::stability;
1010
use crate::mir::interpret::{self, Allocation, ConstValue, Scalar};
1111
use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted};
@@ -2713,10 +2713,6 @@ impl<'tcx> TyCtxt<'tcx> {
27132713
.map_or(false, |(owner, set)| owner == id.owner && set.contains(&id.local_id))
27142714
}
27152715

2716-
pub fn object_lifetime_defaults(self, id: HirId) -> &'tcx Option<Vec<ObjectLifetimeDefault>> {
2717-
self.object_lifetime_defaults_map(id.owner)
2718-
}
2719-
27202716
pub fn late_bound_vars(self, id: HirId) -> &'tcx List<ty::BoundVariableKind> {
27212717
self.mk_bound_variable_kinds(
27222718
self.late_bound_vars_map(id.owner)

compiler/rustc_resolve/src/late/lifetimes.rs

Lines changed: 71 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
377377

378378
named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
379379
is_late_bound_map,
380-
object_lifetime_defaults_map: |tcx, id| match tcx.hir().find_by_def_id(id) {
380+
object_lifetime_defaults: |tcx, id| match tcx.hir().find_by_def_id(id) {
381381
Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item),
382382
_ => None,
383383
},
@@ -1673,10 +1673,10 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) {
16731673
}
16741674
}
16751675

1676-
fn compute_object_lifetime_defaults(
1677-
tcx: TyCtxt<'_>,
1676+
fn compute_object_lifetime_defaults<'tcx>(
1677+
tcx: TyCtxt<'tcx>,
16781678
item: &hir::Item<'_>,
1679-
) -> Option<Vec<ObjectLifetimeDefault>> {
1679+
) -> Option<&'tcx [ObjectLifetimeDefault]> {
16801680
match item.kind {
16811681
hir::ItemKind::Struct(_, ref generics)
16821682
| hir::ItemKind::Union(_, ref generics)
@@ -1729,10 +1729,10 @@ fn compute_object_lifetime_defaults(
17291729
/// Scan the bounds and where-clauses on parameters to extract bounds
17301730
/// of the form `T:'a` so as to determine the `ObjectLifetimeDefault`
17311731
/// for each type parameter.
1732-
fn object_lifetime_defaults_for_item(
1733-
tcx: TyCtxt<'_>,
1732+
fn object_lifetime_defaults_for_item<'tcx>(
1733+
tcx: TyCtxt<'tcx>,
17341734
generics: &hir::Generics<'_>,
1735-
) -> Vec<ObjectLifetimeDefault> {
1735+
) -> &'tcx [ObjectLifetimeDefault] {
17361736
fn add_bounds(set: &mut Set1<hir::LifetimeName>, bounds: &[hir::GenericBound<'_>]) {
17371737
for bound in bounds {
17381738
if let hir::GenericBound::Outlives(ref lifetime) = *bound {
@@ -1741,81 +1741,75 @@ fn object_lifetime_defaults_for_item(
17411741
}
17421742
}
17431743

1744-
generics
1745-
.params
1746-
.iter()
1747-
.filter_map(|param| match param.kind {
1748-
GenericParamKind::Lifetime { .. } => None,
1749-
GenericParamKind::Type { .. } => {
1750-
let mut set = Set1::Empty;
1751-
1752-
add_bounds(&mut set, &param.bounds);
1753-
1754-
let param_def_id = tcx.hir().local_def_id(param.hir_id);
1755-
for predicate in generics.where_clause.predicates {
1756-
// Look for `type: ...` where clauses.
1757-
let data = match *predicate {
1758-
hir::WherePredicate::BoundPredicate(ref data) => data,
1759-
_ => continue,
1760-
};
1744+
let process_param = |param: &hir::GenericParam<'_>| match param.kind {
1745+
GenericParamKind::Lifetime { .. } => None,
1746+
GenericParamKind::Type { .. } => {
1747+
let mut set = Set1::Empty;
17611748

1762-
// Ignore `for<'a> type: ...` as they can change what
1763-
// lifetimes mean (although we could "just" handle it).
1764-
if !data.bound_generic_params.is_empty() {
1765-
continue;
1766-
}
1749+
add_bounds(&mut set, &param.bounds);
17671750

1768-
let res = match data.bounded_ty.kind {
1769-
hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => path.res,
1770-
_ => continue,
1771-
};
1751+
let param_def_id = tcx.hir().local_def_id(param.hir_id);
1752+
for predicate in generics.where_clause.predicates {
1753+
// Look for `type: ...` where clauses.
1754+
let data = match *predicate {
1755+
hir::WherePredicate::BoundPredicate(ref data) => data,
1756+
_ => continue,
1757+
};
17721758

1773-
if res == Res::Def(DefKind::TyParam, param_def_id.to_def_id()) {
1774-
add_bounds(&mut set, &data.bounds);
1775-
}
1759+
// Ignore `for<'a> type: ...` as they can change what
1760+
// lifetimes mean (although we could "just" handle it).
1761+
if !data.bound_generic_params.is_empty() {
1762+
continue;
17761763
}
17771764

1778-
Some(match set {
1779-
Set1::Empty => Set1::Empty,
1780-
Set1::One(name) => {
1781-
if name == hir::LifetimeName::Static {
1782-
Set1::One(Region::Static)
1783-
} else {
1784-
generics
1785-
.params
1786-
.iter()
1787-
.filter_map(|param| match param.kind {
1788-
GenericParamKind::Lifetime { .. } => Some((
1789-
param.hir_id,
1790-
hir::LifetimeName::Param(param.name),
1791-
LifetimeDefOrigin::from_param(param),
1792-
)),
1793-
_ => None,
1794-
})
1795-
.enumerate()
1796-
.find(|&(_, (_, lt_name, _))| lt_name == name)
1797-
.map_or(Set1::Many, |(i, (id, _, origin))| {
1798-
let def_id = tcx.hir().local_def_id(id);
1799-
Set1::One(Region::EarlyBound(
1800-
i as u32,
1801-
def_id.to_def_id(),
1802-
origin,
1803-
))
1804-
})
1805-
}
1806-
}
1807-
Set1::Many => Set1::Many,
1808-
})
1809-
}
1810-
GenericParamKind::Const { .. } => {
1811-
// Generic consts don't impose any constraints.
1812-
//
1813-
// We still store a dummy value here to allow generic parameters
1814-
// in an arbitrary order.
1815-
Some(Set1::Empty)
1765+
let res = match data.bounded_ty.kind {
1766+
hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) => path.res,
1767+
_ => continue,
1768+
};
1769+
1770+
if res == Res::Def(DefKind::TyParam, param_def_id.to_def_id()) {
1771+
add_bounds(&mut set, &data.bounds);
1772+
}
18161773
}
1817-
})
1818-
.collect()
1774+
1775+
Some(match set {
1776+
Set1::Empty => Set1::Empty,
1777+
Set1::One(name) => {
1778+
if name == hir::LifetimeName::Static {
1779+
Set1::One(Region::Static)
1780+
} else {
1781+
generics
1782+
.params
1783+
.iter()
1784+
.filter_map(|param| match param.kind {
1785+
GenericParamKind::Lifetime { .. } => Some((
1786+
param.hir_id,
1787+
hir::LifetimeName::Param(param.name),
1788+
LifetimeDefOrigin::from_param(param),
1789+
)),
1790+
_ => None,
1791+
})
1792+
.enumerate()
1793+
.find(|&(_, (_, lt_name, _))| lt_name == name)
1794+
.map_or(Set1::Many, |(i, (id, _, origin))| {
1795+
let def_id = tcx.hir().local_def_id(id);
1796+
Set1::One(Region::EarlyBound(i as u32, def_id.to_def_id(), origin))
1797+
})
1798+
}
1799+
}
1800+
Set1::Many => Set1::Many,
1801+
})
1802+
}
1803+
GenericParamKind::Const { .. } => {
1804+
// Generic consts don't impose any constraints.
1805+
//
1806+
// We still store a dummy value here to allow generic parameters
1807+
// in an arbitrary order.
1808+
Some(Set1::Empty)
1809+
}
1810+
};
1811+
1812+
tcx.arena.alloc_from_iter(generics.params.iter().filter_map(process_param))
18191813
}
18201814

18211815
impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
@@ -2510,8 +2504,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
25102504
if let Some(def_id) = def_id.as_local() {
25112505
let id = self.tcx.hir().local_def_id_to_hir_id(def_id);
25122506
self.tcx
2513-
.object_lifetime_defaults(id)
2514-
.as_ref()
2507+
.object_lifetime_defaults(id.owner)
25152508
.unwrap()
25162509
.iter()
25172510
.map(set_to_region)

compiler/rustc_typeck/src/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,7 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
16951695
kind: ty::GenericParamDefKind::Lifetime,
16961696
}));
16971697

1698-
let object_lifetime_defaults = tcx.object_lifetime_defaults(hir_id);
1698+
let object_lifetime_defaults = tcx.object_lifetime_defaults(hir_id.owner);
16991699

17001700
// Now create the real type and const parameters.
17011701
let type_start = own_start - has_self as u32 + params.len() as u32;

0 commit comments

Comments
 (0)