2023-02-20 11:37:28 +00:00
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
2023-11-22 23:44:58 +00:00
|
|
|
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
|
2024-05-10 18:59:56 +00:00
|
|
|
use rustc_type_ir::inherent::*;
|
2024-02-13 15:53:15 +00:00
|
|
|
use rustc_type_ir::visit::TypeVisitableExt;
|
2023-11-22 23:44:58 +00:00
|
|
|
use rustc_type_ir::{
|
2024-07-05 20:39:24 +00:00
|
|
|
self as ty, Canonical, CanonicalTyVarKind, CanonicalVarInfo, CanonicalVarKind, InferCtxtLike,
|
|
|
|
Interner,
|
2023-11-22 23:44:58 +00:00
|
|
|
};
|
2023-02-20 11:37:28 +00:00
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
use crate::delegate::SolverDelegate;
|
2024-06-17 15:58:38 +00:00
|
|
|
|
2023-04-10 20:02:52 +00:00
|
|
|
/// Whether we're canonicalizing a query input or the query response.
|
2023-02-20 11:37:28 +00:00
|
|
|
///
|
|
|
|
/// When canonicalizing an input we're in the context of the caller
|
|
|
|
/// while canonicalizing the response happens in the context of the
|
|
|
|
/// query.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum CanonicalizeMode {
|
|
|
|
Input,
|
2023-04-10 20:02:52 +00:00
|
|
|
/// FIXME: We currently return region constraints referring to
|
2023-02-20 11:37:28 +00:00
|
|
|
/// placeholders and inference variables from a binder instantiated
|
|
|
|
/// inside of the query.
|
|
|
|
///
|
|
|
|
/// In the long term we should eagerly deal with these constraints
|
|
|
|
/// inside of the query and only propagate constraints which are
|
|
|
|
/// actually nameable by the caller.
|
|
|
|
Response {
|
|
|
|
/// The highest universe nameable by the caller.
|
|
|
|
///
|
|
|
|
/// All variables in a universe nameable by the caller get mapped
|
|
|
|
/// to the root universe in the response and then mapped back to
|
|
|
|
/// their correct universe when applying the query response in the
|
|
|
|
/// context of the caller.
|
|
|
|
///
|
|
|
|
/// This doesn't work for universes created inside of the query so
|
|
|
|
/// we do remember their universe in the response.
|
2023-12-05 18:10:23 +00:00
|
|
|
max_input_universe: ty::UniverseIndex,
|
2023-02-20 11:37:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
pub struct Canonicalizer<'a, D: SolverDelegate<Interner = I>, I: Interner> {
|
|
|
|
delegate: &'a D,
|
2023-02-20 11:37:28 +00:00
|
|
|
canonicalize_mode: CanonicalizeMode,
|
|
|
|
|
2023-12-05 18:10:23 +00:00
|
|
|
variables: &'a mut Vec<I::GenericArg>,
|
|
|
|
primitive_var_infos: Vec<CanonicalVarInfo<I>>,
|
|
|
|
binder_index: ty::DebruijnIndex,
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
|
2023-11-22 23:44:58 +00:00
|
|
|
pub fn canonicalize<T: TypeFoldable<I>>(
|
2024-06-18 23:13:54 +00:00
|
|
|
delegate: &'a D,
|
2023-02-20 11:37:28 +00:00
|
|
|
canonicalize_mode: CanonicalizeMode,
|
2023-12-05 18:10:23 +00:00
|
|
|
variables: &'a mut Vec<I::GenericArg>,
|
2023-02-20 11:37:28 +00:00
|
|
|
value: T,
|
2023-12-05 18:10:23 +00:00
|
|
|
) -> ty::Canonical<I, T> {
|
2023-02-20 11:37:28 +00:00
|
|
|
let mut canonicalizer = Canonicalizer {
|
2024-06-18 23:13:54 +00:00
|
|
|
delegate,
|
2023-02-20 11:37:28 +00:00
|
|
|
canonicalize_mode,
|
|
|
|
|
|
|
|
variables,
|
|
|
|
primitive_var_infos: Vec::new(),
|
2023-12-05 18:10:23 +00:00
|
|
|
binder_index: ty::INNERMOST,
|
2023-02-20 11:37:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let value = value.fold_with(&mut canonicalizer);
|
2023-11-22 23:44:58 +00:00
|
|
|
// FIXME: Restore these assertions. Should we uplift type flags?
|
2024-01-30 15:59:19 +00:00
|
|
|
assert!(!value.has_infer(), "unexpected infer in {value:?}");
|
|
|
|
assert!(!value.has_placeholders(), "unexpected placeholders in {value:?}");
|
2023-02-20 11:37:28 +00:00
|
|
|
|
|
|
|
let (max_universe, variables) = canonicalizer.finalize();
|
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
let defining_opaque_types = delegate.defining_opaque_types();
|
2024-03-07 10:29:11 +00:00
|
|
|
Canonical { defining_opaque_types, max_universe, variables, value }
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2023-12-05 18:10:23 +00:00
|
|
|
fn finalize(self) -> (ty::UniverseIndex, I::CanonicalVars) {
|
2023-02-20 11:37:28 +00:00
|
|
|
let mut var_infos = self.primitive_var_infos;
|
|
|
|
// See the rustc-dev-guide section about how we deal with universes
|
|
|
|
// during canonicalization in the new solver.
|
|
|
|
match self.canonicalize_mode {
|
|
|
|
// We try to deduplicate as many query calls as possible and hide
|
|
|
|
// all information which should not matter for the solver.
|
|
|
|
//
|
|
|
|
// For this we compress universes as much as possible.
|
|
|
|
CanonicalizeMode::Input => {}
|
|
|
|
// When canonicalizing a response we map a universes already entered
|
|
|
|
// by the caller to the root universe and only return useful universe
|
|
|
|
// information for placeholders and inference variables created inside
|
|
|
|
// of the query.
|
|
|
|
CanonicalizeMode::Response { max_input_universe } => {
|
|
|
|
for var in var_infos.iter_mut() {
|
|
|
|
let uv = var.universe();
|
2023-12-05 18:10:23 +00:00
|
|
|
let new_uv = ty::UniverseIndex::from(
|
|
|
|
uv.index().saturating_sub(max_input_universe.index()),
|
|
|
|
);
|
2023-02-20 11:37:28 +00:00
|
|
|
*var = var.with_updated_universe(new_uv);
|
|
|
|
}
|
|
|
|
let max_universe = var_infos
|
|
|
|
.iter()
|
|
|
|
.map(|info| info.universe())
|
|
|
|
.max()
|
2023-12-05 18:10:23 +00:00
|
|
|
.unwrap_or(ty::UniverseIndex::ROOT);
|
2023-02-20 11:37:28 +00:00
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
let var_infos = self.delegate.cx().mk_canonical_var_infos(&var_infos);
|
2023-02-20 11:37:28 +00:00
|
|
|
return (max_universe, var_infos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a `var_infos` with existentials `En` and universals `Un` in
|
|
|
|
// universes `n`, this algorithm compresses them in place so that:
|
|
|
|
//
|
|
|
|
// - the new universe indices are as small as possible
|
2024-02-26 09:17:43 +00:00
|
|
|
// - we create a new universe if we would otherwise
|
|
|
|
// 1. put existentials from a different universe into the same one
|
|
|
|
// 2. put a placeholder in the same universe as an existential which cannot name it
|
2023-02-20 11:37:28 +00:00
|
|
|
//
|
|
|
|
// Let's walk through an example:
|
|
|
|
// - var_infos: [E0, U1, E5, U2, E2, E6, U6], curr_compressed_uv: 0, next_orig_uv: 0
|
|
|
|
// - var_infos: [E0, U1, E5, U2, E2, E6, U6], curr_compressed_uv: 0, next_orig_uv: 1
|
|
|
|
// - var_infos: [E0, U1, E5, U2, E2, E6, U6], curr_compressed_uv: 1, next_orig_uv: 2
|
|
|
|
// - var_infos: [E0, U1, E5, U1, E1, E6, U6], curr_compressed_uv: 1, next_orig_uv: 5
|
2024-02-26 09:17:43 +00:00
|
|
|
// - var_infos: [E0, U1, E2, U1, E1, E6, U6], curr_compressed_uv: 2, next_orig_uv: 6
|
|
|
|
// - var_infos: [E0, U1, E1, U1, E1, E3, U3], curr_compressed_uv: 2, next_orig_uv: -
|
2023-02-20 11:37:28 +00:00
|
|
|
//
|
2023-07-27 03:16:34 +00:00
|
|
|
// This algorithm runs in `O(n²)` where `n` is the number of different universe
|
|
|
|
// indices in the input. This should be fine as `n` is expected to be small.
|
2023-12-05 18:10:23 +00:00
|
|
|
let mut curr_compressed_uv = ty::UniverseIndex::ROOT;
|
2024-02-26 09:17:43 +00:00
|
|
|
let mut existential_in_new_uv = None;
|
2023-12-05 18:10:23 +00:00
|
|
|
let mut next_orig_uv = Some(ty::UniverseIndex::ROOT);
|
2023-02-20 11:37:28 +00:00
|
|
|
while let Some(orig_uv) = next_orig_uv.take() {
|
2023-11-22 23:44:58 +00:00
|
|
|
let mut update_uv = |var: &mut CanonicalVarInfo<I>, orig_uv, is_existential| {
|
2023-02-20 11:37:28 +00:00
|
|
|
let uv = var.universe();
|
|
|
|
match uv.cmp(&orig_uv) {
|
|
|
|
Ordering::Less => (), // Already updated
|
|
|
|
Ordering::Equal => {
|
|
|
|
if is_existential {
|
2024-02-26 09:17:43 +00:00
|
|
|
if existential_in_new_uv.is_some_and(|uv| uv < orig_uv) {
|
|
|
|
// Condition 1.
|
|
|
|
//
|
|
|
|
// We already put an existential from a outer universe
|
|
|
|
// into the current compressed universe, so we need to
|
|
|
|
// create a new one.
|
|
|
|
curr_compressed_uv = curr_compressed_uv.next_universe();
|
|
|
|
}
|
|
|
|
|
|
|
|
// `curr_compressed_uv` will now contain an existential from
|
|
|
|
// `orig_uv`. Trying to canonicalizing an existential from
|
|
|
|
// a higher universe has to therefore use a new compressed
|
|
|
|
// universe.
|
|
|
|
existential_in_new_uv = Some(orig_uv);
|
|
|
|
} else if existential_in_new_uv.is_some() {
|
|
|
|
// Condition 2.
|
|
|
|
//
|
2023-02-20 11:37:28 +00:00
|
|
|
// `var` is a placeholder from a universe which is not nameable
|
|
|
|
// by an existential which we already put into the compressed
|
|
|
|
// universe `curr_compressed_uv`. We therefore have to create a
|
|
|
|
// new universe for `var`.
|
|
|
|
curr_compressed_uv = curr_compressed_uv.next_universe();
|
2024-02-26 09:17:43 +00:00
|
|
|
existential_in_new_uv = None;
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*var = var.with_updated_universe(curr_compressed_uv);
|
|
|
|
}
|
|
|
|
Ordering::Greater => {
|
|
|
|
// We can ignore this variable in this iteration. We only look at
|
|
|
|
// universes which actually occur in the input for performance.
|
|
|
|
//
|
|
|
|
// For this we set `next_orig_uv` to the next smallest, not yet compressed,
|
|
|
|
// universe of the input.
|
|
|
|
if next_orig_uv.map_or(true, |curr_next_uv| uv.cannot_name(curr_next_uv)) {
|
|
|
|
next_orig_uv = Some(uv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// For each universe which occurs in the input, we first iterate over all
|
|
|
|
// placeholders and then over all inference variables.
|
|
|
|
//
|
|
|
|
// Whenever we compress the universe of a placeholder, no existential with
|
|
|
|
// an already compressed universe can name that placeholder.
|
|
|
|
for is_existential in [false, true] {
|
|
|
|
for var in var_infos.iter_mut() {
|
|
|
|
// We simply put all regions from the input into the highest
|
|
|
|
// compressed universe, so we only deal with them at the end.
|
|
|
|
if !var.is_region() {
|
|
|
|
if is_existential == var.is_existential() {
|
|
|
|
update_uv(var, orig_uv, is_existential)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 09:17:43 +00:00
|
|
|
// We uniquify regions and always put them into their own universe
|
|
|
|
let mut first_region = true;
|
2023-02-20 11:37:28 +00:00
|
|
|
for var in var_infos.iter_mut() {
|
|
|
|
if var.is_region() {
|
2024-02-26 09:17:43 +00:00
|
|
|
if first_region {
|
|
|
|
first_region = false;
|
|
|
|
curr_compressed_uv = curr_compressed_uv.next_universe();
|
|
|
|
}
|
2023-02-20 11:37:28 +00:00
|
|
|
assert!(var.is_existential());
|
2023-12-05 18:10:23 +00:00
|
|
|
*var = var.with_updated_universe(curr_compressed_uv);
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
let var_infos = self.delegate.cx().mk_canonical_var_infos(&var_infos);
|
2023-02-20 11:37:28 +00:00
|
|
|
(curr_compressed_uv, var_infos)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicalizer<'_, D, I> {
|
|
|
|
fn cx(&self) -> I {
|
|
|
|
self.delegate.cx()
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2024-05-20 16:57:07 +00:00
|
|
|
fn fold_binder<T>(&mut self, t: ty::Binder<I, T>) -> ty::Binder<I, T>
|
2023-02-20 11:37:28 +00:00
|
|
|
where
|
2023-11-22 23:44:58 +00:00
|
|
|
T: TypeFoldable<I>,
|
2023-02-20 11:37:28 +00:00
|
|
|
{
|
|
|
|
self.binder_index.shift_in(1);
|
|
|
|
let t = t.super_fold_with(self);
|
|
|
|
self.binder_index.shift_out(1);
|
|
|
|
t
|
|
|
|
}
|
|
|
|
|
2023-11-22 23:44:58 +00:00
|
|
|
fn fold_region(&mut self, r: I::Region) -> I::Region {
|
|
|
|
let kind = match r.kind() {
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::ReBound(..) => return r,
|
2023-02-20 11:37:28 +00:00
|
|
|
|
2023-10-04 21:58:38 +00:00
|
|
|
// We may encounter `ReStatic` in item signatures or the hidden type
|
|
|
|
// of an opaque. `ReErased` should only be encountered in the hidden
|
|
|
|
// type of an opaque for regions that are ignored for the purposes of
|
|
|
|
// captures.
|
|
|
|
//
|
|
|
|
// FIXME: We should investigate the perf implications of not uniquifying
|
|
|
|
// `ReErased`. We may be able to short-circuit registering region
|
|
|
|
// obligations if we encounter a `ReErased` on one side, for example.
|
2024-03-22 20:35:44 +00:00
|
|
|
ty::ReStatic | ty::ReErased | ty::ReError(_) => match self.canonicalize_mode {
|
2023-12-05 18:10:23 +00:00
|
|
|
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
|
2023-02-20 11:37:28 +00:00
|
|
|
CanonicalizeMode::Response { .. } => return r,
|
|
|
|
},
|
|
|
|
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::ReEarlyParam(_) | ty::ReLateParam(_) => match self.canonicalize_mode {
|
|
|
|
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
|
|
|
|
CanonicalizeMode::Response { .. } => {
|
|
|
|
panic!("unexpected region in response: {r:?}")
|
2023-11-22 23:44:58 +00:00
|
|
|
}
|
2023-12-05 18:10:23 +00:00
|
|
|
},
|
2023-02-20 11:37:28 +00:00
|
|
|
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::RePlaceholder(placeholder) => match self.canonicalize_mode {
|
2023-02-20 11:37:28 +00:00
|
|
|
// We canonicalize placeholder regions as existentials in query inputs.
|
2023-12-05 18:10:23 +00:00
|
|
|
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
|
2023-02-20 11:37:28 +00:00
|
|
|
CanonicalizeMode::Response { max_input_universe } => {
|
|
|
|
// If we have a placeholder region inside of a query, it must be from
|
|
|
|
// a new universe.
|
2023-11-22 23:44:58 +00:00
|
|
|
if max_input_universe.can_name(placeholder.universe()) {
|
|
|
|
panic!("new placeholder in universe {max_input_universe:?}: {r:?}");
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
CanonicalVarKind::PlaceholderRegion(placeholder)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::ReVar(vid) => {
|
2023-11-22 23:44:58 +00:00
|
|
|
assert_eq!(
|
2024-06-18 23:13:54 +00:00
|
|
|
self.delegate.opportunistic_resolve_lt_var(vid),
|
2024-05-19 17:04:44 +00:00
|
|
|
r,
|
2023-11-22 23:44:58 +00:00
|
|
|
"region vid should have been resolved fully before canonicalization"
|
|
|
|
);
|
|
|
|
match self.canonicalize_mode {
|
2023-12-05 18:10:23 +00:00
|
|
|
CanonicalizeMode::Input => CanonicalVarKind::Region(ty::UniverseIndex::ROOT),
|
2023-11-22 23:44:58 +00:00
|
|
|
CanonicalizeMode::Response { .. } => {
|
2024-06-18 23:13:54 +00:00
|
|
|
CanonicalVarKind::Region(self.delegate.universe_of_lt(vid).unwrap())
|
2023-11-22 23:44:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-20 11:37:28 +00:00
|
|
|
};
|
|
|
|
|
2023-07-27 03:16:34 +00:00
|
|
|
let existing_bound_var = match self.canonicalize_mode {
|
|
|
|
CanonicalizeMode::Input => None,
|
|
|
|
CanonicalizeMode::Response { .. } => {
|
2023-12-05 18:10:23 +00:00
|
|
|
self.variables.iter().position(|&v| v == r.into()).map(ty::BoundVar::from)
|
2023-07-27 03:16:34 +00:00
|
|
|
}
|
|
|
|
};
|
2023-11-22 23:44:58 +00:00
|
|
|
|
2023-07-27 03:16:34 +00:00
|
|
|
let var = existing_bound_var.unwrap_or_else(|| {
|
2023-12-05 18:10:23 +00:00
|
|
|
let var = ty::BoundVar::from(self.variables.len());
|
2023-07-27 03:16:34 +00:00
|
|
|
self.variables.push(r.into());
|
|
|
|
self.primitive_var_infos.push(CanonicalVarInfo { kind });
|
|
|
|
var
|
|
|
|
});
|
2023-11-22 23:44:58 +00:00
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
Region::new_anon_bound(self.cx(), self.binder_index, var)
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 16:17:17 +00:00
|
|
|
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
|
2023-11-22 23:44:58 +00:00
|
|
|
let kind = match t.kind() {
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::Infer(i) => match i {
|
|
|
|
ty::TyVar(vid) => {
|
2023-11-22 23:44:58 +00:00
|
|
|
assert_eq!(
|
2024-06-18 23:13:54 +00:00
|
|
|
self.delegate.opportunistic_resolve_ty_var(vid),
|
2024-05-19 17:04:44 +00:00
|
|
|
t,
|
2023-11-22 23:44:58 +00:00
|
|
|
"ty vid should have been resolved fully before canonicalization"
|
|
|
|
);
|
|
|
|
|
|
|
|
CanonicalVarKind::Ty(CanonicalTyVarKind::General(
|
2024-06-18 23:13:54 +00:00
|
|
|
self.delegate
|
2023-11-22 23:44:58 +00:00
|
|
|
.universe_of_ty(vid)
|
|
|
|
.unwrap_or_else(|| panic!("ty var should have been resolved: {t:?}")),
|
|
|
|
))
|
|
|
|
}
|
2024-05-19 17:04:44 +00:00
|
|
|
ty::IntVar(vid) => {
|
|
|
|
assert_eq!(
|
2024-06-18 23:13:54 +00:00
|
|
|
self.delegate.opportunistic_resolve_int_var(vid),
|
2024-05-19 17:04:44 +00:00
|
|
|
t,
|
|
|
|
"ty vid should have been resolved fully before canonicalization"
|
|
|
|
);
|
|
|
|
CanonicalVarKind::Ty(CanonicalTyVarKind::Int)
|
|
|
|
}
|
|
|
|
ty::FloatVar(vid) => {
|
|
|
|
assert_eq!(
|
2024-06-18 23:13:54 +00:00
|
|
|
self.delegate.opportunistic_resolve_float_var(vid),
|
2024-05-19 17:04:44 +00:00
|
|
|
t,
|
|
|
|
"ty vid should have been resolved fully before canonicalization"
|
|
|
|
);
|
|
|
|
CanonicalVarKind::Ty(CanonicalTyVarKind::Float)
|
|
|
|
}
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
|
2024-05-19 17:04:44 +00:00
|
|
|
panic!("fresh vars not expected in canonicalization")
|
2023-11-22 23:44:58 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::Placeholder(placeholder) => match self.canonicalize_mode {
|
|
|
|
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(PlaceholderLike::new(
|
2023-11-22 23:44:58 +00:00
|
|
|
placeholder.universe(),
|
|
|
|
self.variables.len().into(),
|
|
|
|
)),
|
2023-02-20 11:37:28 +00:00
|
|
|
CanonicalizeMode::Response { .. } => CanonicalVarKind::PlaceholderTy(placeholder),
|
|
|
|
},
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::Param(_) => match self.canonicalize_mode {
|
|
|
|
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderTy(PlaceholderLike::new(
|
|
|
|
ty::UniverseIndex::ROOT,
|
2023-11-22 23:44:58 +00:00
|
|
|
self.variables.len().into(),
|
|
|
|
)),
|
|
|
|
CanonicalizeMode::Response { .. } => panic!("param ty in response: {t:?}"),
|
2023-02-20 11:37:28 +00:00
|
|
|
},
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Adt(_, _)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Str
|
|
|
|
| ty::Array(_, _)
|
|
|
|
| ty::Slice(_)
|
2024-03-21 21:11:06 +00:00
|
|
|
| ty::RawPtr(_, _)
|
2023-12-05 18:10:23 +00:00
|
|
|
| ty::Ref(_, _, _)
|
2023-02-02 13:57:36 +00:00
|
|
|
| ty::Pat(_, _)
|
2023-12-05 18:10:23 +00:00
|
|
|
| ty::FnDef(_, _)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::Dynamic(_, _, _)
|
2024-01-24 18:01:56 +00:00
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::CoroutineClosure(..)
|
2023-12-21 01:52:10 +00:00
|
|
|
| ty::Coroutine(_, _)
|
2023-12-05 18:10:23 +00:00
|
|
|
| ty::CoroutineWitness(..)
|
|
|
|
| ty::Never
|
|
|
|
| ty::Tuple(_)
|
|
|
|
| ty::Alias(_, _)
|
|
|
|
| ty::Bound(_, _)
|
|
|
|
| ty::Error(_) => return t.super_fold_with(self),
|
2023-02-20 11:37:28 +00:00
|
|
|
};
|
|
|
|
|
2023-12-05 18:10:23 +00:00
|
|
|
let var = ty::BoundVar::from(
|
|
|
|
self.variables.iter().position(|&v| v == t.into()).unwrap_or_else(|| {
|
2023-02-20 11:37:28 +00:00
|
|
|
let var = self.variables.len();
|
2023-12-05 18:10:23 +00:00
|
|
|
self.variables.push(t.into());
|
2023-02-20 11:37:28 +00:00
|
|
|
self.primitive_var_infos.push(CanonicalVarInfo { kind });
|
|
|
|
var
|
2023-12-05 18:10:23 +00:00
|
|
|
}),
|
|
|
|
);
|
2023-11-22 23:44:58 +00:00
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
Ty::new_anon_bound(self.cx(), self.binder_index, var)
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 16:17:17 +00:00
|
|
|
fn fold_const(&mut self, c: I::Const) -> I::Const {
|
2023-02-20 11:37:28 +00:00
|
|
|
let kind = match c.kind() {
|
2024-03-28 16:10:44 +00:00
|
|
|
ty::ConstKind::Infer(i) => match i {
|
|
|
|
ty::InferConst::Var(vid) => {
|
|
|
|
assert_eq!(
|
2024-06-18 23:13:54 +00:00
|
|
|
self.delegate.opportunistic_resolve_ct_var(vid),
|
2024-06-03 00:10:24 +00:00
|
|
|
c,
|
|
|
|
"const vid should have been resolved fully before canonicalization"
|
2024-03-28 16:10:44 +00:00
|
|
|
);
|
2024-06-18 23:13:54 +00:00
|
|
|
CanonicalVarKind::Const(self.delegate.universe_of_ct(vid).unwrap())
|
2023-11-22 23:44:58 +00:00
|
|
|
}
|
2024-03-28 16:10:44 +00:00
|
|
|
ty::InferConst::EffectVar(_) => CanonicalVarKind::Effect,
|
|
|
|
ty::InferConst::Fresh(_) => todo!(),
|
|
|
|
},
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::ConstKind::Placeholder(placeholder) => match self.canonicalize_mode {
|
2023-02-20 11:37:28 +00:00
|
|
|
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderConst(
|
2023-12-05 18:10:23 +00:00
|
|
|
PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),
|
2023-02-20 11:37:28 +00:00
|
|
|
),
|
|
|
|
CanonicalizeMode::Response { .. } => {
|
2024-06-03 00:10:24 +00:00
|
|
|
CanonicalVarKind::PlaceholderConst(placeholder)
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::ConstKind::Param(_) => match self.canonicalize_mode {
|
2023-02-20 11:37:28 +00:00
|
|
|
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderConst(
|
2023-12-05 18:10:23 +00:00
|
|
|
PlaceholderLike::new(ty::UniverseIndex::ROOT, self.variables.len().into()),
|
2023-02-20 11:37:28 +00:00
|
|
|
),
|
2023-11-22 23:44:58 +00:00
|
|
|
CanonicalizeMode::Response { .. } => panic!("param ty in response: {c:?}"),
|
2023-02-20 11:37:28 +00:00
|
|
|
},
|
2024-03-28 16:10:44 +00:00
|
|
|
// FIXME: See comment above -- we could fold the region separately or something.
|
2023-12-05 18:10:23 +00:00
|
|
|
ty::ConstKind::Bound(_, _)
|
|
|
|
| ty::ConstKind::Unevaluated(_)
|
2024-06-03 01:34:35 +00:00
|
|
|
| ty::ConstKind::Value(_, _)
|
2023-12-05 18:10:23 +00:00
|
|
|
| ty::ConstKind::Error(_)
|
|
|
|
| ty::ConstKind::Expr(_) => return c.super_fold_with(self),
|
2023-02-20 11:37:28 +00:00
|
|
|
};
|
|
|
|
|
2023-12-05 18:10:23 +00:00
|
|
|
let var = ty::BoundVar::from(
|
|
|
|
self.variables.iter().position(|&v| v == c.into()).unwrap_or_else(|| {
|
2023-02-20 11:37:28 +00:00
|
|
|
let var = self.variables.len();
|
2023-12-05 18:10:23 +00:00
|
|
|
self.variables.push(c.into());
|
2023-02-20 11:37:28 +00:00
|
|
|
self.primitive_var_infos.push(CanonicalVarInfo { kind });
|
|
|
|
var
|
2023-12-05 18:10:23 +00:00
|
|
|
}),
|
|
|
|
);
|
2023-11-22 23:44:58 +00:00
|
|
|
|
2024-06-18 23:13:54 +00:00
|
|
|
Const::new_anon_bound(self.cx(), self.binder_index, var)
|
2023-02-20 11:37:28 +00:00
|
|
|
}
|
|
|
|
}
|