Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow monomorphization time const eval failures if the cause is a type layout issue #124516

Merged
merged 2 commits into from
May 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions compiler/rustc_const_eval/src/const_eval/error.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use std::mem;

use rustc_errors::{DiagArgName, DiagArgValue, DiagMessage, Diagnostic, IntoDiagArg};
use rustc_hir::CRATE_HIR_ID;
use rustc_middle::mir::interpret::Provenance;
use rustc_middle::mir::interpret::{Provenance, ReportedErrorInfo};
use rustc_middle::mir::AssertKind;
use rustc_middle::query::TyCtxtAt;
use rustc_middle::ty::TyCtxt;
@@ -139,9 +139,10 @@ where
ErrorHandled::TooGeneric(span)
}
err_inval!(AlreadyReported(guar)) => ErrorHandled::Reported(guar, span),
err_inval!(Layout(LayoutError::ReferencesError(guar))) => {
ErrorHandled::Reported(guar.into(), span)
}
err_inval!(Layout(LayoutError::ReferencesError(guar))) => ErrorHandled::Reported(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there is aSizeOverflow in a promoted?

This PR seems like a good step so I am okay with landing it, but it seems to me there's still a potential for ICEs here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test for promoteds. On master we got a bogus "erroneous constant" note. Now it's gone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was specifically wondering about SizeOverflow as that's a different LayoutError variant that would bypass your new case here,

ReportedErrorInfo::tainted_by_errors(guar),
span,
),
// Report remaining errors.
_ => {
let (our_span, frames) = get_span_and_frames();
17 changes: 14 additions & 3 deletions compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
@@ -1181,9 +1181,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
M::eval_mir_constant(self, *val, span, layout, |ecx, val, span, layout| {
let const_val = val.eval(*ecx.tcx, ecx.param_env, span).map_err(|err| {
if M::ALL_CONSTS_ARE_PRECHECKED && !matches!(err, ErrorHandled::TooGeneric(..)) {
// Looks like the const is not captued by `required_consts`, that's bad.
bug!("interpret const eval failure of {val:?} which is not in required_consts");
if M::ALL_CONSTS_ARE_PRECHECKED {
match err {
ErrorHandled::TooGeneric(..) => {},
ErrorHandled::Reported(reported, span) => {
if reported.is_tainted_by_errors() {
// const-eval will return "tainted" errors if e.g. the layout cannot
// be computed as the type references non-existing names.
// See <https://github.com/rust-lang/rust/issues/124348>.
} else {
// Looks like the const is not captued by `required_consts`, that's bad.
span_bug!(span, "interpret const eval failure of {val:?} which is not in required_consts");
}
}
}
}
err.emit_note(*ecx.tcx);
err
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
@@ -66,6 +66,9 @@ impl ReportedErrorInfo {
pub fn tainted_by_errors(error: ErrorGuaranteed) -> ReportedErrorInfo {
ReportedErrorInfo { is_tainted_by_errors: true, error }
}
pub fn is_tainted_by_errors(&self) -> bool {
self.is_tainted_by_errors
}
}

impl From<ErrorGuaranteed> for ReportedErrorInfo {
7 changes: 0 additions & 7 deletions tests/crashes/124348.rs

This file was deleted.

12 changes: 12 additions & 0 deletions tests/ui/consts/erroneous_type_in_const_return_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! ICE test #124348
//! We should not be running const eval if the layout has errors.

enum Eek {
TheConst,
UnusedByTheConst(Sum),
//~^ ERROR cannot find type `Sum` in this scope
}

const EEK_ZERO: &[Eek] = &[];

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/consts/erroneous_type_in_const_return_value.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0412]: cannot find type `Sum` in this scope
--> $DIR/erroneous_type_in_const_return_value.rs:6:22
|
LL | UnusedByTheConst(Sum),
| ^^^ not found in this scope
|
help: consider importing this trait
|
LL + use std::iter::Sum;
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0412`.
14 changes: 14 additions & 0 deletions tests/ui/consts/erroneous_type_in_promoted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! ICE test #124348
//! We should not be running const eval if the layout has errors.

enum Eek {
TheConst,
UnusedByTheConst(Sum),
//~^ ERROR cannot find type `Sum` in this scope
}

const fn foo() {
let x: &'static [Eek] = &[];
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/consts/erroneous_type_in_promoted.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0412]: cannot find type `Sum` in this scope
--> $DIR/erroneous_type_in_promoted.rs:6:22
|
LL | UnusedByTheConst(Sum),
| ^^^ not found in this scope
|
help: consider importing this trait
|
LL + use std::iter::Sum;
|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0412`.