-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Adding type annotation equivalent to existing type changes code semantics #35919
Comments
I don't think autoderef is involved here, the type of |
Looks like a duplicate of #25899, but I think we should keep it open anyways since this is very confusing |
Well, actually, this isn't really a diagnostics issue--I'm going to nominate for a lang team discussion so that we can see what we want to do here, and can we do anything here. For reference the first example compiles with the following error today:
|
The reason that this happens is that, in the second case, where the expected type is known, we will do a "reborrow coercion" (in particular, when coercing from I think we could fix this (potentially) by detecting the case that we are coercing an |
Don't have time to write up detailed stuff. The basic idea is to look into the |
Thanks for the information. It is helpful. I tried to fix this problem, but failed. Because "always re-borrow" is not a good strategy for the time being. let mut buf : &mut [u8] = init();
{
let tmp = buf; // If we automatically add re-borrow here, below line will be failed to compile
buf = &mut tmp[n..];
}
// Error Message:
// error[E0499]: cannot borrow `*buf` as mutable more than once at a time I guess it could be solved when NLL is ready. Test case as below: #![feature(nll)] // enable nll feature, and the compile error is gone
fn main()
{
let mut buf : &mut [u8] = &mut [1,2,3];
{
let tmp : &mut [u8] = &mut *buf; // enforce re-borrow occurs
buf = &mut tmp[1..];
}
println!("{:?}", buf);
} I'll try to revisit this issue again after NLL become the default. |
Triage: this still errors today:
|
This piece of code can't compile:
But, if we add the type annotation, it compiles:
I guess the later form triggers "auto-deref" mechanism, which equals to:
But this behaviour is still very confusing. Type annotation or not doesn't change the type of
x
at all. Type ofx
is always&mut i32
. But they lead to different compile result. I have no idea whether should we fix it.The text was updated successfully, but these errors were encountered: