Skip to content

Commit

Permalink
merge coercion test folders
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed Jun 19, 2020
1 parent a39c778 commit 0b5007e
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 97 deletions.
68 changes: 0 additions & 68 deletions src/test/ui/coerce/coerce-overloaded-autoderef.rs

This file was deleted.

File renamed without changes.
32 changes: 32 additions & 0 deletions src/test/ui/coercion/coerce-overloaded-autoderef-fail.rs
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() {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0499]: cannot borrow `*x` as mutable more than once at a time
--> $DIR/coerce-overloaded-autoderef.rs:9:24
--> $DIR/coerce-overloaded-autoderef-fail.rs:9:24
|
LL | let y = borrow_mut(x);
| - first mutable borrow occurs here
Expand All @@ -10,7 +10,7 @@ LL | drop((y, z));
| - first borrow later used here

error[E0506]: cannot assign to `**x` because it is borrowed
--> $DIR/coerce-overloaded-autoderef.rs:17:5
--> $DIR/coerce-overloaded-autoderef-fail.rs:17:5
|
LL | let y = borrow(x);
| - borrow of `**x` occurs here
Expand All @@ -22,7 +22,7 @@ LL | drop((y, z));
| - borrow later used here

error[E0499]: cannot borrow `*x` as mutable more than once at a time
--> $DIR/coerce-overloaded-autoderef.rs:23:20
--> $DIR/coerce-overloaded-autoderef-fail.rs:23:20
|
LL | borrow_mut2(x, x);
| ----------- - ^ second mutable borrow occurs here
Expand All @@ -31,7 +31,7 @@ LL | borrow_mut2(x, x);
| first borrow later used by call

error[E0502]: cannot borrow `*x` as mutable because it is also borrowed as immutable
--> $DIR/coerce-overloaded-autoderef.rs:28:5
--> $DIR/coerce-overloaded-autoderef-fail.rs:28:5
|
LL | borrow2(x, x);
| -------^^^^-^
Expand Down
78 changes: 57 additions & 21 deletions src/test/ui/coercion/coerce-overloaded-autoderef.rs
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() {}
2 changes: 0 additions & 2 deletions src/test/ui/coercion/coerce-to-bang-cast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![feature(never_type)]

fn foo(x: usize, y: !, z: usize) { }

fn cast_a() {
let y = {return; 22} as !;
//~^ ERROR non-primitive cast
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/coercion/coerce-to-bang-cast.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0605]: non-primitive cast: `i32` as `!`
--> $DIR/coerce-to-bang-cast.rs:6:13
--> $DIR/coerce-to-bang-cast.rs:4:13
|
LL | let y = {return; 22} as !;
| ^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

error[E0605]: non-primitive cast: `i32` as `!`
--> $DIR/coerce-to-bang-cast.rs:11:13
--> $DIR/coerce-to-bang-cast.rs:9:13
|
LL | let y = 22 as !;
| ^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 0b5007e

Please sign in to comment.