-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Prohibit cycles behind references while static initialization #149973
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
base: main
Are you sure you want to change the base?
Conversation
| if !tcx.is_foreign_item(static_def_id) { | ||
| // Fire the query to detect cycles. We cannot restrict this to only when | ||
| // evaluating statics, since static reference cycles can also be formed | ||
| // through consts, especially promoted ones. |
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.
Not skiping firing the query when machine.static_root_ids.is_none() here further prohibits some currently compiled codes such as
static FOO: i32 = {
let x = &BAR;
42
};
const BAR: i32 = {
let x = &FOO;
42
};but I guess we might have to refute const cases as well because the following codes slightly modified from the original issue still make problems with it:
enum Never {}
// &&X creates a promoted const
static X: &Never = weird(&&X);
const fn weird(a: &&&Never) -> &'static Never {
// SAFETY: our argument type has an unsatisfiable
// library invariant; therefore, this code is unreachable.
unsafe { std::hint::unreachable_unchecked() };
}// for privacy
mod mrow {
pub struct InBoundsIndex<const N: usize>(());
impl<const N: usize> InBoundsIndex<N> {
pub const fn new() -> Option<InBoundsIndex<N>> {
if N < 32 {
Some(Self(()))
} else {
None
}
}
}
}
use mrow::InBoundsIndex;
static IDX: InBoundsIndex<64> = {
FOO;
// THIS SHOULD PANIC!!! but we do UB first on the line above
InBoundsIndex::<64>::new().unwrap()
};
const FOO: () = {
let _x = index([0; 32], &IDX);
};
const fn index<const N: usize>(arr: [u8; 32], _: &InBoundsIndex<N>) -> u8 {
// SAFETY: InBoundsIndex can only be created by its new, which ensures N is < 32
unsafe { arr.as_ptr().add(N).read() }
}|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
Oh, this already breaks things as hell like in: static POSTFIX: [(Input, Action); 10] = [
(Keyword("as"), SetState(&[(ExpectType, SetState(&POSTFIX))])),
... |
|
@bors try |
Prohibit cycles behind references while static initialization
This comment has been minimized.
This comment has been minimized.
|
💔 Test for 352f3f3 failed: CI. Failed jobs:
|
|
The job Click to see the possible cause of the failure (guessed by this bot) |
Fixes #143047