1
Fork 0

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`
This commit is contained in:
bors 2023-12-26 02:24:39 +00:00
commit 2271c26e4a
347 changed files with 2334 additions and 2696 deletions

View file

@ -818,7 +818,7 @@ impl<D: Deps> DepGraphData<D> {
None => {}
}
if let None = qcx.dep_context().sess().has_errors_or_span_delayed_bugs() {
if let None = qcx.dep_context().sess().dcx().has_errors_or_span_delayed_bugs() {
panic!("try_mark_previous_green() - Forcing the DepNode should have set its color")
}

View file

@ -126,7 +126,7 @@ where
}
Fatal => {
error.emit();
qcx.dep_context().sess().abort_if_errors();
qcx.dep_context().sess().dcx().abort_if_errors();
unreachable!()
}
DelayBug => {
@ -138,7 +138,7 @@ where
&& let Some(span) = root.query.span
{
error.stash(span, StashKey::Cycle);
qcx.dep_context().sess().span_delayed_bug(span, "delayed cycle error")
qcx.dep_context().sess().dcx().span_delayed_bug(span, "delayed cycle error")
} else {
error.emit()
};
@ -421,7 +421,7 @@ where
// We have an inconsistency. This can happen if one of the two
// results is tainted by errors. In this case, delay a bug to
// ensure compilation is doomed.
qcx.dep_context().sess().span_delayed_bug(
qcx.dep_context().sess().dcx().span_delayed_bug(
DUMMY_SP,
format!(
"Computed query value for {:?}({:?}) is inconsistent with fed value,\n\
@ -705,7 +705,7 @@ fn incremental_verify_ich_failed<Tcx>(
let old_in_panic = INSIDE_VERIFY_PANIC.with(|in_panic| in_panic.replace(true));
if old_in_panic {
tcx.sess().emit_err(crate::error::Reentrant);
tcx.sess().dcx().emit_err(crate::error::Reentrant);
} else {
let run_cmd = if let Some(crate_name) = &tcx.sess().opts.crate_name {
format!("`cargo clean -p {crate_name}` or `cargo clean`")
@ -714,7 +714,7 @@ fn incremental_verify_ich_failed<Tcx>(
};
let dep_node = tcx.dep_graph().data().unwrap().prev_node_of(prev_index);
tcx.sess().emit_err(crate::error::IncrementCompilation {
tcx.sess().dcx().emit_err(crate::error::IncrementCompilation {
run_cmd,
dep_node: format!("{dep_node:?}"),
});

View file

@ -9,7 +9,7 @@ pub trait Value<Tcx: DepContext>: Sized {
impl<Tcx: DepContext, T> Value<Tcx> for T {
default fn from_cycle_error(tcx: Tcx, cycle: &[QueryInfo], _guar: ErrorGuaranteed) -> T {
tcx.sess().abort_if_errors();
tcx.sess().dcx().abort_if_errors();
// Ideally we would use `bug!` here. But bug! is only defined in rustc_middle, and it's
// non-trivial to define it earlier.
panic!(