Skip to content

Commit 1b3df6f

Browse files
committed
Use expand_weak_alias_tys when collecting constrained generics params in impls
1 parent da01cce commit 1b3df6f

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

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
}

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
}

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);

0 commit comments

Comments
 (0)