Factor decorate closure out into a method

This commit is contained in:
Oli Scherer 2022-11-22 12:00:07 +00:00
parent 98dc76a374
commit dec05e9c73

View File

@ -86,26 +86,9 @@ impl<'tcx> ConstEvalErr<'tcx> {
self.report_decorated(tcx, message, |_| {})
}
/// 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 `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.)
#[instrument(skip(self, tcx, decorate), level = "debug")]
pub(super) fn report_decorated(
&self,
tcx: TyCtxtAt<'tcx>,
message: &str,
decorate: impl FnOnce(&mut Diagnostic),
) -> ErrorHandled {
let finish = |err: &mut Diagnostic, span_msg: Option<String>| {
#[instrument(level = "trace", skip(self, decorate))]
pub(super) fn decorate(&self, err: &mut Diagnostic, decorate: impl FnOnce(&mut Diagnostic)) {
trace!("reporting const eval failure at {:?}", self.span);
if let Some(span_msg) = span_msg {
err.span_label(self.span, span_msg);
}
// Add some more context for select error types.
match self.error {
InterpError::Unsupported(
@ -154,35 +137,47 @@ impl<'tcx> ConstEvalErr<'tcx> {
}
// Let the caller attach any additional information it wants.
decorate(err);
};
}
/// 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 `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.)
#[instrument(skip(self, tcx, decorate), level = "debug")]
pub(super) fn report_decorated(
&self,
tcx: TyCtxtAt<'tcx>,
message: &str,
decorate: impl FnOnce(&mut Diagnostic),
) -> ErrorHandled {
debug!("self.error: {:?}", self.error);
// Special handling for certain errors
match &self.error {
// Don't emit a new diagnostic for these errors
err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => {
return ErrorHandled::TooGeneric;
}
err_inval!(AlreadyReported(error_reported)) => {
return ErrorHandled::Reported(*error_reported);
ErrorHandled::TooGeneric
}
err_inval!(AlreadyReported(error_reported)) => ErrorHandled::Reported(*error_reported),
err_inval!(Layout(LayoutError::SizeOverflow(_))) => {
// We must *always* hard error on these, even if the caller wants just a lint.
// 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>.
let mut err = struct_error(tcx, &self.error.to_string());
finish(&mut err, None);
return ErrorHandled::Reported(err.emit());
self.decorate(&mut err, decorate);
ErrorHandled::Reported(err.emit())
}
_ => {}
};
let err_msg = self.error.to_string();
_ => {
// Report as hard error.
let mut err = struct_error(tcx, message);
finish(&mut err, Some(err_msg));
err.span_label(self.span, self.error.to_string());
self.decorate(&mut err, decorate);
ErrorHandled::Reported(err.emit())
}
}
}
}