about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBastian Kauschke <bastian_kauschke@hotmail.de>2020-07-08 01:03:19 +0200
committerBastian Kauschke <bastian_kauschke@hotmail.de>2020-07-15 12:58:33 +0200
commitae80d7e012e6b0efcacc7b648690a9257445afd7 (patch)
tree5af95457168653a89d3b0f86251989ad2de62476
parent08394eb12174ad14aeef739a876d1456187b4d66 (diff)
downloadrust-ae80d7e012e6b0efcacc7b648690a9257445afd7.tar.gz
rust-ae80d7e012e6b0efcacc7b648690a9257445afd7.zip
update const arg queries
-rw-r--r--src/librustc_middle/mir/query.rs25
-rw-r--r--src/librustc_middle/query/mod.rs36
-rw-r--r--src/librustc_middle/ty/context.rs6
-rw-r--r--src/librustc_middle/ty/instance.rs9
-rw-r--r--src/librustc_middle/ty/mod.rs19
-rw-r--r--src/librustc_middle/ty/query/keys.rs15
-rw-r--r--src/librustc_middle/ty/structural_impls.rs13
-rw-r--r--src/librustc_mir/borrow_check/mod.rs10
-rw-r--r--src/librustc_mir/const_eval/eval_queries.rs3
-rw-r--r--src/librustc_mir/interpret/eval_context.rs6
-rw-r--r--src/librustc_mir/transform/check_consts/qualifs.rs4
-rw-r--r--src/librustc_mir/transform/check_unsafety.rs12
-rw-r--r--src/librustc_mir/transform/mod.rs48
-rw-r--r--src/librustc_mir_build/hair/cx/expr.rs8
-rw-r--r--src/librustc_mir_build/hair/cx/mod.rs2
-rw-r--r--src/librustc_ty/instance.rs35
-rw-r--r--src/librustc_typeck/check/mod.rs16
-rw-r--r--src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr5
-rw-r--r--src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr5
-rw-r--r--src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr5
-rw-r--r--src/test/ui/impl-trait/auto-trait-leak.stderr10
21 files changed, 154 insertions, 138 deletions
diff --git a/src/librustc_middle/mir/query.rs b/src/librustc_middle/mir/query.rs
index 8dddaf40c82..76b8ac4e80a 100644
--- a/src/librustc_middle/mir/query.rs
+++ b/src/librustc_middle/mir/query.rs
@@ -1,10 +1,10 @@
 //! Values computed by queries that use MIR.
 
-use crate::ty::{self, Ty};
+use crate::ty::{self, Ty, TyCtxt};
 use rustc_data_structures::fx::FxHashMap;
 use rustc_data_structures::sync::Lrc;
 use rustc_hir as hir;
-use rustc_hir::def_id::DefId;
+use rustc_hir::def_id::{DefId, LocalDefId};
 use rustc_index::bit_set::BitMatrix;
 use rustc_index::vec::IndexVec;
 use rustc_span::{Span, Symbol};
@@ -323,3 +323,24 @@ pub struct CoverageInfo {
     /// The total number of coverage region counters added to the MIR `Body`.
     pub num_counters: u32,
 }
+
+impl<'tcx> TyCtxt<'tcx> {
+    pub fn mir_borrowck_opt_const_arg(
+        self,
+        def: ty::WithOptParam<LocalDefId>,
+    ) -> &'tcx BorrowCheckResult<'tcx> {
+        if let Some(param_did) = def.param_did {
+            self.mir_borrowck_const_arg((def.did, param_did))
+        } else {
+            self.mir_borrowck(def.did)
+        }
+    }
+
+    pub fn mir_const_qualif_opt_const_arg(self, def: ty::WithOptParam<LocalDefId>) -> ConstQualifs {
+        if let Some(param_did) = def.param_did {
+            self.mir_const_qualif_const_arg((def.did, param_did))
+        } else {
+            self.mir_const_qualif(def.did)
+        }
+    }
+}
diff --git a/src/librustc_middle/query/mod.rs b/src/librustc_middle/query/mod.rs
index 56cd93a509a..76702fab9dc 100644
--- a/src/librustc_middle/query/mod.rs
+++ b/src/librustc_middle/query/mod.rs
@@ -209,11 +209,11 @@ rustc_queries! {
             cache_on_disk_if { key.is_local() }
         }
         query mir_const_qualif_const_arg(
-            key: ty::WithOptParam<LocalDefId>
+            key: (LocalDefId, DefId)
         ) -> mir::ConstQualifs {
             desc {
-                |tcx| "const checking the potential const argument `{}`",
-                tcx.def_path_str(key.did.to_def_id())
+                |tcx| "const checking the const argument `{}`",
+                tcx.def_path_str(key.0.to_def_id())
             }
         }
 
@@ -257,10 +257,10 @@ rustc_queries! {
             desc { |tcx| "optimizing MIR for `{}`", tcx.def_path_str(key) }
             cache_on_disk_if { key.is_local() }
         }
-        query optimized_mir_of_const_arg(key: ty::WithOptParam<LocalDefId>) -> &'tcx mir::Body<'tcx> {
+        query optimized_mir_of_const_arg(key: (LocalDefId, DefId)) -> &'tcx mir::Body<'tcx> {
             desc {
-                |tcx| "optimizing MIR for the potential const argument `{}`",
-                tcx.def_path_str(key.did.to_def_id())
+                |tcx| "optimizing MIR for the const argument `{}`",
+                tcx.def_path_str(key.0.to_def_id())
             }
         }
 
@@ -280,7 +280,7 @@ rustc_queries! {
             key: ty::WithOptParam<LocalDefId>
         ) -> &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>> {
             desc {
-                |tcx| "optimizing promoted MIR for the potential const argument `{}`",
+                |tcx| "optimizing promoted MIR for the const argument `{}`",
                 tcx.def_path_str(key.did.to_def_id()),
             }
         }
@@ -496,8 +496,8 @@ rustc_queries! {
             desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key.to_def_id()) }
             cache_on_disk_if { true }
         }
-        query unsafety_check_result_const_arg(key: ty::WithOptParam<LocalDefId>) -> &'tcx mir::UnsafetyCheckResult {
-            desc { |tcx| "unsafety-checking the potential const arg `{}`", tcx.def_path_str(key.did.to_def_id()) }
+        query unsafety_check_result_const_arg(key: (LocalDefId, DefId)) -> &'tcx mir::UnsafetyCheckResult {
+            desc { |tcx| "unsafety-checking the const arg `{}`", tcx.def_path_str(key.0.to_def_id()) }
         }
 
         /// HACK: when evaluated, this reports a "unsafe derive on repr(packed)" error.
@@ -579,12 +579,12 @@ rustc_queries! {
             desc { |tcx| "type-checking `{}`", tcx.def_path_str(key.to_def_id()) }
             cache_on_disk_if { true }
         }
-        query _typeck_tables_of_const_arg(
-            key: ty::WithOptParam<LocalDefId>
+        query typeck_tables_of_const_arg(
+            key: (LocalDefId, DefId)
         ) -> &'tcx ty::TypeckTables<'tcx> {
             desc {
                 |tcx| "type-checking the const argument `{}`",
-                tcx.def_path_str(key.did.to_def_id()),
+                tcx.def_path_str(key.0.to_def_id()),
             }
         }
         query diagnostic_only_typeck_tables_of(key: LocalDefId) -> &'tcx ty::TypeckTables<'tcx> {
@@ -627,10 +627,10 @@ rustc_queries! {
                     || opt_result.map_or(false, |r| !r.concrete_opaque_types.is_empty())
             }
         }
-        query mir_borrowck_const_arg(key: ty::WithOptParam<LocalDefId>) -> &'tcx mir::BorrowCheckResult<'tcx> {
+        query mir_borrowck_const_arg(key: (LocalDefId, DefId)) -> &'tcx mir::BorrowCheckResult<'tcx> {
             desc {
-                |tcx| "borrow-checking the potential const argument`{}`",
-                tcx.def_path_str(key.did.to_def_id())
+                |tcx| "borrow-checking the const argument`{}`",
+                tcx.def_path_str(key.0.to_def_id())
             }
         }
     }
@@ -1501,11 +1501,11 @@ rustc_queries! {
         }
 
         query resolve_instance_of_const_arg(
-            key: ty::ParamEnvAnd<'tcx, (ty::WithOptParam<DefId>, SubstsRef<'tcx>)>
+            key: ty::ParamEnvAnd<'tcx, (LocalDefId, DefId, SubstsRef<'tcx>)>
         ) -> Result<Option<ty::Instance<'tcx>>, ErrorReported> {
             desc {
-                "resolving instance of the potential const argument `{}`",
-                ty::Instance::new(key.value.0.did, key.value.1),
+                "resolving instance of the const argument `{}`",
+                ty::Instance::new(key.value.0.to_def_id(), key.value.2),
             }
         }
     }
diff --git a/src/librustc_middle/ty/context.rs b/src/librustc_middle/ty/context.rs
index f79297aeac6..fd7be0dbc0b 100644
--- a/src/librustc_middle/ty/context.rs
+++ b/src/librustc_middle/ty/context.rs
@@ -980,12 +980,12 @@ pub struct GlobalCtxt<'tcx> {
 }
 
 impl<'tcx> TyCtxt<'tcx> {
-    pub fn typeck_tables_of_const_arg(
+    pub fn typeck_tables_of_opt_const_arg(
         self,
         def: ty::WithOptParam<LocalDefId>,
     ) -> &'tcx TypeckTables<'tcx> {
-        if def.param_did.is_some() {
-            self._typeck_tables_of_const_arg(def)
+        if let Some(param_did) = def.param_did {
+            self.typeck_tables_of_const_arg((def.did, param_did))
         } else {
             self.typeck_tables_of(def.did)
         }
diff --git a/src/librustc_middle/ty/instance.rs b/src/librustc_middle/ty/instance.rs
index 4c96e1f965f..a00afab9599 100644
--- a/src/librustc_middle/ty/instance.rs
+++ b/src/librustc_middle/ty/instance.rs
@@ -359,7 +359,14 @@ impl<'tcx> Instance<'tcx> {
         substs: SubstsRef<'tcx>,
     ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
         let substs = tcx.erase_regions(&substs);
-        tcx.resolve_instance_of_const_arg(tcx.erase_regions(&param_env.and((def, substs))))
+
+        if let Some((did, param_did)) = def.as_const_arg() {
+            tcx.resolve_instance_of_const_arg(
+                tcx.erase_regions(&param_env.and((did, param_did, substs))),
+            )
+        } else {
+            tcx.resolve_instance(tcx.erase_regions(&param_env.and((def.did, substs))))
+        }
     }
 
     pub fn resolve_for_fn_ptr(
diff --git a/src/librustc_middle/ty/mod.rs b/src/librustc_middle/ty/mod.rs
index 9ebf1ed60e2..92ebe9b2d18 100644
--- a/src/librustc_middle/ty/mod.rs
+++ b/src/librustc_middle/ty/mod.rs
@@ -1600,6 +1600,16 @@ impl WithOptParam<DefId> {
         self.did.as_local().map(|did| WithOptParam { did, param_did: self.param_did })
     }
 
+    pub fn as_const_arg(self) -> Option<(LocalDefId, DefId)> {
+        if let Some(param_did) = self.param_did {
+            if let Some(did) = self.did.as_local() {
+                return Some((did, param_did));
+            }
+        }
+
+        None
+    }
+
     pub fn expect_local(self) -> WithOptParam<LocalDefId> {
         self.as_local().unwrap()
     }
@@ -1611,10 +1621,6 @@ impl WithOptParam<DefId> {
     pub fn ty_def_id(self) -> DefId {
         self.param_did.unwrap_or(self.did)
     }
-
-    pub fn init_me_bby(tcx: TyCtxt<'_>, did: DefId) -> WithOptParam<DefId> {
-        WithOptParam { did, param_did: did.as_local().and_then(|did| tcx.opt_const_param_of(did)) }
-    }
 }
 
 /// When type checking, we use the `ParamEnv` to track
@@ -2889,8 +2895,9 @@ impl<'tcx> TyCtxt<'tcx> {
     pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
         match instance {
             ty::InstanceDef::Item(def) => {
-                if let Some(def) = def.as_local() {
-                    self.optimized_mir_of_const_arg(def)
+                if let Some((did, param_did)) = def.as_const_arg() {
+                    // The `param_did` is only `Some` for local `DefId`s.
+                    self.optimized_mir_of_const_arg((did, param_did))
                 } else {
                     self.optimized_mir(def.did)
                 }
diff --git a/src/librustc_middle/ty/query/keys.rs b/src/librustc_middle/ty/query/keys.rs
index ad205e1f83b..557f64d3c19 100644
--- a/src/librustc_middle/ty/query/keys.rs
+++ b/src/librustc_middle/ty/query/keys.rs
@@ -138,6 +138,17 @@ impl Key for (DefId, LocalDefId) {
     }
 }
 
+impl Key for (LocalDefId, DefId) {
+    type CacheSelector = DefaultCacheSelector;
+
+    fn query_crate(&self) -> CrateNum {
+        LOCAL_CRATE
+    }
+    fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
+        self.0.default_span(tcx)
+    }
+}
+
 impl Key for (CrateNum, DefId) {
     type CacheSelector = DefaultCacheSelector;
 
@@ -182,14 +193,14 @@ impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
     }
 }
 
-impl<'tcx> Key for (ty::WithOptParam<DefId>, SubstsRef<'tcx>) {
+impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
     type CacheSelector = DefaultCacheSelector;
 
     fn query_crate(&self) -> CrateNum {
         LOCAL_CRATE
     }
     fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
-        self.0.did.default_span(tcx)
+        self.0.default_span(tcx)
     }
 }
 
diff --git a/src/librustc_middle/ty/structural_impls.rs b/src/librustc_middle/ty/structural_impls.rs
index 67d0030dea8..f04bfe648fb 100644
--- a/src/librustc_middle/ty/structural_impls.rs
+++ b/src/librustc_middle/ty/structural_impls.rs
@@ -272,6 +272,7 @@ CloneTypeFoldableAndLiftImpls! {
     ::rustc_span::symbol::Symbol,
     ::rustc_hir::def::Res,
     ::rustc_hir::def_id::DefId,
+    ::rustc_hir::def_id::LocalDefId,
     ::rustc_hir::LlvmInlineAsmInner,
     ::rustc_hir::MatchSource,
     ::rustc_hir::Mutability,
@@ -719,6 +720,18 @@ impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for
     }
 }
 
+impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
+    for (A, B, C)
+{
+    fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (A, B, C) {
+        (self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
+    }
+
+    fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
+        self.0.visit_with(visitor) || self.1.visit_with(visitor) || self.2.visit_with(visitor)
+    }
+}
+
 EnumTypeFoldableImpl! {
     impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
         (Some)(a),
diff --git a/src/librustc_mir/borrow_check/mod.rs b/src/librustc_mir/borrow_check/mod.rs
index b4ddda5c03d..0ca165ee847 100644
--- a/src/librustc_mir/borrow_check/mod.rs
+++ b/src/librustc_mir/borrow_check/mod.rs
@@ -89,8 +89,8 @@ const DEREF_PROJECTION: &[PlaceElem<'_>; 1] = &[ProjectionElem::Deref];
 pub fn provide(providers: &mut Providers) {
     *providers = Providers {
         mir_borrowck: |tcx, did| mir_borrowck(tcx, ty::WithOptParam::dummy(did)),
-        mir_borrowck_const_arg: |tcx, def| {
-            if def.param_did.is_none() { tcx.mir_borrowck(def.did) } else { mir_borrowck(tcx, def) }
+        mir_borrowck_const_arg: |tcx, (did, param_did)| {
+            mir_borrowck(tcx, ty::WithOptParam { did, param_did: Some(param_did) })
         },
         ..*providers
     };
@@ -101,8 +101,8 @@ fn mir_borrowck<'tcx>(
     def: ty::WithOptParam<LocalDefId>,
 ) -> &'tcx BorrowCheckResult<'tcx> {
     if def.param_did.is_none() {
-        if let param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
-            return tcx.mir_borrowck_const_arg(ty::WithOptParam { param_did, ..def });
+        if let Some(param_did) = tcx.opt_const_param_of(def.did) {
+            return tcx.mir_borrowck_const_arg((def.did, param_did));
         }
     }
 
@@ -150,7 +150,7 @@ fn do_mir_borrowck<'a, 'tcx>(
     }
 
     // Gather the upvars of a closure, if any.
-    let tables = tcx.typeck_tables_of_const_arg(def);
+    let tables = tcx.typeck_tables_of_opt_const_arg(def);
     if let Some(ErrorReported) = tables.tainted_by_errors {
         infcx.set_tainted_by_errors();
     }
diff --git a/src/librustc_mir/const_eval/eval_queries.rs b/src/librustc_mir/const_eval/eval_queries.rs
index c3f992877ea..38894398343 100644
--- a/src/librustc_mir/const_eval/eval_queries.rs
+++ b/src/librustc_mir/const_eval/eval_queries.rs
@@ -292,7 +292,8 @@ pub fn const_eval_raw_provider<'tcx>(
 
     if let Some(def) = def.as_local() {
         if tcx.has_typeck_tables(def.did) {
-            if let Some(error_reported) = tcx.typeck_tables_of_const_arg(def).tainted_by_errors {
+            if let Some(error_reported) = tcx.typeck_tables_of_opt_const_arg(def).tainted_by_errors
+            {
                 return Err(ErrorHandled::Reported(error_reported));
             }
         }
diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs
index bfef9c1b353..9cbc6d4dcea 100644
--- a/src/librustc_mir/interpret/eval_context.rs
+++ b/src/librustc_mir/interpret/eval_context.rs
@@ -398,7 +398,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         if let Some(def) = def.as_local() {
             if self.tcx.has_typeck_tables(def.did) {
                 if let Some(error_reported) =
-                    self.tcx.typeck_tables_of_const_arg(def).tainted_by_errors
+                    self.tcx.typeck_tables_of_opt_const_arg(def).tainted_by_errors
                 {
                     throw_inval!(TypeckError(error_reported))
                 }
@@ -415,8 +415,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
         match instance {
             ty::InstanceDef::Item(def) => {
                 if self.tcx.is_mir_available(def.did) {
-                    if let Some(def) = def.as_local() {
-                        Ok(self.tcx.optimized_mir_of_const_arg(def))
+                    if let Some((did, param_did)) = def.as_const_arg() {
+                        Ok(self.tcx.optimized_mir_of_const_arg((did, param_did)))
                     } else {
                         Ok(self.tcx.optimized_mir(def.did))
                     }
diff --git a/src/librustc_mir/transform/check_consts/qualifs.rs b/src/librustc_mir/transform/check_consts/qualifs.rs
index a03d3fbb68e..445a0230afd 100644
--- a/src/librustc_mir/transform/check_consts/qualifs.rs
+++ b/src/librustc_mir/transform/check_consts/qualifs.rs
@@ -248,8 +248,8 @@ where
         assert!(promoted.is_none());
         // Don't peek inside trait associated constants.
         if cx.tcx.trait_of_item(def.did).is_none() {
-            let qualifs = if let Some(def) = def.as_local() {
-                cx.tcx.at(constant.span).mir_const_qualif_const_arg(def)
+            let qualifs = if let Some((did, param_did)) = def.as_const_arg() {
+                cx.tcx.at(constant.span).mir_const_qualif_const_arg((did, param_did))
             } else {
                 cx.tcx.at(constant.span).mir_const_qualif(def.did)
             };
diff --git a/src/librustc_mir/transform/check_unsafety.rs b/src/librustc_mir/transform/check_unsafety.rs
index 6d270a9c684..5ee93dfc446 100644
--- a/src/librustc_mir/transform/check_unsafety.rs
+++ b/src/librustc_mir/transform/check_unsafety.rs
@@ -493,12 +493,8 @@ pub(crate) fn provide(providers: &mut Providers) {
         unsafety_check_result: |tcx, def_id| {
             unsafety_check_result(tcx, ty::WithOptParam::dummy(def_id))
         },
-        unsafety_check_result_const_arg: |tcx, def| {
-            if def.param_did.is_none() {
-                tcx.unsafety_check_result(def.did)
-            } else {
-                unsafety_check_result(tcx, def)
-            }
+        unsafety_check_result_const_arg: |tcx, (did, param_did)| {
+            unsafety_check_result(tcx, ty::WithOptParam { did, param_did: Some(param_did) })
         },
         unsafe_derive_on_repr_packed,
         ..*providers
@@ -553,8 +549,8 @@ fn unsafety_check_result<'tcx>(
     def: ty::WithOptParam<LocalDefId>,
 ) -> &'tcx UnsafetyCheckResult {
     if def.param_did.is_none() {
-        if let param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
-            return tcx.unsafety_check_result_const_arg(ty::WithOptParam { param_did, ..def });
+        if let Some(param_did) = tcx.opt_const_param_of(def.did) {
+            return tcx.unsafety_check_result_const_arg((def.did, param_did));
         }
     }
 
diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs
index 8259e1288bb..fdcf36a3162 100644
--- a/src/librustc_mir/transform/mod.rs
+++ b/src/librustc_mir/transform/mod.rs
@@ -51,12 +51,8 @@ pub(crate) fn provide(providers: &mut Providers) {
         mir_const_qualif: |tcx, did| {
             mir_const_qualif(tcx, ty::WithOptParam::dummy(did.expect_local()))
         },
-        mir_const_qualif_const_arg: |tcx, def| {
-            if def.param_did.is_none() {
-                tcx.mir_const_qualif(def.did.to_def_id())
-            } else {
-                mir_const_qualif(tcx, def)
-            }
+        mir_const_qualif_const_arg: |tcx, (did, param_did)| {
+            mir_const_qualif(tcx, ty::WithOptParam { did, param_did: Some(param_did) })
         },
         mir_validated,
         mir_drops_elaborated_and_const_checked,
@@ -227,8 +223,8 @@ pub fn run_passes(
 
 fn mir_const_qualif(tcx: TyCtxt<'_>, def: ty::WithOptParam<LocalDefId>) -> ConstQualifs {
     if def.param_did.is_none() {
-        if let param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
-            return tcx.mir_const_qualif_const_arg(ty::WithOptParam { param_did, ..def });
+        if let Some(param_did) = tcx.opt_const_param_of(def.did) {
+            return tcx.mir_const_qualif_const_arg((def.did, param_did));
         }
     }
 
@@ -278,7 +274,11 @@ fn mir_const<'tcx>(
     }
 
     // Unsafety check uses the raw mir, so make sure it is run.
-    let _ = tcx.unsafety_check_result_const_arg(def);
+    if let Some(param_did) = def.param_did {
+        tcx.ensure().unsafety_check_result_const_arg((def.did, param_did));
+    } else {
+        tcx.ensure().unsafety_check_result(def.did);
+    }
 
     let mut body = tcx.mir_built(def).steal();
 
@@ -321,7 +321,7 @@ fn mir_validated(
 
     // Ensure that we compute the `mir_const_qualif` for constants at
     // this point, before we steal the mir-const result.
-    let _ = tcx.mir_const_qualif_const_arg(def);
+    let _ = tcx.mir_const_qualif_opt_const_arg(def);
 
     let mut body = tcx.mir_const(def).steal();
 
@@ -367,7 +367,11 @@ fn mir_drops_elaborated_and_const_checked<'tcx>(
 
     // (Mir-)Borrowck uses `mir_validated`, so we have to force it to
     // execute before we can steal.
-    tcx.ensure().mir_borrowck_const_arg(def);
+    if let Some(param_did) = def.param_did {
+        tcx.ensure().mir_borrowck_const_arg((def.did, param_did));
+    } else {
+        tcx.ensure().mir_borrowck(def.did);
+    }
 
     let (body, _) = tcx.mir_validated(def);
     let mut body = body.steal();
@@ -485,8 +489,8 @@ fn run_optimization_passes<'tcx>(
 
 fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> {
     let did = did.expect_local();
-    if let param_did @ Some(_) = tcx.opt_const_param_of(did) {
-        tcx.optimized_mir_of_const_arg(ty::WithOptParam { did, param_did })
+    if let Some(param_did) = tcx.opt_const_param_of(did) {
+        tcx.optimized_mir_of_const_arg((did, param_did))
     } else {
         tcx.arena.alloc(inner_optimized_mir(tcx, ty::WithOptParam::dummy(did)))
     }
@@ -494,17 +498,9 @@ fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> {
 
 fn optimized_mir_of_const_arg<'tcx>(
     tcx: TyCtxt<'tcx>,
-    def: ty::WithOptParam<LocalDefId>,
+    (did, param_did): (LocalDefId, DefId),
 ) -> &'tcx Body<'tcx> {
-    if def.param_did.is_none() {
-        if let param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
-            tcx.optimized_mir_of_const_arg(ty::WithOptParam { param_did, ..def })
-        } else {
-            tcx.optimized_mir(def.did)
-        }
-    } else {
-        tcx.arena.alloc(inner_optimized_mir(tcx, def))
-    }
+    tcx.arena.alloc(inner_optimized_mir(tcx, ty::WithOptParam { did, param_did: Some(param_did) }))
 }
 
 fn inner_optimized_mir(tcx: TyCtxt<'_>, def: ty::WithOptParam<LocalDefId>) -> Body<'_> {
@@ -538,7 +534,11 @@ fn promoted_mir<'tcx>(
         return tcx.arena.alloc(IndexVec::new());
     }
 
-    tcx.ensure().mir_borrowck_const_arg(def);
+    if let Some(param_did) = def.param_did {
+        tcx.ensure().mir_borrowck_const_arg((def.did, param_did));
+    } else {
+        tcx.ensure().mir_borrowck(def.did);
+    }
     let (_, promoted) = tcx.mir_validated(def);
     let mut promoted = promoted.steal();
 
diff --git a/src/librustc_mir_build/hair/cx/expr.rs b/src/librustc_mir_build/hair/cx/expr.rs
index e5f9f7328b8..025ef1ece46 100644
--- a/src/librustc_mir_build/hair/cx/expr.rs
+++ b/src/librustc_mir_build/hair/cx/expr.rs
@@ -601,7 +601,7 @@ fn make_mirror_unadjusted<'a, 'tcx>(
                             let substs = InternalSubsts::identity_for_item(cx.tcx(), did);
                             let lhs = mk_const(cx.tcx().mk_const(ty::Const {
                                 val: ty::ConstKind::Unevaluated(
-                                    ty::WithOptParam::init_me_bby(cx.tcx(), did),
+                                    ty::WithOptParam::dummy(did),
                                     substs,
                                     None,
                                 ),
@@ -800,11 +800,7 @@ fn convert_path_expr<'a, 'tcx>(
             debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
             ExprKind::Literal {
                 literal: cx.tcx.mk_const(ty::Const {
-                    val: ty::ConstKind::Unevaluated(
-                        ty::WithOptParam::init_me_bby(cx.tcx, def_id),
-                        substs,
-                        None,
-                    ),
+                    val: ty::ConstKind::Unevaluated(ty::WithOptParam::dummy(def_id), substs, None),
                     ty: cx.tables().node_type(expr.hir_id),
                 }),
                 user_ty,
diff --git a/src/librustc_mir_build/hair/cx/mod.rs b/src/librustc_mir_build/hair/cx/mod.rs
index 5505559fc8e..7daf7b629d8 100644
--- a/src/librustc_mir_build/hair/cx/mod.rs
+++ b/src/librustc_mir_build/hair/cx/mod.rs
@@ -56,7 +56,7 @@ impl<'a, 'tcx> Cx<'a, 'tcx> {
         src_id: hir::HirId,
     ) -> Cx<'a, 'tcx> {
         let tcx = infcx.tcx;
-        let tables = tcx.typeck_tables_of_const_arg(def);
+        let tables = tcx.typeck_tables_of_opt_const_arg(def);
         let body_owner_kind = tcx.hir().body_owner_kind(src_id);
 
         let constness = match body_owner_kind {
diff --git a/src/librustc_ty/instance.rs b/src/librustc_ty/instance.rs
index 7c58963fccc..0a1d8c1077a 100644
--- a/src/librustc_ty/instance.rs
+++ b/src/librustc_ty/instance.rs
@@ -1,5 +1,5 @@
 use rustc_errors::ErrorReported;
-use rustc_hir::def_id::DefId;
+use rustc_hir::def_id::{DefId, LocalDefId};
 use rustc_infer::infer::TyCtxtInferExt;
 use rustc_middle::ty::subst::SubstsRef;
 use rustc_middle::ty::{self, Instance, TyCtxt, TypeFoldable};
@@ -15,32 +15,25 @@ fn resolve_instance<'tcx>(
     key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>,
 ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
     let (param_env, (did, substs)) = key.into_parts();
-    if let param_did @ Some(_) = did.as_local().and_then(|did| tcx.opt_const_param_of(did)) {
-        tcx.resolve_instance_of_const_arg(
-            param_env.and((ty::WithOptParam { did, param_did }, substs)),
-        )
-    } else {
-        inner_resolve_instance(tcx, param_env.and((ty::WithOptParam::dummy(did), substs)))
+    if let Some(did) = did.as_local() {
+        if let Some(param_did) = tcx.opt_const_param_of(did) {
+            return tcx.resolve_instance_of_const_arg(param_env.and((did, param_did, substs)));
+        }
     }
+
+    inner_resolve_instance(tcx, param_env.and((ty::WithOptParam::dummy(did), substs)))
 }
 
 fn resolve_instance_of_const_arg<'tcx>(
     tcx: TyCtxt<'tcx>,
-    key: ty::ParamEnvAnd<'tcx, (ty::WithOptParam<DefId>, SubstsRef<'tcx>)>,
+    key: ty::ParamEnvAnd<'tcx, (LocalDefId, DefId, SubstsRef<'tcx>)>,
 ) -> Result<Option<Instance<'tcx>>, ErrorReported> {
-    let (param_env, (def, substs)) = key.into_parts();
-    if def.param_did.is_none() {
-        if let Some(did) = def.did.as_local() {
-            if let param_did @ Some(_) = tcx.opt_const_param_of(did) {
-                return tcx.resolve_instance_of_const_arg(
-                    param_env.and((ty::WithOptParam { param_did, ..def }, substs)),
-                );
-            }
-        }
-        tcx.resolve_instance(param_env.and((def.did, substs)))
-    } else {
-        inner_resolve_instance(tcx, param_env.and((def, substs)))
-    }
+    let (param_env, (did, param_did, substs)) = key.into_parts();
+    inner_resolve_instance(
+        tcx,
+        param_env
+            .and((ty::WithOptParam { did: did.to_def_id(), param_did: Some(param_did) }, substs)),
+    )
 }
 
 fn inner_resolve_instance<'tcx>(
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index fdeb01cc6b3..fd2a0175cf6 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -764,7 +764,7 @@ pub fn provide(providers: &mut Providers) {
     method::provide(providers);
     *providers = Providers {
         typeck_item_bodies,
-        _typeck_tables_of_const_arg: typeck_tables_of_const_arg,
+        typeck_tables_of_const_arg,
         typeck_tables_of,
         diagnostic_only_typeck_tables_of,
         has_typeck_tables,
@@ -958,19 +958,15 @@ where
 
 fn typeck_tables_of_const_arg<'tcx>(
     tcx: TyCtxt<'tcx>,
-    def: ty::WithOptParam<LocalDefId>,
+    (did, param_did): (LocalDefId, DefId),
 ) -> &ty::TypeckTables<'tcx> {
-    if let Some(param_did) = def.param_did {
-        let fallback = move || tcx.type_of(param_did);
-        typeck_tables_of_with_fallback(tcx, def.did, fallback)
-    } else {
-        bug!("missing param_did")
-    }
+    let fallback = move || tcx.type_of(param_did);
+    typeck_tables_of_with_fallback(tcx, did, fallback)
 }
 
 fn typeck_tables_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &ty::TypeckTables<'tcx> {
-    if let param_did @ Some(_) = tcx.opt_const_param_of(def_id) {
-        tcx.typeck_tables_of_const_arg(ty::WithOptParam { did: def_id, param_did })
+    if let Some(param_did) = tcx.opt_const_param_of(def_id) {
+        tcx.typeck_tables_of_const_arg((def_id, param_did))
     } else {
         let fallback = move || tcx.type_of(def_id.to_def_id());
         typeck_tables_of_with_fallback(tcx, def_id, fallback)
diff --git a/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr b/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr
index 846e2979f84..1b4326ea56a 100644
--- a/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr
+++ b/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-impl.stderr
@@ -30,11 +30,6 @@ note: ...which requires const-evaluating `<impl at $DIR/issue-24949-assoc-const-
    |
 LL |     const BAR: u32 = IMPL_REF_BAR;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-note: ...which requires optimizing MIR for the potential const argument `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`...
-  --> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
-   |
-LL |     const BAR: u32 = IMPL_REF_BAR;
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 note: ...which requires optimizing MIR for `<impl at $DIR/issue-24949-assoc-const-static-recursion-impl.rs:11:1: 13:2>::BAR`...
   --> $DIR/issue-24949-assoc-const-static-recursion-impl.rs:12:5
    |
diff --git a/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr b/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr
index 305d20c05c9..8efa56a9a2e 100644
--- a/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr
+++ b/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait-default.stderr
@@ -30,11 +30,6 @@ note: ...which requires const-evaluating `FooDefault::BAR`...
    |
 LL |     const BAR: u32 = DEFAULT_REF_BAR;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-note: ...which requires optimizing MIR for the potential const argument `FooDefault::BAR`...
-  --> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
-   |
-LL |     const BAR: u32 = DEFAULT_REF_BAR;
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 note: ...which requires optimizing MIR for `FooDefault::BAR`...
   --> $DIR/issue-24949-assoc-const-static-recursion-trait-default.rs:8:5
    |
diff --git a/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr b/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr
index 7a4912aaa73..78ce1a28a3f 100644
--- a/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr
+++ b/src/test/ui/associated-consts/issue-24949-assoc-const-static-recursion-trait.stderr
@@ -30,11 +30,6 @@ note: ...which requires const-evaluating `<impl at $DIR/issue-24949-assoc-const-
    |
 LL |     const BAR: u32 = TRAIT_REF_BAR;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-note: ...which requires optimizing MIR for the potential const argument `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 13:2>::BAR`...
-  --> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:5
-   |
-LL |     const BAR: u32 = TRAIT_REF_BAR;
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 note: ...which requires optimizing MIR for `<impl at $DIR/issue-24949-assoc-const-static-recursion-trait.rs:11:1: 13:2>::BAR`...
   --> $DIR/issue-24949-assoc-const-static-recursion-trait.rs:12:5
    |
diff --git a/src/test/ui/impl-trait/auto-trait-leak.stderr b/src/test/ui/impl-trait/auto-trait-leak.stderr
index 146704a623c..cba894bec9d 100644
--- a/src/test/ui/impl-trait/auto-trait-leak.stderr
+++ b/src/test/ui/impl-trait/auto-trait-leak.stderr
@@ -19,11 +19,6 @@ note: ...which requires processing MIR for `cycle1`...
    |
 LL | fn cycle1() -> impl Clone {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^
-note: ...which requires unsafety-checking the potential const arg `cycle1`...
-  --> $DIR/auto-trait-leak.rs:12:1
-   |
-LL | fn cycle1() -> impl Clone {
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^
 note: ...which requires unsafety-checking `cycle1`...
   --> $DIR/auto-trait-leak.rs:12:1
    |
@@ -60,11 +55,6 @@ note: ...which requires processing MIR for `cycle2`...
    |
 LL | fn cycle2() -> impl Clone {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^
-note: ...which requires unsafety-checking the potential const arg `cycle2`...
-  --> $DIR/auto-trait-leak.rs:20:1
-   |
-LL | fn cycle2() -> impl Clone {
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^
 note: ...which requires unsafety-checking `cycle2`...
   --> $DIR/auto-trait-leak.rs:20:1
    |