Skip to content

Commit 1b2a494

Browse files
committed
Auto merge of #119146 - nnethercote:rm-DiagCtxt-api-duplication, r=compiler-errors
Remove `DiagCtxt` API duplication `DiagCtxt` defines the internal API for creating and emitting diagnostics: methods like `struct_err`, `struct_span_warn`, `note`, `create_fatal`, `emit_bug`. There are over 50 methods. Some of these methods are then duplicated across several other types: `Session`, `ParseSess`, `Parser`, `ExtCtxt`, and `MirBorrowckCtxt`. `Session` duplicates the most, though half the ones it does are unused. Each duplicated method just calls forward to the corresponding method in `DiagCtxt`. So this duplication exists to (in the best case) shorten chains like `ecx.tcx.sess.parse_sess.dcx.emit_err()` to `ecx.emit_err()`. This API duplication is ugly and has been bugging me for a while. And it's inconsistent: there's no real logic about which methods are duplicated, and the use of `#[rustc_lint_diagnostic]` and `#[track_caller]` attributes vary across the duplicates. This PR removes the duplicated API methods and makes all diagnostic creation and emission go through `DiagCtxt`. It also adds `dcx` getter methods to several types to shorten chains. This approach scales *much* better than API duplication; indeed, the PR adds `dcx()` to numerous types that didn't have API duplication: `TyCtxt`, `LoweringCtxt`, `ConstCx`, `FnCtxt`, `TypeErrCtxt`, `InferCtxt`, `CrateLoader`, `CheckAttrVisitor`, and `Resolver`. These result in a lot of changes from `foo.tcx.sess.emit_err()` to `foo.dcx().emit_err()`. (You could do this with more types, but it gets into diminishing returns territory for types that don't emit many diagnostics.) After all these changes, some call sites are more verbose, some are less verbose, and many are the same. The total number of lines is reduced, mostly because of the removed API duplication. And consistency is increased, because calls to `emit_err` and friends are always preceded with `.dcx()` or `.dcx`. r? `@compiler-errors`
2 parents 07cf435 + 06dcc4d commit 1b2a494

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

src/bin/miri.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,19 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
6666
) -> Compilation {
6767
queries.global_ctxt().unwrap().enter(|tcx| {
6868
if tcx.sess.compile_status().is_err() {
69-
tcx.sess.fatal("miri cannot be run on programs that fail compilation");
69+
tcx.dcx().fatal("miri cannot be run on programs that fail compilation");
7070
}
7171

7272
let early_dcx = EarlyDiagCtxt::new(tcx.sess.opts.error_format);
7373
init_late_loggers(&early_dcx, tcx);
7474
if !tcx.crate_types().contains(&CrateType::Executable) {
75-
tcx.sess.fatal("miri only makes sense on bin crates");
75+
tcx.dcx().fatal("miri only makes sense on bin crates");
7676
}
7777

7878
let (entry_def_id, entry_type) = if let Some(entry_def) = tcx.entry_fn(()) {
7979
entry_def
8080
} else {
81-
tcx.sess.fatal("miri can only run programs that have a main function");
81+
tcx.dcx().fatal("miri can only run programs that have a main function");
8282
};
8383
let mut config = self.miri_config.clone();
8484

@@ -91,13 +91,13 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
9191
}
9292

9393
if tcx.sess.opts.optimize != OptLevel::No {
94-
tcx.sess.warn("Miri does not support optimizations. If you have enabled optimizations \
94+
tcx.dcx().warn("Miri does not support optimizations. If you have enabled optimizations \
9595
by selecting a Cargo profile (such as --release) which changes other profile settings \
9696
such as whether debug assertions and overflow checks are enabled, those settings are \
9797
still applied.");
9898
}
9999
if tcx.sess.mir_opt_level() > 0 {
100-
tcx.sess.warn("You have explicitly enabled MIR optimizations, overriding Miri's default \
100+
tcx.dcx().warn("You have explicitly enabled MIR optimizations, overriding Miri's default \
101101
which is to completely disable them. Any optimizations may hide UB that Miri would \
102102
otherwise detect, and it is not necessarily possible to predict what kind of UB will \
103103
be missed. If you are enabling optimizations to make Miri run faster, we advise using \
@@ -110,7 +110,7 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
110110
i32::try_from(return_code).expect("Return value was too large!"),
111111
);
112112
}
113-
tcx.sess.abort_if_errors();
113+
tcx.dcx().abort_if_errors();
114114
});
115115

116116
Compilation::Stop

src/borrow_tracker/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
343343
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
344344
match method {
345345
BorrowTrackerMethod::StackedBorrows => {
346-
this.tcx.tcx.sess.warn("Stacked Borrows does not support named pointers; `miri_pointer_name` is a no-op");
346+
this.tcx.tcx.dcx().warn("Stacked Borrows does not support named pointers; `miri_pointer_name` is a no-op");
347347
Ok(())
348348
}
349349
BorrowTrackerMethod::TreeBorrows =>

src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ pub fn report_error<'tcx, 'mir>(
384384

385385
// Include a note like `std` does when we omit frames from a backtrace
386386
if was_pruned {
387-
ecx.tcx.sess.dcx().note(
387+
ecx.tcx.dcx().note(
388388
"some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace",
389389
);
390390
}
@@ -431,7 +431,7 @@ pub fn report_leaks<'mir, 'tcx>(
431431
);
432432
}
433433
if any_pruned {
434-
ecx.tcx.sess.dcx().note(
434+
ecx.tcx.dcx().note(
435435
"some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace",
436436
);
437437
}

src/eval.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
278278
// Make sure we have MIR. We check MIR for some stable monomorphic function in libcore.
279279
let sentinel = ecx.try_resolve_path(&["core", "ascii", "escape_default"], Namespace::ValueNS);
280280
if !matches!(sentinel, Some(s) if tcx.is_mir_available(s.def.def_id())) {
281-
tcx.sess.fatal(
281+
tcx.dcx().fatal(
282282
"the current sysroot was built without `-Zalways-encode-mir`, or libcore seems missing. \
283283
Use `cargo miri setup` to prepare a sysroot that is suitable for Miri."
284284
);
@@ -363,7 +363,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
363363
match entry_type {
364364
EntryFnType::Main { .. } => {
365365
let start_id = tcx.lang_items().start_fn().unwrap_or_else(|| {
366-
tcx.sess.fatal(
366+
tcx.dcx().fatal(
367367
"could not find start function. Make sure the entry point is marked with `#[start]`."
368368
);
369369
});
@@ -462,8 +462,8 @@ pub fn eval_entry<'tcx>(
462462
if leak_check && !ignore_leaks {
463463
// Check for thread leaks.
464464
if !ecx.have_all_terminated() {
465-
tcx.sess.err("the main thread terminated without waiting for all remaining threads");
466-
tcx.sess.note("pass `-Zmiri-ignore-leaks` to disable this check");
465+
tcx.dcx().err("the main thread terminated without waiting for all remaining threads");
466+
tcx.dcx().note("pass `-Zmiri-ignore-leaks` to disable this check");
467467
return None;
468468
}
469469
// Check for memory leaks.
@@ -474,10 +474,10 @@ pub fn eval_entry<'tcx>(
474474
let leak_message = "the evaluated program leaked memory, pass `-Zmiri-ignore-leaks` to disable this check";
475475
if ecx.machine.collect_leak_backtraces {
476476
// If we are collecting leak backtraces, each leak is a distinct error diagnostic.
477-
tcx.sess.note(leak_message);
477+
tcx.dcx().note(leak_message);
478478
} else {
479479
// If we do not have backtraces, we just report an error without any span.
480-
tcx.sess.err(leak_message);
480+
tcx.dcx().err(leak_message);
481481
};
482482
// Ignore the provided return code - let the reported error
483483
// determine the return code.

src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
539539
RejectOpWith::Abort => isolation_abort_error(op_name),
540540
RejectOpWith::WarningWithoutBacktrace => {
541541
this.tcx
542-
.sess
542+
.dcx()
543543
.warn(format!("{op_name} was made to return an error due to isolation"));
544544
Ok(())
545545
}

0 commit comments

Comments
 (0)