diff options
| author | Eduard-Mihai Burtescu <eddyb@lyken.rs> | 2022-01-27 09:44:25 +0000 |
|---|---|---|
| committer | Eduard-Mihai Burtescu <eddyb@lyken.rs> | 2022-02-23 06:38:52 +0000 |
| commit | b7e95dee65c35db8f8e07046d445b12d92cbae12 (patch) | |
| tree | af9131de515325e67c68ab9bbd3c514298ca57f4 /compiler/rustc_const_eval/src/const_eval | |
| parent | 0b9d70cf6d47df456280f83b58c04c96aa58e89e (diff) | |
| download | rust-b7e95dee65c35db8f8e07046d445b12d92cbae12.tar.gz rust-b7e95dee65c35db8f8e07046d445b12d92cbae12.zip | |
rustc_errors: let `DiagnosticBuilder::emit` return a "guarantee of emission".
Diffstat (limited to 'compiler/rustc_const_eval/src/const_eval')
| -rw-r--r-- | compiler/rustc_const_eval/src/const_eval/error.rs | 40 | ||||
| -rw-r--r-- | compiler/rustc_const_eval/src/const_eval/eval_queries.rs | 3 |
2 files changed, 23 insertions, 20 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/error.rs b/compiler/rustc_const_eval/src/const_eval/error.rs index 89a0f8245e5..3bd092263c1 100644 --- a/compiler/rustc_const_eval/src/const_eval/error.rs +++ b/compiler/rustc_const_eval/src/const_eval/error.rs @@ -1,7 +1,7 @@ use std::error::Error; use std::fmt; -use rustc_errors::{DiagnosticBuilder, ErrorReported}; +use rustc_errors::Diagnostic; use rustc_hir as hir; use rustc_middle::mir::AssertKind; use rustc_middle::ty::{layout::LayoutError, query::TyCtxtAt, ConstInt}; @@ -94,13 +94,13 @@ impl<'tcx> ConstEvalErr<'tcx> { &self, tcx: TyCtxtAt<'tcx>, message: &str, - emit: impl FnOnce(DiagnosticBuilder<'_>), + decorate: impl FnOnce(&mut Diagnostic), ) -> ErrorHandled { - self.struct_generic(tcx, message, emit, None) + self.struct_generic(tcx, message, decorate, None) } pub fn report_as_error(&self, tcx: TyCtxtAt<'tcx>, message: &str) -> ErrorHandled { - self.struct_error(tcx, message, |mut e| e.emit()) + self.struct_error(tcx, message, |_| {}) } pub fn report_as_lint( @@ -113,7 +113,7 @@ impl<'tcx> ConstEvalErr<'tcx> { self.struct_generic( tcx, message, - |mut lint: DiagnosticBuilder<'_>| { + |lint: &mut Diagnostic| { // Apply the span. if let Some(span) = span { let primary_spans = lint.span.primary_spans().to_vec(); @@ -127,7 +127,6 @@ impl<'tcx> ConstEvalErr<'tcx> { } } } - lint.emit(); }, Some(lint_root), ) @@ -136,9 +135,8 @@ impl<'tcx> ConstEvalErr<'tcx> { /// Create a diagnostic for this const eval error. /// /// Sets the message passed in via `message` and adds span labels with detailed error - /// information before handing control back to `emit` to do any final processing. - /// It's the caller's responsibility to call emit(), stash(), etc. within the `emit` - /// function to dispose of the diagnostic properly. + /// information before handing control back to `decorate` to do any final annotations, + /// after which the diagnostic is emitted. /// /// If `lint_root.is_some()` report it as a lint, else report it as a hard error. /// (Except that for some errors, we ignore all that -- see `must_error` below.) @@ -146,10 +144,10 @@ impl<'tcx> ConstEvalErr<'tcx> { &self, tcx: TyCtxtAt<'tcx>, message: &str, - emit: impl FnOnce(DiagnosticBuilder<'_>), + decorate: impl FnOnce(&mut Diagnostic), lint_root: Option<hir::HirId>, ) -> ErrorHandled { - let finish = |mut err: DiagnosticBuilder<'_>, span_msg: Option<String>| { + let finish = |err: &mut Diagnostic, span_msg: Option<String>| { trace!("reporting const eval failure at {:?}", self.span); if let Some(span_msg) = span_msg { err.span_label(self.span, span_msg); @@ -188,8 +186,8 @@ impl<'tcx> ConstEvalErr<'tcx> { } flush_last_line(last_frame, times); } - // Let the caller finish the job. - emit(err) + // Let the caller attach any additional information it wants. + decorate(err); }; // Special handling for certain errors @@ -206,8 +204,9 @@ impl<'tcx> ConstEvalErr<'tcx> { // The `message` makes little sense here, this is a more serious error than the // caller thinks anyway. // See <https://github.com/rust-lang/rust/pull/63152>. - finish(struct_error(tcx, &self.error.to_string()), None); - return ErrorHandled::Reported(ErrorReported); + let mut err = struct_error(tcx, &self.error.to_string()); + finish(&mut err, None); + return ErrorHandled::Reported(err.emit()); } _ => {} }; @@ -223,13 +222,18 @@ impl<'tcx> ConstEvalErr<'tcx> { rustc_session::lint::builtin::CONST_ERR, hir_id, tcx.span, - |lint| finish(lint.build(message), Some(err_msg)), + |lint| { + let mut lint = lint.build(message); + finish(&mut lint, Some(err_msg)); + lint.emit(); + }, ); ErrorHandled::Linted } else { // Report as hard error. - finish(struct_error(tcx, message), Some(err_msg)); - ErrorHandled::Reported(ErrorReported) + let mut err = struct_error(tcx, message); + finish(&mut err, Some(err_msg)); + ErrorHandled::Reported(err.emit()) } } } diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index 533c32f807d..dad57274104 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -361,7 +361,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>( Err(err.struct_error( ecx.tcx, "it is undefined behavior to use this value", - |mut diag| { + |diag| { diag.note(note_on_undefined_behavior_error()); diag.note(&format!( "the raw bytes of the constant ({}", @@ -370,7 +370,6 @@ pub fn eval_to_allocation_raw_provider<'tcx>( ecx.tcx.global_alloc(alloc_id).unwrap_memory() ) )); - diag.emit(); }, )) } else { |
