1
Fork 0

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, |_| {}) self.report_decorated(tcx, message, |_| {})
} }
/// Create a diagnostic for this const eval error. #[instrument(level = "trace", skip(self, decorate))]
/// pub(super) fn decorate(&self, err: &mut Diagnostic, decorate: impl FnOnce(&mut Diagnostic)) {
/// 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>| {
trace!("reporting const eval failure at {:?}", self.span); 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. // Add some more context for select error types.
match self.error { match self.error {
InterpError::Unsupported( InterpError::Unsupported(
@ -154,35 +137,47 @@ impl<'tcx> ConstEvalErr<'tcx> {
} }
// Let the caller attach any additional information it wants. // Let the caller attach any additional information it wants.
decorate(err); 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); debug!("self.error: {:?}", self.error);
// Special handling for certain errors // Special handling for certain errors
match &self.error { match &self.error {
// Don't emit a new diagnostic for these errors // Don't emit a new diagnostic for these errors
err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => { err_inval!(Layout(LayoutError::Unknown(_))) | err_inval!(TooGeneric) => {
return ErrorHandled::TooGeneric; ErrorHandled::TooGeneric
}
err_inval!(AlreadyReported(error_reported)) => {
return ErrorHandled::Reported(*error_reported);
} }
err_inval!(AlreadyReported(error_reported)) => ErrorHandled::Reported(*error_reported),
err_inval!(Layout(LayoutError::SizeOverflow(_))) => { err_inval!(Layout(LayoutError::SizeOverflow(_))) => {
// We must *always* hard error on these, even if the caller wants just a lint. // 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 // The `message` makes little sense here, this is a more serious error than the
// caller thinks anyway. // caller thinks anyway.
// See <https://github.com/rust-lang/rust/pull/63152>. // See <https://github.com/rust-lang/rust/pull/63152>.
let mut err = struct_error(tcx, &self.error.to_string()); let mut err = struct_error(tcx, &self.error.to_string());
finish(&mut err, None); self.decorate(&mut err, decorate);
return 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));
ErrorHandled::Reported(err.emit()) ErrorHandled::Reported(err.emit())
} }
_ => {
// Report as hard error.
let mut err = struct_error(tcx, message);
err.span_label(self.span, self.error.to_string());
self.decorate(&mut err, decorate);
ErrorHandled::Reported(err.emit())
}
}
}
} }