Skip to content

Commit 609bfe8

Browse files
committed
cherry-pick over the tests I wrote for the reachability code
For the most part, the current code performs similarly, although it differs in some particulars. I'll be nice to have these tests for judging future changes, as well.
1 parent a60e27e commit 609bfe8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1023
-0
lines changed

src/test/ui/reachable/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
A variety of tests around reachability. These tests in general check
2+
two things:
3+
4+
- that we get unreachable code warnings in reasonable locations;
5+
- that we permit coercions **into** `!` from expressions which
6+
diverge, where an expression "diverges" if it must execute some
7+
subexpression of type `!`, or it has type `!` itself.

src/test/ui/reachable/expr_add.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(never_type)]
12+
#![allow(unused_variables)]
13+
#![deny(unreachable_code)]
14+
15+
use std::ops;
16+
17+
struct Foo;
18+
19+
impl ops::Add<!> for Foo {
20+
type Output = !;
21+
fn add(self, rhs: !) -> ! {
22+
unimplemented!()
23+
}
24+
}
25+
26+
fn main() {
27+
let x = Foo + return;
28+
}

src/test/ui/reachable/expr_add.stderr

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unreachable expression
2+
--> $DIR/expr_add.rs:27:13
3+
|
4+
27 | let x = Foo + return;
5+
| ^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/expr_add.rs:13:9
9+
|
10+
13 | #![deny(unreachable_code)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

src/test/ui/reachable/expr_again.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(box_syntax)]
12+
#![allow(unused_variables)]
13+
#![deny(unreachable_code)]
14+
15+
fn main() {
16+
let x = loop {
17+
continue;
18+
println!("hi");
19+
};
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: unreachable statement
2+
--> $DIR/expr_again.rs:18:9
3+
|
4+
18 | println!("hi");
5+
| ^^^^^^^^^^^^^^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/expr_again.rs:13:9
9+
|
10+
13 | #![deny(unreachable_code)]
11+
| ^^^^^^^^^^^^^^^^
12+
= note: this error originates in a macro outside of the current crate
13+
14+
error: aborting due to previous error
15+

src/test/ui/reachable/expr_andand.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(unused_variables)]
12+
#![allow(dead_code)]
13+
#![deny(unreachable_code)]
14+
15+
fn foo() {
16+
// No error here.
17+
let x = false && (return);
18+
println!("I am not dead.");
19+
}
20+
21+
fn main() { }

src/test/ui/reachable/expr_andand.stderr

Whitespace-only changes.

src/test/ui/reachable/expr_array.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(unused_variables)]
12+
#![allow(unused_assignments)]
13+
#![allow(dead_code)]
14+
#![deny(unreachable_code)]
15+
#![feature(never_type)]
16+
#![feature(type_ascription)]
17+
18+
fn a() {
19+
// the `22` is unreachable:
20+
let x: [usize; 2] = [return, 22];
21+
}
22+
23+
fn b() {
24+
// the `array is unreachable:
25+
let x: [usize; 2] = [22, return];
26+
}
27+
28+
fn main() { }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: unreachable expression
2+
--> $DIR/expr_array.rs:20:34
3+
|
4+
20 | let x: [usize; 2] = [return, 22];
5+
| ^^
6+
|
7+
note: lint level defined here
8+
--> $DIR/expr_array.rs:14:9
9+
|
10+
14 | #![deny(unreachable_code)]
11+
| ^^^^^^^^^^^^^^^^
12+
13+
error: unreachable expression
14+
--> $DIR/expr_array.rs:25:25
15+
|
16+
25 | let x: [usize; 2] = [22, return];
17+
| ^^^^^^^^^^^^
18+
19+
error: aborting due to 2 previous errors
20+

src/test/ui/reachable/expr_assign.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(unused_variables)]
12+
#![allow(unused_assignments)]
13+
#![allow(dead_code)]
14+
#![deny(unreachable_code)]
15+
#![feature(never_type)]
16+
17+
fn foo() {
18+
// No error here.
19+
let x;
20+
x = return;
21+
}
22+
23+
fn bar() {
24+
use std::ptr;
25+
let p: *mut ! = ptr::null_mut::<!>();
26+
unsafe {
27+
// Here we consider the `return` unreachable because
28+
// "evaluating" the `*p` has type `!`. This is somewhat
29+
// dubious, I suppose.
30+
*p = return;
31+
}
32+
}
33+
34+
fn baz() {
35+
let mut i = 0;
36+
*{return; &mut i} = 22;
37+
}
38+
39+
fn main() { }

0 commit comments

Comments
 (0)