-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
make intern_const_alloc_recursive return error #78742
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
use super::validity::RefTracking; | ||
use rustc_data_structures::fx::{FxHashMap, FxHashSet}; | ||
use rustc_errors::ErrorReported; | ||
use rustc_hir as hir; | ||
use rustc_middle::mir::interpret::InterpResult; | ||
use rustc_middle::ty::{self, layout::TyAndLayout, Ty}; | ||
|
@@ -285,11 +286,13 @@ pub enum InternKind { | |
/// tracks where in the value we are and thus can show much better error messages. | ||
/// Any errors here would anyway be turned into `const_err` lints, whereas validation failures | ||
/// are hard errors. | ||
#[tracing::instrument(skip(ecx))] | ||
pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>( | ||
ecx: &mut InterpCx<'mir, 'tcx, M>, | ||
intern_kind: InternKind, | ||
ret: MPlaceTy<'tcx>, | ||
) where | ||
) -> Result<(), ErrorReported> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I had quite deliberately made interning infallible a while ago.^^ Is this really necessary or just yet another hack to work around #76064 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well... infallible or not, it was reporting errors, so it wasn't really infallible, it just didn't report its failures. The problem isn't #76064 in this case, even if the repro test would also be fixed by a fix for #76064 . This issue also occurs for const FOO: *const u32 = {
let x = 42;
&x
}; which typechecks just fine. Basically any dangling pointer was causing this. As you noted in #78655 (comment) that error should stop compilation. We could change the interning error to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's fair.
It was causing an error, but usually not an ICE -- IIRC we even had a testcase for that. But I guess what I really care about is that interning does not return an So, LGTM. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Interning is in a much better place to do this, so unless we really want the error to point to where in the value the problem occurs, I think the current approach works better. |
||
where | ||
'tcx: 'mir, | ||
{ | ||
let tcx = ecx.tcx; | ||
|
@@ -405,12 +408,14 @@ pub fn intern_const_alloc_recursive<M: CompileTimeMachine<'mir, 'tcx>>( | |
// Codegen does not like dangling pointers, and generally `tcx` assumes that | ||
// all allocations referenced anywhere actually exist. So, make sure we error here. | ||
ecx.tcx.sess.span_err(ecx.tcx.span, "encountered dangling pointer in final constant"); | ||
return Err(ErrorReported); | ||
} else if ecx.tcx.get_global_alloc(alloc_id).is_none() { | ||
// We have hit an `AllocId` that is neither in local or global memory and isn't | ||
// marked as dangling by local memory. That should be impossible. | ||
span_bug!(ecx.tcx.span, "encountered unknown alloc id {:?}", alloc_id); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const FOO: *const u32 = { //~ ERROR encountered dangling pointer in final constant | ||
let x; | ||
&x //~ ERROR borrow of possibly-uninitialized variable: `x` | ||
}; | ||
|
||
fn main() { | ||
let FOO = FOO; | ||
//~^ ERROR could not evaluate constant pattern | ||
//~| ERROR could not evaluate constant pattern | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
error[E0381]: borrow of possibly-uninitialized variable: `x` | ||
--> $DIR/issue-78655.rs:3:5 | ||
| | ||
LL | &x | ||
| ^^ use of possibly-uninitialized `x` | ||
|
||
error: encountered dangling pointer in final constant | ||
--> $DIR/issue-78655.rs:1:1 | ||
| | ||
LL | / const FOO: *const u32 = { | ||
LL | | let x; | ||
LL | | &x | ||
LL | | }; | ||
| |__^ | ||
|
||
error: could not evaluate constant pattern | ||
--> $DIR/issue-78655.rs:7:9 | ||
| | ||
LL | let FOO = FOO; | ||
| ^^^ | ||
|
||
error: could not evaluate constant pattern | ||
--> $DIR/issue-78655.rs:7:9 | ||
| | ||
LL | let FOO = FOO; | ||
| ^^^ | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0381`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message text does not match the new variant name.