2020-03-05 21:07:42 +00:00
|
|
|
//! Module for inferring the variance of type and lifetime parameters. See the [rustc dev guide]
|
2018-03-13 01:05:18 +00:00
|
|
|
//! chapter for more info.
|
|
|
|
//!
|
2020-03-09 21:33:04 +00:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/variance.html
|
2016-02-03 16:37:23 +00:00
|
|
|
|
2020-10-20 09:05:00 +00:00
|
|
|
use rustc_arena::DroplessArena;
|
2022-03-30 10:25:23 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2022-10-23 14:40:14 +00:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_middle::ty::query::Providers;
|
2022-12-04 13:54:41 +00:00
|
|
|
use rustc_middle::ty::{self, CrateVariancesMap, SubstsRef, Ty, TyCtxt};
|
2023-02-22 15:51:17 +00:00
|
|
|
use rustc_middle::ty::{TypeSuperVisitable, TypeVisitable};
|
2022-10-23 14:40:14 +00:00
|
|
|
use std::ops::ControlFlow;
|
2016-02-03 16:37:23 +00:00
|
|
|
|
|
|
|
/// Defines the `TermsContext` basically houses an arena where we can
|
|
|
|
/// allocate terms.
|
|
|
|
mod terms;
|
|
|
|
|
|
|
|
/// Code to gather up constraints.
|
|
|
|
mod constraints;
|
|
|
|
|
|
|
|
/// Code to solve constraints and write out the results.
|
|
|
|
mod solve;
|
|
|
|
|
2017-04-25 09:45:59 +00:00
|
|
|
/// Code to write unit tests of variance.
|
|
|
|
pub mod test;
|
|
|
|
|
2016-02-03 16:37:23 +00:00
|
|
|
/// Code for transforming variances.
|
|
|
|
mod xform;
|
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2017-04-24 15:15:12 +00:00
|
|
|
*providers = Providers { variances_of, crate_variances, ..*providers };
|
|
|
|
}
|
|
|
|
|
2021-05-11 10:29:52 +00:00
|
|
|
fn crate_variances(tcx: TyCtxt<'_>, (): ()) -> CrateVariancesMap<'_> {
|
2020-10-20 09:05:00 +00:00
|
|
|
let arena = DroplessArena::default();
|
|
|
|
let terms_cx = terms::determine_parameters_to_be_inferred(tcx, &arena);
|
2016-02-03 21:00:23 +00:00
|
|
|
let constraints_cx = constraints::add_constraints_from_crate(terms_cx);
|
2020-03-27 19:26:20 +00:00
|
|
|
solve::solve_constraints(constraints_cx)
|
2017-04-24 15:15:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 18:54:05 +00:00
|
|
|
fn variances_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
|
2022-06-28 19:46:42 +00:00
|
|
|
// Skip items with no generics - there's nothing to infer in them.
|
|
|
|
if tcx.generics_of(item_def_id).count() == 0 {
|
|
|
|
return &[];
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:25:23 +00:00
|
|
|
match tcx.def_kind(item_def_id) {
|
|
|
|
DefKind::Fn
|
|
|
|
| DefKind::AssocFn
|
|
|
|
| DefKind::Enum
|
|
|
|
| DefKind::Struct
|
|
|
|
| DefKind::Union
|
|
|
|
| DefKind::Variant
|
|
|
|
| DefKind::Ctor(..) => {}
|
2022-10-23 14:40:14 +00:00
|
|
|
DefKind::OpaqueTy | DefKind::ImplTraitPlaceholder => {
|
2023-03-13 18:54:05 +00:00
|
|
|
return variance_of_opaque(tcx, item_def_id);
|
2022-10-23 14:40:14 +00:00
|
|
|
}
|
2022-03-30 10:25:23 +00:00
|
|
|
_ => {
|
|
|
|
// Variance not relevant.
|
|
|
|
span_bug!(tcx.def_span(item_def_id), "asked to compute variance for wrong kind of item")
|
|
|
|
}
|
2017-06-02 19:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Everything else must be inferred.
|
|
|
|
|
2021-05-11 10:29:52 +00:00
|
|
|
let crate_map = tcx.crate_variances(());
|
2023-03-13 18:54:05 +00:00
|
|
|
crate_map.variances.get(&item_def_id.to_def_id()).copied().unwrap_or(&[])
|
2016-02-03 16:37:23 +00:00
|
|
|
}
|
2022-10-23 14:40:14 +00:00
|
|
|
|
|
|
|
#[instrument(level = "trace", skip(tcx), ret)]
|
|
|
|
fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Variance] {
|
|
|
|
let generics = tcx.generics_of(item_def_id);
|
|
|
|
|
|
|
|
// Opaque types may only use regions that are bound. So for
|
|
|
|
// ```rust
|
|
|
|
// type Foo<'a, 'b, 'c> = impl Trait<'a> + 'b;
|
|
|
|
// ```
|
|
|
|
// we may not use `'c` in the hidden type.
|
2022-12-04 13:54:41 +00:00
|
|
|
struct OpaqueTypeLifetimeCollector<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
root_def_id: DefId,
|
2022-10-23 14:40:14 +00:00
|
|
|
variances: Vec<ty::Variance>,
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:54:41 +00:00
|
|
|
impl<'tcx> OpaqueTypeLifetimeCollector<'tcx> {
|
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn visit_opaque(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) -> ControlFlow<!> {
|
|
|
|
if def_id != self.root_def_id && self.tcx.is_descendant_of(def_id, self.root_def_id) {
|
|
|
|
let child_variances = self.tcx.variances_of(def_id);
|
|
|
|
for (a, v) in substs.iter().zip(child_variances) {
|
|
|
|
if *v != ty::Bivariant {
|
|
|
|
a.visit_with(self)?;
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 07:17:13 +00:00
|
|
|
ControlFlow::Continue(())
|
2022-12-04 13:54:41 +00:00
|
|
|
} else {
|
|
|
|
substs.visit_with(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 02:18:40 +00:00
|
|
|
impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeLifetimeCollector<'tcx> {
|
2022-10-23 14:40:14 +00:00
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
|
|
|
|
if let ty::RegionKind::ReEarlyBound(ebr) = r.kind() {
|
|
|
|
self.variances[ebr.index as usize] = ty::Invariant;
|
|
|
|
}
|
2023-04-14 23:31:11 +00:00
|
|
|
ControlFlow::Continue(())
|
2022-10-23 14:40:14 +00:00
|
|
|
}
|
2022-12-04 13:54:41 +00:00
|
|
|
|
|
|
|
#[instrument(level = "trace", skip(self), ret)]
|
|
|
|
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
|
2022-11-26 21:32:01 +00:00
|
|
|
match t.kind() {
|
2022-12-13 11:07:42 +00:00
|
|
|
ty::Alias(_, ty::AliasTy { def_id, substs, .. })
|
2023-03-14 21:28:48 +00:00
|
|
|
if matches!(self.tcx.def_kind(*def_id), DefKind::OpaqueTy) =>
|
|
|
|
{
|
|
|
|
self.visit_opaque(*def_id, substs)
|
|
|
|
}
|
|
|
|
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) check whether this is necessary
|
|
|
|
// at all for RPITITs.
|
|
|
|
ty::Alias(_, ty::AliasTy { def_id, substs, .. })
|
|
|
|
if self.tcx.is_impl_trait_in_trait(*def_id) =>
|
2022-12-04 13:54:41 +00:00
|
|
|
{
|
2022-12-13 17:54:52 +00:00
|
|
|
self.visit_opaque(*def_id, substs)
|
2022-12-04 13:54:41 +00:00
|
|
|
}
|
|
|
|
_ => t.super_visit_with(self),
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 14:40:14 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 18:21:21 +00:00
|
|
|
// By default, RPIT are invariant wrt type and const generics, but they are bivariant wrt
|
2022-10-23 14:40:14 +00:00
|
|
|
// lifetime generics.
|
|
|
|
let mut variances: Vec<_> = std::iter::repeat(ty::Invariant).take(generics.count()).collect();
|
|
|
|
|
|
|
|
// Mark all lifetimes from parent generics as unused (Bivariant).
|
|
|
|
// This will be overridden later if required.
|
|
|
|
{
|
|
|
|
let mut generics = generics;
|
|
|
|
while let Some(def_id) = generics.parent {
|
|
|
|
generics = tcx.generics_of(def_id);
|
|
|
|
for param in &generics.params {
|
|
|
|
match param.kind {
|
|
|
|
ty::GenericParamDefKind::Lifetime => {
|
|
|
|
variances[param.index as usize] = ty::Bivariant;
|
|
|
|
}
|
|
|
|
ty::GenericParamDefKind::Type { .. }
|
|
|
|
| ty::GenericParamDefKind::Const { .. } => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:54:41 +00:00
|
|
|
let mut collector =
|
|
|
|
OpaqueTypeLifetimeCollector { tcx, root_def_id: item_def_id.to_def_id(), variances };
|
2023-03-13 19:35:29 +00:00
|
|
|
let id_substs = ty::InternalSubsts::identity_for_item(tcx, item_def_id);
|
2023-04-17 23:19:08 +00:00
|
|
|
for (pred, _) in tcx.explicit_item_bounds(item_def_id).subst_iter_copied(tcx, id_substs) {
|
2022-10-23 14:40:14 +00:00
|
|
|
debug!(?pred);
|
|
|
|
|
|
|
|
// We only ignore opaque type substs if the opaque type is the outermost type.
|
|
|
|
// The opaque type may be nested within itself via recursion in e.g.
|
|
|
|
// type Foo<'a> = impl PartialEq<Foo<'a>>;
|
|
|
|
// which thus mentions `'a` and should thus accept hidden types that borrow 'a
|
|
|
|
// instead of requiring an additional `+ 'a`.
|
|
|
|
match pred.kind().skip_binder() {
|
2022-11-24 21:14:58 +00:00
|
|
|
ty::PredicateKind::Clause(ty::Clause::Trait(ty::TraitPredicate {
|
2022-12-13 11:18:58 +00:00
|
|
|
trait_ref: ty::TraitRef { def_id: _, substs, .. },
|
2022-10-23 14:40:14 +00:00
|
|
|
constness: _,
|
|
|
|
polarity: _,
|
2022-11-24 21:14:58 +00:00
|
|
|
})) => {
|
2022-10-23 14:40:14 +00:00
|
|
|
for subst in &substs[1..] {
|
|
|
|
subst.visit_with(&mut collector);
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 21:14:58 +00:00
|
|
|
ty::PredicateKind::Clause(ty::Clause::Projection(ty::ProjectionPredicate {
|
2022-12-13 11:07:42 +00:00
|
|
|
projection_ty: ty::AliasTy { substs, .. },
|
2022-10-23 14:40:14 +00:00
|
|
|
term,
|
2022-11-24 21:14:58 +00:00
|
|
|
})) => {
|
2022-10-23 14:40:14 +00:00
|
|
|
for subst in &substs[1..] {
|
|
|
|
subst.visit_with(&mut collector);
|
|
|
|
}
|
|
|
|
term.visit_with(&mut collector);
|
|
|
|
}
|
2022-11-24 21:14:58 +00:00
|
|
|
ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate(
|
|
|
|
_,
|
|
|
|
region,
|
|
|
|
))) => {
|
2022-10-23 14:40:14 +00:00
|
|
|
region.visit_with(&mut collector);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
pred.visit_with(&mut collector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tcx.arena.alloc_from_iter(collector.variances.into_iter())
|
|
|
|
}
|