diff options
author | Bastian Kauschke <bastian_kauschke@hotmail.de> | 2020-12-30 22:35:41 +0100 |
---|---|---|
committer | Bastian Kauschke <bastian_kauschke@hotmail.de> | 2020-12-30 22:35:41 +0100 |
commit | 56ac3a43c299bbd84f3dba58b0e4645344e11a73 (patch) | |
tree | e5720475ba675835ea964489ff33aca4c78655c2 | |
parent | 507bff92fadf1f25a830da5065a5a87113345163 (diff) | |
download | rust-yeet-unused-substs.tar.gz |
-rw-r--r-- | compiler/rustc_middle/src/ty/consts.rs | 1 | ||||
-rw-r--r-- | compiler/rustc_middle/src/ty/consts/normalize.rs | 59 | ||||
-rw-r--r-- | compiler/rustc_trait_selection/src/traits/select/confirmation.rs | 4 |
3 files changed, 63 insertions, 1 deletions
diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 0af884a286d..3f5ada95b70 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -10,6 +10,7 @@ use rustc_macros::HashStable; mod int; mod kind; +mod normalize; pub use int::*; pub use kind::*; diff --git a/compiler/rustc_middle/src/ty/consts/normalize.rs b/compiler/rustc_middle/src/ty/consts/normalize.rs new file mode 100644 index 00000000000..6a0455a20b9 --- /dev/null +++ b/compiler/rustc_middle/src/ty/consts/normalize.rs @@ -0,0 +1,59 @@ +use crate::ty::{self, Ty, TyCtxt, TypeFoldable}; + +impl<'tcx> TyCtxt<'tcx> { + pub fn normalize_consts<T: TypeFoldable<'tcx>>(self, value: T) -> T { + value.fold_with(&mut ConstNormalizer::new(self)) + } +} + +pub struct ConstNormalizer<'tcx> { + tcx: TyCtxt<'tcx> +} + +impl ConstNormalizer<'_> { + pub fn new(tcx: TyCtxt<'_>) -> ConstNormalizer<'_> { + ConstNormalizer { tcx } + } +} + +impl<'tcx> ty::TypeFolder<'tcx> for ConstNormalizer<'tcx> { + fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx + } + + fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { + if t.flags().intersects(ty::TypeFlags::HAS_CT_PROJECTION) { + t.super_fold_with(self) + } else { + t + } + } + + fn fold_const(&mut self, ct: &'tcx ty::Const<'tcx>) -> &'tcx ty::Const<'tcx> { + match ct.val { + ty::ConstKind::Unevaluated(def, substs, None) => { + match self.tcx.mir_abstract_const_opt_const_arg(def) { + // FIXME(const_evaluatable_checked): Replace the arguments not used + // in the abstract const with dummy ones while keeping everything that is + // used. + Ok(Some(_abstr_ct)) => self.tcx.mk_const(ty::Const { + ty: ct.ty, + val: ty::ConstKind::Unevaluated(def, substs, None) + }), + Ok(None) => { + let dummy_substs = ty::InternalSubsts::for_item(self.tcx, def.did, |param, _| { + match param.kind { + ty::GenericParamDefKind::Lifetime => self.tcx.lifetimes.re_static.into(), + ty::GenericParamDefKind::Type { .. } => self.tcx.types.unit.into(), + ty::GenericParamDefKind::Const => self.tcx.consts.unit.into(), // TODO + } + }); + self.tcx.mk_const(ty::Const { ty: ct.ty, val: ty::ConstKind::Unevaluated(def, dummy_substs, None) }) + } + Err(_) => self.tcx.const_error(ct.ty), + } + } + _ => ct.super_fold_with(self), + } + } +} diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index 030c29171a1..fd19f6c6128 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -846,7 +846,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // by putting it in a query; it would only need the `DefId` as it // looks at declared field types, not anything substituted. for field in prefix_fields { - for arg in tcx.type_of(field.did).walk() { + let ty = tcx.normalize_consts(tcx.type_of(field.did)); + + for arg in ty.walk() { if let Some(i) = maybe_unsizing_param_idx(arg) { if unsizing_params.contains(i) { return Err(Unimplemented); |