-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
RFC: Constants in array repeat expressions #2203
Conversation
+1 from me. This looks like a small and intuitive change. I really like the application to array initializers. |
See also #1169. |
@ExpHP Are they mutually exclusive? Can't both be done? |
@Centril all I can really say is I hope they are compatible! (I mean, who doesn't want to have their 🍰 and eat it too?) |
@ExpHP A workable rule for having your 🍰 and eating it too would be:
Thus, this RFC should be forward compatible with |
Hm, I'm not sure that the solution is that simple. That creates a potentially confusing scenario where two different expressions that evaluate to the same value could have different semantics when they appear inside an array repetition, as one could get On the other hand, upon reflection I'm not sure how attached I actually am to |
A compelling argument from @Centril on IRC is that |
(summarizing what we discussed on IRC): As you put it I think If there is confusion as to whether the I also think |
@burdges You'd have to decide what to do when the iterator has less - panic seems the (only) reasonable option... But sure, it should be doable eventually =) |
Yes, you might want |
@burdges Right. Do note that the result of such an expression is non-const unlike the result of |
Note for future self/implementers: moving the That makes |
@eddyb can you say a bit about how we would implement this? I haven't though deeply here but I am mildly worried that it will be hard for us to test if an expression "is const" -- i.e., do we just have to like run it through miri and see what happens? Put another way: it's one way to have a set of expressions that must be const and then check that they are. It's another to have something that might or might not be const and to impose different requirements depending on the result. Seems like it could interact poorly with type inference etc. Otherwise, I like the idea. |
@nikomatsakis No, we'd just run a special case for rvalue promotion, just like we do for borrowed temporaries and for SIMD shuffle indices. |
OK, so we'd be defining this not really for const expresions per se but for 'promotable expressions', which is (I think) not the same set, right? That is, we do some analysis to decide if something is promotable, and it is presumably a bit conservative, or could at least in theory be. Still, it seems like this case will be mildly harder to handle. For example, we can do rvalue promotion on the MIR level before borrow check runs, and hence borrowed temporaries having a longer lifetime just "falls out", but we can't do the same here -- well, I suppose we could wait to check the requirement that I am basically hoping we can avoid having to know whether something is constant during type check but instead have that be computed from the MIR alone. |
That's what I was saying in #2203 (comment) ("necessary to avoid
That last part is what I was saying ("so the current rule could just be "
In the I don't understand the part about "a longer lifetime", but I hope the above explanation suffices. |
rfc, const-repeat-expr: notes on is_rvalue_promotable(expr)
@nikomatsakis: I've updated and aligned the RFC according to @eddyb's ideas.. Hopefully that addresses your concerns =) |
I think @eddyb and I are aligned here in terms of how we want this to work in the compiler, so if they are satisfied I think I am. And reading over their comments, it makes sense. |
@rfcbot fcp merge I propose that we accept this RFC. The RFC expands |
Team member @nikomatsakis has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once a majority of reviewers approve (and none object), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period is now complete. |
Huzzah! The RFC has been accepted. Tracking issue: rust-lang/rust#49147 |
Rendered.
Tracking issue
Relaxes the rules for repeat expressions,
[x; N]
such thatx
may also beconst
, in addition totypeof(x): Copy
. The result of[x; N]
wherex
isconst
is itself alsoconst
.