Skip to content

Commit 1ed39bc

Browse files
committed
Add some tests
1 parent c5691a8 commit 1ed39bc

File tree

7 files changed

+145
-0
lines changed

7 files changed

+145
-0
lines changed

src/test/ui/never-fallback/box_it.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
// Verifies that our fallback-UB check doesn't trigger
3+
// on correct code
4+
#![feature(never_type)]
5+
#![feature(never_type_fallback)]
6+
7+
fn foo(e: !) -> Box<dyn std::error::Error> {
8+
Box::<_>::new(e)
9+
}
10+
11+
fn main() {}

src/test/ui/never-fallback/from.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
// Tests that we correctly infer types to !
3+
#![feature(never_type)]
4+
#![feature(never_type_fallback)]
5+
6+
struct E;
7+
impl From<!> for E {
8+
fn from(x: !) -> E { x }
9+
}
10+
fn foo(never: !) {
11+
<E as From<_>>::from(never);
12+
}
13+
14+
fn main() {}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// check-pass
2+
use std::convert::{TryFrom, Infallible};
3+
4+
struct E;
5+
6+
impl From<Infallible> for E {
7+
fn from(_: Infallible) -> E {
8+
E
9+
}
10+
}
11+
12+
fn foo() -> Result<(), E> {
13+
u32::try_from(1u32)?;
14+
Ok(())
15+
}
16+
17+
fn main() {}

src/test/ui/never-fallback/obj.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(never_type)]
2+
#![feature(never_type_fallback)]
3+
4+
fn get_type<T>(_: T) -> &'static str {
5+
std::any::type_name::<T>()
6+
}
7+
8+
fn unconstrained_return<T>() -> Result<T, String> {
9+
Err("Hi".to_string())
10+
}
11+
12+
fn foo() {
13+
let a = || {
14+
match unconstrained_return::<_>() { //~ ERROR Fallback to `!` may introduce undefined behavior
15+
Ok(x) => x, // `x` has type `_`, which is unconstrained
16+
Err(s) => panic!(s), // … except for unifying with the type of `panic!()`
17+
// so that both `match` arms have the same type.
18+
// Therefore `_` resolves to `!` and we "return" an `Ok(!)` value.
19+
}
20+
};
21+
22+
let cast: &dyn FnOnce() -> _ = &a;
23+
println!("Return type: {:?}", get_type(cast));
24+
}
25+
26+
fn main() {
27+
foo()
28+
}

src/test/ui/never-fallback/obj.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: Fallback to `!` may introduce undefined behavior
2+
--> $DIR/obj.rs:14:15
3+
|
4+
LL | match unconstrained_return::<_>() {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the type here was inferred to `!`
8+
--> $DIR/obj.rs:14:38
9+
|
10+
LL | match unconstrained_return::<_>() {
11+
| ^
12+
note: ... due to this expression evaluating to `!`
13+
--> $DIR/obj.rs:16:23
14+
|
15+
LL | Err(s) => panic!(s), // … except for unifying with the type of `panic!()`
16+
| ^^^^^^^^^
17+
= note: If you want the `!` type to be used here, add explicit type annotations
18+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
19+
20+
error: aborting due to previous error
21+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(never_type)]
2+
#![feature(never_type_fallback)]
3+
4+
fn get_type<T>(_: T) -> &'static str {
5+
std::any::type_name::<T>()
6+
}
7+
8+
fn unconstrained_return<T>() -> Result<T, String> {
9+
Err("Hi".to_string())
10+
}
11+
12+
fn foo() {
13+
let a = || {
14+
match unconstrained_return() { //~ ERROR Fallback to `!` may introduce undefined behavior
15+
Ok(x) => x, // `x` has type `_`, which is unconstrained
16+
Err(s) => panic!(s), // … except for unifying with the type of `panic!()`
17+
// so that both `match` arms have the same type.
18+
// Therefore `_` resolves to `!` and we "return" an `Ok(!)` value.
19+
}
20+
};
21+
22+
let cast: &dyn FnOnce() -> _ = &a;
23+
println!("Return type: {:?}", get_type(cast));
24+
}
25+
26+
fn main() {
27+
foo()
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: Fallback to `!` may introduce undefined behavior
2+
--> $DIR/obj_implicit.rs:14:15
3+
|
4+
LL | match unconstrained_return() {
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the type parameter T here was inferred to `!`
8+
--> $DIR/obj_implicit.rs:14:15
9+
|
10+
LL | match unconstrained_return() {
11+
| ^^^^^^^^^^^^^^^^^^^^
12+
note: (type parameter defined here)
13+
--> $DIR/obj_implicit.rs:8:25
14+
|
15+
LL | fn unconstrained_return<T>() -> Result<T, String> {
16+
| ^
17+
note: ... due to this expression evaluating to `!`
18+
--> $DIR/obj_implicit.rs:16:23
19+
|
20+
LL | Err(s) => panic!(s), // … except for unifying with the type of `panic!()`
21+
| ^^^^^^^^^
22+
= note: If you want the `!` type to be used here, add explicit type annotations
23+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
24+
25+
error: aborting due to previous error
26+

0 commit comments

Comments
 (0)