Skip to content

Commit bfa098e

Browse files
committed
Auto merge of #126439 - matthiaskrgr:rollup-856xt18, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - #123726 (Clarify `Command::new` behavior for programs with arguments) - #126088 ([1/2] clean-up / general improvements) - #126238 (Fix Miri sysroot for `x run`) - #126315 (Add pub struct with allow(dead_code) into worklist) - #126360 (Uplift `structural_traits.rs` into the new trait solver) - #126371 (Tweak output of import suggestions) - #126388 (const-eval: make lint scope computation consistent) - #126390 (Fix wording in {checked_}next_power_of_two) - #126392 (Small style improvement in `gvn.rs`) - #126402 (Fix wrong `assert_unsafe_precondition` message for `core::ptr::copy`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 0ef0dd2 + 3494ea1 commit bfa098e

File tree

96 files changed

+1245
-698
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+1245
-698
lines changed

compiler/rustc_const_eval/src/const_eval/error.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use std::mem;
22

33
use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, Diagnostic, IntoDiagArg};
4-
use rustc_hir::CRATE_HIR_ID;
54
use rustc_middle::mir::interpret::{Provenance, ReportedErrorInfo};
65
use rustc_middle::mir::AssertKind;
76
use rustc_middle::query::TyCtxtAt;
87
use rustc_middle::ty::TyCtxt;
98
use rustc_middle::ty::{layout::LayoutError, ConstInt};
109
use rustc_span::{Span, Symbol};
1110

12-
use super::CompileTimeInterpreter;
11+
use super::CompileTimeMachine;
1312
use crate::errors::{self, FrameNote, ReportErrorExt};
1413
use crate::interpret::{err_inval, err_machine_stop};
1514
use crate::interpret::{ErrorHandled, Frame, InterpError, InterpErrorInfo, MachineStopType};
@@ -156,24 +155,17 @@ where
156155
}
157156
}
158157

159-
/// Emit a lint from a const-eval situation.
158+
/// Emit a lint from a const-eval situation, with a backtrace.
160159
// Even if this is unused, please don't remove it -- chances are we will need to emit a lint during const-eval again in the future!
161160
pub(super) fn lint<'tcx, L>(
162161
tcx: TyCtxtAt<'tcx>,
163-
machine: &CompileTimeInterpreter<'tcx>,
162+
machine: &CompileTimeMachine<'tcx>,
164163
lint: &'static rustc_session::lint::Lint,
165164
decorator: impl FnOnce(Vec<errors::FrameNote>) -> L,
166165
) where
167166
L: for<'a> rustc_errors::LintDiagnostic<'a, ()>,
168167
{
169168
let (span, frames) = get_span_and_frames(tcx, &machine.stack);
170169

171-
tcx.emit_node_span_lint(
172-
lint,
173-
// We use the root frame for this so the crate that defines the const defines whether the
174-
// lint is emitted.
175-
machine.stack.first().and_then(|frame| frame.lint_root()).unwrap_or(CRATE_HIR_ID),
176-
span,
177-
decorator(frames),
178-
);
170+
tcx.emit_node_span_lint(lint, machine.best_lint_scope(*tcx), span, decorator(frames));
179171
}

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_span::def_id::LocalDefId;
1717
use rustc_span::{Span, DUMMY_SP};
1818
use rustc_target::abi::{self, Abi};
1919

20-
use super::{CanAccessMutGlobal, CompileTimeEvalContext, CompileTimeInterpreter};
20+
use super::{CanAccessMutGlobal, CompileTimeInterpCx, CompileTimeMachine};
2121
use crate::const_eval::CheckAlignment;
2222
use crate::errors::ConstEvalError;
2323
use crate::errors::{self, DanglingPtrInFinal};
@@ -32,7 +32,7 @@ use crate::CTRL_C_RECEIVED;
3232
// Returns a pointer to where the result lives
3333
#[instrument(level = "trace", skip(ecx, body))]
3434
fn eval_body_using_ecx<'tcx, R: InterpretationResult<'tcx>>(
35-
ecx: &mut CompileTimeEvalContext<'tcx>,
35+
ecx: &mut CompileTimeInterpCx<'tcx>,
3636
cid: GlobalId<'tcx>,
3737
body: &'tcx mir::Body<'tcx>,
3838
) -> InterpResult<'tcx, R> {
@@ -114,7 +114,7 @@ fn eval_body_using_ecx<'tcx, R: InterpretationResult<'tcx>>(
114114
let err_diag = errors::MutablePtrInFinal { span: ecx.tcx.span, kind: intern_kind };
115115
ecx.tcx.emit_node_span_lint(
116116
lint::builtin::CONST_EVAL_MUTABLE_PTR_IN_FINAL_VALUE,
117-
ecx.best_lint_scope(),
117+
ecx.machine.best_lint_scope(*ecx.tcx),
118118
err_diag.span,
119119
err_diag,
120120
)
@@ -139,13 +139,13 @@ pub(crate) fn mk_eval_cx_to_read_const_val<'tcx>(
139139
root_span: Span,
140140
param_env: ty::ParamEnv<'tcx>,
141141
can_access_mut_global: CanAccessMutGlobal,
142-
) -> CompileTimeEvalContext<'tcx> {
142+
) -> CompileTimeInterpCx<'tcx> {
143143
debug!("mk_eval_cx: {:?}", param_env);
144144
InterpCx::new(
145145
tcx,
146146
root_span,
147147
param_env,
148-
CompileTimeInterpreter::new(can_access_mut_global, CheckAlignment::No),
148+
CompileTimeMachine::new(can_access_mut_global, CheckAlignment::No),
149149
)
150150
}
151151

@@ -156,7 +156,7 @@ pub fn mk_eval_cx_for_const_val<'tcx>(
156156
param_env: ty::ParamEnv<'tcx>,
157157
val: mir::ConstValue<'tcx>,
158158
ty: Ty<'tcx>,
159-
) -> Option<(CompileTimeEvalContext<'tcx>, OpTy<'tcx>)> {
159+
) -> Option<(CompileTimeInterpCx<'tcx>, OpTy<'tcx>)> {
160160
let ecx = mk_eval_cx_to_read_const_val(tcx.tcx, tcx.span, param_env, CanAccessMutGlobal::No);
161161
let op = ecx.const_val_to_op(val, ty, None).ok()?;
162162
Some((ecx, op))
@@ -170,7 +170,7 @@ pub fn mk_eval_cx_for_const_val<'tcx>(
170170
/// encounter an `Indirect` they cannot handle.
171171
#[instrument(skip(ecx), level = "debug")]
172172
pub(super) fn op_to_const<'tcx>(
173-
ecx: &CompileTimeEvalContext<'tcx>,
173+
ecx: &CompileTimeInterpCx<'tcx>,
174174
op: &OpTy<'tcx>,
175175
for_diagnostics: bool,
176176
) -> ConstValue<'tcx> {
@@ -328,14 +328,14 @@ pub trait InterpretationResult<'tcx> {
328328
/// evaluation query.
329329
fn make_result(
330330
mplace: MPlaceTy<'tcx>,
331-
ecx: &mut InterpCx<'tcx, CompileTimeInterpreter<'tcx>>,
331+
ecx: &mut InterpCx<'tcx, CompileTimeMachine<'tcx>>,
332332
) -> Self;
333333
}
334334

335335
impl<'tcx> InterpretationResult<'tcx> for ConstAlloc<'tcx> {
336336
fn make_result(
337337
mplace: MPlaceTy<'tcx>,
338-
_ecx: &mut InterpCx<'tcx, CompileTimeInterpreter<'tcx>>,
338+
_ecx: &mut InterpCx<'tcx, CompileTimeMachine<'tcx>>,
339339
) -> Self {
340340
ConstAlloc { alloc_id: mplace.ptr().provenance.unwrap().alloc_id(), ty: mplace.layout.ty }
341341
}
@@ -383,7 +383,7 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
383383
// they do not have to behave "as if" they were evaluated at runtime.
384384
// For consts however we want to ensure they behave "as if" they were evaluated at runtime,
385385
// so we have to reject reading mutable global memory.
386-
CompileTimeInterpreter::new(CanAccessMutGlobal::from(is_static), CheckAlignment::Error),
386+
CompileTimeMachine::new(CanAccessMutGlobal::from(is_static), CheckAlignment::Error),
387387
);
388388
let res = ecx.load_mir(cid.instance.def, cid.promoted);
389389
res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, body)).map_err(|error| {
@@ -417,7 +417,7 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
417417

418418
#[inline(always)]
419419
fn const_validate_mplace<'tcx>(
420-
ecx: &InterpCx<'tcx, CompileTimeInterpreter<'tcx>>,
420+
ecx: &InterpCx<'tcx, CompileTimeMachine<'tcx>>,
421421
mplace: &MPlaceTy<'tcx>,
422422
cid: GlobalId<'tcx>,
423423
) -> Result<(), ErrorHandled> {
@@ -447,7 +447,7 @@ fn const_validate_mplace<'tcx>(
447447

448448
#[inline(always)]
449449
fn report_validation_error<'tcx>(
450-
ecx: &InterpCx<'tcx, CompileTimeInterpreter<'tcx>>,
450+
ecx: &InterpCx<'tcx, CompileTimeMachine<'tcx>>,
451451
error: InterpErrorInfo<'tcx>,
452452
alloc_id: AllocId,
453453
) -> ErrorHandled {

compiler/rustc_const_eval/src/const_eval/machine.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ use rustc_data_structures::fx::IndexEntry;
99
use rustc_hir::def_id::DefId;
1010
use rustc_hir::def_id::LocalDefId;
1111
use rustc_hir::LangItem;
12+
use rustc_hir::{self as hir, CRATE_HIR_ID};
1213
use rustc_middle::bug;
1314
use rustc_middle::mir;
1415
use rustc_middle::mir::AssertMessage;
1516
use rustc_middle::query::TyCtxtAt;
16-
use rustc_middle::ty;
1717
use rustc_middle::ty::layout::{FnAbiOf, TyAndLayout};
18+
use rustc_middle::ty::{self, TyCtxt};
1819
use rustc_session::lint::builtin::WRITES_THROUGH_IMMUTABLE_POINTER;
1920
use rustc_span::symbol::{sym, Symbol};
2021
use rustc_span::Span;
@@ -44,7 +45,7 @@ const TINY_LINT_TERMINATOR_LIMIT: usize = 20;
4445
const PROGRESS_INDICATOR_START: usize = 4_000_000;
4546

4647
/// Extra machine state for CTFE, and the Machine instance
47-
pub struct CompileTimeInterpreter<'tcx> {
48+
pub struct CompileTimeMachine<'tcx> {
4849
/// The number of terminators that have been evaluated.
4950
///
5051
/// This is used to produce lints informing the user that the compiler is not stuck.
@@ -89,12 +90,12 @@ impl From<bool> for CanAccessMutGlobal {
8990
}
9091
}
9192

92-
impl<'tcx> CompileTimeInterpreter<'tcx> {
93+
impl<'tcx> CompileTimeMachine<'tcx> {
9394
pub(crate) fn new(
9495
can_access_mut_global: CanAccessMutGlobal,
9596
check_alignment: CheckAlignment,
9697
) -> Self {
97-
CompileTimeInterpreter {
98+
CompileTimeMachine {
9899
num_evaluated_steps: 0,
99100
stack: Vec::new(),
100101
can_access_mut_global,
@@ -163,7 +164,7 @@ impl<K: Hash + Eq, V> interpret::AllocMap<K, V> for FxIndexMap<K, V> {
163164
}
164165
}
165166

166-
pub(crate) type CompileTimeEvalContext<'tcx> = InterpCx<'tcx, CompileTimeInterpreter<'tcx>>;
167+
pub(crate) type CompileTimeInterpCx<'tcx> = InterpCx<'tcx, CompileTimeMachine<'tcx>>;
167168

168169
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
169170
pub enum MemoryKind {
@@ -195,7 +196,7 @@ impl interpret::MayLeak for ! {
195196
}
196197
}
197198

198-
impl<'tcx> CompileTimeEvalContext<'tcx> {
199+
impl<'tcx> CompileTimeInterpCx<'tcx> {
199200
fn location_triple_for_span(&self, span: Span) -> (Symbol, u32, u32) {
200201
let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
201202
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
@@ -369,7 +370,16 @@ impl<'tcx> CompileTimeEvalContext<'tcx> {
369370
}
370371
}
371372

372-
impl<'tcx> interpret::Machine<'tcx> for CompileTimeInterpreter<'tcx> {
373+
impl<'tcx> CompileTimeMachine<'tcx> {
374+
#[inline(always)]
375+
/// Find the first stack frame that is within the current crate, if any.
376+
/// Otherwise, return the crate's HirId
377+
pub fn best_lint_scope(&self, tcx: TyCtxt<'tcx>) -> hir::HirId {
378+
self.stack.iter().find_map(|frame| frame.lint_root(tcx)).unwrap_or(CRATE_HIR_ID)
379+
}
380+
}
381+
382+
impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
373383
compile_time_machine!(<'tcx>);
374384

375385
type MemoryKind = MemoryKind;
@@ -600,7 +610,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeInterpreter<'tcx> {
600610
// By default, we stop after a million steps, but the user can disable this lint
601611
// to be able to run until the heat death of the universe or power loss, whichever
602612
// comes first.
603-
let hir_id = ecx.best_lint_scope();
613+
let hir_id = ecx.machine.best_lint_scope(*ecx.tcx);
604614
let is_error = ecx
605615
.tcx
606616
.lint_level_at_node(

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_target::abi::{Abi, VariantIdx};
99
use tracing::{debug, instrument, trace};
1010

1111
use super::eval_queries::{mk_eval_cx_to_read_const_val, op_to_const};
12-
use super::machine::CompileTimeEvalContext;
12+
use super::machine::CompileTimeInterpCx;
1313
use super::{ValTreeCreationError, ValTreeCreationResult, VALTREE_MAX_NODES};
1414
use crate::const_eval::CanAccessMutGlobal;
1515
use crate::errors::MaxNumNodesInConstErr;
@@ -21,7 +21,7 @@ use crate::interpret::{
2121

2222
#[instrument(skip(ecx), level = "debug")]
2323
fn branches<'tcx>(
24-
ecx: &CompileTimeEvalContext<'tcx>,
24+
ecx: &CompileTimeInterpCx<'tcx>,
2525
place: &MPlaceTy<'tcx>,
2626
n: usize,
2727
variant: Option<VariantIdx>,
@@ -59,7 +59,7 @@ fn branches<'tcx>(
5959

6060
#[instrument(skip(ecx), level = "debug")]
6161
fn slice_branches<'tcx>(
62-
ecx: &CompileTimeEvalContext<'tcx>,
62+
ecx: &CompileTimeInterpCx<'tcx>,
6363
place: &MPlaceTy<'tcx>,
6464
num_nodes: &mut usize,
6565
) -> ValTreeCreationResult<'tcx> {
@@ -77,7 +77,7 @@ fn slice_branches<'tcx>(
7777

7878
#[instrument(skip(ecx), level = "debug")]
7979
fn const_to_valtree_inner<'tcx>(
80-
ecx: &CompileTimeEvalContext<'tcx>,
80+
ecx: &CompileTimeInterpCx<'tcx>,
8181
place: &MPlaceTy<'tcx>,
8282
num_nodes: &mut usize,
8383
) -> ValTreeCreationResult<'tcx> {
@@ -219,7 +219,7 @@ fn reconstruct_place_meta<'tcx>(
219219

220220
#[instrument(skip(ecx), level = "debug", ret)]
221221
fn create_valtree_place<'tcx>(
222-
ecx: &mut CompileTimeEvalContext<'tcx>,
222+
ecx: &mut CompileTimeInterpCx<'tcx>,
223223
layout: TyAndLayout<'tcx>,
224224
valtree: ty::ValTree<'tcx>,
225225
) -> MPlaceTy<'tcx> {
@@ -364,7 +364,7 @@ pub fn valtree_to_const_value<'tcx>(
364364

365365
/// Put a valtree into memory and return a reference to that.
366366
fn valtree_to_ref<'tcx>(
367-
ecx: &mut CompileTimeEvalContext<'tcx>,
367+
ecx: &mut CompileTimeInterpCx<'tcx>,
368368
valtree: ty::ValTree<'tcx>,
369369
pointee_ty: Ty<'tcx>,
370370
) -> Immediate {
@@ -380,7 +380,7 @@ fn valtree_to_ref<'tcx>(
380380

381381
#[instrument(skip(ecx), level = "debug")]
382382
fn valtree_into_mplace<'tcx>(
383-
ecx: &mut CompileTimeEvalContext<'tcx>,
383+
ecx: &mut CompileTimeInterpCx<'tcx>,
384384
place: &MPlaceTy<'tcx>,
385385
valtree: ty::ValTree<'tcx>,
386386
) {
@@ -457,6 +457,6 @@ fn valtree_into_mplace<'tcx>(
457457
}
458458
}
459459

460-
fn dump_place<'tcx>(ecx: &CompileTimeEvalContext<'tcx>, place: &MPlaceTy<'tcx>) {
460+
fn dump_place<'tcx>(ecx: &CompileTimeInterpCx<'tcx>, place: &MPlaceTy<'tcx>) {
461461
trace!("{:?}", ecx.dump_place(&PlaceTy::from(place.clone())));
462462
}

compiler/rustc_const_eval/src/interpret/eval_context.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::{fmt, mem};
44
use either::{Either, Left, Right};
55
use tracing::{debug, info, info_span, instrument, trace};
66

7-
use hir::CRATE_HIR_ID;
87
use rustc_errors::DiagCtxt;
98
use rustc_hir::{self as hir, def_id::DefId, definitions::DefPathData};
109
use rustc_index::IndexVec;
@@ -271,13 +270,18 @@ impl<'tcx, Prov: Provenance, Extra> Frame<'tcx, Prov, Extra> {
271270
}
272271
}
273272

274-
pub fn lint_root(&self) -> Option<hir::HirId> {
275-
self.current_source_info().and_then(|source_info| {
276-
match &self.body.source_scopes[source_info.scope].local_data {
273+
pub fn lint_root(&self, tcx: TyCtxt<'tcx>) -> Option<hir::HirId> {
274+
// We first try to get a HirId via the current source scope,
275+
// and fall back to `body.source`.
276+
self.current_source_info()
277+
.and_then(|source_info| match &self.body.source_scopes[source_info.scope].local_data {
277278
mir::ClearCrossCrate::Set(data) => Some(data.lint_root),
278279
mir::ClearCrossCrate::Clear => None,
279-
}
280-
})
280+
})
281+
.or_else(|| {
282+
let def_id = self.body.source.def_id().as_local();
283+
def_id.map(|def_id| tcx.local_def_id_to_hir_id(def_id))
284+
})
281285
}
282286

283287
/// Returns the address of the buffer where the locals are stored. This is used by `Place` as a
@@ -509,17 +513,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
509513
self.stack().last().map_or(self.tcx.span, |f| f.current_span())
510514
}
511515

512-
/// Find the first stack frame that is within the current crate, if any;
513-
/// otherwise return the crate's HirId.
514-
#[inline(always)]
515-
pub fn best_lint_scope(&self) -> hir::HirId {
516-
self.stack()
517-
.iter()
518-
.find_map(|frame| frame.body.source.def_id().as_local())
519-
.map_or(CRATE_HIR_ID, |def_id| self.tcx.local_def_id_to_hir_id(def_id))
520-
}
521-
522-
#[inline(always)]
523516
pub(crate) fn stack(&self) -> &[Frame<'tcx, M::Provenance, M::FrameExtra>] {
524517
M::stack(self)
525518
}

compiler/rustc_const_eval/src/interpret/intern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub trait HasStaticRootDefId {
4545
fn static_def_id(&self) -> Option<LocalDefId>;
4646
}
4747

48-
impl HasStaticRootDefId for const_eval::CompileTimeInterpreter<'_> {
48+
impl HasStaticRootDefId for const_eval::CompileTimeMachine<'_> {
4949
fn static_def_id(&self) -> Option<LocalDefId> {
5050
Some(self.static_root_ids?.1)
5151
}

compiler/rustc_const_eval/src/interpret/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::const_eval::{CompileTimeEvalContext, CompileTimeInterpreter, InterpretationResult};
1+
use crate::const_eval::{CompileTimeInterpCx, CompileTimeMachine, InterpretationResult};
22
use rustc_hir::def_id::LocalDefId;
33
use rustc_middle::mir;
44
use rustc_middle::mir::interpret::{Allocation, InterpResult, Pointer};
@@ -84,7 +84,7 @@ where
8484
impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx> {
8585
fn make_result(
8686
mplace: MPlaceTy<'tcx>,
87-
ecx: &mut InterpCx<'tcx, CompileTimeInterpreter<'tcx>>,
87+
ecx: &mut InterpCx<'tcx, CompileTimeMachine<'tcx>>,
8888
) -> Self {
8989
let alloc_id = mplace.ptr().provenance.unwrap().alloc_id();
9090
let alloc = ecx.memory.alloc_map.swap_remove(&alloc_id).unwrap().1;
@@ -93,7 +93,7 @@ impl<'tcx> InterpretationResult<'tcx> for mir::interpret::ConstAllocation<'tcx>
9393
}
9494

9595
pub(crate) fn create_static_alloc<'tcx>(
96-
ecx: &mut CompileTimeEvalContext<'tcx>,
96+
ecx: &mut CompileTimeInterpCx<'tcx>,
9797
static_def_id: LocalDefId,
9898
layout: TyAndLayout<'tcx>,
9999
) -> InterpResult<'tcx, MPlaceTy<'tcx>> {

0 commit comments

Comments
 (0)