Skip to content

Commit 764aac2

Browse files
committed
Always use const param envs for const eval.
Nothing else makes sense, and there is no "danger" in doing so, as it only does something if there are const bounds, which are unstable. This used to happen implicitly via the inferctxt before, which was much more fragile.
1 parent afbac75 commit 764aac2

File tree

5 files changed

+12
-0
lines changed

5 files changed

+12
-0
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::interpret::{
77
};
88

99
use rustc_errors::ErrorReported;
10+
use rustc_hir as hir;
1011
use rustc_hir::def::DefKind;
1112
use rustc_middle::mir;
1213
use rustc_middle::mir::interpret::ErrorHandled;
@@ -214,6 +215,7 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
214215
tcx: TyCtxt<'tcx>,
215216
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
216217
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
218+
assert!(key.param_env.constness() == hir::Constness::Const);
217219
// see comment in eval_to_allocation_raw_provider for what we're doing here
218220
if key.param_env.reveal() == Reveal::All {
219221
let mut key = key;
@@ -248,6 +250,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
248250
tcx: TyCtxt<'tcx>,
249251
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
250252
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
253+
assert!(key.param_env.constness() == hir::Constness::Const);
251254
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
252255
// reporting the same error twice here. To resolve this, we check whether we can evaluate the
253256
// constant in the more restrictive `Reveal::UserFacing`, which most likely already was

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
918918
} else {
919919
self.param_env
920920
};
921+
let param_env = param_env.with_const();
921922
let val = self.tcx.eval_to_allocation_raw(param_env.and(gid))?;
922923
self.raw_const_to_mplace(val)
923924
}

compiler/rustc_hir/src/hir.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3244,6 +3244,7 @@ impl<'hir> Node<'hir> {
32443244
Node::Item(Item { kind: ItemKind::Const(..), .. })
32453245
| Node::Item(Item { kind: ItemKind::Static(..), .. })
32463246
| Node::TraitItem(TraitItem { kind: TraitItemKind::Const(..), .. })
3247+
| Node::AnonConst(_)
32473248
| Node::ImplItem(ImplItem { kind: ImplItemKind::Const(..), .. }) => Constness::Const,
32483249

32493250
_ => Constness::NotConst,

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

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl<'tcx> TyCtxt<'tcx> {
6464
cid: GlobalId<'tcx>,
6565
span: Option<Span>,
6666
) -> EvalToConstValueResult<'tcx> {
67+
let param_env = param_env.with_const();
6768
// Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should
6869
// improve caching of queries.
6970
let inputs = self.erase_regions(param_env.and(cid));
@@ -92,6 +93,7 @@ impl<'tcx> TyCtxt<'tcx> {
9293
gid: GlobalId<'tcx>,
9394
param_env: ty::ParamEnv<'tcx>,
9495
) -> Result<&'tcx mir::Allocation, ErrorHandled> {
96+
let param_env = param_env.with_const();
9597
trace!("eval_to_allocation: Need to compute {:?}", gid);
9698
let raw_const = self.eval_to_allocation_raw(param_env.and(gid))?;
9799
Ok(self.global_alloc(raw_const.alloc_id).unwrap_memory())

compiler/rustc_middle/src/ty/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,11 @@ impl<'tcx> ParamEnv<'tcx> {
13181318
self
13191319
}
13201320

1321+
pub fn with_const(mut self) -> Self {
1322+
self.packed.set_tag(ParamTag { constness: hir::Constness::Const, ..self.packed.tag() });
1323+
self
1324+
}
1325+
13211326
/// Returns a new parameter environment with the same clauses, but
13221327
/// which "reveals" the true results of projections in all cases
13231328
/// (even for associated types that are specializable). This is

0 commit comments

Comments
 (0)