Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f394bb5

Browse files
oli-obkfee1-dead
authored andcommitted
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 a9a79f6 commit f394bb5

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

Lines changed: 3 additions & 0 deletions
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;
@@ -215,6 +216,7 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
215216
tcx: TyCtxt<'tcx>,
216217
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
217218
) -> ::rustc_middle::mir::interpret::EvalToConstValueResult<'tcx> {
219+
assert!(key.param_env.constness() == hir::Constness::Const);
218220
// see comment in eval_to_allocation_raw_provider for what we're doing here
219221
if key.param_env.reveal() == Reveal::All {
220222
let mut key = key;
@@ -249,6 +251,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
249251
tcx: TyCtxt<'tcx>,
250252
key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>,
251253
) -> ::rustc_middle::mir::interpret::EvalToAllocationRawResult<'tcx> {
254+
assert!(key.param_env.constness() == hir::Constness::Const);
252255
// Because the constant is computed twice (once per value of `Reveal`), we are at risk of
253256
// reporting the same error twice here. To resolve this, we check whether we can evaluate the
254257
// constant in the more restrictive `Reveal::UserFacing`, which most likely already was

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,6 +3248,7 @@ impl<'hir> Node<'hir> {
32483248
Node::Item(Item { kind: ItemKind::Const(..), .. })
32493249
| Node::Item(Item { kind: ItemKind::Static(..), .. })
32503250
| Node::TraitItem(TraitItem { kind: TraitItemKind::Const(..), .. })
3251+
| Node::AnonConst(_)
32513252
| Node::ImplItem(ImplItem { kind: ImplItemKind::Const(..), .. }) => Constness::Const,
32523253

32533254
_ => Constness::NotConst,

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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,11 @@ impl<'tcx> ParamEnv<'tcx> {
13231323
self
13241324
}
13251325

1326+
pub fn with_const(mut self) -> Self {
1327+
self.packed.set_tag(ParamTag { constness: hir::Constness::Const, ..self.packed.tag() });
1328+
self
1329+
}
1330+
13261331
/// Returns a new parameter environment with the same clauses, but
13271332
/// which "reveals" the true results of projections in all cases
13281333
/// (even for associated types that are specializable). This is

0 commit comments

Comments
 (0)