-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #72791 - lcnr:coerce-refactor, r=estebank
update coerce docs and unify relevant tests Merges `test/ui/coerce` with `test/ui/coercion`. Updates the documentation of `librustc_typeck/check/coercion.rs`. Adds 2 new coercion tests.
- Loading branch information
Showing
22 changed files
with
140 additions
and
128 deletions.
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.