Skip to content

Commit 67a30ab

Browse files
committed
wip on moving const RHS special-casing
1 parent eb5c4d6 commit 67a30ab

File tree

5 files changed

+48
-25
lines changed

5 files changed

+48
-25
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -294,29 +294,6 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
294294
)
295295
},
296296
);
297-
} else if let ty::InstanceKind::Item(def_id) = key.value.instance.def
298-
&& matches!(tcx.def_kind(def_id), DefKind::Const | DefKind::AssocConst)
299-
{
300-
let ct = tcx.const_of_item(def_id).instantiate(tcx, key.value.instance.args);
301-
match ct.kind() {
302-
ty::ConstKind::Unevaluated(_) => {
303-
return Err(ErrorHandled::TooGeneric(DUMMY_SP));
304-
}
305-
ty::ConstKind::Value(cv) => return Ok(tcx.valtree_to_const_val(cv)),
306-
ty::ConstKind::Error(guar) => {
307-
return Err(ErrorHandled::Reported(
308-
ReportedErrorInfo::const_eval_error(guar),
309-
DUMMY_SP,
310-
));
311-
}
312-
ty::ConstKind::Expr(_) => return Err(ErrorHandled::TooGeneric(DUMMY_SP)),
313-
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => {
314-
return Err(ErrorHandled::TooGeneric(DUMMY_SP));
315-
}
316-
ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) => {
317-
bug!("unexpected constant {ct:?}")
318-
}
319-
}
320297
}
321298

322299
tcx.eval_to_allocation_raw(key).map(|val| turn_into_const_value(tcx, val, key))
@@ -374,6 +351,31 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
374351
trace!("const eval: {:?} ({})", key, instance);
375352
}
376353

354+
if let ty::InstanceKind::Item(def_id) = key.value.instance.def
355+
&& matches!(tcx.def_kind(def_id), DefKind::Const | DefKind::AssocConst)
356+
{
357+
let ct = tcx.const_of_item(def_id).instantiate(tcx, key.value.instance.args);
358+
match ct.kind() {
359+
ty::ConstKind::Unevaluated(_) => {
360+
return Err(ErrorHandled::TooGeneric(DUMMY_SP));
361+
}
362+
ty::ConstKind::Value(cv) => return Ok(tcx.valtree_to_const_alloc(cv)),
363+
ty::ConstKind::Error(guar) => {
364+
return Err(ErrorHandled::Reported(
365+
ReportedErrorInfo::const_eval_error(guar),
366+
DUMMY_SP,
367+
));
368+
}
369+
ty::ConstKind::Expr(_) => return Err(ErrorHandled::TooGeneric(DUMMY_SP)),
370+
ty::ConstKind::Param(_) | ty::ConstKind::Placeholder(_) => {
371+
return Err(ErrorHandled::TooGeneric(DUMMY_SP));
372+
}
373+
ty::ConstKind::Infer(_) | ty::ConstKind::Bound(..) => {
374+
bug!("unexpected constant {ct:?}")
375+
}
376+
}
377+
}
378+
377379
eval_in_interpreter(tcx, key.value, key.typing_env)
378380
}
379381

compiler/rustc_const_eval/src/const_eval/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use self::error::*;
2121
pub use self::eval_queries::*;
2222
pub use self::fn_queries::*;
2323
pub use self::machine::*;
24-
pub(crate) use self::valtrees::{eval_to_valtree, valtree_to_const_value};
24+
pub(crate) use self::valtrees::{eval_to_valtree, valtree_to_const_alloc, valtree_to_const_value};
2525

2626
// We forbid type-level constants that contain more than `VALTREE_MAX_NODES` nodes.
2727
const VALTREE_MAX_NODES: usize = 100000;

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use tracing::{debug, instrument, trace};
99

1010
use super::eval_queries::{mk_eval_cx_to_read_const_val, op_to_const};
1111
use super::machine::CompileTimeInterpCx;
12-
use super::{VALTREE_MAX_NODES, ValTreeCreationError, ValTreeCreationResult};
12+
use super::{
13+
InterpretationResult as _, VALTREE_MAX_NODES, ValTreeCreationError, ValTreeCreationResult,
14+
};
1315
use crate::const_eval::CanAccessMutGlobal;
1416
use crate::errors::MaxNumNodesInConstErr;
1517
use crate::interpret::{
@@ -365,6 +367,18 @@ pub fn valtree_to_const_value<'tcx>(
365367
}
366368
}
367369

370+
#[instrument(skip(tcx), level = "debug", ret)]
371+
pub(crate) fn valtree_to_const_alloc<'tcx>(
372+
tcx: TyCtxt<'tcx>,
373+
env: ty::TypingEnv<'tcx>,
374+
val: ty::Value<'tcx>,
375+
) -> mir::ConstAlloc<'tcx> {
376+
let mut ecx = mk_eval_cx_to_read_const_val(tcx, DUMMY_SP, env, CanAccessMutGlobal::No);
377+
let layout = ecx.layout_of(val.ty).unwrap();
378+
let mplace = create_valtree_place(&mut ecx, layout, val.valtree);
379+
mir::ConstAlloc::make_result(mplace, &mut ecx)
380+
}
381+
368382
/// Put a valtree into memory and return a reference to that.
369383
fn valtree_to_ref<'tcx>(
370384
ecx: &mut CompileTimeInterpCx<'tcx>,

compiler/rustc_const_eval/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub fn provide(providers: &mut Providers) {
4646
const_eval::try_destructure_mir_constant_for_user_output;
4747
providers.valtree_to_const_val =
4848
|tcx, cv| const_eval::valtree_to_const_value(tcx, ty::TypingEnv::fully_monomorphized(), cv);
49+
providers.valtree_to_const_alloc =
50+
|tcx, cv| const_eval::valtree_to_const_alloc(tcx, ty::TypingEnv::fully_monomorphized(), cv);
4951
providers.check_validity_requirement = |tcx, (init_kind, param_env_and_ty)| {
5052
util::check_validity_requirement(tcx, init_kind, param_env_and_ty)
5153
};

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,11 @@ rustc_queries! {
13291329
desc { "converting type-level constant value to MIR constant value"}
13301330
}
13311331

1332+
/// Converts a type-level constant value into a MIR constant allocation.
1333+
query valtree_to_const_alloc(key: ty::Value<'tcx>) -> mir::ConstAlloc<'tcx> {
1334+
desc { "converting type-level constant value to MIR constant allocation"}
1335+
}
1336+
13321337
/// Destructures array, ADT or tuple constants into the constants
13331338
/// of their fields.
13341339
query destructure_const(key: ty::Const<'tcx>) -> ty::DestructuredConst<'tcx> {

0 commit comments

Comments
 (0)