Skip to content

Commit

Permalink
Remove carveouts.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Apr 7, 2024
1 parent 827be96 commit bfbc9e8
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 59 deletions.
54 changes: 2 additions & 52 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// diverging expression (e.g. it arose from desugaring of `try { return }`),
// we skip issuing a warning because it is autogenerated code.
ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {}
ExprKind::Call(callee, _) => {
let emit_warning = if let ExprKind::Path(ref qpath) = callee.kind {
// Do not emit a warning for a call to a constructor.
let res = self.typeck_results.borrow().qpath_res(qpath, callee.hir_id);
!matches!(res, Res::Def(DefKind::Ctor(..), _))
} else {
true
};
if emit_warning {
self.warn_if_unreachable(expr.hir_id, callee.span, "call")
}
}
ExprKind::Call(callee, _) => self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
ExprKind::MethodCall(segment, ..) => {
self.warn_if_unreachable(expr.hir_id, segment.ident.span, "call")
}
Expand All @@ -272,7 +261,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if ty.is_never() {
// Any expression that produces a value of type `!` must have diverged.
self.diverges.set(Diverges::Always(DivergeReason::Other, expr.span));
} else if expr_may_be_uninhabited(expr) && self.ty_is_uninhabited(ty) {
} else if self.ty_is_uninhabited(ty) {
// This expression produces a value of uninhabited type.
// This means it has diverged somehow.
self.diverges
Expand Down Expand Up @@ -3481,42 +3470,3 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.tcx.types.usize
}
}

fn expr_may_be_uninhabited(expr: &hir::Expr<'_>) -> bool {
match expr.kind {
ExprKind::Call(..)
| ExprKind::MethodCall(..)
| ExprKind::Cast(..)
| ExprKind::Unary(hir::UnOp::Deref, _)
| ExprKind::Field(..)
| ExprKind::Path(..)
| ExprKind::Struct(..) => true,
ExprKind::ConstBlock(..)
| ExprKind::Array(..)
| ExprKind::Tup(..)
| ExprKind::Binary(..)
| ExprKind::Unary(hir::UnOp::Neg | hir::UnOp::Not, _)
| ExprKind::Lit(..)
| ExprKind::Type(..)
| ExprKind::DropTemps(..)
| ExprKind::OffsetOf(..)
| ExprKind::Let(..)
| ExprKind::If(..)
| ExprKind::Loop(..)
| ExprKind::Match(..)
| ExprKind::Closure(..)
| ExprKind::Block(..)
| ExprKind::Assign(..)
| ExprKind::AssignOp(..)
| ExprKind::Index(..)
| ExprKind::AddrOf(..)
| ExprKind::Break(..)
| ExprKind::Continue(..)
| ExprKind::Ret(..)
| ExprKind::Become(..)
| ExprKind::InlineAsm(..)
| ExprKind::Repeat(..)
| ExprKind::Yield(..)
| ExprKind::Err(_) => false,
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub struct FnCtxt<'a, 'tcx> {
/// eventually).
pub(super) param_env: ty::ParamEnv<'tcx>,

/// The module in which the current function is defined. This
/// is used to compute type inhabitedness, which accounts for
/// visibility information.
pub(super) parent_module: DefId,

/// If `Some`, this stores coercion information for returned
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ run-pass

#![allow(unreachable_code)]
#![allow(dead_code)]

enum Empty { }
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/enum-discriminant/issue-46519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum SystemFont {}

impl FontLanguageOverride {
fn system_font(f: SystemFont) -> Self {
FontLanguageOverride::System(f)
FontLanguageOverride::System(f) //~ unreachable call
}
}

Expand Down
10 changes: 9 additions & 1 deletion tests/ui/enum-discriminant/issue-46519.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@ LL | FontLanguageOverride::system_font(SystemFont::new());
|
= note: `#[warn(unreachable_code)]` on by default

warning: 1 warning emitted
warning: unreachable call
--> $DIR/issue-46519.rs:22:9
|
LL | FontLanguageOverride::System(f)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - this expression has type `SystemFont`, which is uninhabited
| |
| unreachable call

warning: 2 warnings emitted

3 changes: 2 additions & 1 deletion tests/ui/reachable/type-dependent-ctor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Verify that we do not warn on type-dependent constructors (`Self::A` below).
//@ check-pass
#![deny(unreachable_code)]

enum Void {}
Expand All @@ -11,10 +10,12 @@ enum Foo {
impl Foo {
fn wrap(x: Void) -> Self {
Self::A(x)
//~^ ERROR unreachable call
}

fn make() -> Self {
Self::A(produce())
//~^ ERROR unreachable call
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/ui/reachable/type-dependent-ctor.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error: unreachable call
--> $DIR/type-dependent-ctor.rs:12:9
|
LL | Self::A(x)
| ^^^^^^^ - this expression has type `Void`, which is uninhabited
| |
| unreachable call
|
note: the lint level is defined here
--> $DIR/type-dependent-ctor.rs:2:9
|
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^

error: unreachable call
--> $DIR/type-dependent-ctor.rs:17:9
|
LL | Self::A(produce())
| ^^^^^^^ --------- this expression has type `Void`, which is uninhabited
| |
| unreachable call

error: aborting due to 2 previous errors

1 change: 1 addition & 0 deletions tests/ui/reachable/unreachable-try-pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fn qux(x: Result<u32, Void>) -> Result<u32, i32> {
fn vom(x: Result<u32, Void>) -> Result<u32, i32> {
let y = (match x { Ok(n) => Ok(n), Err(e) => Err(e) })?;
//~^ WARN unreachable pattern
//~| WARN unreachable call
Ok(y)
}

Expand Down
10 changes: 9 additions & 1 deletion tests/ui/reachable/unreachable-try-pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ note: the lint level is defined here
LL | #![warn(unreachable_code)]
| ^^^^^^^^^^^^^^^^

warning: unreachable call
--> $DIR/unreachable-try-pattern.rs:30:50
|
LL | let y = (match x { Ok(n) => Ok(n), Err(e) => Err(e) })?;
| ^^^ - this expression has type `Void`, which is uninhabited
| |
| unreachable call

warning: unreachable pattern
--> $DIR/unreachable-try-pattern.rs:19:24
|
Expand All @@ -31,5 +39,5 @@ warning: unreachable pattern
LL | let y = (match x { Ok(n) => Ok(n), Err(e) => Err(e) })?;
| ^^^^^^

warning: 3 warnings emitted
warning: 4 warnings emitted

1 change: 1 addition & 0 deletions tests/ui/try-block/try-block-unreachable-code-lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn test_try_block_after_divergent_stmt() {
fn test_wrapped_divergent_expr() {
let _: Result<u32, ()> = {
Err(return)
//~^ WARN unreachable call
};
}

Expand Down
12 changes: 10 additions & 2 deletions tests/ui/try-block/try-block-unreachable-code-lint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ note: the lint level is defined here
LL | #![warn(unreachable_code)]
| ^^^^^^^^^^^^^^^^

warning: unreachable call
--> $DIR/try-block-unreachable-code-lint.rs:52:9
|
LL | Err(return)
| ^^^ ------ any code following this expression is unreachable
| |
| unreachable call

warning: unreachable expression
--> $DIR/try-block-unreachable-code-lint.rs:62:9
--> $DIR/try-block-unreachable-code-lint.rs:63:9
|
LL | / loop {
LL | | err()?;
Expand All @@ -28,5 +36,5 @@ LL |
LL | 42
| ^^ unreachable expression

warning: 2 warnings emitted
warning: 3 warnings emitted

0 comments on commit bfbc9e8

Please sign in to comment.