Skip to content

Commit 10e75d3

Browse files
committed
remove UnevaluatedConstKind::def_id
1 parent 53509ca commit 10e75d3

26 files changed

Lines changed: 236 additions & 167 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
@@ -775,8 +775,10 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
775775
if has_default {
776776
// need to store default and type of default
777777
let ct = tcx.const_param_default(param.def_id).skip_binder();
778-
if let ty::ConstKind::Unevaluated(uv) = ct.kind() {
779-
tcx.ensure_ok().type_of(uv.kind.def_id());
778+
if let ty::ConstKind::Unevaluated(uv) = ct.kind()
779+
&& let Some(def_id) = uv.kind.opt_def_id()
780+
{
781+
tcx.ensure_ok().type_of(def_id);
780782
}
781783
}
782784
}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,11 +1535,7 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id:
15351535
| ty::ConstKind::Bound(_, _) => unreachable!(),
15361536
ty::ConstKind::Error(_) | ty::ConstKind::Expr(_) => continue,
15371537
ty::ConstKind::Value(cv) => cv.ty,
1538-
ty::ConstKind::Unevaluated(uv) => infcx
1539-
.tcx
1540-
.type_of(uv.kind.def_id())
1541-
.instantiate(infcx.tcx, uv.args)
1542-
.skip_norm_wip(),
1538+
ty::ConstKind::Unevaluated(uv) => uv.type_of(infcx.tcx).skip_norm_wip(),
15431539
ty::ConstKind::Param(param_ct) => {
15441540
param_ct.find_const_ty_from_env(wfcx.param_env)
15451541
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,16 @@ fn const_evaluatable_predicates_of<'tcx>(
419419
preds: FxIndexSet<(ty::Clause<'tcx>, Span)>,
420420
}
421421

422-
fn is_const_param_default(tcx: TyCtxt<'_>, def: LocalDefId) -> bool {
423-
let hir_id = tcx.local_def_id_to_hir_id(def);
422+
fn is_const_param_default(tcx: TyCtxt<'_>, kind: ty::UnevaluatedConstKind<'_>) -> bool {
423+
let def_id = match kind {
424+
ty::UnevaluatedConstKind::Projection { def_id } => def_id,
425+
ty::UnevaluatedConstKind::Inherent { def_id } => def_id,
426+
ty::UnevaluatedConstKind::Free { def_id } => def_id,
427+
ty::UnevaluatedConstKind::Anon { def_id } => def_id,
428+
};
429+
let Some(local) = def_id.as_local() else { return false };
430+
431+
let hir_id = tcx.local_def_id_to_hir_id(local);
424432
let (_, parent_node) = tcx
425433
.hir_parent_iter(hir_id)
426434
.skip_while(|(_, n)| matches!(n, Node::ConstArg(..)))
@@ -435,9 +443,7 @@ fn const_evaluatable_predicates_of<'tcx>(
435443
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ConstCollector<'tcx> {
436444
fn visit_const(&mut self, c: ty::Const<'tcx>) {
437445
if let ty::ConstKind::Unevaluated(uv) = c.kind() {
438-
if let Some(local) = uv.kind.def_id().as_local()
439-
&& is_const_param_default(self.tcx, local)
440-
{
446+
if is_const_param_default(self.tcx, uv.kind) {
441447
// Do not look into const param defaults,
442448
// these get checked when they are actually instantiated.
443449
//
@@ -449,11 +455,11 @@ fn const_evaluatable_predicates_of<'tcx>(
449455
}
450456

451457
// Skip type consts as mGCA doesn't support evaluatable clauses.
452-
if self.tcx.is_type_const(uv.kind.def_id()) {
458+
if uv.kind.is_type_const(self.tcx) {
453459
return;
454460
}
455461

456-
let span = self.tcx.def_span(uv.kind.def_id());
462+
let span = uv.kind.def_span(self.tcx);
457463
self.preds.insert((ty::ClauseKind::ConstEvaluatable(c).upcast(self.tcx), span));
458464
}
459465
}

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

Lines changed: 13 additions & 4 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.
@@ -137,12 +144,14 @@ impl<'tcx> TyCtxt<'tcx> {
137144
&& !self.is_trivial_const(cid.instance.def_id())
138145
{
139146
let mir_body = self.mir_for_ctfe(cid.instance.def_id());
140-
if mir_body.is_polymorphic {
141-
let Some(local_def_id) = ct.kind.def_id().as_local() else { return };
147+
if mir_body.is_polymorphic
148+
&& let Some(def_id) = ct.kind.opt_def_id()
149+
&& let Some(local_def_id) = def_id.as_local()
150+
{
142151
self.emit_node_span_lint(
143152
lint::builtin::CONST_EVALUATABLE_UNCHECKED,
144153
self.local_def_id_to_hir_id(local_def_id),
145-
self.def_span(ct.kind.def_id()),
154+
self.def_span(def_id),
146155
rustc_errors::DiagDecorator(|lint| {
147156
lint.primary_message(
148157
"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
@@ -1497,11 +1497,15 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
14971497
Const::Ty(_, ct) => match ct.kind() {
14981498
ty::ConstKind::Param(p) => format!("ty::Param({p})"),
14991499
ty::ConstKind::Unevaluated(uv) => {
1500-
format!(
1501-
"ty::Unevaluated({}, {:?})",
1502-
self.tcx.def_path_str(uv.kind.def_id()),
1503-
uv.args,
1504-
)
1500+
let kind = match uv.kind {
1501+
ty::UnevaluatedConstKind::Projection { def_id }
1502+
| ty::UnevaluatedConstKind::Inherent { def_id }
1503+
| ty::UnevaluatedConstKind::Free { def_id }
1504+
| ty::UnevaluatedConstKind::Anon { def_id } => {
1505+
self.tcx.def_path_str(def_id)
1506+
}
1507+
};
1508+
format!("ty::Unevaluated({}, {:?})", kind, uv.args)
15051509
}
15061510
ty::ConstKind::Value(cv) => {
15071511
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
@@ -186,7 +186,7 @@ impl<'tcx> ConstToPat<'tcx> {
186186

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

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)