Skip to content

Point at diverging expression causing unreachable lint to trigger #64624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// If there are no arms, that is a diverging match; a special case.
if arms.is_empty() {
self.diverges.set(self.diverges.get() | Diverges::Always);
self.divergence_span.set(expr.span);
return tcx.types.never;
}

Expand Down
1 change: 1 addition & 0 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Any expression that produces a value of type `!` must have diverged
if ty.is_never() {
self.diverges.set(self.diverges.get() | Diverges::Always);
self.divergence_span.set(expr.span);
}

// Record the type, which applies it effects.
Expand Down
7 changes: 6 additions & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ pub struct FnCtxt<'a, 'tcx> {
/// An expression represents dead code if, after checking it,
/// the diverges flag is set to something other than `Maybe`.
diverges: Cell<Diverges>,
divergence_span: Cell<Span>,

/// Whether any child nodes have any type errors.
has_errors: Cell<bool>,
Expand Down Expand Up @@ -2287,6 +2288,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ps: RefCell::new(UnsafetyState::function(hir::Unsafety::Normal,
hir::CRATE_HIR_ID)),
diverges: Cell::new(Diverges::Maybe),
divergence_span: Cell::new(DUMMY_SP),
has_errors: Cell::new(false),
enclosing_breakables: RefCell::new(EnclosingBreakables {
stack: Vec::new(),
Expand Down Expand Up @@ -2317,7 +2319,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);

let msg = format!("unreachable {}", kind);
self.tcx().lint_hir(lint::builtin::UNREACHABLE_CODE, id, span, &msg);
self.tcx().struct_span_lint_hir(lint::builtin::UNREACHABLE_CODE, id, span, &msg)
.span_label(span, &msg)
.span_label(self.divergence_span.get(), "diverging due to this")
.emit();
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/dead-code-ret.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: unreachable statement
--> $DIR/dead-code-ret.rs:7:5
|
LL | return;
| ------ diverging due to this
LL | println!("Paul is dead");
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/dead-code-ret.rs:3:9
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/if-ret.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ warning: unreachable block in `if` expression
--> $DIR/if-ret.rs:6:24
|
LL | fn foo() { if (return) { } }
| ^^^
| -------- ^^^ unreachable block in `if` expression
| |
| diverging due to this
|
= note: `#[warn(unreachable_code)]` on by default

5 changes: 4 additions & 1 deletion src/test/ui/issues/issue-2150.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
error: unreachable statement
--> $DIR/issue-2150.rs:8:5
|
LL | panic!();
| --------- diverging due to this
LL | for x in &v { i += 1; }
| ^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/issue-2150.rs:1:9
|
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error

4 changes: 3 additions & 1 deletion src/test/ui/issues/issue-7246.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: unreachable statement
--> $DIR/issue-7246.rs:7:5
|
LL | return;
| ------ diverging due to this
LL | if *ptr::null() {};
| ^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/issue-7246.rs:1:9
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/lint/lint-attr-non-item-node.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: unreachable statement
--> $DIR/lint-attr-non-item-node.rs:7:9
|
LL | break;
| ----- diverging due to this
LL | "unreachable";
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/lint-attr-non-item-node.rs:4:12
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/liveness/liveness-unused.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
warning: unreachable statement
--> $DIR/liveness-unused.rs:92:9
|
LL | continue;
| -------- diverging due to this
LL | drop(*x as i32);
| ^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/liveness-unused.rs:1:9
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/match/match-no-arms-unreachable-after.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: unreachable statement
--> $DIR/match-no-arms-unreachable-after.rs:8:5
|
LL | match v { }
| ----------- diverging due to this
LL | let x = 2;
| ^^^^^^^^^^
| ^^^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/match-no-arms-unreachable-after.rs:2:9
Expand Down
9 changes: 7 additions & 2 deletions src/test/ui/never-assign-dead-code.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
warning: unreachable statement
--> $DIR/never-assign-dead-code.rs:10:5
|
LL | let x: ! = panic!("aah");
| ------------- diverging due to this
LL | drop(x);
| ^^^^^^^^
| ^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/never-assign-dead-code.rs:5:9
|
LL | #![warn(unused)]
| ^^^^^^
= note: `#[warn(unreachable_code)]` implied by `#[warn(unused)]`
= note: this warning originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

warning: unreachable call
--> $DIR/never-assign-dead-code.rs:10:5
|
LL | drop(x);
| ^^^^
| ^^^^ - diverging due to this
| |
| unreachable call

warning: unused variable: `x`
--> $DIR/never-assign-dead-code.rs:9:9
Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/reachable/expr_add.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error: unreachable expression
--> $DIR/expr_add.rs:17:13
|
LL | let x = Foo + return;
| ^^^^^^^^^^^^
| ^^^^^^------
| | |
| | diverging due to this
| unreachable expression
|
note: lint level defined here
--> $DIR/expr_add.rs:3:9
Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/reachable/expr_again.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: unreachable statement
--> $DIR/expr_again.rs:8:9
|
LL | continue;
| -------- diverging due to this
LL | println!("hi");
| ^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/expr_again.rs:3:9
Expand Down
9 changes: 7 additions & 2 deletions src/test/ui/reachable/expr_array.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error: unreachable expression
--> $DIR/expr_array.rs:9:34
|
LL | let x: [usize; 2] = [return, 22];
| ^^
| ------ ^^ unreachable expression
| |
| diverging due to this
|
note: lint level defined here
--> $DIR/expr_array.rs:4:9
Expand All @@ -14,7 +16,10 @@ error: unreachable expression
--> $DIR/expr_array.rs:14:25
|
LL | let x: [usize; 2] = [22, return];
| ^^^^^^^^^^^^
| ^^^^^------^
| | |
| | diverging due to this
| unreachable expression

error: aborting due to 2 previous errors

13 changes: 10 additions & 3 deletions src/test/ui/reachable/expr_assign.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error: unreachable expression
--> $DIR/expr_assign.rs:10:5
|
LL | x = return;
| ^^^^^^^^^^
| ^^^^------
| | |
| | diverging due to this
| unreachable expression
|
note: lint level defined here
--> $DIR/expr_assign.rs:5:9
Expand All @@ -14,13 +17,17 @@ error: unreachable expression
--> $DIR/expr_assign.rs:20:14
|
LL | *p = return;
| ^^^^^^
| -- ^^^^^^ unreachable expression
| |
| diverging due to this

error: unreachable expression
--> $DIR/expr_assign.rs:26:15
|
LL | *{return; &mut i} = 22;
| ^^^^^^
| ------ ^^^^^^ unreachable expression
| |
| diverging due to this

error: aborting due to 3 previous errors

8 changes: 6 additions & 2 deletions src/test/ui/reachable/expr_block.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: unreachable expression
--> $DIR/expr_block.rs:10:9
|
LL | return;
| ------ diverging due to this
LL | 22
| ^^
| ^^ unreachable expression
|
note: lint level defined here
--> $DIR/expr_block.rs:4:9
Expand All @@ -13,8 +15,10 @@ LL | #![deny(unreachable_code)]
error: unreachable statement
--> $DIR/expr_block.rs:25:9
|
LL | return;
| ------ diverging due to this
LL | println!("foo");
| ^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^ unreachable statement
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Expand Down
5 changes: 4 additions & 1 deletion src/test/ui/reachable/expr_box.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error: unreachable expression
--> $DIR/expr_box.rs:6:13
|
LL | let x = box return;
| ^^^^^^^^^^
| ^^^^------
| | |
| | diverging due to this
| unreachable expression
|
note: lint level defined here
--> $DIR/expr_box.rs:3:9
Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/reachable/expr_call.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error: unreachable expression
--> $DIR/expr_call.rs:13:17
|
LL | foo(return, 22);
| ^^
| ------ ^^ unreachable expression
| |
| diverging due to this
|
note: lint level defined here
--> $DIR/expr_call.rs:5:9
Expand All @@ -14,7 +16,9 @@ error: unreachable call
--> $DIR/expr_call.rs:18:5
|
LL | bar(return);
| ^^^
| ^^^ ------ diverging due to this
| |
| unreachable call

error: aborting due to 2 previous errors

5 changes: 4 additions & 1 deletion src/test/ui/reachable/expr_cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error: unreachable expression
--> $DIR/expr_cast.rs:9:13
|
LL | let x = {return} as !;
| ^^^^^^^^^^^^^
| ^------^^^^^^
| ||
| |diverging due to this
| unreachable expression
|
note: lint level defined here
--> $DIR/expr_cast.rs:4:9
Expand Down
16 changes: 12 additions & 4 deletions src/test/ui/reachable/expr_if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ error: unreachable block in `if` expression
--> $DIR/expr_if.rs:7:17
|
LL | if {return} {
| _________________^
| _________------__^
| | |
| | diverging due to this
LL | | println!("Hello, world!");
LL | | }
| |_____^
| |_____^ unreachable block in `if` expression
|
note: lint level defined here
--> $DIR/expr_if.rs:4:9
Expand All @@ -16,8 +18,14 @@ LL | #![deny(unreachable_code)]
error: unreachable statement
--> $DIR/expr_if.rs:27:5
|
LL | println!("But I am.");
| ^^^^^^^^^^^^^^^^^^^^^^
LL | } else {
| ____________-
LL | | return;
LL | | }
| |_____- diverging due to this
...
LL | println!("But I am.");
| ^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Expand Down
12 changes: 9 additions & 3 deletions src/test/ui/reachable/expr_loop.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
error: unreachable statement
--> $DIR/expr_loop.rs:8:5
|
LL | loop { return; }
| ---------------- diverging due to this
LL | println!("I am dead.");
| ^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
|
note: lint level defined here
--> $DIR/expr_loop.rs:4:9
Expand All @@ -14,16 +16,20 @@ LL | #![deny(unreachable_code)]
error: unreachable statement
--> $DIR/expr_loop.rs:21:5
|
LL | loop { return; }
| ---------------- diverging due to this
LL | println!("I am dead.");
| ^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: unreachable statement
--> $DIR/expr_loop.rs:32:5
|
LL | loop { 'middle: loop { loop { break 'middle; } } }
| -------------------------------------------------- diverging due to this
LL | println!("I am dead.");
| ^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^ unreachable statement
|
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Expand Down
Loading