Skip to content

Commit 0e017fc

Browse files
committed
Feed type_of query instead of using WithOptconstParam.
1 parent 4224b4b commit 0e017fc

File tree

7 files changed

+40
-39
lines changed

7 files changed

+40
-39
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
447447
handle_ty_args(has_default, &inf.to_ty())
448448
}
449449
(GenericParamDefKind::Const { .. }, GenericArg::Const(ct)) => {
450-
ty::Const::from_opt_const_arg_anon_const(
451-
tcx,
452-
ty::WithOptConstParam {
453-
did: ct.value.def_id,
454-
const_param_did: Some(param.def_id),
455-
},
456-
)
457-
.into()
450+
let did = ct.value.def_id;
451+
tcx.feed_anon_const_type(did, tcx.type_of(param.def_id));
452+
ty::Const::from_anon_const(tcx, did).into()
458453
}
459454
(&GenericParamDefKind::Const { .. }, hir::GenericArg::Infer(inf)) => {
460455
let ty = tcx

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
420420
ast_c: &hir::AnonConst,
421421
param_def_id: DefId,
422422
) -> ty::Const<'tcx> {
423-
let const_def =
424-
ty::WithOptConstParam { did: ast_c.def_id, const_param_did: Some(param_def_id) };
425-
let c = ty::Const::from_opt_const_arg_anon_const(self.tcx, const_def);
423+
let did = ast_c.def_id;
424+
self.tcx.feed_anon_const_type(did, self.tcx.type_of(param_def_id));
425+
let c = ty::Const::from_anon_const(self.tcx, did);
426426
self.register_wf_obligation(
427427
c.into(),
428428
self.tcx.hir().span(ast_c.hir_id),

compiler/rustc_hir_typeck/src/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,8 @@ fn typeck_const_arg<'tcx>(
165165
}
166166

167167
fn typeck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &ty::TypeckResults<'tcx> {
168-
if let Some(param_did) = tcx.opt_const_param_of(def_id) {
169-
tcx.typeck_const_arg((def_id, param_did))
170-
} else {
171-
let fallback = move || tcx.type_of(def_id.to_def_id()).subst_identity();
172-
typeck_with_fallback(tcx, def_id, fallback)
173-
}
168+
let fallback = move || tcx.type_of(def_id.to_def_id()).subst_identity();
169+
typeck_with_fallback(tcx, def_id, fallback)
174170
}
175171

176172
/// Used only to get `TypeckResults` for type inference during error recovery.

compiler/rustc_middle/src/ty/context.rs

+8
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,14 @@ impl<'tcx> TyCtxt<'tcx> {
449449
pub fn feed_local_crate(self) -> TyCtxtFeed<'tcx, CrateNum> {
450450
TyCtxtFeed { tcx: self, key: LOCAL_CRATE }
451451
}
452+
453+
/// In order to break cycles involving `AnonConst`, we need to set the expected type by side
454+
/// effect. However, we do not want this as a general capability, so this interface restricts
455+
/// to the only allowed case.
456+
pub fn feed_anon_const_type(self, key: LocalDefId, value: ty::EarlyBinder<Ty<'tcx>>) {
457+
debug_assert_eq!(self.def_kind(key), DefKind::AnonConst);
458+
TyCtxtFeed { tcx: self, key }.type_of(value)
459+
}
452460
}
453461

454462
impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {

compiler/rustc_middle/src/ty/mod.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -1548,20 +1548,14 @@ impl WithOptConstParam<LocalDefId> {
15481548
/// Returns `Some((did, param_did))` if `def_id` is a const argument,
15491549
/// `None` otherwise.
15501550
#[inline(always)]
1551-
pub fn try_lookup(did: LocalDefId, tcx: TyCtxt<'_>) -> Option<(LocalDefId, DefId)> {
1552-
tcx.opt_const_param_of(did).map(|param_did| (did, param_did))
1551+
pub fn try_lookup(_: LocalDefId, _: TyCtxt<'_>) -> Option<(LocalDefId, DefId)> {
1552+
None
15531553
}
15541554

15551555
/// In case `self` is unknown but `self.did` is a const argument, this returns
15561556
/// a `WithOptConstParam` with the correct `const_param_did`.
15571557
#[inline(always)]
1558-
pub fn try_upgrade(self, tcx: TyCtxt<'_>) -> Option<WithOptConstParam<LocalDefId>> {
1559-
if self.const_param_did.is_none() {
1560-
if let const_param_did @ Some(_) = tcx.opt_const_param_of(self.did) {
1561-
return Some(WithOptConstParam { did: self.did, const_param_did });
1562-
}
1563-
}
1564-
1558+
pub fn try_upgrade(self, _: TyCtxt<'_>) -> Option<WithOptConstParam<LocalDefId>> {
15651559
None
15661560
}
15671561

@@ -1570,7 +1564,7 @@ impl WithOptConstParam<LocalDefId> {
15701564
}
15711565

15721566
pub fn def_id_for_type_of(self) -> DefId {
1573-
if let Some(did) = self.const_param_did { did } else { self.did.to_def_id() }
1567+
self.did.to_def_id()
15741568
}
15751569
}
15761570

compiler/rustc_middle/src/ty/query.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ use std::ops::Deref;
7676
use std::path::PathBuf;
7777
use std::sync::Arc;
7878

79+
use rustc_data_structures::fingerprint::Fingerprint;
80+
use rustc_query_system::ich::StableHashingContext;
81+
7982
#[derive(Default)]
8083
pub struct QuerySystem<'tcx> {
8184
pub arenas: QueryArenas<'tcx>,
@@ -477,21 +480,33 @@ macro_rules! define_feedable {
477480
$(impl<'tcx, K: IntoQueryParam<$($K)*> + Copy> TyCtxtFeed<'tcx, K> {
478481
$(#[$attr])*
479482
#[inline(always)]
480-
pub fn $name(self, value: query_provided::$name<'tcx>) -> $V {
483+
pub fn $name(self, value: query_provided::$name<'tcx>) {
481484
let key = self.key().into_query_param();
482485

483486
let tcx = self.tcx;
484487
let erased = query_provided_to_value::$name(tcx, value);
485488
let value = restore::<$V>(erased);
486489
let cache = &tcx.query_system.caches.$name;
487490

491+
let hasher: Option<fn(&mut StableHashingContext<'_>, &_) -> _> = hash_result!([$($modifiers)*]);
488492
match try_get_cached(tcx, cache, &key) {
489493
Some(old) => {
490494
let old = restore::<$V>(old);
491-
bug!(
492-
"Trying to feed an already recorded value for query {} key={key:?}:\nold value: {old:?}\nnew value: {value:?}",
493-
stringify!($name),
494-
)
495+
if let Some(hasher) = hasher {
496+
let (value_hash, old_hash): (Fingerprint, Fingerprint) = tcx.with_stable_hashing_context(|mut hcx|
497+
(hasher(&mut hcx, &value), hasher(&mut hcx, &old))
498+
);
499+
assert_eq!(
500+
old_hash, value_hash,
501+
"Trying to feed an already recorded value for query {} key={key:?}:\nold value: {old:?}\nnew value: {value:?}",
502+
stringify!($name),
503+
)
504+
} else {
505+
bug!(
506+
"Trying to feed an already recorded value for query {} key={key:?}:\nold value: {old:?}\nnew value: {value:?}",
507+
stringify!($name),
508+
)
509+
}
495510
}
496511
None => {
497512
let dep_node = dep_graph::DepNode::construct(tcx, dep_graph::DepKind::$name, &key);
@@ -503,7 +518,6 @@ macro_rules! define_feedable {
503518
hash_result!([$($modifiers)*]),
504519
);
505520
cache.complete(key, erased, dep_node_index);
506-
value
507521
}
508522
}
509523
}

compiler/rustc_ty_utils/src/instance.rs

-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ fn resolve_instance<'tcx>(
1515
key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)>,
1616
) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> {
1717
let (param_env, (did, substs)) = key.into_parts();
18-
if let Some(did) = did.as_local() {
19-
if let Some(param_did) = tcx.opt_const_param_of(did) {
20-
return tcx.resolve_instance_of_const_arg(param_env.and((did, param_did, substs)));
21-
}
22-
}
23-
2418
inner_resolve_instance(tcx, param_env.and((ty::WithOptConstParam::unknown(did), substs)))
2519
}
2620

0 commit comments

Comments
 (0)