Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add new tests
  • Loading branch information
b-naber committed Dec 5, 2020
commit 6f117e096735ff937b06337873f24cf5e9bb5097
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(type_ascription)]

// build-pass

fn foo(_arg : [&[u32];3]) {}

fn main() {
let arr = [4,5,6];
foo([&arr : &[u32]; 3]);
}
13 changes: 13 additions & 0 deletions src/test/ui/coercion/type-ascription/coerce-enum-variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// build-pass

#![feature(type_ascription)]

enum Foo<'a> {
A((u32, &'a [u32])),
B((u32, &'a [u32; 4])),
}

fn main() {
let arr = [4,5,6];
let temp = Foo::A((10, &arr : &[u32]));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// run-pass

#![feature(type_ascription)]

use std::any::type_name;
use std::assert_eq;

fn type_of<T>(_: T) -> &'static str {
type_name::<T>()
}

fn main() {
let arr = [5,6,7];
let tup = (5, (3, (12, &arr : &[u32])), &arr : &[u32]);
assert_eq!(type_of(tup), "(i32, (i32, (i32, &[u32])), &[u32])");
}
12 changes: 12 additions & 0 deletions src/test/ui/coercion/type-ascription/coerce-return-expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#![feature(type_ascription)]

fn foo<'a>(arg : (u32, (u32, &'a [u32; 3]))) -> (u32, (u32, &'a [u32])) {
arg : (u32, (u32, &[u32]))
//~^ ERROR: mismatched types
}

fn main() {
let arr = [4,5,6];
let tup = (3, (9, &arr));
let result = foo(tup);
}
12 changes: 12 additions & 0 deletions src/test/ui/coercion/type-ascription/coerce-return-expr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0308]: mismatched types
--> $DIR/coerce-return-expr.rs:4:3
|
LL | arg : (u32, (u32, &[u32]))
| ^^^ expected slice `[u32]`, found array `[u32; 3]`
|
= note: expected tuple `(_, (_, &[u32]))`
found tuple `(_, (_, &'a [u32; 3]))`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Here we test for coercions in struct fields of nested type ascriptions
// inside a tuple using an unsized coercion and a coercion from &mut -> &

// run-pass

#![feature(type_ascription)]

use std::any::type_name;
use std::assert_eq;

fn type_of<T>(_: T) -> &'static str {
type_name::<T>()
}

struct Foo<'a, 'b, T1, T2> {
_a : (T1, (T1, &'a [T1]), &'b T2),
}

struct Bar {
_b : u32,
}

fn main() {
let mut bar = Bar {_b : 26};
let arr = [4,5,6];
let tup = (9, (3, &arr : &[u32]), &mut bar);
assert_eq!(type_of(tup), "(i32, (i32, &[u32]), &mut coerce_struct_fields_pass_in_let_stmt::Bar)");
let _ = Foo { _a : (9, (3, &arr : &[u32]), &mut bar) };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// build-pass

#![feature(type_ascription)]

fn foo<'a>(arg : &'a [u32; 3]) -> &'a [u32] {
arg : &[u32]
}

fn main() {
let arr = [4,5,6];
let _ = foo(&arr);
}
14 changes: 14 additions & 0 deletions src/test/ui/coercion/type-ascription/coerce-union-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// build-pass

#![feature(type_ascription)]

union Foo<'a> {
f1: (&'a u32, (u32, &'a [u32])),
_f2: u32,
}

fn main() {
let arr = [4,5,6];
let x = &mut 26;
let _ = Foo { f1: (x : &u32, (5, &arr : &[u32])) };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(type_ascription)]

fn f() {
let mut x: &'static u32 = &22;
let y = &44;
foo(x, y);
}

fn foo<'a, 'b>(arg1 : &'a u32, arg2 : &'b u32) {
let p = &mut (arg1 : &'b u32);
//~^ ERROR: lifetime mismatch
*p = arg2;
}

fn main() {
f();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0623]: lifetime mismatch
--> $DIR/unsound-type-ascription-coercion.rs:10:17
|
LL | fn foo<'a, 'b>(arg1 : &'a u32, arg2 : &'b u32) {
| ------- -------
| |
| these two types are declared with different lifetimes...
LL | let p = &mut (arg1 : &'b u32);
| ^^^^ ...but data from `arg1` flows into `arg2` here

error: aborting due to previous error

For more information about this error, try `rustc --explain E0623`.