1
Fork 0

Rollup merge of #111261 - compiler-errors:error-guaranteed-should-be-scarier-to-construct, r=BoxyUwU

Mark `ErrorGuaranteed` constructor as deprecated so people don't use it

You should never ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever use this function unless you know what you're doing, so make it harder to accidentally use it!

Alternatives are to change the name to sound scarier, make it `unsafe` (though it's not really a soundness thing), or work on deeper refactors to make it private.

r? `@BoxyUwU`
This commit is contained in:
Yuki Okushi 2023-05-06 09:09:34 +09:00 committed by GitHub
commit 393e285e14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 16 deletions

View file

@ -1178,6 +1178,7 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuaranteed> { pub fn catch_fatal_errors<F: FnOnce() -> R, R>(f: F) -> Result<R, ErrorGuaranteed> {
catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| { catch_unwind(panic::AssertUnwindSafe(f)).map_err(|value| {
if value.is::<rustc_errors::FatalErrorMarker>() { if value.is::<rustc_errors::FatalErrorMarker>() {
#[allow(deprecated)]
ErrorGuaranteed::unchecked_claim_error_was_emitted() ErrorGuaranteed::unchecked_claim_error_was_emitted()
} else { } else {
panic::resume_unwind(value); panic::resume_unwind(value);

View file

@ -192,6 +192,7 @@ impl EmissionGuarantee for ErrorGuaranteed {
became non-error ({:?}), after original `.emit()`", became non-error ({:?}), after original `.emit()`",
db.inner.diagnostic.level, db.inner.diagnostic.level,
); );
#[allow(deprecated)]
ErrorGuaranteed::unchecked_claim_error_was_emitted() ErrorGuaranteed::unchecked_claim_error_was_emitted()
} }
} }

View file

@ -1069,26 +1069,29 @@ impl Handler {
} }
pub fn has_errors(&self) -> Option<ErrorGuaranteed> { pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
self.inner.borrow().has_errors().then(ErrorGuaranteed::unchecked_claim_error_was_emitted) self.inner.borrow().has_errors().then(|| {
#[allow(deprecated)]
ErrorGuaranteed::unchecked_claim_error_was_emitted()
})
} }
pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> { pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
self.inner self.inner.borrow().has_errors_or_lint_errors().then(|| {
.borrow() #[allow(deprecated)]
.has_errors_or_lint_errors() ErrorGuaranteed::unchecked_claim_error_was_emitted()
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted) })
} }
pub fn has_errors_or_delayed_span_bugs(&self) -> Option<ErrorGuaranteed> { pub fn has_errors_or_delayed_span_bugs(&self) -> Option<ErrorGuaranteed> {
self.inner self.inner.borrow().has_errors_or_delayed_span_bugs().then(|| {
.borrow() #[allow(deprecated)]
.has_errors_or_delayed_span_bugs() ErrorGuaranteed::unchecked_claim_error_was_emitted()
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted) })
} }
pub fn is_compilation_going_to_fail(&self) -> Option<ErrorGuaranteed> { pub fn is_compilation_going_to_fail(&self) -> Option<ErrorGuaranteed> {
self.inner self.inner.borrow().is_compilation_going_to_fail().then(|| {
.borrow() #[allow(deprecated)]
.is_compilation_going_to_fail() ErrorGuaranteed::unchecked_claim_error_was_emitted()
.then(ErrorGuaranteed::unchecked_claim_error_was_emitted) })
} }
pub fn print_error_count(&self, registry: &Registry) { pub fn print_error_count(&self, registry: &Registry) {
@ -1333,6 +1336,7 @@ impl HandlerInner {
.push(DelayedDiagnostic::with_backtrace(diagnostic.clone(), backtrace)); .push(DelayedDiagnostic::with_backtrace(diagnostic.clone(), backtrace));
if !self.flags.report_delayed_bugs { if !self.flags.report_delayed_bugs {
#[allow(deprecated)]
return Some(ErrorGuaranteed::unchecked_claim_error_was_emitted()); return Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
} }
} }
@ -1411,7 +1415,10 @@ impl HandlerInner {
self.bump_err_count(); self.bump_err_count();
} }
#[allow(deprecated)]
{
guaranteed = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted()); guaranteed = Some(ErrorGuaranteed::unchecked_claim_error_was_emitted());
}
} else { } else {
self.bump_warn_count(); self.bump_warn_count();
} }

View file

@ -854,9 +854,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let result = self let result = self
.resolve_fully_qualified_call(span, item_name, ty.normalized, qself.span, hir_id) .resolve_fully_qualified_call(span, item_name, ty.normalized, qself.span, hir_id)
.or_else(|error| { .or_else(|error| {
let guar = self
.tcx
.sess
.delay_span_bug(span, "method resolution should've emitted an error");
let result = match error { let result = match error {
method::MethodError::PrivateMatch(kind, def_id, _) => Ok((kind, def_id)), method::MethodError::PrivateMatch(kind, def_id, _) => Ok((kind, def_id)),
_ => Err(ErrorGuaranteed::unchecked_claim_error_was_emitted()), _ => Err(guar),
}; };
// If we have a path like `MyTrait::missing_method`, then don't register // If we have a path like `MyTrait::missing_method`, then don't register

View file

@ -2199,6 +2199,7 @@ pub struct ErrorGuaranteed(());
impl ErrorGuaranteed { impl ErrorGuaranteed {
/// To be used only if you really know what you are doing... ideally, we would find a way to /// To be used only if you really know what you are doing... ideally, we would find a way to
/// eliminate all calls to this method. /// eliminate all calls to this method.
#[deprecated = "`Session::delay_span_bug` should be preferred over this function"]
pub fn unchecked_claim_error_was_emitted() -> Self { pub fn unchecked_claim_error_was_emitted() -> Self {
ErrorGuaranteed(()) ErrorGuaranteed(())
} }

View file

@ -172,7 +172,11 @@ pub fn main() {
let exit_code = rustc_driver::catch_with_exit_code(|| match get_args() { let exit_code = rustc_driver::catch_with_exit_code(|| match get_args() {
Some(args) => main_args(&args), Some(args) => main_args(&args),
_ => Err(ErrorGuaranteed::unchecked_claim_error_was_emitted()), _ =>
{
#[allow(deprecated)]
Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
}
}); });
process::exit(exit_code); process::exit(exit_code);
} }
@ -725,6 +729,7 @@ fn main_args(at_args: &[String]) -> MainResult {
return if code == 0 { return if code == 0 {
Ok(()) Ok(())
} else { } else {
#[allow(deprecated)]
Err(ErrorGuaranteed::unchecked_claim_error_was_emitted()) Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
}; };
} }