-
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
update coerce docs and unify relevant tests #72791
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
fn borrow_mut<T>(x: &mut T) -> &mut T { x } | ||
fn borrow<T>(x: &T) -> &T { x } | ||
|
||
fn borrow_mut2<T>(_: &mut T, _: &mut T) {} | ||
fn borrow2<T>(_: &mut T, _: &T) {} | ||
|
||
fn double_mut_borrow<T>(x: &mut Box<T>) { | ||
let y = borrow_mut(x); | ||
let z = borrow_mut(x); | ||
//~^ ERROR cannot borrow `*x` as mutable more than once at a time | ||
drop((y, z)); | ||
} | ||
|
||
fn double_imm_borrow(x: &mut Box<i32>) { | ||
let y = borrow(x); | ||
let z = borrow(x); | ||
**x += 1; | ||
//~^ ERROR cannot assign to `**x` because it is borrowed | ||
drop((y, z)); | ||
} | ||
|
||
fn double_mut_borrow2<T>(x: &mut Box<T>) { | ||
borrow_mut2(x, x); | ||
//~^ ERROR cannot borrow `*x` as mutable more than once at a time | ||
} | ||
|
||
fn double_borrow2<T>(x: &mut Box<T>) { | ||
borrow2(x, x); | ||
//~^ ERROR cannot borrow `*x` as mutable because it is also borrowed as immutable | ||
} | ||
|
||
pub fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,68 @@ | ||
fn borrow_mut<T>(x: &mut T) -> &mut T { x } | ||
fn borrow<T>(x: &T) -> &T { x } | ||
// run-pass | ||
#![allow(unused_braces)] | ||
#![allow(dead_code)] | ||
// pretty-expanded FIXME #23616 | ||
|
||
fn borrow_mut2<T>(_: &mut T, _: &mut T) {} | ||
fn borrow2<T>(_: &mut T, _: &T) {} | ||
use std::rc::Rc; | ||
|
||
fn double_mut_borrow<T>(x: &mut Box<T>) { | ||
let y = borrow_mut(x); | ||
let z = borrow_mut(x); | ||
//~^ ERROR cannot borrow `*x` as mutable more than once at a time | ||
drop((y, z)); | ||
// Examples from the "deref coercions" RFC, at rust-lang/rfcs#241. | ||
|
||
fn use_ref<T>(_: &T) {} | ||
fn use_mut<T>(_: &mut T) {} | ||
|
||
fn use_rc<T>(t: Rc<T>) { | ||
use_ref(&*t); // what you have to write today | ||
use_ref(&t); // what you'd be able to write | ||
use_ref(&&&&&&t); | ||
use_ref(&mut &&&&&t); | ||
use_ref(&&&mut &&&t); | ||
} | ||
|
||
fn use_mut_box<T>(mut t: &mut Box<T>) { | ||
use_mut(&mut *t); // what you have to write today | ||
use_mut(t); // what you'd be able to write | ||
use_mut(&mut &mut &mut t); | ||
|
||
use_ref(&*t); // what you have to write today | ||
use_ref(t); // what you'd be able to write | ||
use_ref(&&&&&&t); | ||
use_ref(&mut &&&&&t); | ||
use_ref(&&&mut &&&t); | ||
} | ||
|
||
fn double_imm_borrow(x: &mut Box<i32>) { | ||
let y = borrow(x); | ||
let z = borrow(x); | ||
**x += 1; | ||
//~^ ERROR cannot assign to `**x` because it is borrowed | ||
drop((y, z)); | ||
fn use_nested<T>(t: &Box<T>) { | ||
use_ref(&**t); // what you have to write today | ||
use_ref(t); // what you'd be able to write (note: recursive deref) | ||
use_ref(&&&&&&t); | ||
use_ref(&mut &&&&&t); | ||
use_ref(&&&mut &&&t); | ||
} | ||
|
||
fn use_slice(_: &[u8]) {} | ||
fn use_slice_mut(_: &mut [u8]) {} | ||
|
||
fn use_vec(mut v: Vec<u8>) { | ||
use_slice_mut(&mut v[..]); // what you have to write today | ||
use_slice_mut(&mut v); // what you'd be able to write | ||
use_slice_mut(&mut &mut &mut v); | ||
|
||
use_slice(&v[..]); // what you have to write today | ||
use_slice(&v); // what you'd be able to write | ||
use_slice(&&&&&&v); | ||
use_slice(&mut &&&&&v); | ||
use_slice(&&&mut &&&v); | ||
} | ||
|
||
fn double_mut_borrow2<T>(x: &mut Box<T>) { | ||
borrow_mut2(x, x); | ||
//~^ ERROR cannot borrow `*x` as mutable more than once at a time | ||
fn use_vec_ref(v: &Vec<u8>) { | ||
use_slice(&v[..]); // what you have to write today | ||
use_slice(v); // what you'd be able to write | ||
use_slice(&&&&&&v); | ||
use_slice(&mut &&&&&v); | ||
use_slice(&&&mut &&&v); | ||
} | ||
|
||
fn double_borrow2<T>(x: &mut Box<T>) { | ||
borrow2(x, x); | ||
//~^ ERROR cannot borrow `*x` as mutable because it is also borrowed as immutable | ||
fn use_op_rhs(s: &mut String) { | ||
*s += {&String::from(" ")}; | ||
} | ||
|
||
pub fn main() {} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn test<T>(_a: T, _b: T) {} | ||
|
||
fn main() { | ||
test(&mut 7, &7); | ||
//~^ mismatched types | ||
} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/coercion/coerce-reborrow-multi-arg-fail.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/coerce-reborrow-multi-arg-fail.rs:4:18 | ||
| | ||
LL | test(&mut 7, &7); | ||
| ^^ types differ in mutability | ||
| | ||
= note: expected mutable reference `&mut {integer}` | ||
found reference `&{integer}` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// build-pass | ||
fn test<T>(_a: T, _b: T) {} | ||
|
||
fn main() { | ||
test(&7, &7); | ||
test(&7, &mut 7); | ||
test::<&i32>(&mut 7, &7); | ||
test::<&i32>(&mut 7, &mut 7); | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we add an explanation for the
foo::<&i32>(&7, &mut 7)
case?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.
Hmm isn't
foo::<&i32>(&7, &mut 7)
fairly trivial as there are no type variables at play here?Or do you want me to add a more detailed explanation for
foo(&7, &mut 7)
? My current explanation isfairly short.
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'm referring at keeping part of the following info around, unless it is no longer valid:
rust/src/librustc_typeck/check/coercion.rs
Lines 34 to 51 in 72417d8
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 think this isn't relevant anymore as we now auto-borrow both
foo::<&i32>(&7, &mut 7)
andfoo(&7, &mut 7)
.Afaict there was a time where we did not compile
foo(&7, &mut 7)
to prevent the inconsistency we have rn (wherefoo(&7, &mut 7)
compiles andfoo(&mut 7, &7)
does not).So the fact that coercion doesn't distinguish
foo<&i32>
fromfn foo(_: &i32, _: &i32)
shouldn't matter anymore.