-
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
Constants and statics are not required to be Sized. #34390
Comments
Should it be // as sugar for:
const A: &'static [i32] = &[1, 2, 3];
fn main() { let x = (*A)[0]; ... } ? I don't think A needs to be an lvalue. |
It makes more sense for constants, but I gave the example with |
Hmm? I mean the static is desugared into a const. |
The idea being that a static and a const reference both give access to one lvalue. |
@Ericson2314 No, |
Hmm? #![feature(const_fn)]
use std::fmt::Debug;
use std::cell::UnsafeCell;
#[derive(Debug)]
struct Doop(UnsafeCell<u32>);
unsafe impl Send for Doop {}
unsafe impl Sync for Doop {}
//const ASDF: &'static (Debug + Sync) = &Doop(UnsafeCell::new(1));
static ASDF: &'static (Debug + Sync) = &Doop(UnsafeCell::new(1));
fn main() {
println!("{:#?}", ASDF);
} Both of these gives me a "error: cannot borrow a constant which contains interior mutability, create a static instead". This is what I'd suspect as the lvalue from the promoted |
@Ericson2314 Let me rewrite my desugaring to account for that, oops. |
#![feature(const_fn)]
use std::fmt::Debug;
use std::cell::UnsafeCell;
#[derive(Debug)]
struct Doop(UnsafeCell<u32>);
unsafe impl Send for Doop {}
unsafe impl Sync for Doop {}
static ASDF_INNER: Doop = Doop(UnsafeCell::new(1));
//static ASDF: &'static (Debug + Sync) = &ASDF_INNER;
const ASDF: &'static (Debug + Sync) = &ASDF_INNER;
fn main() {
println!("{:#?}", ASDF);
} Oh and my confusion really just stemmed from forgetting about the constants referring to statics rule. [Kind of a funny rule when |
I think in the meantime unsized statics should probably error, perhaps dependent on a crater run, though I doubt there's many people relying on said bug. It's much easier to relax restrictions than tighten them. |
Will prepare a patch to turn this into an error, hopefully nobody has been abusing it. |
Disallow constants and statics from having unsized types. This is a `[breaking-change]` which fixes #34390 by banning unsized `const` and `static`, e.g.: ```rust const A: [i32] = *(&[0, 1, 2] as &[i32]); static B: str = *"foo"; ``` This was not intentionally allowed, and other than for `static` since some versions ago, it ICE'd. If you've been taking advantage of this with `static`, you should be able to just use references instead.
This looks unintentional, and has been broken in all cases, until a few releases ago, AFAICT.
Should this be covered by WF rules? cc @rust-lang/lang @ubsan @solson
Unsized constants cause an ICE, in both old trans and MIR trans:
Unsized statics appear to work, but only in old trans:
I believe this is possible because a constant DST lvalue in old trans is merely the fat pointer.
It seems useful to allow an unsized
static
, but IMO it should require a sized RHS instead, i.e.:And, of course, this should all go through the RFC process, to figure out all the finer details.
The text was updated successfully, but these errors were encountered: