frontend 0.4.0

rustc's frontend with no LLVM and no std: parsing through MIR, as a library
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use hashbrown::hash_map::Entry;
use core::mem;

use crate::rustc_type_ir::inherent::*;
use crate::rustc_type_ir::solve::{Goal, QueryInput};
use crate::rustc_type_ir::{
    self as ty, Canonical, CanonicalParamEnvCacheEntry, CanonicalVarKind, CanonicalizerState,
    Flags, InferCtxtLike, Interner, PlaceholderConst, PlaceholderType, Region, TypeFlags,
    TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
};
use thin_vec::ThinVec;

use crate::rustc_next_trait_solver::delegate::SolverDelegate;

/// Does this have infer/placeholder/param, free regions or ReErased?
const NEEDS_CANONICAL: TypeFlags = TypeFlags::from_bits(
    TypeFlags::HAS_INFER.bits()
        | TypeFlags::HAS_PLACEHOLDER.bits()
        | TypeFlags::HAS_PARAM.bits()
        | TypeFlags::HAS_FREE_REGIONS.bits()
        | TypeFlags::HAS_RE_ERASED.bits(),
)
.unwrap();

#[derive(Debug, Clone, Copy)]
enum CanonicalizeInputKind {
    /// When canonicalizing the `param_env`, we keep `'static` as merging
    /// trait candidates relies on it when deciding whether a where-bound
    /// is trivial.
    ParamEnv,
    /// When canonicalizing predicates, we don't keep `'static`.
    Predicate,
}

/// Whether we're canonicalizing a query input or the query response.
///
/// 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)]
enum CanonicalizeMode {
    Input(CanonicalizeInputKind),
    /// FIXME: We currently return region constraints referring to
    /// 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.
        max_input_universe: ty::UniverseIndex,
    },
}

pub(super) struct Canonicalizer<'a, D: SolverDelegate<Interner = I>, I: Interner> {
    delegate: &'a D,

    // Immutable field.
    canonicalize_mode: CanonicalizeMode,

    // Mutable fields.
    state: CanonicalizerState<I>,
}

impl<'a, D: SolverDelegate<Interner = I>, I: Interner> Canonicalizer<'a, D, I> {
    fn new(delegate: &'a D, canonicalize_mode: CanonicalizeMode) -> Self {
        Canonicalizer { delegate, canonicalize_mode, state: delegate.obtain_canonicalizer_state() }
    }

    pub(super) fn canonicalize_response<T: TypeFoldable<I>>(
        delegate: &'a D,
        max_input_universe: ty::UniverseIndex,
        value: T,
    ) -> ty::Canonical<I, T> {
        let mut canonicalizer =
            Canonicalizer::new(delegate, CanonicalizeMode::Response { max_input_universe });
        let value = if value.has_type_flags(NEEDS_CANONICAL) {
            value.fold_with(&mut canonicalizer)
        } else {
            value
        };
        debug_assert!(!value.has_infer(), "unexpected infer in {value:?}");
        debug_assert!(!value.has_placeholders(), "unexpected placeholders in {value:?}");
        let (max_universe, _variables, var_kinds) = canonicalizer.finalize();

        Canonical { max_universe, var_kinds, value }
    }

    // The return value is the canonicalized `param_env`, plus a canonicalizer suitable for
    // canonicalizing the rest of the input. (For efficiency, and when appropriate, the returned
    // canonicalizer will be the same one used on `param_env`, with suitable modifications.)
    fn canonicalize_param_env(delegate: &'a D, param_env: I::ParamEnv) -> (I::ParamEnv, Self) {
        if !param_env.has_type_flags(NEEDS_CANONICAL) {
            let rest_canonicalizer = Canonicalizer::new(
                delegate,
                CanonicalizeMode::Input(CanonicalizeInputKind::Predicate),
            );

            return (param_env, rest_canonicalizer);
        }

        // Do the `env` canonicalization, and then convert the canonicalizer to `rest` form for
        // subsequent use.
        let do_env_and_make_rest = || {
            let mut env_canonicalizer = Canonicalizer::new(
                delegate,
                CanonicalizeMode::Input(CanonicalizeInputKind::ParamEnv),
            );
            let param_env = param_env.fold_with(&mut env_canonicalizer);

            debug_assert!(env_canonicalizer.state.sub_root_lookup_table.is_empty());

            // Transform the `env_canonicalizer` into the `rest_canonicalizer`, keeping some things
            // and replacing others.
            //
            // We do not reuse the cache as it may contain entries whose canonicalized
            // value contains `'static`. While we could alternatively handle this by
            // checking for `'static` when using cached entries, this does not
            // feel worth the effort. I do not expect that a `ParamEnv` will ever
            // contain large enough types for caching to be necessary.
            //
            // We clear the cache rather than deleting it or replacing it with an empty cache. This
            // lets the allocated capacity be reused later.
            let mut rest_canonicalizer = env_canonicalizer;
            rest_canonicalizer.canonicalize_mode =
                CanonicalizeMode::Input(CanonicalizeInputKind::Predicate);
            rest_canonicalizer.state.cache.clear();

            (param_env, rest_canonicalizer)
        };

        // Check whether we can use the global cache for this param_env. As we only use
        // the `param_env` itself as the cache key, considering any additional information
        // during its canonicalization would be incorrect. We always canonicalize region
        // inference variables in a separate universe, so these are fine. However, we do
        // track the universe of type and const inference variables so these must not be
        // globally cached. We don't rely on any additional information when canonicalizing
        // placeholders.
        if !param_env.has_non_region_infer() {
            delegate.cx().with_canonical_param_env_cache(|cache| match cache.0.entry(param_env) {
                Entry::Vacant(e) => {
                    // Cache miss. Do `env` canonicalization and get `rest_canonicalizer`, and
                    // fill in the cache entry.
                    let (param_env, rest_canonicalizer) = do_env_and_make_rest();
                    e.insert(CanonicalParamEnvCacheEntry {
                        param_env,
                        variables: rest_canonicalizer.state.variables.clone(),
                        var_kinds: rest_canonicalizer.state.var_kinds.clone(),
                        // SAFETY: The iterated elements go straight back into a hashmap.
                        variable_lookup_table: rest_canonicalizer
                            .state
                            .variable_lookup_table
                            .iter()
                            .map(|(&arg, &idx)| (arg, idx))
                            .collect(),
                    });
                    (param_env, rest_canonicalizer)
                }
                Entry::Occupied(e) => {
                    // Cache hit; no canonicalization required. Just set up `rest_canonicalizer`.
                    let e = e.get();
                    let mut rest_canonicalizer = Canonicalizer::new(
                        delegate,
                        CanonicalizeMode::Input(CanonicalizeInputKind::Predicate),
                    );
                    rest_canonicalizer.state.variables.extend(e.variables.iter().copied());
                    rest_canonicalizer.state.var_kinds.extend(e.var_kinds.iter().copied());
                    // SAFETY: The iterated elements go straight back into a hashmap.
                    rest_canonicalizer
                        .state
                        .variable_lookup_table
                        .extend(e.variable_lookup_table.iter().map(|(&arg, &idx)| (arg, idx)));
                    (e.param_env, rest_canonicalizer)
                }
            })
        } else {
            // Do `env` canonicalization and get `rest_canonicalizer`.
            do_env_and_make_rest()
        }
    }

    /// When canonicalizing query inputs, we keep `'static` in the `param_env`
    /// but erase it everywhere else. We generally don't want to depend on region
    /// identity, so while it should not matter whether `'static` is kept in the
    /// value or opaque type storage as well, this prevents us from accidentally
    /// relying on it in the future.
    ///
    /// We want to keep the option of canonicalizing `'static` to an existential
    /// variable in the future by changing the way we detect global where-bounds.
    pub(super) fn canonicalize_input<P: TypeFoldable<I>>(
        delegate: &'a D,
        input: QueryInput<I, P>,
    ) -> (ThinVec<I::GenericArg>, ty::Canonical<I, QueryInput<I, P>>) {
        // First canonicalize the `param_env` while keeping `'static`. This produces a
        // canonicalizer that can canonicalize the rest of the input without keeping `'static`.
        let (param_env, mut rest_canonicalizer) =
            Self::canonicalize_param_env(delegate, input.goal.param_env);

        let predicate = input.goal.predicate;
        let predicate = predicate.fold_with(&mut rest_canonicalizer);
        let goal = Goal { param_env, predicate };

        let predefined_opaques_in_body = input.predefined_opaques_in_body;
        let predefined_opaques_in_body =
            if predefined_opaques_in_body.has_type_flags(NEEDS_CANONICAL) {
                predefined_opaques_in_body.fold_with(&mut rest_canonicalizer)
            } else {
                predefined_opaques_in_body
            };

        let value = QueryInput { goal, predefined_opaques_in_body };

        debug_assert!(!value.has_infer(), "unexpected infer in {value:?}");
        debug_assert!(!value.has_placeholders(), "unexpected placeholders in {value:?}");
        let (max_universe, variables, var_kinds) = rest_canonicalizer.finalize();
        (variables, Canonical { max_universe, var_kinds, value })
    }

    fn get_or_insert_bound_var(
        &mut self,
        arg: impl Into<I::GenericArg>,
        kind: CanonicalVarKind<I>,
    ) -> ty::BoundVar {
        // The exact value of 16 here doesn't matter that much (8 and 32 give extremely similar
        // results). So long as we have protection against the rare cases where the length reaches
        // 1000+ (e.g. `wg-grammar`).
        let arg = arg.into();
        let idx = if self.state.variables.len() > 16 {
            if self.state.variable_lookup_table.is_empty() {
                self.state
                    .variable_lookup_table
                    .extend(self.state.variables.iter().copied().zip(0..));
            }

            *self.state.variable_lookup_table.entry(arg).or_insert_with(|| {
                let var = self.state.variables.len();
                self.state.variables.push(arg);
                self.state.var_kinds.push(kind);
                var
            })
        } else {
            self.state.variables.iter().position(|&v| v == arg).unwrap_or_else(|| {
                let var = self.state.variables.len();
                self.state.variables.push(arg);
                self.state.var_kinds.push(kind);
                var
            })
        };

        ty::BoundVar::from(idx)
    }

    fn get_or_insert_sub_root(&mut self, vid: ty::TyVid) -> ty::BoundVar {
        let root_vid = self.delegate.sub_unification_table_root_var(vid);
        let idx = *self
            .state
            .sub_root_lookup_table
            .entry(root_vid)
            .or_insert_with(|| self.state.variables.len());
        ty::BoundVar::from(idx)
    }

    fn finalize(mut self) -> (ty::UniverseIndex, ThinVec<I::GenericArg>, I::CanonicalVarKinds) {
        // See the rustc-dev-guide section about how we deal with universes
        // during canonicalization in the new solver.
        let max_universe = match self.canonicalize_mode {
            // All placeholders and vars are canonicalized in the root universe.
            CanonicalizeMode::Input { .. } => {
                debug_assert!(
                    self.state
                        .var_kinds
                        .iter()
                        .all(|var| var.universe() == ty::UniverseIndex::ROOT),
                    "expected all vars to be canonicalized in root universe: {:#?}",
                    self.state.var_kinds,
                );
                ty::UniverseIndex::ROOT
            }
            // 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 self.state.var_kinds.iter_mut() {
                    let uv = var.universe();
                    let new_uv = ty::UniverseIndex::from(
                        uv.index().saturating_sub(max_input_universe.index()),
                    );
                    *var = var.with_updated_universe(new_uv);
                }
                self.state
                    .var_kinds
                    .iter()
                    .map(|kind| kind.universe())
                    .max()
                    .unwrap_or(ty::UniverseIndex::ROOT)
            }
        };
        let variables = mem::take(&mut self.state.variables);
        let var_kinds = self.delegate.cx().mk_canonical_var_kinds(&self.state.var_kinds);

        // We have finished with this canonicalizer and can return its state to the delegate for
        // later reuse.
        self.delegate.release_canonicalizer_state(self.state);

        (max_universe, variables, var_kinds)
    }

    fn inner_fold_ty(&mut self, t: I::Ty) -> I::Ty {
        let kind = match t.kind() {
            ty::Infer(i) => match i {
                ty::TyVar(vid) => {
                    debug_assert_eq!(
                        self.delegate.opportunistic_resolve_ty_var(vid),
                        t,
                        "ty vid should have been resolved fully before canonicalization"
                    );

                    let sub_root = self.get_or_insert_sub_root(vid);
                    let ui = match self.canonicalize_mode {
                        CanonicalizeMode::Input { .. } => ty::UniverseIndex::ROOT,
                        CanonicalizeMode::Response { .. } => self
                            .delegate
                            .universe_of_ty(vid)
                            .unwrap_or_else(|| panic!("ty var should have been resolved: {t:?}")),
                    };
                    CanonicalVarKind::Ty { ui, sub_root }
                }
                ty::IntVar(vid) => {
                    debug_assert_eq!(
                        self.delegate.opportunistic_resolve_int_var(vid),
                        t,
                        "ty vid should have been resolved fully before canonicalization"
                    );
                    CanonicalVarKind::Int
                }
                ty::FloatVar(vid) => {
                    debug_assert_eq!(
                        self.delegate.opportunistic_resolve_float_var(vid),
                        t,
                        "ty vid should have been resolved fully before canonicalization"
                    );
                    CanonicalVarKind::Float
                }
                ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => {
                    panic!("fresh vars not expected in canonicalization")
                }
            },
            ty::Placeholder(placeholder) => match self.canonicalize_mode {
                CanonicalizeMode::Input { .. } => {
                    CanonicalVarKind::PlaceholderTy(PlaceholderType::new_anon(
                        ty::UniverseIndex::ROOT,
                        self.state.variables.len().into(),
                    ))
                }
                CanonicalizeMode::Response { .. } => CanonicalVarKind::PlaceholderTy(placeholder),
            },
            ty::Param(_) => match self.canonicalize_mode {
                CanonicalizeMode::Input { .. } => {
                    CanonicalVarKind::PlaceholderTy(PlaceholderType::new_anon(
                        ty::UniverseIndex::ROOT,
                        self.state.variables.len().into(),
                    ))
                }
                CanonicalizeMode::Response { .. } => panic!("param ty in response: {t:?}"),
            },
            ty::Bool
            | ty::Char
            | ty::Int(_)
            | ty::Uint(_)
            | ty::Float(_)
            | ty::Adt(_, _)
            | ty::Foreign(_)
            | ty::Str
            | ty::Array(_, _)
            | ty::Slice(_)
            | ty::RawPtr(_, _)
            | ty::Ref(_, _, _)
            | ty::Pat(_, _)
            | ty::FnDef(_, _)
            | ty::FnPtr(..)
            | ty::UnsafeBinder(_)
            | ty::Dynamic(_, _)
            | ty::Closure(..)
            | ty::CoroutineClosure(..)
            | ty::Coroutine(_, _)
            | ty::CoroutineWitness(..)
            | ty::Never
            | ty::Tuple(_)
            | ty::Alias(_, _)
            | ty::Bound(_, _)
            | ty::Error(_) => {
                return t.super_fold_with(self);
            }
        };

        let var = self.get_or_insert_bound_var(t, kind);

        Ty::new_canonical_bound(self.cx(), var)
    }
}

impl<D: SolverDelegate<Interner = I>, I: Interner> TypeFolder<I> for Canonicalizer<'_, D, I> {
    fn cx(&self) -> I {
        self.delegate.cx()
    }

    fn fold_region(&mut self, r: Region<I>) -> Region<I> {
        // We canonicalize free regions from the input into placeholder regions so that
        // region constraints created in nested contexts can be propagated back to the
        // caller, instead of unifying them.
        // See the following Zulip discussion for details:
        // https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/A.20question.20on.20.23251/near/579240238
        let kind = match r.kind() {
            ty::ReBound(..) => return r,

            // We don't canonicalize `ReStatic` in the `param_env` as we use it
            // when checking whether a `ParamEnv` candidate is global.
            ty::ReStatic => match self.canonicalize_mode {
                CanonicalizeMode::Input(CanonicalizeInputKind::Predicate) => {
                    CanonicalVarKind::PlaceholderRegion(ty::PlaceholderRegion::new_anon(
                        ty::UniverseIndex::ROOT,
                        self.state.variables.len().into(),
                    ))
                }
                CanonicalizeMode::Input(CanonicalizeInputKind::ParamEnv)
                | CanonicalizeMode::Response { .. } => return r,
            },

            // `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.
            ty::ReErased | ty::ReError(_) => match self.canonicalize_mode {
                CanonicalizeMode::Input(_) => {
                    CanonicalVarKind::PlaceholderRegion(ty::PlaceholderRegion::new_anon(
                        ty::UniverseIndex::ROOT,
                        self.state.variables.len().into(),
                    ))
                }
                CanonicalizeMode::Response { .. } => return r,
            },

            ty::ReEarlyParam(_) | ty::ReLateParam(_) => match self.canonicalize_mode {
                CanonicalizeMode::Input(_) => {
                    CanonicalVarKind::PlaceholderRegion(ty::PlaceholderRegion::new_anon(
                        ty::UniverseIndex::ROOT,
                        self.state.variables.len().into(),
                    ))
                }
                CanonicalizeMode::Response { .. } => {
                    panic!("unexpected region in response: {r:?}")
                }
            },

            ty::RePlaceholder(placeholder) => match self.canonicalize_mode {
                CanonicalizeMode::Input(_) => {
                    CanonicalVarKind::PlaceholderRegion(ty::PlaceholderRegion::new_anon(
                        ty::UniverseIndex::ROOT,
                        self.state.variables.len().into(),
                    ))
                }
                CanonicalizeMode::Response { max_input_universe } => {
                    // If we have a placeholder region inside of a query, it must be from
                    // a new universe, unless from the root universe, which is used for
                    // canonicalization of any free region from the input.
                    if placeholder.universe() != ty::UniverseIndex::ROOT
                        && max_input_universe.can_name(placeholder.universe())
                    {
                        panic!("new placeholder in universe {max_input_universe:?}: {r:?}");
                    }
                    CanonicalVarKind::PlaceholderRegion(placeholder)
                }
            },

            ty::ReVar(vid) => {
                debug_assert_eq!(
                    self.delegate.opportunistic_resolve_lt_var(vid),
                    r,
                    "region vid should have been resolved fully before canonicalization"
                );
                match self.canonicalize_mode {
                    CanonicalizeMode::Input(_) => {
                        CanonicalVarKind::PlaceholderRegion(ty::PlaceholderRegion::new_anon(
                            ty::UniverseIndex::ROOT,
                            self.state.variables.len().into(),
                        ))
                    }
                    CanonicalizeMode::Response { .. } => {
                        CanonicalVarKind::Region(self.delegate.universe_of_lt(vid).unwrap())
                    }
                }
            }
        };

        let var = self.get_or_insert_bound_var(r, kind);

        Region::new_canonical_bound(self.cx(), var)
    }

    fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
        if !t.flags().intersects(NEEDS_CANONICAL) {
            t
        } else if let Some(&ty) = self.state.cache.get(&t) {
            ty
        } else {
            let res = self.inner_fold_ty(t);
            let is_unseen = self.state.cache.insert(t, res);
            assert!(is_unseen);
            res
        }
    }

    fn fold_const(&mut self, c: I::Const) -> I::Const {
        if !c.flags().intersects(NEEDS_CANONICAL) {
            return c;
        }

        let kind = match c.kind() {
            ty::ConstKind::Infer(i) => match i {
                ty::InferConst::Var(vid) => {
                    debug_assert_eq!(
                        self.delegate.opportunistic_resolve_ct_var(vid),
                        c,
                        "const vid should have been resolved fully before canonicalization"
                    );

                    match self.canonicalize_mode {
                        CanonicalizeMode::Input { .. } => {
                            CanonicalVarKind::Const(ty::UniverseIndex::ROOT)
                        }
                        CanonicalizeMode::Response { .. } => {
                            CanonicalVarKind::Const(self.delegate.universe_of_ct(vid).unwrap())
                        }
                    }
                }
                ty::InferConst::Fresh(_) => unimplemented!(),
            },
            ty::ConstKind::Placeholder(placeholder) => match self.canonicalize_mode {
                CanonicalizeMode::Input { .. } => {
                    CanonicalVarKind::PlaceholderConst(PlaceholderConst::new_anon(
                        ty::UniverseIndex::ROOT,
                        self.state.variables.len().into(),
                    ))
                }
                CanonicalizeMode::Response { .. } => {
                    CanonicalVarKind::PlaceholderConst(placeholder)
                }
            },
            ty::ConstKind::Param(_) => match self.canonicalize_mode {
                CanonicalizeMode::Input { .. } => {
                    CanonicalVarKind::PlaceholderConst(PlaceholderConst::new_anon(
                        ty::UniverseIndex::ROOT,
                        self.state.variables.len().into(),
                    ))
                }
                CanonicalizeMode::Response { .. } => panic!("param ty in response: {c:?}"),
            },
            // FIXME: See comment above -- we could fold the region separately or something.
            ty::ConstKind::Bound(_, _)
            | ty::ConstKind::Alias(_, _)
            | ty::ConstKind::Value(_)
            | ty::ConstKind::Error(_)
            | ty::ConstKind::Expr(_) => return c.super_fold_with(self),
        };

        let var = self.get_or_insert_bound_var(c, kind);

        Const::new_canonical_bound(self.cx(), var)
    }

    fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
        if !p.flags().intersects(NEEDS_CANONICAL) { p } else { p.super_fold_with(self) }
    }

    fn fold_clauses(&mut self, c: I::Clauses) -> I::Clauses {
        match self.canonicalize_mode {
            CanonicalizeMode::Input(CanonicalizeInputKind::ParamEnv)
            | CanonicalizeMode::Response { max_input_universe: _ } => {}
            CanonicalizeMode::Input(CanonicalizeInputKind::Predicate) => {
                panic!("erasing 'static in env")
            }
        }
        if !c.flags().intersects(NEEDS_CANONICAL) { c } else { c.super_fold_with(self) }
    }
}