-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Desugared x.index(y) is not equivalent to x[y] #30127
Comments
This might be a bug caused by missing checks for overloaded operators, depends when the destructors run. |
triage: P-medium |
triage: P-high The high priority is just to investigate and make sure we understand what is going on. |
@pnkfelix Offers to take this one. |
This is (indeed) by design. (Ideally we would figure out a way to tweak the Rvalue lifetime rules so that the two forms are equivalent, but I do not know if that is feasible.) The reason this is occurring: when determining the lifetime for an rvalue, there are a couple different tricks for extending the lifetimes of temporary rvalues to last as long as the scope of some variable binding, rather than the statement the rvalues happen to appear within. (These still need to be documented somewhere outside of the code, see also #12032.) The particular detail here is we descend expressions according to a grammar to determine which rvalues should have their lifetimes extended, and right now, that grammar looks like this:
Note that the above does not have method call That's the heart of why this is happening: the temporary lifetime rules are based on the syntax that has not had the index expressions desugared to method calls, so you end up with more convenient rules in terms of how long the receiver lives in those cases. I think it might be interesting to try to extend the rules here so that the grammar looks like this:
Update: we probably would also want the lifetime Its also possible that the extension under consideration belongs in the |
(removing P-high on basis of prior investigation.) |
triage: P-medium |
Hmm, my idea from above won't work because we do not have a |
@pnkfelix Could we compute the extents later, before regionck, but after type inference? |
@eddyb it may be possible, but it would require a pretty serious refactoring of the code, since we compute these extents in the same |
@eddyb (plus I'm not sure its actually what we want ... part of the overall idea has been to have a relatively simple syntactic rule for deriving the temporary r-value lifetimes. The idea I wrote above is ... somewhat in conflict with that goal.) |
@pnkfelix @eddyb I think that the extension would be subsumed by the proposal in rust-lang/rfcs#66 -- however, the need to refactor (and compute the temporary lifetimes during typeck) is the main reason that this RFC has not been implemented. |
@pnkfelix Argh. I just revisited this for some reason. Those temporary rules were designed before deref and |
I am going to unassign @pnkfelix since I don't think they're actively investigating. |
Triage: this compiles and does not error on 2018, but if we try to print
2015 errors without trying to print:
|
Triage: no change Latest example code is: use std::ops::Index;
fn main() {
let _sugar = &"a".to_owned()[..];
let _desugar1 = "a".to_owned().index(..);
let _desugar2 = &*"a".to_owned().index(..);
println!("{:?}", _desugar2);
} |
If the
let _desugar
s are commented out, it compiles fine.It seems a method call is treated differently to the
[]
syntax. This may be a purposeful consequence of the design of temporary lifetimes, I don't know.The text was updated successfully, but these errors were encountered: