-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Implement deref coercions. #21351
Implement deref coercions. #21351
Conversation
r? @pnkfelix (rust_highfive has picked a reviewer for you, use r? to override) |
c518f9e
to
b4febf8
Compare
💃 |
@nikomatsakis I'm not sure I'm the best person to review this. Maybe @nick29581 ? |
Do you know what change made all the coercion cruft in commit three obsolete? Do we have tests to ensure nothing has regressed here? |
LGTM. I'd like to see a few more tests - in particular some more compile fail tests to check we don't allow anything we shouldn't. The rpass tests look OK, I think we should probably have some more to check more extreme edge cases - coercions equivalent to And of course we need to wait for the RFC to land |
The removed trait object coercions were dead code, the cases are handled by |
FYI: The RFC has been merged. |
|
||
use std::rc::Rc; | ||
|
||
// Examples from the "deref coercions" RFC, at rust-lang/rfcs#241. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see some negative tests for borrow checker failures.
For example, passing the same &mut Box<T>
value for two distinct &mut T
arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, passing to one &T
and one &mut T
argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And one like this:
fn copy<T>(x: &mut T) -> &mut T { x }
fn foo<T>(x: &mut Box<T>) {
let y = copy(x);
let z = copy(x); // ERROR (I hope)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And one like this:
fn copy<T>(x: &T) -> &T { x }
fn foo(x: &mut Box<int>) {
let y = copy(x); // OK
let z = copy(x); // OK
**x += 1; // ERROR, borrowed
}
b4febf8
to
e6064dd
Compare
e6064dd
to
46696c8
Compare
@bors r+ 46696c84fd2d2459186252657bcf1e76428c5043 |
The coercion logic around unsafe pointers in particular looks strange to me, but that seems to be a pre-existing problem. e.g. this line here https://github.com/eddyb/rust/blob/67718004499d1e446eef4def35aeafc8bf4c0858/src/librustc_typeck/check/coercion.rs#L235 is strange, since it seems to only permit coercions from |
⌛ Testing commit 46696c8 with merge 40c00b4... |
💔 Test failed - auto-mac-32-opt |
@bors retry q_q |
☔ Merge conflict |
46696c8
to
ae076e1
Compare
⌛ Testing commit ae076e1 with merge 398ac8f... |
💔 Test failed - auto-win-32-nopt-t |
@bors retry |
Coercions will now attempt to autoderef as needed before reborrowing. This includes overloaded `Deref`, e.g. `&Rc<T>` coerces to `&T`, and `DerefMut`, e.g. `&mut Vec<T>` coerces to `&mut [T]` (in addition to `&[T]`). Closes #21432.
Coercions will now attempt to autoderef as needed before reborrowing.
This includes overloaded
Deref
, e.g.&Rc<T>
coerces to&T
, andDerefMut
, e.g.&mut Vec<T>
coerces to&mut [T]
(in addition to&[T]
).Closes #21432.