Skip to content

Commit 2d5591d

Browse files
Make make_identity take CanonicalVarInfos
1 parent 4ff674f commit 2d5591d

File tree

2 files changed

+27
-48
lines changed

2 files changed

+27
-48
lines changed

compiler/rustc_middle/src/infer/canonical.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -337,51 +337,49 @@ TrivialTypeTraversalAndLiftImpls! {
337337
}
338338

339339
impl<'tcx> CanonicalVarValues<'tcx> {
340-
/// Creates dummy var values which should not be used in a
341-
/// canonical response.
342-
pub fn dummy() -> CanonicalVarValues<'tcx> {
343-
CanonicalVarValues { var_values: ty::List::empty() }
344-
}
345-
346-
#[inline]
347-
pub fn len(&self) -> usize {
348-
self.var_values.len()
349-
}
350-
351-
/// Makes an identity substitution from this one: each bound var
352-
/// is matched to the same bound var, preserving the original kinds.
353-
/// For example, if we have:
354-
/// `self.var_values == [Type(u32), Lifetime('a), Type(u64)]`
355-
/// we'll return a substitution `subst` with:
356-
/// `subst.var_values == [Type(^0), Lifetime(^1), Type(^2)]`.
357-
pub fn make_identity(&self, tcx: TyCtxt<'tcx>) -> Self {
358-
use crate::ty::subst::GenericArgKind;
359-
340+
// Given a list of canonical variables, construct a set of values which are
341+
// the identity response.
342+
pub fn make_identity(
343+
tcx: TyCtxt<'tcx>,
344+
infos: CanonicalVarInfos<'tcx>,
345+
) -> CanonicalVarValues<'tcx> {
360346
CanonicalVarValues {
361-
var_values: tcx.mk_substs(self.var_values.iter().enumerate().map(
362-
|(i, kind)| -> ty::GenericArg<'tcx> {
363-
match kind.unpack() {
364-
GenericArgKind::Type(..) => tcx
347+
var_values: tcx.mk_substs(infos.iter().enumerate().map(
348+
|(i, info)| -> ty::GenericArg<'tcx> {
349+
match info.kind {
350+
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => tcx
365351
.mk_ty(ty::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i).into()))
366352
.into(),
367-
GenericArgKind::Lifetime(..) => {
353+
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => {
368354
let br = ty::BoundRegion {
369355
var: ty::BoundVar::from_usize(i),
370356
kind: ty::BrAnon(i as u32, None),
371357
};
372358
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)).into()
373359
}
374-
GenericArgKind::Const(ct) => tcx
360+
CanonicalVarKind::Const(_, ty)
361+
| CanonicalVarKind::PlaceholderConst(_, ty) => tcx
375362
.mk_const(
376363
ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i)),
377-
ct.ty(),
364+
ty,
378365
)
379366
.into(),
380367
}
381368
},
382369
)),
383370
}
384371
}
372+
373+
/// Creates dummy var values which should not be used in a
374+
/// canonical response.
375+
pub fn dummy() -> CanonicalVarValues<'tcx> {
376+
CanonicalVarValues { var_values: ty::List::empty() }
377+
}
378+
379+
#[inline]
380+
pub fn len(&self) -> usize {
381+
self.var_values.len()
382+
}
385383
}
386384

387385
impl<'a, 'tcx> IntoIterator for &'a CanonicalVarValues<'tcx> {

compiler/rustc_trait_selection/src/solve/mod.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use std::mem;
1919

2020
use rustc_hir::def_id::DefId;
21-
use rustc_infer::infer::canonical::{Canonical, CanonicalVarKind, CanonicalVarValues};
21+
use rustc_infer::infer::canonical::{Canonical, CanonicalVarValues};
2222
use rustc_infer::infer::canonical::{OriginalQueryValues, QueryRegionConstraints, QueryResponse};
2323
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
2424
use rustc_infer::traits::query::NoSolution;
@@ -481,30 +481,11 @@ pub(super) fn response_no_constraints<'tcx>(
481481
goal: Canonical<'tcx, impl Sized>,
482482
certainty: Certainty,
483483
) -> QueryResult<'tcx> {
484-
let var_values =
485-
tcx.mk_substs(goal.variables.iter().enumerate().map(|(i, info)| -> ty::GenericArg<'tcx> {
486-
match info.kind {
487-
CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => {
488-
tcx.mk_ty(ty::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i).into())).into()
489-
}
490-
CanonicalVarKind::Region(_) | CanonicalVarKind::PlaceholderRegion(_) => {
491-
let br = ty::BoundRegion {
492-
var: ty::BoundVar::from_usize(i),
493-
kind: ty::BrAnon(i as u32, None),
494-
};
495-
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)).into()
496-
}
497-
CanonicalVarKind::Const(_, ty) | CanonicalVarKind::PlaceholderConst(_, ty) => tcx
498-
.mk_const(ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i)), ty)
499-
.into(),
500-
}
501-
}));
502-
503484
Ok(Canonical {
504485
max_universe: goal.max_universe,
505486
variables: goal.variables,
506487
value: Response {
507-
var_values: CanonicalVarValues { var_values },
488+
var_values: CanonicalVarValues::make_identity(tcx, goal.variables),
508489
external_constraints: Default::default(),
509490
certainty,
510491
},

0 commit comments

Comments
 (0)