Skip to content

Commit 4d340b6

Browse files
committed
Add better unreachable code warnings when a panic is before a use expression.
With previous changes, a test was broken when a panic was right before a use expression. The test was like that: ```gleam pub fn wibble(_) { 1 } pub fn main() { panic use <- wibble 1 } ``` This was the case because, before these changes, `UntypedExpr::Var` has not special call path in `do_infer_call` function. `infer` and `infer_or_error` was called has expected and a warning was emitted by `warn_for_unreachable_code` in case of a previous panic expression. Actually, `UntypedExpr::FieldAccess` has a special call path in `do_infer_call` and compiler does not warned about a program like that: ```gleam // Define pub fn wibble(_) { 1 } import mylib/mymod pub fn main() { panic use <- mymod.wibble 1 } ``` With this change, use expression are always warned after a panic expression.
1 parent 29ec32d commit 4d340b6

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

compiler-core/src/type_/expression.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,14 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
852852
// We use `stacker` to prevent overflowing the stack when many `use`
853853
// expressions are chained. See https://github.com/gleam-lang/gleam/issues/4287
854854
let infer_call = || {
855+
// We need this in the case where `call.function` has a special call path depending
856+
// on the type such as `UntypedExpr::Var`. In these cases, `infer_call` does not call
857+
// `infer_or_error`. `infer_or_error` is responsible for registering warnings about
858+
// unreachable code and thus, warnings about unreachable code are not registered.
859+
if self.previous_panics {
860+
self.warn_for_unreachable_code(call_location, PanicPosition::PreviousExpression);
861+
}
862+
855863
self.infer_call(
856864
*call.function,
857865
call.arguments,

compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__warnings__unreachable_use_after_panic.snap

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ expression: "\n pub fn wibble(_) { 1 }\n pub fn main() {\n
1414

1515
----- WARNING
1616
warning: Unreachable code
17-
┌─ /src/warning/wrn.gleam:5:20
18-
19-
5use <- wibble
20-
^^^^^^
17+
┌─ /src/warning/wrn.gleam:5:13
18+
19+
5 │ ╭ use <- wibble
20+
6 │ │ 1
21+
│ ╰─────────────^
2122

2223
This code is unreachable because it comes after a `panic`.

0 commit comments

Comments
 (0)