Skip to content

Commit 4a86573

Browse files
committed
Auto merge of rust-lang#74404 - lcnr:ty-dep-path-cleanup-aaaaa, r=eddyb
remove some const arg in ty dep path boilerplate followup to rust-lang#74113, together with rust-lang#74376, this closes rust-lang#74360. r? @eddyb
2 parents bbebe73 + 4bffe8f commit 4a86573

File tree

5 files changed

+56
-50
lines changed

5 files changed

+56
-50
lines changed

src/librustc_middle/ty/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1617,12 +1617,33 @@ pub struct WithOptConstParam<T> {
16171617

16181618
impl<T> WithOptConstParam<T> {
16191619
/// Creates a new `WithOptConstParam` setting `const_param_did` to `None`.
1620+
#[inline(always)]
16201621
pub fn unknown(did: T) -> WithOptConstParam<T> {
16211622
WithOptConstParam { did, const_param_did: None }
16221623
}
16231624
}
16241625

16251626
impl WithOptConstParam<LocalDefId> {
1627+
/// Returns `Some((did, param_did))` if `def_id` is a const argument,
1628+
/// `None` otherwise.
1629+
#[inline(always)]
1630+
pub fn try_lookup(did: LocalDefId, tcx: TyCtxt<'_>) -> Option<(LocalDefId, DefId)> {
1631+
tcx.opt_const_param_of(did).map(|param_did| (did, param_did))
1632+
}
1633+
1634+
/// In case `self` is unknown but `self.did` is a const argument, this returns
1635+
/// a `WithOptConstParam` with the correct `const_param_did`.
1636+
#[inline(always)]
1637+
pub fn try_upgrade(self, tcx: TyCtxt<'_>) -> Option<WithOptConstParam<LocalDefId>> {
1638+
if self.const_param_did.is_none() {
1639+
if let const_param_did @ Some(_) = tcx.opt_const_param_of(self.did) {
1640+
return Some(WithOptConstParam { did: self.did, const_param_did });
1641+
}
1642+
}
1643+
1644+
None
1645+
}
1646+
16261647
pub fn to_global(self) -> WithOptConstParam<DefId> {
16271648
WithOptConstParam { did: self.did.to_def_id(), const_param_did: self.const_param_did }
16281649
}

src/librustc_mir/borrow_check/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ const DEREF_PROJECTION: &[PlaceElem<'_>; 1] = &[ProjectionElem::Deref];
8888

8989
pub fn provide(providers: &mut Providers) {
9090
*providers = Providers {
91-
mir_borrowck: |tcx, did| mir_borrowck(tcx, ty::WithOptConstParam::unknown(did)),
91+
mir_borrowck: |tcx, did| {
92+
if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) {
93+
tcx.mir_borrowck_const_arg(def)
94+
} else {
95+
mir_borrowck(tcx, ty::WithOptConstParam::unknown(did))
96+
}
97+
},
9298
mir_borrowck_const_arg: |tcx, (did, param_did)| {
9399
mir_borrowck(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
94100
},
@@ -100,12 +106,6 @@ fn mir_borrowck<'tcx>(
100106
tcx: TyCtxt<'tcx>,
101107
def: ty::WithOptConstParam<LocalDefId>,
102108
) -> &'tcx BorrowCheckResult<'tcx> {
103-
if def.const_param_did.is_none() {
104-
if let Some(param_did) = tcx.opt_const_param_of(def.did) {
105-
return tcx.mir_borrowck_const_arg((def.did, param_did));
106-
}
107-
}
108-
109109
let (input_body, promoted) = tcx.mir_validated(def);
110110
debug!("run query mir_borrowck: {}", tcx.def_path_str(def.did.to_def_id()));
111111

src/librustc_mir/transform/check_unsafety.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,11 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
439439
pub(crate) fn provide(providers: &mut Providers) {
440440
*providers = Providers {
441441
unsafety_check_result: |tcx, def_id| {
442-
unsafety_check_result(tcx, ty::WithOptConstParam::unknown(def_id))
442+
if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
443+
tcx.unsafety_check_result_for_const_arg(def)
444+
} else {
445+
unsafety_check_result(tcx, ty::WithOptConstParam::unknown(def_id))
446+
}
443447
},
444448
unsafety_check_result_for_const_arg: |tcx, (did, param_did)| {
445449
unsafety_check_result(
@@ -499,12 +503,6 @@ fn unsafety_check_result<'tcx>(
499503
tcx: TyCtxt<'tcx>,
500504
def: ty::WithOptConstParam<LocalDefId>,
501505
) -> &'tcx UnsafetyCheckResult {
502-
if def.const_param_did.is_none() {
503-
if let Some(param_did) = tcx.opt_const_param_of(def.did) {
504-
return tcx.unsafety_check_result_for_const_arg((def.did, param_did));
505-
}
506-
}
507-
508506
debug!("unsafety_violations({:?})", def);
509507

510508
// N.B., this borrow is valid because all the consumers of

src/librustc_mir/transform/mod.rs

+21-32
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,13 @@ pub(crate) fn provide(providers: &mut Providers) {
4848
*providers = Providers {
4949
mir_keys,
5050
mir_const,
51-
mir_const_qualif: |tcx, did| {
52-
mir_const_qualif(tcx, ty::WithOptConstParam::unknown(did.expect_local()))
51+
mir_const_qualif: |tcx, def_id| {
52+
let def_id = def_id.expect_local();
53+
if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
54+
tcx.mir_const_qualif_const_arg(def)
55+
} else {
56+
mir_const_qualif(tcx, ty::WithOptConstParam::unknown(def_id))
57+
}
5358
},
5459
mir_const_qualif_const_arg: |tcx, (did, param_did)| {
5560
mir_const_qualif(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
@@ -60,7 +65,12 @@ pub(crate) fn provide(providers: &mut Providers) {
6065
optimized_mir_of_const_arg,
6166
is_mir_available,
6267
promoted_mir: |tcx, def_id| {
63-
promoted_mir(tcx, ty::WithOptConstParam::unknown(def_id.expect_local()))
68+
let def_id = def_id.expect_local();
69+
if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
70+
tcx.promoted_mir_of_const_arg(def)
71+
} else {
72+
promoted_mir(tcx, ty::WithOptConstParam::unknown(def_id))
73+
}
6474
},
6575
promoted_mir_of_const_arg: |tcx, (did, param_did)| {
6676
promoted_mir(tcx, ty::WithOptConstParam { did, const_param_did: Some(param_did) })
@@ -221,12 +231,6 @@ pub fn run_passes(
221231
}
222232

223233
fn mir_const_qualif(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> ConstQualifs {
224-
if def.const_param_did.is_none() {
225-
if let Some(param_did) = tcx.opt_const_param_of(def.did) {
226-
return tcx.mir_const_qualif_const_arg((def.did, param_did));
227-
}
228-
}
229-
230234
let const_kind = tcx.hir().body_const_context(def.did);
231235

232236
// No need to const-check a non-const `fn`.
@@ -266,10 +270,8 @@ fn mir_const<'tcx>(
266270
tcx: TyCtxt<'tcx>,
267271
def: ty::WithOptConstParam<LocalDefId>,
268272
) -> &'tcx Steal<Body<'tcx>> {
269-
if def.const_param_did.is_none() {
270-
if let const_param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
271-
return tcx.mir_const(ty::WithOptConstParam { const_param_did, ..def });
272-
}
273+
if let Some(def) = def.try_upgrade(tcx) {
274+
return tcx.mir_const(def);
273275
}
274276

275277
// Unsafety check uses the raw mir, so make sure it is run.
@@ -312,10 +314,8 @@ fn mir_validated(
312314
tcx: TyCtxt<'tcx>,
313315
def: ty::WithOptConstParam<LocalDefId>,
314316
) -> (&'tcx Steal<Body<'tcx>>, &'tcx Steal<IndexVec<Promoted, Body<'tcx>>>) {
315-
if def.const_param_did.is_none() {
316-
if let const_param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
317-
return tcx.mir_validated(ty::WithOptConstParam { const_param_did, ..def });
318-
}
317+
if let Some(def) = def.try_upgrade(tcx) {
318+
return tcx.mir_validated(def);
319319
}
320320

321321
// Ensure that we compute the `mir_const_qualif` for constants at
@@ -357,13 +357,8 @@ fn mir_drops_elaborated_and_const_checked<'tcx>(
357357
tcx: TyCtxt<'tcx>,
358358
def: ty::WithOptConstParam<LocalDefId>,
359359
) -> &'tcx Steal<Body<'tcx>> {
360-
if def.const_param_did.is_none() {
361-
if let const_param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
362-
return tcx.mir_drops_elaborated_and_const_checked(ty::WithOptConstParam {
363-
const_param_did,
364-
..def
365-
});
366-
}
360+
if let Some(def) = def.try_upgrade(tcx) {
361+
return tcx.mir_drops_elaborated_and_const_checked(def);
367362
}
368363

369364
// (Mir-)Borrowck uses `mir_validated`, so we have to force it to
@@ -490,8 +485,8 @@ fn run_optimization_passes<'tcx>(
490485

491486
fn optimized_mir<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx Body<'tcx> {
492487
let did = did.expect_local();
493-
if let Some(param_did) = tcx.opt_const_param_of(did) {
494-
tcx.optimized_mir_of_const_arg((did, param_did))
488+
if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) {
489+
tcx.optimized_mir_of_const_arg(def)
495490
} else {
496491
tcx.arena.alloc(inner_optimized_mir(tcx, ty::WithOptConstParam::unknown(did)))
497492
}
@@ -528,12 +523,6 @@ fn promoted_mir<'tcx>(
528523
tcx: TyCtxt<'tcx>,
529524
def: ty::WithOptConstParam<LocalDefId>,
530525
) -> &'tcx IndexVec<Promoted, Body<'tcx>> {
531-
if def.const_param_did.is_none() {
532-
if let Some(param_did) = tcx.opt_const_param_of(def.did) {
533-
return tcx.promoted_mir_of_const_arg((def.did, param_did));
534-
}
535-
}
536-
537526
if tcx.is_constructor(def.did.to_def_id()) {
538527
return tcx.arena.alloc(IndexVec::new());
539528
}

src/librustc_mir_build/build/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ crate fn mir_built<'tcx>(
2525
tcx: TyCtxt<'tcx>,
2626
def: ty::WithOptConstParam<LocalDefId>,
2727
) -> &'tcx ty::steal::Steal<Body<'tcx>> {
28-
if def.const_param_did.is_none() {
29-
if let const_param_did @ Some(_) = tcx.opt_const_param_of(def.did) {
30-
return tcx.mir_built(ty::WithOptConstParam { const_param_did, ..def });
31-
}
28+
if let Some(def) = def.try_upgrade(tcx) {
29+
return tcx.mir_built(def);
3230
}
3331

3432
tcx.alloc_steal_mir(mir_build(tcx, def))

0 commit comments

Comments
 (0)