2020-03-05 18:07:42 -03:00
|
|
|
//! Module for inferring the variance of type and lifetime parameters. See the [rustc dev guide]
|
2018-03-12 20:05:18 -05:00
|
|
|
//! chapter for more info.
|
|
|
|
|
//!
|
2020-03-09 18:33:04 -03:00
|
|
|
//! [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/variance.html
|
2016-02-03 11:37:23 -05:00
|
|
|
|
2020-10-20 11:05:00 +02:00
|
|
|
use rustc_arena::DroplessArena;
|
2022-03-30 12:25:23 +02:00
|
|
|
use rustc_hir::def::DefKind;
|
2021-05-11 12:29:52 +02:00
|
|
|
use rustc_hir::def_id::DefId;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
|
use rustc_middle::ty::{self, CrateVariancesMap, TyCtxt};
|
2016-02-03 11:37:23 -05: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 05:45:59 -04:00
|
|
|
/// Code to write unit tests of variance.
|
|
|
|
|
pub mod test;
|
|
|
|
|
|
2016-02-03 11:37:23 -05:00
|
|
|
/// Code for transforming variances.
|
|
|
|
|
mod xform;
|
|
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-12-22 17:42:04 -05:00
|
|
|
*providers = Providers { variances_of, crate_variances, ..*providers };
|
2017-04-24 11:15:12 -04:00
|
|
|
}
|
|
|
|
|
|
2021-05-11 12:29:52 +02:00
|
|
|
fn crate_variances(tcx: TyCtxt<'_>, (): ()) -> CrateVariancesMap<'_> {
|
2020-10-20 11:05:00 +02:00
|
|
|
let arena = DroplessArena::default();
|
|
|
|
|
let terms_cx = terms::determine_parameters_to_be_inferred(tcx, &arena);
|
2016-02-03 16:00:23 -05:00
|
|
|
let constraints_cx = constraints::add_constraints_from_crate(terms_cx);
|
2020-03-27 20:26:20 +01:00
|
|
|
solve::solve_constraints(constraints_cx)
|
2017-04-24 11:15:12 -04:00
|
|
|
}
|
|
|
|
|
|
2019-06-21 23:49:03 +02:00
|
|
|
fn variances_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[ty::Variance] {
|
2022-06-28 12:46:42 -07: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 12:25:23 +02:00
|
|
|
match tcx.def_kind(item_def_id) {
|
|
|
|
|
DefKind::Fn
|
|
|
|
|
| DefKind::AssocFn
|
|
|
|
|
| DefKind::Enum
|
|
|
|
|
| DefKind::Struct
|
|
|
|
|
| DefKind::Union
|
|
|
|
|
| DefKind::Variant
|
|
|
|
|
| DefKind::Ctor(..) => {}
|
|
|
|
|
_ => {
|
|
|
|
|
// Variance not relevant.
|
|
|
|
|
span_bug!(tcx.def_span(item_def_id), "asked to compute variance for wrong kind of item")
|
|
|
|
|
}
|
2017-06-02 22:05:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Everything else must be inferred.
|
|
|
|
|
|
2021-05-11 12:29:52 +02:00
|
|
|
let crate_map = tcx.crate_variances(());
|
2020-02-29 13:14:52 +01:00
|
|
|
crate_map.variances.get(&item_def_id).copied().unwrap_or(&[])
|
2016-02-03 11:37:23 -05:00
|
|
|
}
|