Skip to content

Commit f3ef3bd

Browse files
committed
Auto merge of #157374 - khyperia:remove-UnevaluatedConstKind-def_id, r=BoxyUwU
remove UnevaluatedConstKind::def_id this is some of the const side of #152245 not quite a _full_ removal, there's still some spicy things such as `UnevaluatedConstKind::def_span` remaining that won't quite work for new non-DefID `UnevaluatedConstKind` cases, but IMO this is the bulk of the work, and feature-specific things can deal with their quirks in their own PRs when they know their own use cases. r? @BoxyUwU self-reminder: file an issue on what to do about rustc_public's handling of the raw DefIds in rustc_public AliasTy/AliasConst
2 parents 877a131 + f1099c0 commit f3ef3bd

26 files changed

Lines changed: 232 additions & 168 deletions

File tree

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,11 +1760,14 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
17601760
let tcx = self.tcx();
17611761
let maybe_uneval = match constant.const_ {
17621762
Const::Ty(_, ct) => match ct.kind() {
1763-
ty::ConstKind::Unevaluated(uv) => Some(UnevaluatedConst {
1764-
def: uv.kind.def_id(),
1765-
args: uv.args,
1766-
promoted: None,
1767-
}),
1763+
ty::ConstKind::Unevaluated(uv) => match uv.kind {
1764+
ty::UnevaluatedConstKind::Projection { def_id }
1765+
| ty::UnevaluatedConstKind::Inherent { def_id }
1766+
| ty::UnevaluatedConstKind::Free { def_id }
1767+
| ty::UnevaluatedConstKind::Anon { def_id } => {
1768+
Some(UnevaluatedConst { def: def_id, args: uv.args, promoted: None })
1769+
}
1770+
},
17681771
_ => None,
17691772
},
17701773
Const::Unevaluated(uv, _) => Some(uv),

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,10 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
777777
if has_default {
778778
// need to store default and type of default
779779
let ct = tcx.const_param_default(param.def_id).skip_binder();
780-
if let ty::ConstKind::Unevaluated(uv) = ct.kind() {
781-
tcx.ensure_ok().type_of(uv.kind.def_id());
780+
if let ty::ConstKind::Unevaluated(uv) = ct.kind()
781+
&& let Some(def_id) = uv.kind.opt_def_id()
782+
{
783+
tcx.ensure_ok().type_of(def_id);
782784
}
783785
}
784786
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,11 +1506,7 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id:
15061506
| ty::ConstKind::Bound(_, _) => unreachable!(),
15071507
ty::ConstKind::Error(_) | ty::ConstKind::Expr(_) => continue,
15081508
ty::ConstKind::Value(cv) => cv.ty,
1509-
ty::ConstKind::Unevaluated(uv) => infcx
1510-
.tcx
1511-
.type_of(uv.kind.def_id())
1512-
.instantiate(infcx.tcx, uv.args)
1513-
.skip_norm_wip(),
1509+
ty::ConstKind::Unevaluated(uv) => uv.type_of(infcx.tcx).skip_norm_wip(),
15141510
ty::ConstKind::Param(param_ct) => {
15151511
param_ct.find_const_ty_from_env(wfcx.param_env)
15161512
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,11 @@ fn const_evaluatable_predicates_of<'tcx>(
415415
preds: FxIndexSet<(ty::Clause<'tcx>, Span)>,
416416
}
417417

418-
fn is_const_param_default(tcx: TyCtxt<'_>, def: LocalDefId) -> bool {
419-
let hir_id = tcx.local_def_id_to_hir_id(def);
418+
fn is_const_param_default(tcx: TyCtxt<'_>, kind: ty::UnevaluatedConstKind<'_>) -> bool {
419+
let ty::UnevaluatedConstKind::Anon { def_id } = kind else { return false };
420+
let Some(local) = def_id.as_local() else { return false };
421+
422+
let hir_id = tcx.local_def_id_to_hir_id(local);
420423
let (_, parent_node) = tcx
421424
.hir_parent_iter(hir_id)
422425
.skip_while(|(_, n)| matches!(n, Node::ConstArg(..)))
@@ -431,9 +434,7 @@ fn const_evaluatable_predicates_of<'tcx>(
431434
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ConstCollector<'tcx> {
432435
fn visit_const(&mut self, c: ty::Const<'tcx>) {
433436
if let ty::ConstKind::Unevaluated(uv) = c.kind() {
434-
if let Some(local) = uv.kind.def_id().as_local()
435-
&& is_const_param_default(self.tcx, local)
436-
{
437+
if is_const_param_default(self.tcx, uv.kind) {
437438
// Do not look into const param defaults,
438439
// these get checked when they are actually instantiated.
439440
//
@@ -445,11 +446,11 @@ fn const_evaluatable_predicates_of<'tcx>(
445446
}
446447

447448
// Skip type consts as mGCA doesn't support evaluatable clauses.
448-
if self.tcx.is_type_const(uv.kind.def_id()) {
449+
if uv.kind.is_type_const(self.tcx) {
449450
return;
450451
}
451452

452-
let span = self.tcx.def_span(uv.kind.def_id());
453+
let span = uv.kind.def_span(self.tcx);
453454
self.preds.insert((ty::ClauseKind::ConstEvaluatable(c).upcast(self.tcx), span));
454455
}
455456
}

compiler/rustc_middle/src/mir/interpret/queries.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,14 @@ impl<'tcx> TyCtxt<'tcx> {
103103
bug!("did not expect inference variables here");
104104
}
105105

106-
let cid = match ty::Instance::try_resolve(self, typing_env, ct.kind.def_id(), ct.args) {
106+
let def_id = match ct.kind {
107+
ty::UnevaluatedConstKind::Projection { def_id }
108+
| ty::UnevaluatedConstKind::Inherent { def_id }
109+
| ty::UnevaluatedConstKind::Free { def_id }
110+
| ty::UnevaluatedConstKind::Anon { def_id } => def_id,
111+
};
112+
113+
let cid = match ty::Instance::try_resolve(self, typing_env, def_id, ct.args) {
107114
Ok(Some(instance)) => GlobalId { instance, promoted: None },
108115
// For errors during resolution, we deliberately do not point at the usage site of the constant,
109116
// since for these errors the place the constant is used shouldn't matter.
@@ -124,6 +131,7 @@ impl<'tcx> TyCtxt<'tcx> {
124131
// @lcnr believes that successfully evaluating even though there are
125132
// used generic parameters is a bug of evaluation, so checking for it
126133
// here does feel somewhat sensible.
134+
let def_id = cid.instance.def_id();
127135
if !self.features().generic_const_exprs()
128136
&& ct.args.has_non_region_param()
129137
// We only FCW for anon consts as repeat expr counts with anon consts are the only place
@@ -132,16 +140,16 @@ impl<'tcx> TyCtxt<'tcx> {
132140
//
133141
// If we don't *only* FCW anon consts we can wind up incorrectly FCW'ing uses of assoc
134142
// consts in pattern positions. #140447
135-
&& self.def_kind(cid.instance.def_id()) == DefKind::AnonConst
136-
&& !self.is_trivial_const(cid.instance.def_id())
143+
&& self.def_kind(def_id) == DefKind::AnonConst
144+
&& !self.is_trivial_const(def_id)
145+
&& let Some(local_def_id) = def_id.as_local()
137146
{
138-
let mir_body = self.mir_for_ctfe(cid.instance.def_id());
147+
let mir_body = self.mir_for_ctfe(def_id);
139148
if mir_body.is_polymorphic {
140-
let Some(local_def_id) = ct.kind.def_id().as_local() else { return };
141149
self.emit_node_span_lint(
142150
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
143151
self.local_def_id_to_hir_id(local_def_id),
144-
self.def_span(ct.kind.def_id()),
152+
self.def_span(def_id),
145153
rustc_errors::DiagDecorator(|lint| {
146154
lint.primary_message(
147155
"cannot use constants which depend on generic parameters in types",

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,11 +1492,15 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
14921492
Const::Ty(_, ct) => match ct.kind() {
14931493
ty::ConstKind::Param(p) => format!("ty::Param({p})"),
14941494
ty::ConstKind::Unevaluated(uv) => {
1495-
format!(
1496-
"ty::Unevaluated({}, {:?})",
1497-
self.tcx.def_path_str(uv.kind.def_id()),
1498-
uv.args,
1499-
)
1495+
let kind = match uv.kind {
1496+
ty::UnevaluatedConstKind::Projection { def_id }
1497+
| ty::UnevaluatedConstKind::Inherent { def_id }
1498+
| ty::UnevaluatedConstKind::Free { def_id }
1499+
| ty::UnevaluatedConstKind::Anon { def_id } => {
1500+
self.tcx.def_path_str(def_id)
1501+
}
1502+
};
1503+
format!("ty::Unevaluated({}, {:?})", kind, uv.args)
15001504
}
15011505
ty::ConstKind::Value(cv) => {
15021506
format!("ty::Valtree({})", fmt_valtree(&cv))

compiler/rustc_middle/src/ty/abstract_const.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ impl<'tcx> TyCtxt<'tcx> {
5252
}
5353
fn fold_const(&mut self, c: Const<'tcx>) -> Const<'tcx> {
5454
let ct = match c.kind() {
55-
ty::ConstKind::Unevaluated(uv) => {
56-
match self.tcx.thir_abstract_const(uv.kind.def_id()) {
55+
ty::ConstKind::Unevaluated(uv) if let Some(def_id) = uv.kind.opt_def_id() => {
56+
match self.tcx.thir_abstract_const(def_id) {
5757
Err(e) => ty::Const::new_error(self.tcx, e),
5858
Ok(Some(bac)) => {
5959
let args = self.tcx.erase_and_anonymize_regions(uv.args);

compiler/rustc_middle/src/ty/context/impl_interner.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
199199
self.anon_const_kind(def_id)
200200
}
201201

202+
fn def_span(self, def_id: DefId) -> Span {
203+
self.def_span(def_id)
204+
}
205+
202206
type AdtDef = ty::AdtDef<'tcx>;
203207
fn adt_def(self, adt_def_id: DefId) -> Self::AdtDef {
204208
self.adt_def(adt_def_id)

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<'tcx> ConstToPat<'tcx> {
184184

185185
// Mark the pattern to indicate that it is the result of lowering a named
186186
// constant. This is used for diagnostics.
187-
thir_pat.extra.get_or_insert_default().expanded_const = Some(uv.kind.def_id());
187+
thir_pat.extra.get_or_insert_default().expanded_const = uv.kind.opt_def_id();
188188
thir_pat
189189
}
190190

compiler/rustc_next_trait_solver/src/solve/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,7 @@ where
246246
.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
247247
.map_err(Into::into);
248248
}
249-
ty::ConstKind::Unevaluated(uv) => {
250-
self.cx().type_of(uv.kind.def_id()).instantiate(self.cx(), uv.args).skip_norm_wip()
251-
}
249+
ty::ConstKind::Unevaluated(uv) => uv.type_of(self.cx()).skip_norm_wip(),
252250
ty::ConstKind::Expr(_) => unimplemented!(
253251
"`feature(generic_const_exprs)` is not supported in the new trait solver"
254252
),

0 commit comments

Comments
 (0)