Skip to content

Commit

Permalink
Fix issue 11051 - Unmatched case in a final switch should throw in bo…
Browse files Browse the repository at this point in the history
…th release and non-release mode (#14841)

* Fix issue 11051

Keep a HALT instruction in a final switch statement if
the function is @safe and -release mode is enabled.

* @System -release test should also be non-zero

* don't suppress test errors

* Remove unreported error line

* Disable core dumps

Co-authored-by: Andrej Mitrovic <[email protected]>
  • Loading branch information
ntrel and AndrejMitrovic authored Jan 24, 2023
1 parent 2e196ae commit 49771d7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dmd/statementsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,7 @@ package (dmd) extern (C++) final class StatementSemanticVisitor : Visitor
}

if (!sc.sw.sdefault &&
(!ss.isFinal || needswitcherror || global.params.useAssert == CHECKENABLE.on))
(!ss.isFinal || needswitcherror || global.params.useAssert == CHECKENABLE.on || sc.func.isSafe))
{
ss.hasNoDefault = 1;

Expand Down
30 changes: 30 additions & 0 deletions compiler/test/runnable/extra-files/test11051.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module test11051;

version (Safe)
{
void main() @safe
{
enum E { A, B }
E e = cast(E)-1;

final switch (e)
{
case E.A: break;
case E.B: break;
}
}
}
else
{
void main()
{
enum E { A, B }
E e = cast(E)-1;

final switch (e)
{
case E.A: break;
case E.B: break;
}
}
}
29 changes: 29 additions & 0 deletions compiler/test/runnable/test_safe_final_switch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

# tests various @safe behavior for final switches in
# -release and non-release builds

src_file=runnable/extra-files/test11051.d

die()
{
echo "test_safe_final_switch.sh error: test #$1 failed"
exit 1
}

# some tests cause a core dump rather than throwing an Error
ulimit -c 0

# returns 1 (failure)
$DMD -run ${src_file} 2> /dev/null && die 1

# returns 1 (failure)
$DMD -release -run ${src_file} 2> /dev/null && die 2

# returns 1 (failure)
$DMD -version=Safe -run ${src_file} 2> /dev/null && die 3

# returns 1 (failure)
$DMD -release -version=Safe -run ${src_file} 2> /dev/null && die 4

exit 0

0 comments on commit 49771d7

Please sign in to comment.