2020-03-29 10:19:48 -05:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2023-02-21 20:18:40 -06:00
|
|
|
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
|
2020-03-29 09:41:09 -05:00
|
|
|
use rustc_middle::ty::{self, Ty, TyCtxt};
|
2020-01-01 12:25:28 -06:00
|
|
|
use rustc_span::source_map::Span;
|
2020-10-21 07:26:34 -05:00
|
|
|
use std::ops::ControlFlow;
|
2015-02-12 04:16:02 -06:00
|
|
|
|
2015-04-15 08:13:52 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
2016-08-29 16:03:29 -05:00
|
|
|
pub struct Parameter(pub u32);
|
|
|
|
|
|
|
|
impl From<ty::ParamTy> for Parameter {
|
2019-05-06 07:12:04 -05:00
|
|
|
fn from(param: ty::ParamTy) -> Self {
|
|
|
|
Parameter(param.index)
|
|
|
|
}
|
2016-08-29 16:03:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ty::EarlyBoundRegion> for Parameter {
|
|
|
|
fn from(param: ty::EarlyBoundRegion) -> Self {
|
|
|
|
Parameter(param.index)
|
|
|
|
}
|
2015-04-15 08:13:52 -05:00
|
|
|
}
|
|
|
|
|
2019-02-19 19:19:42 -06:00
|
|
|
impl From<ty::ParamConst> for Parameter {
|
|
|
|
fn from(param: ty::ParamConst) -> Self {
|
|
|
|
Parameter(param.index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 07:53:55 -06:00
|
|
|
/// Returns the set of parameters constrained by the impl header.
|
2019-09-02 20:03:54 -05:00
|
|
|
pub fn parameters_for_impl<'tcx>(
|
|
|
|
impl_self_ty: Ty<'tcx>,
|
|
|
|
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
|
|
|
|
) -> FxHashSet<Parameter> {
|
2016-11-08 18:23:09 -06:00
|
|
|
let vec = match impl_trait_ref {
|
2022-01-11 21:19:52 -06:00
|
|
|
Some(tr) => parameters_for(&tr, false),
|
|
|
|
None => parameters_for(&impl_self_ty, false),
|
2016-11-08 18:23:09 -06:00
|
|
|
};
|
|
|
|
vec.into_iter().collect()
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:43:51 -05:00
|
|
|
/// If `include_nonconstraining` is false, returns the list of parameters that are
|
2018-11-26 20:59:49 -06:00
|
|
|
/// constrained by `t` - i.e., the value of each parameter in the list is
|
2016-07-31 15:01:02 -05:00
|
|
|
/// uniquely determined by `t` (see RFC 447). If it is true, return the list
|
2015-11-18 16:43:31 -06:00
|
|
|
/// of parameters whose values are needed in order to constrain `ty` - these
|
|
|
|
/// differ, with the latter being a superset, in the presence of projections.
|
2019-09-02 22:51:31 -05:00
|
|
|
pub fn parameters_for<'tcx>(
|
2023-02-21 20:18:40 -06:00
|
|
|
t: &impl TypeVisitable<TyCtxt<'tcx>>,
|
2019-09-02 20:03:54 -05:00
|
|
|
include_nonconstraining: bool,
|
|
|
|
) -> Vec<Parameter> {
|
2022-01-11 21:19:52 -06:00
|
|
|
let mut collector = ParameterCollector { parameters: vec![], include_nonconstraining };
|
2016-07-31 15:01:02 -05:00
|
|
|
t.visit_with(&mut collector);
|
|
|
|
collector.parameters
|
2015-04-15 08:13:52 -05:00
|
|
|
}
|
|
|
|
|
2022-01-11 21:19:52 -06:00
|
|
|
struct ParameterCollector {
|
2016-07-31 15:01:02 -05:00
|
|
|
parameters: Vec<Parameter>,
|
|
|
|
include_nonconstraining: bool,
|
2015-04-15 08:13:52 -05:00
|
|
|
}
|
|
|
|
|
2023-02-09 13:38:07 -06:00
|
|
|
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
|
2020-11-05 10:30:39 -06:00
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2020-08-02 17:49:11 -05:00
|
|
|
match *t.kind() {
|
2023-08-11 14:05:03 -05:00
|
|
|
ty::Alias(..) if !self.include_nonconstraining => {
|
2016-07-31 15:01:02 -05:00
|
|
|
// projections are not injective
|
2023-01-18 01:17:13 -06:00
|
|
|
return ControlFlow::Continue(());
|
2016-07-31 15:01:02 -05:00
|
|
|
}
|
2018-08-21 19:35:29 -05:00
|
|
|
ty::Param(data) => {
|
2016-08-29 16:03:29 -05:00
|
|
|
self.parameters.push(Parameter::from(data));
|
2016-07-31 15:01:02 -05:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2015-04-15 08:13:52 -05:00
|
|
|
|
2016-07-31 15:01:02 -05:00
|
|
|
t.super_visit_with(self)
|
|
|
|
}
|
2015-04-15 08:13:52 -05:00
|
|
|
|
2020-11-05 10:30:39 -06:00
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2018-09-26 10:32:23 -05:00
|
|
|
if let ty::ReEarlyBound(data) = *r {
|
|
|
|
self.parameters.push(Parameter::from(data));
|
2016-07-31 15:01:02 -05:00
|
|
|
}
|
2023-01-18 01:17:13 -06:00
|
|
|
ControlFlow::Continue(())
|
2015-04-15 08:13:52 -05:00
|
|
|
}
|
2019-02-19 19:19:42 -06:00
|
|
|
|
2022-02-01 21:24:45 -06:00
|
|
|
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-06-09 20:18:06 -05:00
|
|
|
match c.kind() {
|
2020-02-08 14:14:02 -06:00
|
|
|
ty::ConstKind::Unevaluated(..) if !self.include_nonconstraining => {
|
|
|
|
// Constant expressions are not injective
|
2022-02-01 21:24:45 -06:00
|
|
|
return c.ty().visit_with(self);
|
2020-02-08 14:14:02 -06:00
|
|
|
}
|
|
|
|
ty::ConstKind::Param(data) => {
|
|
|
|
self.parameters.push(Parameter::from(data));
|
|
|
|
}
|
|
|
|
_ => {}
|
2019-02-19 19:19:42 -06:00
|
|
|
}
|
2020-02-08 14:14:02 -06:00
|
|
|
|
|
|
|
c.super_visit_with(self)
|
2019-02-19 19:19:42 -06:00
|
|
|
}
|
2015-04-15 08:13:52 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:11:55 -05:00
|
|
|
pub fn identify_constrained_generic_params<'tcx>(
|
2019-06-13 16:48:52 -05:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-10-17 19:14:57 -05:00
|
|
|
predicates: ty::GenericPredicates<'tcx>,
|
2019-06-11 16:11:55 -05:00
|
|
|
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
|
|
|
|
input_parameters: &mut FxHashSet<Parameter>,
|
|
|
|
) {
|
2019-10-17 19:14:57 -05:00
|
|
|
let mut predicates = predicates.predicates.to_vec();
|
2017-07-11 09:33:09 -05:00
|
|
|
setup_constraining_predicates(tcx, &mut predicates, impl_trait_ref, input_parameters);
|
2015-10-20 09:15:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Order the predicates in `predicates` such that each parameter is
|
|
|
|
/// constrained before it is used, if that is possible, and add the
|
2017-08-10 17:16:18 -05:00
|
|
|
/// parameters so constrained to `input_parameters`. For example,
|
2015-10-20 09:15:58 -05:00
|
|
|
/// imagine the following impl:
|
2022-04-15 17:04:34 -05:00
|
|
|
/// ```ignore (illustrative)
|
|
|
|
/// impl<T: Debug, U: Iterator<Item = T>> Trait for U
|
|
|
|
/// ```
|
2015-10-20 09:15:58 -05:00
|
|
|
/// The impl's predicates are collected from left to right. Ignoring
|
|
|
|
/// the implicit `Sized` bounds, these are
|
2022-10-09 09:15:23 -05:00
|
|
|
/// * `T: Debug`
|
|
|
|
/// * `U: Iterator`
|
|
|
|
/// * `<U as Iterator>::Item = T` -- a desugared ProjectionPredicate
|
2015-10-20 09:15:58 -05:00
|
|
|
///
|
|
|
|
/// When we, for example, try to go over the trait-reference
|
|
|
|
/// `IntoIter<u32> as Trait`, we substitute the impl parameters with fresh
|
|
|
|
/// variables and match them with the impl trait-ref, so we know that
|
|
|
|
/// `$U = IntoIter<u32>`.
|
|
|
|
///
|
|
|
|
/// However, in order to process the `$T: Debug` predicate, we must first
|
|
|
|
/// know the value of `$T` - which is only given by processing the
|
|
|
|
/// projection. As we occasionally want to process predicates in a single
|
|
|
|
/// pass, we want the projection to come first. In fact, as projections
|
|
|
|
/// can (acyclically) depend on one another - see RFC447 for details - we
|
|
|
|
/// need to topologically sort them.
|
2015-11-18 16:43:31 -06:00
|
|
|
///
|
|
|
|
/// We *do* have to be somewhat careful when projection targets contain
|
|
|
|
/// projections themselves, for example in
|
2022-10-09 09:15:23 -05:00
|
|
|
///
|
|
|
|
/// ```ignore (illustrative)
|
2015-11-18 16:43:31 -06:00
|
|
|
/// impl<S,U,V,W> Trait for U where
|
2019-02-08 07:53:55 -06:00
|
|
|
/// /* 0 */ S: Iterator<Item = U>,
|
2015-11-18 16:43:31 -06:00
|
|
|
/// /* - */ U: Iterator,
|
|
|
|
/// /* 1 */ <U as Iterator>::Item: ToOwned<Owned=(W,<V as Iterator>::Item)>
|
2019-02-08 07:53:55 -06:00
|
|
|
/// /* 2 */ W: Iterator<Item = V>
|
2015-11-18 16:43:31 -06:00
|
|
|
/// /* 3 */ V: Debug
|
2022-10-09 09:15:23 -05:00
|
|
|
/// ```
|
|
|
|
///
|
2015-11-18 16:43:31 -06:00
|
|
|
/// we have to evaluate the projections in the order I wrote them:
|
|
|
|
/// `V: Debug` requires `V` to be evaluated. The only projection that
|
|
|
|
/// *determines* `V` is 2 (1 contains it, but *does not determine it*,
|
|
|
|
/// as it is only contained within a projection), but that requires `W`
|
|
|
|
/// which is determined by 1, which requires `U`, that is determined
|
|
|
|
/// by 0. I should probably pick a less tangled example, but I can't
|
|
|
|
/// think of any.
|
2019-06-11 16:11:55 -05:00
|
|
|
pub fn setup_constraining_predicates<'tcx>(
|
2019-12-08 11:04:17 -06:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-06-22 13:17:13 -05:00
|
|
|
predicates: &mut [(ty::Clause<'tcx>, Span)],
|
2019-06-11 16:11:55 -05:00
|
|
|
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
|
|
|
|
input_parameters: &mut FxHashSet<Parameter>,
|
|
|
|
) {
|
2015-10-20 09:15:58 -05:00
|
|
|
// The canonical way of doing the needed topological sort
|
|
|
|
// would be a DFS, but getting the graph and its ownership
|
|
|
|
// right is annoying, so I am using an in-place fixed-point iteration,
|
|
|
|
// which is `O(nt)` where `t` is the depth of type-parameter constraints,
|
|
|
|
// remembering that `t` should be less than 7 in practice.
|
|
|
|
//
|
|
|
|
// Basically, I iterate over all projections and swap every
|
|
|
|
// "ready" projection to the start of the list, such that
|
|
|
|
// all of the projections before `i` are topologically sorted
|
|
|
|
// and constrain all the parameters in `input_parameters`.
|
|
|
|
//
|
|
|
|
// In the example, `input_parameters` starts by containing `U` - which
|
|
|
|
// is constrained by the trait-ref - and so on the first pass we
|
|
|
|
// observe that `<U as Iterator>::Item = T` is a "ready" projection that
|
|
|
|
// constrains `T` and swap it to front. As it is the sole projection,
|
|
|
|
// no more swaps can take place afterwards, with the result being
|
|
|
|
// * <U as Iterator>::Item = T
|
|
|
|
// * T: Debug
|
|
|
|
// * U: Iterator
|
2016-08-29 16:03:29 -05:00
|
|
|
debug!(
|
|
|
|
"setup_constraining_predicates: predicates={:?} \
|
|
|
|
impl_trait_ref={:?} input_parameters={:?}",
|
|
|
|
predicates, impl_trait_ref, input_parameters
|
|
|
|
);
|
2015-10-20 09:15:58 -05:00
|
|
|
let mut i = 0;
|
|
|
|
let mut changed = true;
|
|
|
|
while changed {
|
|
|
|
changed = false;
|
|
|
|
|
|
|
|
for j in i..predicates.len() {
|
2020-06-18 13:41:43 -05:00
|
|
|
// Note that we don't have to care about binders here,
|
|
|
|
// as the impl trait ref never contains any late-bound regions.
|
2023-06-22 13:17:13 -05:00
|
|
|
if let ty::ClauseKind::Projection(projection) = predicates[j].0.kind().skip_binder() {
|
2015-10-20 09:15:58 -05:00
|
|
|
// Special case: watch out for some kind of sneaky attempt
|
|
|
|
// to project out an associated type defined by this very
|
|
|
|
// trait.
|
2017-07-11 09:33:09 -05:00
|
|
|
let unbound_trait_ref = projection.projection_ty.trait_ref(tcx);
|
2020-01-22 09:30:15 -06:00
|
|
|
if Some(unbound_trait_ref) == impl_trait_ref {
|
2015-10-20 09:15:58 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-18 16:43:31 -06:00
|
|
|
// A projection depends on its input types and determines its output
|
|
|
|
// type. For example, if we have
|
|
|
|
// `<<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`.
|
2022-01-11 21:19:52 -06:00
|
|
|
let inputs = parameters_for(&projection.projection_ty, true);
|
2021-09-30 12:38:50 -05:00
|
|
|
let relies_only_on_inputs = inputs.iter().all(|p| input_parameters.contains(p));
|
2015-10-20 09:15:58 -05:00
|
|
|
if !relies_only_on_inputs {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-08 03:28:12 -06:00
|
|
|
input_parameters.extend(parameters_for(&projection.term, false));
|
2015-10-20 09:15:58 -05:00
|
|
|
} else {
|
|
|
|
continue;
|
2015-02-12 04:16:02 -06:00
|
|
|
}
|
2015-10-20 09:15:58 -05:00
|
|
|
// fancy control flow to bypass borrow checker
|
|
|
|
predicates.swap(i, j);
|
|
|
|
i += 1;
|
|
|
|
changed = true;
|
2015-02-12 04:16:02 -06:00
|
|
|
}
|
2016-08-29 16:03:29 -05:00
|
|
|
debug!(
|
|
|
|
"setup_constraining_predicates: predicates={:?} \
|
|
|
|
i={} impl_trait_ref={:?} input_parameters={:?}",
|
2018-09-26 10:32:23 -05:00
|
|
|
predicates, i, impl_trait_ref, input_parameters
|
|
|
|
);
|
2015-02-12 04:16:02 -06:00
|
|
|
}
|
|
|
|
}
|