-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Warn about unreachable code following an expression with an uninhabited type #85556
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// A slight variation of issue-85071.rs. Here, a method is called instead | ||
// of a function, and the warning is about an unreachable definition | ||
// instead of an unreachable expression. | ||
|
||
// check-pass | ||
|
||
#![warn(unused_variables,unreachable_code)] | ||
|
||
enum Foo {} | ||
|
||
struct S; | ||
impl S { | ||
fn f(&self) -> Foo {todo!()} | ||
} | ||
|
||
fn main() { | ||
let s = S; | ||
let x = s.f(); | ||
//~^ WARNING: unused variable: `x` | ||
let _y = x; | ||
//~^ WARNING: unreachable definition | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
warning: unreachable definition | ||
--> $DIR/issue-85071-2.rs:20:9 | ||
| | ||
LL | let x = s.f(); | ||
| ----- any code following this expression is unreachable | ||
LL | | ||
LL | let _y = x; | ||
| ^^ unreachable definition | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/issue-85071-2.rs:7:26 | ||
| | ||
LL | #![warn(unused_variables,unreachable_code)] | ||
| ^^^^^^^^^^^^^^^^ | ||
note: this expression has type `Foo`, which is uninhabited | ||
--> $DIR/issue-85071-2.rs:18:13 | ||
| | ||
LL | let x = s.f(); | ||
| ^^^^^ | ||
|
||
warning: unused variable: `x` | ||
--> $DIR/issue-85071-2.rs:18:9 | ||
| | ||
LL | let x = s.f(); | ||
| ^ help: if this is intentional, prefix it with an underscore: `_x` | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/issue-85071-2.rs:7:9 | ||
| | ||
LL | #![warn(unused_variables,unreachable_code)] | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
warning: 2 warnings emitted | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Checks that an unreachable code warning is emitted when an expression is | ||
// preceded by an expression with an uninhabited type. Previously, the | ||
// variable liveness analysis was "smarter" than the reachability analysis | ||
// in this regard, which led to confusing "unused variable" warnings | ||
// without an accompanying explanatory "unreachable expression" warning. | ||
|
||
// check-pass | ||
|
||
#![warn(unused_variables,unreachable_code)] | ||
|
||
enum Foo {} | ||
fn f() -> Foo {todo!()} | ||
|
||
fn main() { | ||
let x = f(); | ||
//~^ WARNING: unused variable: `x` | ||
let _ = x; | ||
//~^ WARNING: unreachable expression | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
warning: unreachable expression | ||
--> $DIR/issue-85071.rs:17:13 | ||
| | ||
LL | let x = f(); | ||
| --- any code following this expression is unreachable | ||
LL | | ||
LL | let _ = x; | ||
| ^ unreachable expression | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/issue-85071.rs:9:26 | ||
| | ||
LL | #![warn(unused_variables,unreachable_code)] | ||
| ^^^^^^^^^^^^^^^^ | ||
note: this expression has type `Foo`, which is uninhabited | ||
--> $DIR/issue-85071.rs:15:13 | ||
| | ||
LL | let x = f(); | ||
| ^^^ | ||
|
||
warning: unused variable: `x` | ||
--> $DIR/issue-85071.rs:15:9 | ||
| | ||
LL | let x = f(); | ||
| ^ help: if this is intentional, prefix it with an underscore: `_x` | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/issue-85071.rs:9:9 | ||
| | ||
LL | #![warn(unused_variables,unreachable_code)] | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
warning: 2 warnings emitted | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason not to remove the current, existing, crude check in favor of this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because type inference makes use of the crude divergence information available, e.g. here:
rust/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs
Lines 654 to 660 in 5dc8789
The crude check is here:
rust/compiler/rustc_typeck/src/check/expr.rs
Lines 202 to 205 in 5dc8789
Unfortunately, we can't replace it with
tcx.is_ty_uninhabited_from()
because type information has not been committed totcx
yet at this point.