-
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
Improve diagnostics to guide users past Borrow checking regression in brotli #47349
Comments
Hmm. |
triage: P-high Have to get to the bottom of this. |
Standalone test case: trait Sweep {
fn sweep(&self) -> usize;
}
trait SliceWrapper<T> {
fn slice(&self) -> &[T];
}
trait SliceWrapperMut<T> {
fn slice_mut(&mut self) -> &mut [T];
}
fn foo<B: SliceWrapper<u32> + SliceWrapperMut<u32> + Sweep>(mut buckets: B) {
let key = 0u32;
buckets.slice_mut()[(key as usize).wrapping_add(22).wrapping_rem(buckets.sweep())] = 22;
}
fn main() { } changing the function
Now to decide if it is legit. =) |
OK, so, I think the error is legit. In fact, what's a bit curious is that the other example works. Here is what is happening, and why the error makes sense. It is easier to see if you view the MIR (I enabled NLL, in fact, just to check, and dumped the MIR; you get the same error there). Basically the code desugars into something like this:
Clearly, as you can see, we started a borrow of Now, the somewhat surprising thing is that the version using a plain
The reason is that, in this case,
In this case, there is no initial borrow of |
Seeing as how the error appears to be legit, and brotli has worked around it, I guess I'm inclined to close this issue. One could imagine doing a forward compatibility lint but it'd be quite tricky to do. |
I think it would be enough to add an automatically applicable suggestion to the error that suggests inserting the intermediate variable. |
triage: P-medium Converting to a A-diagnostics bug -- i.e., we should offer a better suggestion -- and dropping priority from P-high, since the new behavior is correct. |
Triage: Should be removed from the milestone. |
Marking as release candidate. It'd be good to have some suggestions for what the error should be. |
I'm not really sure what this error should be. It occurs in a few crates in practice. I guess the suggestion would be to pull out the subcomputation into a separate variable... the next question then is when to trigger this suggestion. There are a number of scenarios where it applies, e.g. in this example from #51915 as well ( I am imagining something like this:
then we might add a note like "consider extracting this into a fresh variable with a let" |
I'm going to bump this down to RC2 in priority. Just doesn't seem that high priority. |
I've changed my mind and brought this back to the RC. This may be a sort of common scenario and being able to guide users would be good. |
Let me see what I can do for this one. |
So @KiChjang took a stab at this but we came to realize that -- in MIR in particular -- it's a bit tricky to figure out when to offer a suggestion. Basically the suggestion comes down to a kind of code motion: saying "this little chunk of code would borrow-check if it occurred before the Figuring out if that is really true is a bit tricky, and there is always the question of changing semantics (e.g., moving code earlier might mean that the value of some variables is different and so forth). So we have to decide how far we want to go here with this suggestion. On the other hand, maybe we can phrase some kind of neutral text, sort of like "note: consider moving this expression so that it occurs before the mutable borrow begins"? This kind of general advice is basically always applicable though, so how do we decide when to give it? |
discussed in meeting. Going to try to lower priority on this, since its not clear how we should resolve this, and the payoff (to me at least) is not totally clear. |
discussed with @nikomatsakis . Decided that while it might be interesting to have some kind of code motion analysis that would suggest to the user ways they could rearrange their expression to get things compiling, its not something that we have to have built-in. (For example, I think it might be interesting to try to incorporate such things into clippy...) So, since the error is legit, and brotli has worked around it, we are going to close this issue. |
I mean regression in the sense that code that used to compile doesn’t anymore. Maybe that is an intentional soundness fix.
Compiling https://crates.io/crates/brotli / https://github.com/dropbox/rust-brotli with:
Commit range: 61452e5...f62f774. #47167 seems relevant. CC @ivanbakel @eddyb
The text was updated successfully, but these errors were encountered: