Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upConstants and statics are not required to be Sized. #34390
Comments
eddyb
added
I-ICE
I-nominated
T-lang
labels
Jun 20, 2016
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
It makes more sense for constants, but I gave the example with |
This comment has been minimized.
This comment has been minimized.
|
Hmm? I mean the static is desugared into a const. |
This comment has been minimized.
This comment has been minimized.
|
The idea being that a static and a const reference both give access to one lvalue. |
This comment has been minimized.
This comment has been minimized.
|
@Ericson2314 No, |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
@Ericson2314 Let me rewrite my desugaring to account for that, oops. |
This comment has been minimized.
This comment has been minimized.
#![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 |
This comment has been minimized.
This comment has been minimized.
|
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. |
eddyb
self-assigned this
Jun 23, 2016
eddyb
removed
the
I-nominated
label
Jun 23, 2016
This comment has been minimized.
This comment has been minimized.
|
Will prepare a patch to turn this into an error, hopefully nobody has been abusing it. |
eddyb commentedJun 20, 2016
•
edited
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.