diff options
| author | bors <bors@rust-lang.org> | 2023-12-26 02:24:39 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-12-26 02:24:39 +0000 |
| commit | 2271c26e4a8e062bb00d709d0ccb5846e0c341b9 (patch) | |
| tree | a3457e0d9b89710e476158cdaffbddb2b7876c6b /compiler/rustc_const_eval/src/interpret | |
| parent | e4c626dd9a17a23270bf8e7158e59cf2b9c04840 (diff) | |
| parent | 8a9db2545919f945ffbb215e4325917e0bfc5b3a (diff) | |
| download | rust-2271c26e4a8e062bb00d709d0ccb5846e0c341b9.tar.gz rust-2271c26e4a8e062bb00d709d0ccb5846e0c341b9.zip | |
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`
Diffstat (limited to 'compiler/rustc_const_eval/src/interpret')
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/eval_context.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/intern.rs | 10 |
2 files changed, 7 insertions, 7 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/eval_context.rs b/compiler/rustc_const_eval/src/interpret/eval_context.rs index af8e5e7d151..e6370adb983 100644 --- a/compiler/rustc_const_eval/src/interpret/eval_context.rs +++ b/compiler/rustc_const_eval/src/interpret/eval_context.rs @@ -473,9 +473,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { backtrace.print_backtrace(); // FIXME(fee1-dead), HACK: we want to use the error as title therefore we can just extract the // label and arguments from the InterpError. - let dcx = self.tcx.sess.dcx(); + let dcx = self.tcx.dcx(); #[allow(rustc::untranslatable_diagnostic)] - let mut diag = self.tcx.sess.struct_allow(""); + let mut diag = dcx.struct_allow(""); let msg = e.diagnostic_message(); e.add_args(dcx, &mut diag); let s = dcx.eagerly_translate_to_string(msg, diag.args()); diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index 7931789e436..202819ee633 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -96,7 +96,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval: // in the value the dangling reference lies. // The `span_delayed_bug` ensures that we don't forget such a check in validation. if tcx.try_get_global_alloc(alloc_id).is_none() { - tcx.sess.span_delayed_bug(ecx.tcx.span, "tried to intern dangling pointer"); + tcx.dcx().span_delayed_bug(ecx.tcx.span, "tried to intern dangling pointer"); } // treat dangling pointers like other statics // just to stop trying to recurse into them @@ -185,7 +185,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory } else { // Validation will error (with a better message) on an invalid vtable pointer. // Let validation show the error message, but make sure it *does* error. - tcx.sess + tcx.dcx() .span_delayed_bug(tcx.span, "vtables pointers cannot be integer pointers"); } } @@ -375,7 +375,7 @@ pub fn intern_const_alloc_recursive< match res { Ok(()) => {} Err(error) => { - ecx.tcx.sess.span_delayed_bug( + ecx.tcx.dcx().span_delayed_bug( ecx.tcx.span, format!( "error during interning should later cause validation failure: {}", @@ -424,7 +424,7 @@ pub fn intern_const_alloc_recursive< // something that cannot be promoted, which in constants means values that have // drop glue, such as the example above. InternKind::Constant => { - ecx.tcx.sess.emit_err(UnsupportedUntypedPointer { span: ecx.tcx.span }); + ecx.tcx.dcx().emit_err(UnsupportedUntypedPointer { span: ecx.tcx.span }); // For better errors later, mark the allocation as immutable. alloc.mutability = Mutability::Not; } @@ -440,7 +440,7 @@ pub fn intern_const_alloc_recursive< } else if ecx.memory.dead_alloc_map.contains_key(&alloc_id) { // Codegen does not like dangling pointers, and generally `tcx` assumes that // all allocations referenced anywhere actually exist. So, make sure we error here. - let reported = ecx.tcx.sess.emit_err(DanglingPtrInFinal { span: ecx.tcx.span }); + let reported = ecx.tcx.dcx().emit_err(DanglingPtrInFinal { span: ecx.tcx.span }); return Err(reported); } else if ecx.tcx.try_get_global_alloc(alloc_id).is_none() { // We have hit an `AllocId` that is neither in local or global memory and isn't |
