Skip to content

Commit cc149c7

Browse files
committed
put a tcx into the Machine so that we have to pass around fewer things
1 parent 3cb27f5 commit cc149c7

File tree

6 files changed

+130
-132
lines changed

6 files changed

+130
-132
lines changed

src/diagnostics.rs

Lines changed: 93 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::num::NonZeroU64;
33

44
use log::trace;
55

6-
use rustc_middle::ty::TyCtxt;
76
use rustc_span::{source_map::DUMMY_SP, SpanData, Symbol};
87
use rustc_target::abi::{Align, Size};
98

@@ -91,13 +90,12 @@ enum DiagLevel {
9190
fn prune_stacktrace<'tcx>(
9291
mut stacktrace: Vec<FrameInfo<'tcx>>,
9392
machine: &Evaluator<'_, 'tcx>,
94-
tcx: TyCtxt<'tcx>,
9593
) -> (Vec<FrameInfo<'tcx>>, bool) {
9694
match machine.backtrace_style {
9795
BacktraceStyle::Off => {
9896
// Remove all frames marked with `caller_location` -- that attribute indicates we
9997
// usually want to point at the caller, not them.
100-
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(tcx));
98+
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(machine.tcx));
10199
// Retain one frame so that we can print a span for the error itself
102100
stacktrace.truncate(1);
103101
(stacktrace, false)
@@ -111,7 +109,7 @@ fn prune_stacktrace<'tcx>(
111109
if has_local_frame {
112110
// Remove all frames marked with `caller_location` -- that attribute indicates we
113111
// usually want to point at the caller, not them.
114-
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(tcx));
112+
stacktrace.retain(|frame| !frame.instance.def.requires_caller_location(machine.tcx));
115113

116114
// This is part of the logic that `std` uses to select the relevant part of a
117115
// backtrace. But here, we only look for __rust_begin_short_backtrace, not
@@ -121,7 +119,7 @@ fn prune_stacktrace<'tcx>(
121119
.into_iter()
122120
.take_while(|frame| {
123121
let def_id = frame.instance.def_id();
124-
let path = tcx.def_path_str(def_id);
122+
let path = machine.tcx.def_path_str(def_id);
125123
!path.contains("__rust_begin_short_backtrace")
126124
})
127125
.collect::<Vec<_>>();
@@ -256,7 +254,7 @@ pub fn report_error<'tcx, 'mir>(
256254
};
257255

258256
let stacktrace = ecx.generate_stacktrace();
259-
let (stacktrace, was_pruned) = prune_stacktrace(stacktrace, &ecx.machine, *ecx.tcx);
257+
let (stacktrace, was_pruned) = prune_stacktrace(stacktrace, &ecx.machine);
260258
e.print_backtrace();
261259
msg.insert(0, e.to_string());
262260
report_msg(
@@ -267,7 +265,6 @@ pub fn report_error<'tcx, 'mir>(
267265
helps,
268266
&stacktrace,
269267
&ecx.machine,
270-
*ecx.tcx,
271268
);
272269

273270
// Include a note like `std` does when we omit frames from a backtrace
@@ -315,13 +312,13 @@ fn report_msg<'tcx>(
315312
helps: Vec<(Option<SpanData>, String)>,
316313
stacktrace: &[FrameInfo<'tcx>],
317314
machine: &Evaluator<'_, 'tcx>,
318-
tcx: TyCtxt<'tcx>,
319315
) {
320316
let span = stacktrace.first().map_or(DUMMY_SP, |fi| fi.span);
317+
let sess = machine.tcx.sess;
321318
let mut err = match diag_level {
322-
DiagLevel::Error => tcx.sess.struct_span_err(span, title).forget_guarantee(),
323-
DiagLevel::Warning => tcx.sess.struct_span_warn(span, title),
324-
DiagLevel::Note => tcx.sess.diagnostic().span_note_diag(span, title),
319+
DiagLevel::Error => sess.struct_span_err(span, title).forget_guarantee(),
320+
DiagLevel::Warning => sess.struct_span_warn(span, title),
321+
DiagLevel::Note => sess.diagnostic().span_note_diag(span, title),
325322
};
326323

327324
// Show main message.
@@ -370,95 +367,97 @@ fn report_msg<'tcx>(
370367
err.emit();
371368
}
372369

373-
pub fn emit_diagnostic<'tcx>(e: NonHaltingDiagnostic, machine: &Evaluator<'_, 'tcx>, tcx: TyCtxt<'tcx>) {
374-
use NonHaltingDiagnostic::*;
375-
376-
let stacktrace = MiriEvalContext::generate_stacktrace_from_stack(machine.threads.active_thread_stack());
377-
let (stacktrace, _was_pruned) = prune_stacktrace(stacktrace, machine, tcx);
378-
379-
let (title, diag_level) = match e {
380-
RejectedIsolatedOp(_) =>
381-
("operation rejected by isolation", DiagLevel::Warning),
382-
Int2Ptr { .. } => ("integer-to-pointer cast", DiagLevel::Warning),
383-
CreatedPointerTag(..)
384-
| PoppedPointerTag(..)
385-
| CreatedCallId(..)
386-
| CreatedAlloc(..)
387-
| FreedAlloc(..)
388-
| ProgressReport { .. }
389-
| WeakMemoryOutdatedLoad =>
390-
("tracking was triggered", DiagLevel::Note),
391-
};
392-
393-
let msg = match e {
394-
CreatedPointerTag(tag, None) =>
395-
format!("created tag {tag:?}"),
396-
CreatedPointerTag(tag, Some((alloc_id, range))) =>
397-
format!("created tag {tag:?} at {alloc_id:?}{range:?}"),
398-
PoppedPointerTag(item, tag) =>
399-
match tag {
400-
None =>
401-
format!(
402-
"popped tracked tag for item {item:?} due to deallocation",
403-
),
404-
Some((tag, access)) => {
405-
format!(
406-
"popped tracked tag for item {item:?} due to {access:?} access for {tag:?}",
407-
)
408-
}
409-
},
410-
CreatedCallId(id) =>
411-
format!("function call with id {id}"),
412-
CreatedAlloc(AllocId(id), size, align, kind) =>
413-
format!(
414-
"created {kind} allocation of {size} bytes (alignment {align} bytes) with id {id}",
415-
size = size.bytes(),
416-
align = align.bytes(),
417-
),
418-
FreedAlloc(AllocId(id)) =>
419-
format!("freed allocation with id {id}"),
420-
RejectedIsolatedOp(ref op) =>
421-
format!("{op} was made to return an error due to isolation"),
422-
ProgressReport { .. } =>
423-
format!("progress report: current operation being executed is here"),
424-
Int2Ptr { .. } =>
425-
format!("integer-to-pointer cast"),
426-
WeakMemoryOutdatedLoad =>
427-
format!("weak memory emulation: outdated value returned from load"),
428-
};
429-
430-
let notes = match e {
431-
ProgressReport { block_count } => {
432-
// It is important that each progress report is slightly different, since
433-
// identical diagnostics are being deduplicated.
434-
vec![
435-
(None, format!("so far, {block_count} basic blocks have been executed")),
436-
]
437-
}
438-
_ => vec![],
439-
};
440-
441-
let helps = match e {
442-
Int2Ptr { details: true } =>
443-
vec![
444-
(None, format!("This program is using integer-to-pointer casts or (equivalently) `ptr::from_exposed_addr`,")),
445-
(None, format!("which means that Miri might miss pointer bugs in this program.")),
446-
(None, format!("See https://doc.rust-lang.org/nightly/std/ptr/fn.from_exposed_addr.html for more details on that operation.")),
447-
(None, format!("To ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead.")),
448-
(None, format!("You can then pass the `-Zmiri-strict-provenance` flag to Miri, to ensure you are not relying on `from_exposed_addr` semantics.")),
449-
(None, format!("Alternatively, the `-Zmiri-permissive-provenance` flag disables this warning.")),
450-
],
451-
_ => vec![],
452-
};
453-
454-
report_msg(diag_level, title, vec![msg], notes, helps, &stacktrace, machine, tcx);
370+
impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
371+
pub fn emit_diagnostic(&self, e: NonHaltingDiagnostic) {
372+
use NonHaltingDiagnostic::*;
373+
374+
let stacktrace = MiriEvalContext::generate_stacktrace_from_stack(self.threads.active_thread_stack());
375+
let (stacktrace, _was_pruned) = prune_stacktrace(stacktrace, self);
376+
377+
let (title, diag_level) = match e {
378+
RejectedIsolatedOp(_) =>
379+
("operation rejected by isolation", DiagLevel::Warning),
380+
Int2Ptr { .. } => ("integer-to-pointer cast", DiagLevel::Warning),
381+
CreatedPointerTag(..)
382+
| PoppedPointerTag(..)
383+
| CreatedCallId(..)
384+
| CreatedAlloc(..)
385+
| FreedAlloc(..)
386+
| ProgressReport { .. }
387+
| WeakMemoryOutdatedLoad =>
388+
("tracking was triggered", DiagLevel::Note),
389+
};
390+
391+
let msg = match e {
392+
CreatedPointerTag(tag, None) =>
393+
format!("created tag {tag:?}"),
394+
CreatedPointerTag(tag, Some((alloc_id, range))) =>
395+
format!("created tag {tag:?} at {alloc_id:?}{range:?}"),
396+
PoppedPointerTag(item, tag) =>
397+
match tag {
398+
None =>
399+
format!(
400+
"popped tracked tag for item {item:?} due to deallocation",
401+
),
402+
Some((tag, access)) => {
403+
format!(
404+
"popped tracked tag for item {item:?} due to {access:?} access for {tag:?}",
405+
)
406+
}
407+
},
408+
CreatedCallId(id) =>
409+
format!("function call with id {id}"),
410+
CreatedAlloc(AllocId(id), size, align, kind) =>
411+
format!(
412+
"created {kind} allocation of {size} bytes (alignment {align} bytes) with id {id}",
413+
size = size.bytes(),
414+
align = align.bytes(),
415+
),
416+
FreedAlloc(AllocId(id)) =>
417+
format!("freed allocation with id {id}"),
418+
RejectedIsolatedOp(ref op) =>
419+
format!("{op} was made to return an error due to isolation"),
420+
ProgressReport { .. } =>
421+
format!("progress report: current operation being executed is here"),
422+
Int2Ptr { .. } =>
423+
format!("integer-to-pointer cast"),
424+
WeakMemoryOutdatedLoad =>
425+
format!("weak memory emulation: outdated value returned from load"),
426+
};
427+
428+
let notes = match e {
429+
ProgressReport { block_count } => {
430+
// It is important that each progress report is slightly different, since
431+
// identical diagnostics are being deduplicated.
432+
vec![
433+
(None, format!("so far, {block_count} basic blocks have been executed")),
434+
]
435+
}
436+
_ => vec![],
437+
};
438+
439+
let helps = match e {
440+
Int2Ptr { details: true } =>
441+
vec![
442+
(None, format!("This program is using integer-to-pointer casts or (equivalently) `ptr::from_exposed_addr`,")),
443+
(None, format!("which means that Miri might miss pointer bugs in this program.")),
444+
(None, format!("See https://doc.rust-lang.org/nightly/std/ptr/fn.from_exposed_addr.html for more details on that operation.")),
445+
(None, format!("To ensure that Miri does not miss bugs in your program, use Strict Provenance APIs (https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance, https://crates.io/crates/sptr) instead.")),
446+
(None, format!("You can then pass the `-Zmiri-strict-provenance` flag to Miri, to ensure you are not relying on `from_exposed_addr` semantics.")),
447+
(None, format!("Alternatively, the `-Zmiri-permissive-provenance` flag disables this warning.")),
448+
],
449+
_ => vec![],
450+
};
451+
452+
report_msg(diag_level, title, vec![msg], notes, helps, &stacktrace, self);
453+
}
455454
}
456455

457456
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
458457
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
459458
fn emit_diagnostic(&self, e: NonHaltingDiagnostic) {
460459
let this = self.eval_context_ref();
461-
emit_diagnostic(e, &this.machine, *this.tcx);
460+
this.machine.emit_diagnostic(e);
462461
}
463462

464463
/// We had a panic in Miri itself, try to print something useful.
@@ -477,7 +476,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
477476
vec![],
478477
&stacktrace,
479478
&this.machine,
480-
*this.tcx,
481479
);
482480
}
483481
}

src/helpers.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -881,16 +881,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
881881
None => tcx.item_name(def_id),
882882
}
883883
}
884-
885-
fn current_span(&self) -> CurrentSpan<'_, 'mir, 'tcx> {
886-
let this = self.eval_context_ref();
887-
CurrentSpan { current_frame_idx: None, machine: &this.machine, tcx: *this.tcx }
888-
}
889884
}
890885

891886
impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
892-
pub fn current_span(&self, tcx: TyCtxt<'tcx>) -> CurrentSpan<'_, 'mir, 'tcx> {
893-
CurrentSpan { current_frame_idx: None, machine: self, tcx }
887+
pub fn current_span(&self) -> CurrentSpan<'_, 'mir, 'tcx> {
888+
CurrentSpan { current_frame_idx: None, machine: self }
894889
}
895890
}
896891

@@ -901,15 +896,12 @@ impl<'mir, 'tcx> Evaluator<'mir, 'tcx> {
901896
#[derive(Clone)]
902897
pub struct CurrentSpan<'a, 'mir, 'tcx> {
903898
current_frame_idx: Option<usize>,
904-
tcx: TyCtxt<'tcx>,
905899
machine: &'a Evaluator<'mir, 'tcx>,
906900
}
907901

908902
impl<'a, 'mir: 'a, 'tcx: 'a + 'mir> CurrentSpan<'a, 'mir, 'tcx> {
909-
/// Not really about the `CurrentSpan`, but we just happen to have all the things needed to emit
910-
/// diagnostics like that.
911-
pub fn emit_diagnostic(&self, e: NonHaltingDiagnostic) {
912-
emit_diagnostic(e, self.machine, self.tcx);
903+
pub fn machine(&self) -> &'a Evaluator<'mir, 'tcx> {
904+
self.machine
913905
}
914906

915907
/// Get the current span, skipping non-local frames.
@@ -939,13 +931,13 @@ impl<'a, 'mir: 'a, 'tcx: 'a + 'mir> CurrentSpan<'a, 'mir, 'tcx> {
939931
fn current_frame_idx(&mut self) -> usize {
940932
*self
941933
.current_frame_idx
942-
.get_or_insert_with(|| Self::compute_current_frame_index(self.tcx, self.machine))
934+
.get_or_insert_with(|| Self::compute_current_frame_index(self.machine))
943935
}
944936

945937
// Find the position of the inner-most frame which is part of the crate being
946938
// compiled/executed, part of the Cargo workspace, and is also not #[track_caller].
947939
#[inline(never)]
948-
fn compute_current_frame_index(tcx: TyCtxt<'_>, machine: &Evaluator<'_, '_>) -> usize {
940+
fn compute_current_frame_index(machine: &Evaluator<'_, '_>) -> usize {
949941
machine
950942
.threads
951943
.active_thread_stack()
@@ -955,7 +947,7 @@ impl<'a, 'mir: 'a, 'tcx: 'a + 'mir> CurrentSpan<'a, 'mir, 'tcx> {
955947
.find_map(|(idx, frame)| {
956948
let def_id = frame.instance.def_id();
957949
if (def_id.is_local() || machine.local_crates.contains(&def_id.krate))
958-
&& !frame.instance.def.requires_caller_location(tcx)
950+
&& !frame.instance.def.requires_caller_location(machine.tcx)
959951
{
960952
Some(idx)
961953
} else {

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub use crate::concurrency::{
9494
},
9595
};
9696
pub use crate::diagnostics::{
97-
emit_diagnostic, report_error, EvalContextExt as DiagnosticsEvalContextExt,
97+
report_error, EvalContextExt as DiagnosticsEvalContextExt,
9898
NonHaltingDiagnostic, TerminationInfo,
9999
};
100100
pub use crate::eval::{

0 commit comments

Comments
 (0)