Skip to content

Don't warn on never to any as casts as unreachable #144804

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
| ExprKind::Let(..)
| ExprKind::Loop(..)
| ExprKind::Match(..) => {}
// Do not warn on `as` casts from never to any,
// they are sometimes required to appeal typeck.
ExprKind::Cast(_, _) => {}
// If `expr` is a result of desugaring the try block and is an ok-wrapped
// diverging expression (e.g. it arose from desugaring of `try { return }`),
// we skip issuing a warning because it is autogenerated code.
Expand Down
24 changes: 16 additions & 8 deletions tests/ui/reachable/expr_cast.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
#![allow(unused_variables)]
#![allow(unused_assignments)]
#![allow(dead_code)]
//@ check-pass
//@ edition: 2024
//
// Check that we don't warn on `as` casts of never to any as unreachable.
// While they *are* unreachable, sometimes they are required to appeal typeck.
#![deny(unreachable_code)]
#![feature(never_type, type_ascription)]

fn a() {
// the cast is unreachable:
let x = {return} as !; //~ ERROR unreachable
//~| ERROR non-primitive cast
_ = {return} as u32;
}

fn main() { }
fn b() {
(return) as u32;
}

// example that needs an explicit never-to-any `as` cast
fn example() -> impl Iterator<Item = u8> {
todo!() as std::iter::Empty<_>
}

fn main() {}
24 changes: 0 additions & 24 deletions tests/ui/reachable/expr_cast.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/reachable/unreachable-try-pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn bar(x: Result<!, i32>) -> Result<u32, i32> {
fn foo(x: Result<!, i32>) -> Result<u32, i32> {
let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
//~^ WARN unreachable pattern
//~| WARN unreachable expression
//~| WARN unreachable call
Ok(y)
}

Expand Down
11 changes: 5 additions & 6 deletions tests/ui/reachable/unreachable-try-pattern.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
warning: unreachable expression
--> $DIR/unreachable-try-pattern.rs:19:36
warning: unreachable call
--> $DIR/unreachable-try-pattern.rs:19:33
|
LL | let y = (match x { Ok(n) => Ok(n as u32), Err(e) => Err(e) })?;
| -^^^^^^^
| |
| unreachable expression
| any code following this expression is unreachable
| ^^ - any code following this expression is unreachable
| |
| unreachable call
|
note: the lint level is defined here
--> $DIR/unreachable-try-pattern.rs:3:9
Expand Down
Loading