From 2140016d6c7239fe02c87c5ac421c0df4b880e15 Mon Sep 17 00:00:00 2001 From: lcnr Date: Mon, 19 Jul 2021 12:13:25 +0200 Subject: [PATCH] don't just compare `ty::Const` --- compiler/rustc_infer/src/infer/combine.rs | 2 + compiler/rustc_middle/src/ty/fold.rs | 40 +++++++++++++++++++ .../rustc_middle/src/ty/structural_impls.rs | 4 ++ 3 files changed, 46 insertions(+) diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index 4fb3d4bdfe1..32308910aa7 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -129,6 +129,8 @@ impl<'infcx, 'tcx> InferCtxt<'infcx, 'tcx> { where R: ConstEquateRelation<'tcx>, { + let a = self.tcx.expose_default_const_substs(a); + let b = self.tcx.expose_default_const_substs(b); debug!("{}.consts({:?}, {:?})", relation.tag(), a, b); if a == b { return Ok(a); diff --git a/compiler/rustc_middle/src/ty/fold.rs b/compiler/rustc_middle/src/ty/fold.rs index 0c5e292e6eb..b249dc8d6f9 100644 --- a/compiler/rustc_middle/src/ty/fold.rs +++ b/compiler/rustc_middle/src/ty/fold.rs @@ -213,6 +213,10 @@ pub trait TypeFolder<'tcx>: Sized { c.super_fold_with(self) } + fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> { + p.super_fold_with(self) + } + fn fold_mir_const(&mut self, c: mir::ConstantKind<'tcx>) -> mir::ConstantKind<'tcx> { bug!("most type folders should not be folding MIR datastructures: {:?}", c) } @@ -1205,6 +1209,42 @@ impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor<'tcx> { } } +impl<'tcx> TyCtxt<'tcx> { + /// This is a HACK(const_generics) and should probably not be needed. + /// Might however be perf relevant, so who knows. + /// + /// FIXME(@lcnr): explain this function a bit more + pub fn expose_default_const_substs>(self, v: T) -> T { + v.fold_with(&mut ExposeDefaultConstSubstsFolder { tcx: self }) + } +} + +struct ExposeDefaultConstSubstsFolder<'tcx> { + tcx: TyCtxt<'tcx>, +} + +impl<'tcx> TypeFolder<'tcx> for ExposeDefaultConstSubstsFolder<'tcx> { + fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx + } + + fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { + if ty.flags().intersects(TypeFlags::HAS_UNKNOWN_DEFAULT_CONST_SUBSTS) { + ty.super_fold_with(self) + } else { + ty + } + } + + fn fold_predicate(&mut self, pred: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> { + if pred.inner.flags.intersects(TypeFlags::HAS_UNKNOWN_DEFAULT_CONST_SUBSTS) { + pred.super_fold_with(self) + } else { + pred + } + } +} + /// Collects all the late-bound regions at the innermost binding level /// into a hash set. struct LateBoundRegionsCollector<'tcx> { diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 369dfaf32a0..dcdea8025fa 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -974,6 +974,10 @@ impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> { } impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> { + fn fold_with>(self, folder: &mut F) -> Self { + folder.fold_predicate(self) + } + fn super_fold_with>(self, folder: &mut F) -> Self { let new = self.inner.kind.fold_with(folder); folder.tcx().reuse_or_mk_predicate(self, new)