Use expand_weak_alias_tys when collecting constrained generics params in impls
This commit is contained in:
parent
da01cced15
commit
1b3df6f068
3 changed files with 25 additions and 40 deletions
|
@ -1,8 +1,8 @@
|
|||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_data_structures::stack::ensure_sufficient_stack;
|
||||
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
|
||||
use rustc_middle::ty::{self, Ty, TyCtxt};
|
||||
use rustc_span::Span;
|
||||
use rustc_type_ir::fold::TypeFoldable;
|
||||
use std::ops::ControlFlow;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||
|
@ -33,62 +33,47 @@ pub fn parameters_for_impl<'tcx>(
|
|||
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
|
||||
) -> FxHashSet<Parameter> {
|
||||
let vec = match impl_trait_ref {
|
||||
Some(tr) => parameters_for(tcx, &tr, false),
|
||||
None => parameters_for(tcx, &impl_self_ty, false),
|
||||
Some(tr) => parameters_for(tcx, tr, false),
|
||||
None => parameters_for(tcx, impl_self_ty, false),
|
||||
};
|
||||
vec.into_iter().collect()
|
||||
}
|
||||
|
||||
/// If `include_nonconstraining` is false, returns the list of parameters that are
|
||||
/// constrained by `t` - i.e., the value of each parameter in the list is
|
||||
/// uniquely determined by `t` (see RFC 447). If it is true, return the list
|
||||
/// of parameters whose values are needed in order to constrain `ty` - these
|
||||
/// constrained by `value` - i.e., the value of each parameter in the list is
|
||||
/// uniquely determined by `value` (see RFC 447). If it is true, return the list
|
||||
/// of parameters whose values are needed in order to constrain `value` - these
|
||||
/// differ, with the latter being a superset, in the presence of projections.
|
||||
pub fn parameters_for<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
t: &impl TypeVisitable<TyCtxt<'tcx>>,
|
||||
value: impl TypeFoldable<TyCtxt<'tcx>>,
|
||||
include_nonconstraining: bool,
|
||||
) -> Vec<Parameter> {
|
||||
let mut collector =
|
||||
ParameterCollector { tcx, parameters: vec![], include_nonconstraining, depth: 0 };
|
||||
t.visit_with(&mut collector);
|
||||
let mut collector = ParameterCollector { parameters: vec![], include_nonconstraining };
|
||||
let value = if !include_nonconstraining { tcx.expand_weak_alias_tys(value) } else { value };
|
||||
value.visit_with(&mut collector);
|
||||
collector.parameters
|
||||
}
|
||||
|
||||
struct ParameterCollector<'tcx> {
|
||||
tcx: TyCtxt<'tcx>,
|
||||
struct ParameterCollector {
|
||||
parameters: Vec<Parameter>,
|
||||
include_nonconstraining: bool,
|
||||
depth: usize,
|
||||
}
|
||||
|
||||
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector<'tcx> {
|
||||
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
|
||||
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
||||
match *t.kind() {
|
||||
// Projections are not injective in general.
|
||||
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _)
|
||||
if !self.include_nonconstraining =>
|
||||
{
|
||||
// Projections are not injective in general.
|
||||
return ControlFlow::Continue(());
|
||||
}
|
||||
ty::Alias(ty::Weak, alias) if !self.include_nonconstraining => {
|
||||
if !self.tcx.recursion_limit().value_within_limit(self.depth) {
|
||||
// Other constituent types may still constrain some generic params, consider
|
||||
// `<T> (Overflow, T)` for example. Therefore we want to continue instead of
|
||||
// breaking. Only affects diagnostics.
|
||||
return ControlFlow::Continue(());
|
||||
}
|
||||
self.depth += 1;
|
||||
return ensure_sufficient_stack(|| {
|
||||
self.tcx
|
||||
.type_of(alias.def_id)
|
||||
.instantiate(self.tcx, alias.args)
|
||||
.visit_with(self)
|
||||
});
|
||||
}
|
||||
ty::Param(data) => {
|
||||
self.parameters.push(Parameter::from(data));
|
||||
// All weak alias types should've been expanded beforehand.
|
||||
ty::Alias(ty::Weak, _) if !self.include_nonconstraining => {
|
||||
bug!("unexpected weak alias type")
|
||||
}
|
||||
ty::Param(param) => self.parameters.push(Parameter::from(param)),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
|
@ -224,12 +209,12 @@ pub fn setup_constraining_predicates<'tcx>(
|
|||
// `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
|
||||
// Then the projection only applies if `T` is known, but it still
|
||||
// does not determine `U`.
|
||||
let inputs = parameters_for(tcx, &projection.projection_ty, true);
|
||||
let inputs = parameters_for(tcx, projection.projection_ty, true);
|
||||
let relies_only_on_inputs = inputs.iter().all(|p| input_parameters.contains(p));
|
||||
if !relies_only_on_inputs {
|
||||
continue;
|
||||
}
|
||||
input_parameters.extend(parameters_for(tcx, &projection.term, false));
|
||||
input_parameters.extend(parameters_for(tcx, projection.term, false));
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ fn enforce_impl_params_are_constrained(
|
|||
match item.kind {
|
||||
ty::AssocKind::Type => {
|
||||
if item.defaultness(tcx).has_value() {
|
||||
cgp::parameters_for(tcx, &tcx.type_of(def_id).instantiate_identity(), true)
|
||||
cgp::parameters_for(tcx, tcx.type_of(def_id).instantiate_identity(), true)
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
|
|
|
@ -133,7 +133,7 @@ fn check_always_applicable(
|
|||
|
||||
res = res.and(check_constness(tcx, impl1_def_id, impl2_node, span));
|
||||
res = res.and(check_static_lifetimes(tcx, &parent_args, span));
|
||||
res = res.and(check_duplicate_params(tcx, impl1_args, &parent_args, span));
|
||||
res = res.and(check_duplicate_params(tcx, impl1_args, parent_args, span));
|
||||
res = res.and(check_predicates(tcx, impl1_def_id, impl1_args, impl2_node, impl2_args, span));
|
||||
|
||||
res
|
||||
|
@ -266,15 +266,15 @@ fn unconstrained_parent_impl_args<'tcx>(
|
|||
continue;
|
||||
}
|
||||
|
||||
unconstrained_parameters.extend(cgp::parameters_for(tcx, &projection_ty, true));
|
||||
unconstrained_parameters.extend(cgp::parameters_for(tcx, projection_ty, true));
|
||||
|
||||
for param in cgp::parameters_for(tcx, &projected_ty, false) {
|
||||
for param in cgp::parameters_for(tcx, projected_ty, false) {
|
||||
if !unconstrained_parameters.contains(¶m) {
|
||||
constrained_params.insert(param.0);
|
||||
}
|
||||
}
|
||||
|
||||
unconstrained_parameters.extend(cgp::parameters_for(tcx, &projected_ty, true));
|
||||
unconstrained_parameters.extend(cgp::parameters_for(tcx, projected_ty, true));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -309,7 +309,7 @@ fn unconstrained_parent_impl_args<'tcx>(
|
|||
fn check_duplicate_params<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
impl1_args: GenericArgsRef<'tcx>,
|
||||
parent_args: &Vec<GenericArg<'tcx>>,
|
||||
parent_args: Vec<GenericArg<'tcx>>,
|
||||
span: Span,
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
let mut base_params = cgp::parameters_for(tcx, parent_args, true);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue