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 upTracking issue for RFC 2203, "Constants in array repeat expressions" #49147
Comments
Centril
added
B-RFC-approved
T-lang
C-tracking-issue
labels
Mar 18, 2018
This comment has been minimized.
This comment has been minimized.
|
I had an investigation on this issue. I think removing below lines can achieve our goals. But I'm not sure whether it is enough. rust/src/librustc_mir/borrow_check/nll/type_check/mod.rs Lines 1232 to 1238 in 1eab9c5 rust/src/librustc_typeck/check/mod.rs Lines 4017 to 4025 in 1eab9c5 And another question, given that this change should be guarded by a feature gate, how can we suggest user to enable this feature when a compile error occurs? I'd appreciate it if somebody can write a mentoring instruction. |
This comment has been minimized.
This comment has been minimized.
|
See rust-lang/rfcs#2203 (comment) - I don't think we should implement this before the MIR borrowck becomes the default - while it may be possible, it's getting increasingly risky to do such things. |
This comment has been minimized.
This comment has been minimized.
|
So I just realized that "constant" has been a red herring all along for this problem: For One example of a runtime repeated expression we could allow is I propose (a bit late) that we consider a value (dataflow) analysis,
Another advantage of this is that it's not limited to |
This comment has been minimized.
This comment has been minimized.
Trying to recast what you are saying in my terms, we have I think that's a remark I already made somewhere else earlier this year but I have no idea where.^^ |
This comment has been minimized.
This comment has been minimized.
Yes, except I only realized it can apply to all copies at the very end of writing my entire comment, so my reasoning may look quite roundabout. I suspect this might not even be the only place where we can generalize a type-based analysis to a value-based one (like const-checking has). We could even mix this with polymorphism, by putting "refinement types" through generics. |
This comment has been minimized.
This comment has been minimized.
|
@eddyb hmm, interesting thought. So this is basically "doubling down" on the value-based reasoning we use for consts, and applying it here as well? I don't entirely hate that, particularly if we can kind of "reuse" the form of the rule from consts exactly. |
This comment has been minimized.
This comment has been minimized.
runelabs
commented
May 29, 2018
•
|
I ran into the following constraints with trying to use primitive array repeat in enum Wrap { ... }
struct MyStruct<'a> {
arr: &'a mut [Option<Wrap>]
}
impl<'a> MyStruct<'a> {
pub fn new(list: &'a mut [Option<Wrap>]) -> Self { Self{ arr: list} } }
}
// works fine, and is very ergonomic
let mut z = MyStruct{ arr: &mut [] };
// however the logical follow-up and equally ergonomic &mut [None;N] fails...
// all fine, Copy-trait not needed (*1)
let mut empty1: [Option<Wrap>;11] = Default::default();
let mut a = MyStruct{ arr: &mut empty1 };
// need type ascription, Copy-trait not needed (*2)
let mut empty2 = Default::default():[Option<Wrap>;11];
let mut b = MyStruct::new(&mut empty2);
// need type ascription, Copy-trait for Wrap
let mut empty3 = [Default::default();11]:[Option<Wrap>;11]; // (*3)
let mut empty4 = [Option::default();11]:[Option<Wrap>;11]; // (*4)
let mut empty5 = [None;11]:[Option<Wrap>;11]; // (*5)
// need type ascription, parenthesized expression ... but alas, lifetime fails when (expr)
let mut c = MyStruct{ arr: &mut ([Default::default();11]:[Option<Wrap>;11]) }; // (*6) , also parens expr
let mut d = MyStruct::new( &mut ([Default::default();11]:[Option<Wrap>;11]) ); // (*6b) , also parens expr
// need Copy-trait for Wrap
let mut e = MyStruct{ arr: &mut [Default::default();11] }; // (*7)
let mut f = MyStruct{ arr: &mut [None;11] }; // (*8)
let mut g = MyStruct::new(&mut [None;11]); // (*9)All the cases from (*1) to (*9) try to accomplish similar initialization. Ideally, shouldn't they all work similarly? I really want to use (*9), (*8) or (*7) - and not (*1) or (*2) which are not ergonomic.
|
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis Indeed, I think we should double down on value reasoning in favor of ergonomics.
This is not required in the accepted RFC, you will be able to just write
As for your ascription questions: you can make these replacements in your code:
Then your code looks like this (click to expand).enum Wrap { ... }
struct MyStruct<'a> {
arr: &'a mut [Option<Wrap>]
}
impl<'a> MyStruct<'a> {
pub fn new(list: &'a mut [Option<Wrap>]) -> Self { Self{ arr: list} } }
}
// works fine, and is very ergonomic
let mut z = MyStruct{ arr: &mut [] };
// however the logical follow-up and equally ergonomic &mut [None;N] fails...
// all fine, Copy-trait not needed (*1)
let mut empty1: [_;11] = Default::default();
let mut a = MyStruct{ arr: &mut empty1 };
// need type ascription, Copy-trait not needed (*2)
let mut empty2 = <[_;11]>::default();
let mut b = MyStruct::new(&mut empty2);
// need type ascription, Copy-trait for Wrap
let mut empty3 = [Default::default();11]; // (*3)
let mut empty4 = [Option::default();11]; // (*4)
let mut empty5 = [None;11]; // (*5)
// need type ascription, parenthesized expression ... but alas, lifetime fails when (expr)
let mut c = MyStruct{ arr: &mut [Default::default();11] }; // (*6) , also parens expr
let mut d = MyStruct::new( &mut [Default::default();11] ); // (*6b) , also parens expr
// need Copy-trait for Wrap
let mut e = MyStruct{ arr: &mut [Default::default();11] }; // (*7)
let mut f = MyStruct{ arr: &mut [None;11] }; // (*8)
let mut g = MyStruct::new(&mut [None;11]); // (*9)Further notes from looking at the result of that:
@runelabs I hope I've clarified a few things and/or resolved most of your concerns. |
This comment has been minimized.
This comment has been minimized.
runelabs
commented
May 29, 2018
|
@eddyb Thanks, I hadn't thought of |
This comment has been minimized.
This comment has been minimized.
|
|
eddyb
added
the
S-blocked
label
Aug 24, 2018
This comment has been minimized.
This comment has been minimized.
|
Is the |
Centril
referenced this issue
Oct 7, 2018
Closed
make creating arrays of types that are not `Copy` less painful #1109
This comment has been minimized.
This comment has been minimized.
|
@eddyb hmm maybe we could/should just rename "fixed by NLL" to "needs NLL", which is ... strictly more general? (Or is that label not a true generalization? I'm still musing over the english semantics here. I guess "fixed by NLL" implies that NLL is sufficient but not (necessarily) necessary, while "needs NLL" implies NLL is necessary but not (necessarily) sufficient...)
|
This comment has been minimized.
This comment has been minimized.
|
Discussed briefly on Zulip -- would we want to experiment with this on 2018 only to begin with? |
Centril commentedMar 18, 2018
This is a tracking issue for the RFC "Constants in array repeat expressions" (rust-lang/rfcs#2203).
Steps:
Unresolved questions:
None