Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 517c28e

Browse files
committed
Auto merge of rust-lang#87280 - lcnr:lazy-anon-const-default-substs, r=nikomatsakis
lazily "compute" anon const default substs Continuing the work of rust-lang#83086, this implements the discussed solution for the [unused substs problem](https://github.com/rust-lang/project-const-generics/blob/master/design-docs/anon-const-substs.md#unused-substs). As of now, anonymous constants inherit all of their parents generics, even if they do not use them, e.g. in `fn foo<T, const N: usize>() -> [T; N + 1]`, the array length has `T` as a generic parameter even though it doesn't use it. These *unused substs* cause some backwards incompatible, and imo incorrect behavior, e.g. rust-lang#78369. --- We do not actually filter any generic parameters here and the `default_anon_const_substs` query still a dummy which only checks that - we now prevent the previously existing query cycles and are able to call `predicates_of(parent)` when computing the substs of anonymous constants - the default anon consts substs only include the typeflags we assume it does. Implementing that filtering will be left as future work. --- The idea of this PR is to delay the creation of the anon const substs until after we've computed `predicates_of` for the parent of the anon const. As the predicates of the parent can however contain the anon const we still have to create a `ty::Const` for it. We do this by changing the substs field of `ty::Unevaluated` to an option and modifying accesses to instead call the method `unevaluated.substs(tcx)` which returns the substs as before. If the substs - now `substs_` - of `ty::Unevaluated` are `None`, it means that the anon const currently has its default substs, i.e. the substs it has when first constructed, which are the generic parameters it has available. To be able to call `unevaluated.substs(tcx)` in a `TypeVisitor`, we add the non-defaulted method `fn tcx_for_anon_const_substs(&self) -> Option<TyCtxt<'tcx>>`. In case `tcx_for_anon_const_substs` returns `None`, unknown anon const default substs are skipped entirely. Even when `substs_` is `None` we still have to treat the constant as if it has its default substs. To do this, `TypeFlags` are modified so that it is clear whether they can still change when *exposing* any anon const default substs. A new flag, `HAS_UNKNOWN_DEFAULT_CONST_SUBSTS`, is added in case some default flags are missing. The rest of this PR are some smaller changes to either not cause cycles by trying to access the default anon const substs too early or to be able to access the `tcx` in previously unused locations. cc `@rust-lang/project-const-generics` r? `@nikomatsakis`
2 parents ad02dc4 + 7cbfa2e commit 517c28e

File tree

122 files changed

+1023
-521
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+1023
-521
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ pub(crate) fn codegen_constant<'tcx>(
129129
};
130130
let const_val = match const_.val {
131131
ConstKind::Value(const_val) => const_val,
132-
ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
133-
if fx.tcx.is_static(def.did) =>
132+
ConstKind::Unevaluated(uv)
133+
if fx.tcx.is_static(uv.def.did) =>
134134
{
135-
assert!(substs.is_empty());
136-
assert!(promoted.is_none());
135+
assert!(uv.substs(fx.tcx).is_empty());
136+
assert!(uv.promoted.is_none());
137137

138-
return codegen_static_ref(fx, def.did, fx.layout_of(const_.ty)).to_cvalue(fx);
138+
return codegen_static_ref(fx, uv.def.did, fx.layout_of(const_.ty)).to_cvalue(fx);
139139
}
140140
ConstKind::Unevaluated(unevaluated) => {
141141
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
499499
ty::Adt(def, ..) if !def.is_box() => {
500500
// Again, only create type information if full debuginfo is enabled
501501
if cx.sess().opts.debuginfo == DebugInfo::Full
502-
&& !impl_self_ty.needs_subst()
502+
&& !impl_self_ty.definitely_needs_subst(cx.tcx)
503503
{
504504
Some(type_metadata(cx, impl_self_ty, rustc_span::DUMMY_SP))
505505
} else {

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13981398
LocalRef::UnsizedPlace(_) => bug!("transmute must not involve unsized locals"),
13991399
LocalRef::Operand(None) => {
14001400
let dst_layout = bx.layout_of(self.monomorphized_place_ty(dst.as_ref()));
1401-
assert!(!dst_layout.ty.has_erasable_regions());
1401+
assert!(!dst_layout.ty.has_erasable_regions(self.cx.tcx()));
14021402
let place = PlaceRef::alloca(bx, dst_layout);
14031403
place.storage_live(bx);
14041404
self.codegen_transmute_into(bx, src, place);

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
216216
let mut allocate_local = |local| {
217217
let decl = &mir.local_decls[local];
218218
let layout = bx.layout_of(fx.monomorphize(decl.ty));
219-
assert!(!layout.ty.has_erasable_regions());
219+
assert!(!layout.ty.has_erasable_regions(cx.tcx()));
220220

221221
if local == mir::RETURN_PLACE && fx.fn_abi.ret.is_indirect() {
222222
debug!("alloc: {:?} (return place) -> place", local);

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
470470
{
471471
let needs_canonical_flags = if canonicalize_region_mode.any() {
472472
TypeFlags::NEEDS_INFER |
473-
TypeFlags::HAS_FREE_REGIONS | // `HAS_RE_PLACEHOLDER` implies `HAS_FREE_REGIONS`
473+
TypeFlags::HAS_POTENTIAL_FREE_REGIONS | // `HAS_RE_PLACEHOLDER` implies `HAS_POTENTIAL_FREE_REGIONS`
474474
TypeFlags::HAS_TY_PLACEHOLDER |
475475
TypeFlags::HAS_CT_PLACEHOLDER
476476
} else {

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> {
129129
where
130130
R: ConstEquateRelation<'tcx>,
131131
{
132+
let a = self.tcx.expose_default_const_substs(a);
133+
let b = self.tcx.expose_default_const_substs(b);
132134
debug!("{}.consts({:?}, {:?})", relation.tag(), a, b);
133135
if a == b {
134136
return Ok(a);
@@ -742,10 +744,9 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
742744
}
743745
}
744746
}
745-
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
746-
if self.tcx().lazy_normalization() =>
747-
{
748-
assert_eq!(promoted, None);
747+
ty::ConstKind::Unevaluated(uv) if self.tcx().lazy_normalization() => {
748+
assert_eq!(uv.promoted, None);
749+
let substs = uv.substs(self.tcx());
749750
let substs = self.relate_with_variance(
750751
ty::Variance::Invariant,
751752
ty::VarianceDiagInfo::default(),
@@ -754,7 +755,7 @@ impl TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
754755
)?;
755756
Ok(self.tcx().mk_const(ty::Const {
756757
ty: c.ty,
757-
val: ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }),
758+
val: ty::ConstKind::Unevaluated(ty::Unevaluated::new(uv.def, substs)),
758759
}))
759760
}
760761
_ => relate::super_relate_consts(self, c, c),
@@ -976,10 +977,9 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
976977
}
977978
}
978979
}
979-
ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
980-
if self.tcx().lazy_normalization() =>
981-
{
982-
assert_eq!(promoted, None);
980+
ty::ConstKind::Unevaluated(uv) if self.tcx().lazy_normalization() => {
981+
assert_eq!(uv.promoted, None);
982+
let substs = uv.substs(self.tcx());
983983
let substs = self.relate_with_variance(
984984
ty::Variance::Invariant,
985985
ty::VarianceDiagInfo::default(),
@@ -988,7 +988,7 @@ impl TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
988988
)?;
989989
Ok(self.tcx().mk_const(ty::Const {
990990
ty: c.ty,
991-
val: ty::ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }),
991+
val: ty::ConstKind::Unevaluated(ty::Unevaluated::new(uv.def, substs)),
992992
}))
993993
}
994994
_ => relate::super_relate_consts(self, c, c),

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,6 +1537,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15371537
}
15381538

15391539
impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
1540+
fn tcx_for_anon_const_substs(&self) -> Option<TyCtxt<'tcx>> {
1541+
Some(self.tcx)
1542+
}
1543+
15401544
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
15411545
if let Some((kind, def_id)) = TyCategory::from_ty(self.tcx, t) {
15421546
let span = self.tcx.def_span(def_id);

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'a, 'tcx> FindHirNodeVisitor<'a, 'tcx> {
5151

5252
fn node_ty_contains_target(&self, hir_id: HirId) -> Option<Ty<'tcx>> {
5353
self.node_type_opt(hir_id).map(|ty| self.infcx.resolve_vars_if_possible(ty)).filter(|ty| {
54-
ty.walk().any(|inner| {
54+
ty.walk(self.infcx.tcx).any(|inner| {
5555
inner == self.target
5656
|| match (inner.unpack(), self.target.unpack()) {
5757
(GenericArgKind::Type(inner_ty), GenericArgKind::Type(target_ty)) => {

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorRepor
99
use rustc_hir::def_id::DefId;
1010
use rustc_hir::intravisit::{walk_ty, ErasedMap, NestedVisitorMap, Visitor};
1111
use rustc_hir::{self as hir, GenericBound, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind};
12-
use rustc_middle::ty::{self, AssocItemContainer, RegionKind, Ty, TypeFoldable, TypeVisitor};
12+
use rustc_middle::ty::{
13+
self, AssocItemContainer, RegionKind, Ty, TyCtxt, TypeFoldable, TypeVisitor,
14+
};
1315
use rustc_span::symbol::Ident;
1416
use rustc_span::{MultiSpan, Span};
1517

@@ -476,8 +478,14 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
476478
/// Collect all the trait objects in a type that could have received an implicit `'static` lifetime.
477479
pub(super) struct TraitObjectVisitor(pub(super) FxHashSet<DefId>);
478480

479-
impl TypeVisitor<'_> for TraitObjectVisitor {
480-
fn visit_ty(&mut self, t: Ty<'_>) -> ControlFlow<Self::BreakTy> {
481+
impl<'tcx> TypeVisitor<'tcx> for TraitObjectVisitor {
482+
fn tcx_for_anon_const_substs(&self) -> Option<TyCtxt<'tcx>> {
483+
// The default anon const substs cannot include
484+
// trait objects, so we don't have to bother looking.
485+
None
486+
}
487+
488+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
481489
match t.kind() {
482490
ty::Dynamic(preds, RegionKind::ReStatic) => {
483491
if let Some(def_id) = preds.principal_def_id() {

compiler/rustc_infer/src/infer/freshen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'a, 'tcx> TypeFolder<'tcx> for TypeFreshener<'a, 'tcx> {
146146
}
147147

148148
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
149-
if !t.needs_infer() && !t.has_erasable_regions() {
149+
if !t.needs_infer() && !t.has_erasable_regions(self.tcx()) {
150150
return t;
151151
}
152152

0 commit comments

Comments
 (0)