Skip to content

Commit ae80d7e

Browse files
committed
update const arg queries
1 parent 08394eb commit ae80d7e

File tree

21 files changed

+154
-138
lines changed

21 files changed

+154
-138
lines changed

src/librustc_middle/mir/query.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Values computed by queries that use MIR.
22
3-
use crate::ty::{self, Ty};
3+
use crate::ty::{self, Ty, TyCtxt};
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_data_structures::sync::Lrc;
66
use rustc_hir as hir;
7-
use rustc_hir::def_id::DefId;
7+
use rustc_hir::def_id::{DefId, LocalDefId};
88
use rustc_index::bit_set::BitMatrix;
99
use rustc_index::vec::IndexVec;
1010
use rustc_span::{Span, Symbol};
@@ -323,3 +323,24 @@ pub struct CoverageInfo {
323323
/// The total number of coverage region counters added to the MIR `Body`.
324324
pub num_counters: u32,
325325
}
326+
327+
impl<'tcx> TyCtxt<'tcx> {
328+
pub fn mir_borrowck_opt_const_arg(
329+
self,
330+
def: ty::WithOptParam<LocalDefId>,
331+
) -> &'tcx BorrowCheckResult<'tcx> {
332+
if let Some(param_did) = def.param_did {
333+
self.mir_borrowck_const_arg((def.did, param_did))
334+
} else {
335+
self.mir_borrowck(def.did)
336+
}
337+
}
338+
339+
pub fn mir_const_qualif_opt_const_arg(self, def: ty::WithOptParam<LocalDefId>) -> ConstQualifs {
340+
if let Some(param_did) = def.param_did {
341+
self.mir_const_qualif_const_arg((def.did, param_did))
342+
} else {
343+
self.mir_const_qualif(def.did)
344+
}
345+
}
346+
}

src/librustc_middle/query/mod.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ rustc_queries! {
209209
cache_on_disk_if { key.is_local() }
210210
}
211211
query mir_const_qualif_const_arg(
212-
key: ty::WithOptParam<LocalDefId>
212+
key: (LocalDefId, DefId)
213213
) -> mir::ConstQualifs {
214214
desc {
215-
|tcx| "const checking the potential const argument `{}`",
216-
tcx.def_path_str(key.did.to_def_id())
215+
|tcx| "const checking the const argument `{}`",
216+
tcx.def_path_str(key.0.to_def_id())
217217
}
218218
}
219219

@@ -257,10 +257,10 @@ rustc_queries! {
257257
desc { |tcx| "optimizing MIR for `{}`", tcx.def_path_str(key) }
258258
cache_on_disk_if { key.is_local() }
259259
}
260-
query optimized_mir_of_const_arg(key: ty::WithOptParam<LocalDefId>) -> &'tcx mir::Body<'tcx> {
260+
query optimized_mir_of_const_arg(key: (LocalDefId, DefId)) -> &'tcx mir::Body<'tcx> {
261261
desc {
262-
|tcx| "optimizing MIR for the potential const argument `{}`",
263-
tcx.def_path_str(key.did.to_def_id())
262+
|tcx| "optimizing MIR for the const argument `{}`",
263+
tcx.def_path_str(key.0.to_def_id())
264264
}
265265
}
266266

@@ -280,7 +280,7 @@ rustc_queries! {
280280
key: ty::WithOptParam<LocalDefId>
281281
) -> &'tcx IndexVec<mir::Promoted, mir::Body<'tcx>> {
282282
desc {
283-
|tcx| "optimizing promoted MIR for the potential const argument `{}`",
283+
|tcx| "optimizing promoted MIR for the const argument `{}`",
284284
tcx.def_path_str(key.did.to_def_id()),
285285
}
286286
}
@@ -496,8 +496,8 @@ rustc_queries! {
496496
desc { |tcx| "unsafety-checking `{}`", tcx.def_path_str(key.to_def_id()) }
497497
cache_on_disk_if { true }
498498
}
499-
query unsafety_check_result_const_arg(key: ty::WithOptParam<LocalDefId>) -> &'tcx mir::UnsafetyCheckResult {
500-
desc { |tcx| "unsafety-checking the potential const arg `{}`", tcx.def_path_str(key.did.to_def_id()) }
499+
query unsafety_check_result_const_arg(key: (LocalDefId, DefId)) -> &'tcx mir::UnsafetyCheckResult {
500+
desc { |tcx| "unsafety-checking the const arg `{}`", tcx.def_path_str(key.0.to_def_id()) }
501501
}
502502

503503
/// HACK: when evaluated, this reports a "unsafe derive on repr(packed)" error.
@@ -579,12 +579,12 @@ rustc_queries! {
579579
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key.to_def_id()) }
580580
cache_on_disk_if { true }
581581
}
582-
query _typeck_tables_of_const_arg(
583-
key: ty::WithOptParam<LocalDefId>
582+
query typeck_tables_of_const_arg(
583+
key: (LocalDefId, DefId)
584584
) -> &'tcx ty::TypeckTables<'tcx> {
585585
desc {
586586
|tcx| "type-checking the const argument `{}`",
587-
tcx.def_path_str(key.did.to_def_id()),
587+
tcx.def_path_str(key.0.to_def_id()),
588588
}
589589
}
590590
query diagnostic_only_typeck_tables_of(key: LocalDefId) -> &'tcx ty::TypeckTables<'tcx> {
@@ -627,10 +627,10 @@ rustc_queries! {
627627
|| opt_result.map_or(false, |r| !r.concrete_opaque_types.is_empty())
628628
}
629629
}
630-
query mir_borrowck_const_arg(key: ty::WithOptParam<LocalDefId>) -> &'tcx mir::BorrowCheckResult<'tcx> {
630+
query mir_borrowck_const_arg(key: (LocalDefId, DefId)) -> &'tcx mir::BorrowCheckResult<'tcx> {
631631
desc {
632-
|tcx| "borrow-checking the potential const argument`{}`",
633-
tcx.def_path_str(key.did.to_def_id())
632+
|tcx| "borrow-checking the const argument`{}`",
633+
tcx.def_path_str(key.0.to_def_id())
634634
}
635635
}
636636
}
@@ -1501,11 +1501,11 @@ rustc_queries! {
15011501
}
15021502

15031503
query resolve_instance_of_const_arg(
1504-
key: ty::ParamEnvAnd<'tcx, (ty::WithOptParam<DefId>, SubstsRef<'tcx>)>
1504+
key: ty::ParamEnvAnd<'tcx, (LocalDefId, DefId, SubstsRef<'tcx>)>
15051505
) -> Result<Option<ty::Instance<'tcx>>, ErrorReported> {
15061506
desc {
1507-
"resolving instance of the potential const argument `{}`",
1508-
ty::Instance::new(key.value.0.did, key.value.1),
1507+
"resolving instance of the const argument `{}`",
1508+
ty::Instance::new(key.value.0.to_def_id(), key.value.2),
15091509
}
15101510
}
15111511
}

src/librustc_middle/ty/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,12 +980,12 @@ pub struct GlobalCtxt<'tcx> {
980980
}
981981

982982
impl<'tcx> TyCtxt<'tcx> {
983-
pub fn typeck_tables_of_const_arg(
983+
pub fn typeck_tables_of_opt_const_arg(
984984
self,
985985
def: ty::WithOptParam<LocalDefId>,
986986
) -> &'tcx TypeckTables<'tcx> {
987-
if def.param_did.is_some() {
988-
self._typeck_tables_of_const_arg(def)
987+
if let Some(param_did) = def.param_did {
988+
self.typeck_tables_of_const_arg((def.did, param_did))
989989
} else {
990990
self.typeck_tables_of(def.did)
991991
}

src/librustc_middle/ty/instance.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,14 @@ impl<'tcx> Instance<'tcx> {
359359
substs: SubstsRef<'tcx>,
360360
) -> Result<Option<Instance<'tcx>>, ErrorReported> {
361361
let substs = tcx.erase_regions(&substs);
362-
tcx.resolve_instance_of_const_arg(tcx.erase_regions(&param_env.and((def, substs))))
362+
363+
if let Some((did, param_did)) = def.as_const_arg() {
364+
tcx.resolve_instance_of_const_arg(
365+
tcx.erase_regions(&param_env.and((did, param_did, substs))),
366+
)
367+
} else {
368+
tcx.resolve_instance(tcx.erase_regions(&param_env.and((def.did, substs))))
369+
}
363370
}
364371

365372
pub fn resolve_for_fn_ptr(

src/librustc_middle/ty/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,16 @@ impl WithOptParam<DefId> {
16001600
self.did.as_local().map(|did| WithOptParam { did, param_did: self.param_did })
16011601
}
16021602

1603+
pub fn as_const_arg(self) -> Option<(LocalDefId, DefId)> {
1604+
if let Some(param_did) = self.param_did {
1605+
if let Some(did) = self.did.as_local() {
1606+
return Some((did, param_did));
1607+
}
1608+
}
1609+
1610+
None
1611+
}
1612+
16031613
pub fn expect_local(self) -> WithOptParam<LocalDefId> {
16041614
self.as_local().unwrap()
16051615
}
@@ -1611,10 +1621,6 @@ impl WithOptParam<DefId> {
16111621
pub fn ty_def_id(self) -> DefId {
16121622
self.param_did.unwrap_or(self.did)
16131623
}
1614-
1615-
pub fn init_me_bby(tcx: TyCtxt<'_>, did: DefId) -> WithOptParam<DefId> {
1616-
WithOptParam { did, param_did: did.as_local().and_then(|did| tcx.opt_const_param_of(did)) }
1617-
}
16181624
}
16191625

16201626
/// When type checking, we use the `ParamEnv` to track
@@ -2889,8 +2895,9 @@ impl<'tcx> TyCtxt<'tcx> {
28892895
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
28902896
match instance {
28912897
ty::InstanceDef::Item(def) => {
2892-
if let Some(def) = def.as_local() {
2893-
self.optimized_mir_of_const_arg(def)
2898+
if let Some((did, param_did)) = def.as_const_arg() {
2899+
// The `param_did` is only `Some` for local `DefId`s.
2900+
self.optimized_mir_of_const_arg((did, param_did))
28942901
} else {
28952902
self.optimized_mir(def.did)
28962903
}

src/librustc_middle/ty/query/keys.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ impl Key for (DefId, LocalDefId) {
138138
}
139139
}
140140

141+
impl Key for (LocalDefId, DefId) {
142+
type CacheSelector = DefaultCacheSelector;
143+
144+
fn query_crate(&self) -> CrateNum {
145+
LOCAL_CRATE
146+
}
147+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
148+
self.0.default_span(tcx)
149+
}
150+
}
151+
141152
impl Key for (CrateNum, DefId) {
142153
type CacheSelector = DefaultCacheSelector;
143154

@@ -182,14 +193,14 @@ impl<'tcx> Key for (DefId, SubstsRef<'tcx>) {
182193
}
183194
}
184195

185-
impl<'tcx> Key for (ty::WithOptParam<DefId>, SubstsRef<'tcx>) {
196+
impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) {
186197
type CacheSelector = DefaultCacheSelector;
187198

188199
fn query_crate(&self) -> CrateNum {
189200
LOCAL_CRATE
190201
}
191202
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
192-
self.0.did.default_span(tcx)
203+
self.0.default_span(tcx)
193204
}
194205
}
195206

src/librustc_middle/ty/structural_impls.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ CloneTypeFoldableAndLiftImpls! {
272272
::rustc_span::symbol::Symbol,
273273
::rustc_hir::def::Res,
274274
::rustc_hir::def_id::DefId,
275+
::rustc_hir::def_id::LocalDefId,
275276
::rustc_hir::LlvmInlineAsmInner,
276277
::rustc_hir::MatchSource,
277278
::rustc_hir::Mutability,
@@ -719,6 +720,18 @@ impl<'tcx, T: TypeFoldable<'tcx>, U: TypeFoldable<'tcx>> TypeFoldable<'tcx> for
719720
}
720721
}
721722

723+
impl<'tcx, A: TypeFoldable<'tcx>, B: TypeFoldable<'tcx>, C: TypeFoldable<'tcx>> TypeFoldable<'tcx>
724+
for (A, B, C)
725+
{
726+
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> (A, B, C) {
727+
(self.0.fold_with(folder), self.1.fold_with(folder), self.2.fold_with(folder))
728+
}
729+
730+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
731+
self.0.visit_with(visitor) || self.1.visit_with(visitor) || self.2.visit_with(visitor)
732+
}
733+
}
734+
722735
EnumTypeFoldableImpl! {
723736
impl<'tcx, T> TypeFoldable<'tcx> for Option<T> {
724737
(Some)(a),

src/librustc_mir/borrow_check/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ const DEREF_PROJECTION: &[PlaceElem<'_>; 1] = &[ProjectionElem::Deref];
8989
pub fn provide(providers: &mut Providers) {
9090
*providers = Providers {
9191
mir_borrowck: |tcx, did| mir_borrowck(tcx, ty::WithOptParam::dummy(did)),
92-
mir_borrowck_const_arg: |tcx, def| {
93-
if def.param_did.is_none() { tcx.mir_borrowck(def.did) } else { mir_borrowck(tcx, def) }
92+
mir_borrowck_const_arg: |tcx, (did, param_did)| {
93+
mir_borrowck(tcx, ty::WithOptParam { did, param_did: Some(param_did) })
9494
},
9595
..*providers
9696
};
@@ -101,8 +101,8 @@ fn mir_borrowck<'tcx>(
101101
def: ty::WithOptParam<LocalDefId>,
102102
) -> &'tcx BorrowCheckResult<'tcx> {
103103
if def.param_did.is_none() {
104-
if let param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
105-
return tcx.mir_borrowck_const_arg(ty::WithOptParam { param_did, ..def });
104+
if let Some(param_did) = tcx.opt_const_param_of(def.did) {
105+
return tcx.mir_borrowck_const_arg((def.did, param_did));
106106
}
107107
}
108108

@@ -150,7 +150,7 @@ fn do_mir_borrowck<'a, 'tcx>(
150150
}
151151

152152
// Gather the upvars of a closure, if any.
153-
let tables = tcx.typeck_tables_of_const_arg(def);
153+
let tables = tcx.typeck_tables_of_opt_const_arg(def);
154154
if let Some(ErrorReported) = tables.tainted_by_errors {
155155
infcx.set_tainted_by_errors();
156156
}

src/librustc_mir/const_eval/eval_queries.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ pub fn const_eval_raw_provider<'tcx>(
292292

293293
if let Some(def) = def.as_local() {
294294
if tcx.has_typeck_tables(def.did) {
295-
if let Some(error_reported) = tcx.typeck_tables_of_const_arg(def).tainted_by_errors {
295+
if let Some(error_reported) = tcx.typeck_tables_of_opt_const_arg(def).tainted_by_errors
296+
{
296297
return Err(ErrorHandled::Reported(error_reported));
297298
}
298299
}

src/librustc_mir/interpret/eval_context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
398398
if let Some(def) = def.as_local() {
399399
if self.tcx.has_typeck_tables(def.did) {
400400
if let Some(error_reported) =
401-
self.tcx.typeck_tables_of_const_arg(def).tainted_by_errors
401+
self.tcx.typeck_tables_of_opt_const_arg(def).tainted_by_errors
402402
{
403403
throw_inval!(TypeckError(error_reported))
404404
}
@@ -415,8 +415,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
415415
match instance {
416416
ty::InstanceDef::Item(def) => {
417417
if self.tcx.is_mir_available(def.did) {
418-
if let Some(def) = def.as_local() {
419-
Ok(self.tcx.optimized_mir_of_const_arg(def))
418+
if let Some((did, param_did)) = def.as_const_arg() {
419+
Ok(self.tcx.optimized_mir_of_const_arg((did, param_did)))
420420
} else {
421421
Ok(self.tcx.optimized_mir(def.did))
422422
}

0 commit comments

Comments
 (0)