Skip to content

Commit a18f9b5

Browse files
authored
Unrolled build for rust-lang#121344
Rollup merge of rust-lang#121344 - fmease:lta-constr-by-input, r=oli-obk Expand weak alias types before collecting constrained/referenced late bound regions + refactorings Fixes rust-lang#114220. Follow-up to rust-lang#120780. r? `@oli-obk`
2 parents bb59453 + f515f99 commit a18f9b5

27 files changed

+300
-173
lines changed

Diff for: compiler/rustc_hir_analysis/src/astconv/bounds.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
454454
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
455455
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
456456
let late_bound_in_projection_ty =
457-
tcx.collect_constrained_late_bound_regions(&projection_ty);
457+
tcx.collect_constrained_late_bound_regions(projection_ty);
458458
let late_bound_in_term =
459-
tcx.collect_referenced_late_bound_regions(&trait_ref.rebind(term));
459+
tcx.collect_referenced_late_bound_regions(trait_ref.rebind(term));
460460
debug!(?late_bound_in_projection_ty);
461461
debug!(?late_bound_in_term);
462462

Diff for: compiler/rustc_hir_analysis/src/astconv/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2678,9 +2678,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26782678
// for<'a> fn(&'a String) -> &'a str <-- 'a is ok
26792679
let inputs = bare_fn_ty.inputs();
26802680
let late_bound_in_args =
2681-
tcx.collect_constrained_late_bound_regions(&inputs.map_bound(|i| i.to_owned()));
2681+
tcx.collect_constrained_late_bound_regions(inputs.map_bound(|i| i.to_owned()));
26822682
let output = bare_fn_ty.output();
2683-
let late_bound_in_ret = tcx.collect_referenced_late_bound_regions(&output);
2683+
let late_bound_in_ret = tcx.collect_referenced_late_bound_regions(output);
26842684

26852685
self.validate_late_bound_regions(late_bound_in_args, late_bound_in_ret, |br_name| {
26862686
struct_span_code_err!(

Diff for: compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl<'tcx> InherentCollect<'tcx> {
144144
let id = id.owner_id.def_id;
145145
let item_span = self.tcx.def_span(id);
146146
let self_ty = self.tcx.type_of(id).instantiate_identity();
147-
let self_ty = peel_off_weak_aliases(self.tcx, self_ty);
147+
let self_ty = self.tcx.peel_off_weak_alias_tys(self_ty);
148148
match *self_ty.kind() {
149149
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
150150
ty::Foreign(did) => self.check_def_id(id, self_ty, did),
@@ -186,30 +186,3 @@ impl<'tcx> InherentCollect<'tcx> {
186186
}
187187
}
188188
}
189-
190-
/// Peel off all weak alias types in this type until there are none left.
191-
///
192-
/// <div class="warning">
193-
///
194-
/// This assumes that `ty` gets normalized later and that any overflows occurring
195-
/// during said normalization get reported.
196-
///
197-
/// </div>
198-
fn peel_off_weak_aliases<'tcx>(tcx: TyCtxt<'tcx>, mut ty: Ty<'tcx>) -> Ty<'tcx> {
199-
let ty::Alias(ty::Weak, _) = ty.kind() else { return ty };
200-
201-
let limit = tcx.recursion_limit();
202-
let mut depth = 0;
203-
204-
while let ty::Alias(ty::Weak, alias) = ty.kind() {
205-
if !limit.value_within_limit(depth) {
206-
let guar = tcx.dcx().delayed_bug("overflow expanding weak alias type");
207-
return Ty::new_error(tcx, guar);
208-
}
209-
210-
ty = tcx.type_of(alias.def_id).instantiate(tcx, alias.args);
211-
depth += 1;
212-
}
213-
214-
ty
215-
}

Diff for: compiler/rustc_hir_analysis/src/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ fn get_new_lifetime_name<'tcx>(
520520
generics: &hir::Generics<'tcx>,
521521
) -> String {
522522
let existing_lifetimes = tcx
523-
.collect_referenced_late_bound_regions(&poly_trait_ref)
523+
.collect_referenced_late_bound_regions(poly_trait_ref)
524524
.into_iter()
525525
.filter_map(|lt| {
526526
if let ty::BoundRegionKind::BrNamed(_, name) = lt {

Diff for: compiler/rustc_hir_analysis/src/constrained_generic_params.rs

+19-34
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_data_structures::stack::ensure_sufficient_stack;
32
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
43
use rustc_middle::ty::{self, Ty, TyCtxt};
54
use rustc_span::Span;
5+
use rustc_type_ir::fold::TypeFoldable;
66
use std::ops::ControlFlow;
77

88
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
@@ -33,62 +33,47 @@ pub fn parameters_for_impl<'tcx>(
3333
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
3434
) -> FxHashSet<Parameter> {
3535
let vec = match impl_trait_ref {
36-
Some(tr) => parameters_for(tcx, &tr, false),
37-
None => parameters_for(tcx, &impl_self_ty, false),
36+
Some(tr) => parameters_for(tcx, tr, false),
37+
None => parameters_for(tcx, impl_self_ty, false),
3838
};
3939
vec.into_iter().collect()
4040
}
4141

4242
/// If `include_nonconstraining` is false, returns the list of parameters that are
43-
/// constrained by `t` - i.e., the value of each parameter in the list is
44-
/// uniquely determined by `t` (see RFC 447). If it is true, return the list
45-
/// of parameters whose values are needed in order to constrain `ty` - these
43+
/// constrained by `value` - i.e., the value of each parameter in the list is
44+
/// uniquely determined by `value` (see RFC 447). If it is true, return the list
45+
/// of parameters whose values are needed in order to constrain `value` - these
4646
/// differ, with the latter being a superset, in the presence of projections.
4747
pub fn parameters_for<'tcx>(
4848
tcx: TyCtxt<'tcx>,
49-
t: &impl TypeVisitable<TyCtxt<'tcx>>,
49+
value: impl TypeFoldable<TyCtxt<'tcx>>,
5050
include_nonconstraining: bool,
5151
) -> Vec<Parameter> {
52-
let mut collector =
53-
ParameterCollector { tcx, parameters: vec![], include_nonconstraining, depth: 0 };
54-
t.visit_with(&mut collector);
52+
let mut collector = ParameterCollector { parameters: vec![], include_nonconstraining };
53+
let value = if !include_nonconstraining { tcx.expand_weak_alias_tys(value) } else { value };
54+
value.visit_with(&mut collector);
5555
collector.parameters
5656
}
5757

58-
struct ParameterCollector<'tcx> {
59-
tcx: TyCtxt<'tcx>,
58+
struct ParameterCollector {
6059
parameters: Vec<Parameter>,
6160
include_nonconstraining: bool,
62-
depth: usize,
6361
}
6462

65-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector<'tcx> {
63+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
6664
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
6765
match *t.kind() {
66+
// Projections are not injective in general.
6867
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _)
6968
if !self.include_nonconstraining =>
7069
{
71-
// Projections are not injective in general.
7270
return ControlFlow::Continue(());
7371
}
74-
ty::Alias(ty::Weak, alias) if !self.include_nonconstraining => {
75-
if !self.tcx.recursion_limit().value_within_limit(self.depth) {
76-
// Other constituent types may still constrain some generic params, consider
77-
// `<T> (Overflow, T)` for example. Therefore we want to continue instead of
78-
// breaking. Only affects diagnostics.
79-
return ControlFlow::Continue(());
80-
}
81-
self.depth += 1;
82-
return ensure_sufficient_stack(|| {
83-
self.tcx
84-
.type_of(alias.def_id)
85-
.instantiate(self.tcx, alias.args)
86-
.visit_with(self)
87-
});
88-
}
89-
ty::Param(data) => {
90-
self.parameters.push(Parameter::from(data));
72+
// All weak alias types should've been expanded beforehand.
73+
ty::Alias(ty::Weak, _) if !self.include_nonconstraining => {
74+
bug!("unexpected weak alias type")
9175
}
76+
ty::Param(param) => self.parameters.push(Parameter::from(param)),
9277
_ => {}
9378
}
9479

@@ -224,12 +209,12 @@ pub fn setup_constraining_predicates<'tcx>(
224209
// `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
225210
// Then the projection only applies if `T` is known, but it still
226211
// does not determine `U`.
227-
let inputs = parameters_for(tcx, &projection.projection_ty, true);
212+
let inputs = parameters_for(tcx, projection.projection_ty, true);
228213
let relies_only_on_inputs = inputs.iter().all(|p| input_parameters.contains(p));
229214
if !relies_only_on_inputs {
230215
continue;
231216
}
232-
input_parameters.extend(parameters_for(tcx, &projection.term, false));
217+
input_parameters.extend(parameters_for(tcx, projection.term, false));
233218
} else {
234219
continue;
235220
}

Diff for: compiler/rustc_hir_analysis/src/impl_wf_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ fn enforce_impl_params_are_constrained(
111111
match item.kind {
112112
ty::AssocKind::Type => {
113113
if item.defaultness(tcx).has_value() {
114-
cgp::parameters_for(tcx, &tcx.type_of(def_id).instantiate_identity(), true)
114+
cgp::parameters_for(tcx, tcx.type_of(def_id).instantiate_identity(), true)
115115
} else {
116116
vec![]
117117
}

Diff for: compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn check_always_applicable(
133133

134134
res = res.and(check_constness(tcx, impl1_def_id, impl2_node, span));
135135
res = res.and(check_static_lifetimes(tcx, &parent_args, span));
136-
res = res.and(check_duplicate_params(tcx, impl1_args, &parent_args, span));
136+
res = res.and(check_duplicate_params(tcx, impl1_args, parent_args, span));
137137
res = res.and(check_predicates(tcx, impl1_def_id, impl1_args, impl2_node, impl2_args, span));
138138

139139
res
@@ -266,15 +266,15 @@ fn unconstrained_parent_impl_args<'tcx>(
266266
continue;
267267
}
268268

269-
unconstrained_parameters.extend(cgp::parameters_for(tcx, &projection_ty, true));
269+
unconstrained_parameters.extend(cgp::parameters_for(tcx, projection_ty, true));
270270

271-
for param in cgp::parameters_for(tcx, &projected_ty, false) {
271+
for param in cgp::parameters_for(tcx, projected_ty, false) {
272272
if !unconstrained_parameters.contains(&param) {
273273
constrained_params.insert(param.0);
274274
}
275275
}
276276

277-
unconstrained_parameters.extend(cgp::parameters_for(tcx, &projected_ty, true));
277+
unconstrained_parameters.extend(cgp::parameters_for(tcx, projected_ty, true));
278278
}
279279
}
280280

@@ -309,7 +309,7 @@ fn unconstrained_parent_impl_args<'tcx>(
309309
fn check_duplicate_params<'tcx>(
310310
tcx: TyCtxt<'tcx>,
311311
impl1_args: GenericArgsRef<'tcx>,
312-
parent_args: &Vec<GenericArg<'tcx>>,
312+
parent_args: Vec<GenericArg<'tcx>>,
313313
span: Span,
314314
) -> Result<(), ErrorGuaranteed> {
315315
let mut base_params = cgp::parameters_for(tcx, parent_args, true);

Diff for: compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::infer::error_reporting::nice_region_error::NiceRegionError;
55
use crate::infer::TyCtxt;
66
use rustc_hir as hir;
77
use rustc_hir::def_id::LocalDefId;
8-
use rustc_middle::ty::{self, Binder, Region, Ty, TypeVisitable};
8+
use rustc_middle::ty::{self, Binder, Region, Ty, TypeFoldable};
99
use rustc_span::Span;
1010

1111
/// Information about the anonymous region we are searching for.
@@ -142,10 +142,10 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
142142

143143
fn includes_region(
144144
&self,
145-
ty: Binder<'tcx, impl TypeVisitable<TyCtxt<'tcx>>>,
145+
ty: Binder<'tcx, impl TypeFoldable<TyCtxt<'tcx>>>,
146146
region: ty::BoundRegionKind,
147147
) -> bool {
148-
let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(&ty);
148+
let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(ty);
149149
// We are only checking is any region meets the condition so order doesn't matter
150150
#[allow(rustc::potential_query_instability)]
151151
late_bound_regions.iter().any(|r| *r == region)

Diff for: compiler/rustc_middle/src/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ macro_rules! arena_types {
4747
rustc_middle::traits::query::DropckOutlivesResult<'tcx>
4848
>
4949
>,
50-
[] normalize_projection_ty:
50+
[] normalize_canonicalized_projection_ty:
5151
rustc_middle::infer::canonical::Canonical<'tcx,
5252
rustc_middle::infer::canonical::QueryResponse<'tcx,
5353
rustc_middle::traits::query::NormalizationResult<'tcx>

Diff for: compiler/rustc_middle/src/query/mod.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::query::plumbing::{
3131
};
3232
use crate::thir;
3333
use crate::traits::query::{
34-
CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
34+
CanonicalAliasGoal, CanonicalPredicateGoal, CanonicalTyGoal,
3535
CanonicalTypeOpAscribeUserTypeGoal, CanonicalTypeOpEqGoal, CanonicalTypeOpNormalizeGoal,
3636
CanonicalTypeOpProvePredicateGoal, CanonicalTypeOpSubtypeGoal, NoSolution,
3737
};
@@ -1938,29 +1938,41 @@ rustc_queries! {
19381938
arena_cache
19391939
}
19401940

1941-
/// Do not call this query directly: invoke `normalize` instead.
1942-
query normalize_projection_ty(
1943-
goal: CanonicalProjectionGoal<'tcx>
1941+
/// <div class="warning">
1942+
///
1943+
/// Do not call this query directly: Invoke `normalize` instead.
1944+
///
1945+
/// </div>
1946+
query normalize_canonicalized_projection_ty(
1947+
goal: CanonicalAliasGoal<'tcx>
19441948
) -> Result<
19451949
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, NormalizationResult<'tcx>>>,
19461950
NoSolution,
19471951
> {
19481952
desc { "normalizing `{}`", goal.value.value }
19491953
}
19501954

1951-
/// Do not call this query directly: invoke `normalize` instead.
1952-
query normalize_weak_ty(
1953-
goal: CanonicalProjectionGoal<'tcx>
1955+
/// <div class="warning">
1956+
///
1957+
/// Do not call this query directly: Invoke `normalize` instead.
1958+
///
1959+
/// </div>
1960+
query normalize_canonicalized_weak_ty(
1961+
goal: CanonicalAliasGoal<'tcx>
19541962
) -> Result<
19551963
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, NormalizationResult<'tcx>>>,
19561964
NoSolution,
19571965
> {
19581966
desc { "normalizing `{}`", goal.value.value }
19591967
}
19601968

1961-
/// Do not call this query directly: invoke `normalize` instead.
1962-
query normalize_inherent_projection_ty(
1963-
goal: CanonicalProjectionGoal<'tcx>
1969+
/// <div class="warning">
1970+
///
1971+
/// Do not call this query directly: Invoke `normalize` instead.
1972+
///
1973+
/// </div>
1974+
query normalize_canonicalized_inherent_projection_ty(
1975+
goal: CanonicalAliasGoal<'tcx>
19641976
) -> Result<
19651977
&'tcx Canonical<'tcx, canonical::QueryResponse<'tcx, NormalizationResult<'tcx>>>,
19661978
NoSolution,

Diff for: compiler/rustc_middle/src/traits/query.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub mod type_op {
6767
}
6868
}
6969

70-
pub type CanonicalProjectionGoal<'tcx> = Canonical<'tcx, ty::ParamEnvAnd<'tcx, ty::AliasTy<'tcx>>>;
70+
pub type CanonicalAliasGoal<'tcx> = Canonical<'tcx, ty::ParamEnvAnd<'tcx, ty::AliasTy<'tcx>>>;
7171

7272
pub type CanonicalTyGoal<'tcx> = Canonical<'tcx, ty::ParamEnvAnd<'tcx, Ty<'tcx>>>;
7373

@@ -177,10 +177,10 @@ pub struct MethodAutoderefBadTy<'tcx> {
177177
pub ty: Canonical<'tcx, QueryResponse<'tcx, Ty<'tcx>>>,
178178
}
179179

180-
/// Result from the `normalize_projection_ty` query.
180+
/// Result of the `normalize_canonicalized_{{,inherent_}projection,weak}_ty` queries.
181181
#[derive(Clone, Debug, HashStable, TypeFoldable, TypeVisitable)]
182182
pub struct NormalizationResult<'tcx> {
183-
/// Result of normalization.
183+
/// Result of the normalization.
184184
pub normalized_ty: Ty<'tcx>,
185185
}
186186

Diff for: compiler/rustc_middle/src/ty/flags.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,10 @@ impl FlagComputation {
181181

182182
&ty::Alias(kind, data) => {
183183
self.add_flags(match kind {
184-
ty::Weak | ty::Projection => TypeFlags::HAS_TY_PROJECTION,
185-
ty::Inherent => TypeFlags::HAS_TY_INHERENT,
184+
ty::Projection => TypeFlags::HAS_TY_PROJECTION,
185+
ty::Weak => TypeFlags::HAS_TY_WEAK,
186186
ty::Opaque => TypeFlags::HAS_TY_OPAQUE,
187+
ty::Inherent => TypeFlags::HAS_TY_INHERENT,
187188
});
188189

189190
self.add_alias_ty(data);

0 commit comments

Comments
 (0)