In this test, the return value is both true and false at the same time. --- bool schrodingersCat() @safe { union Box { bool b; int state; } Box u; u.state = 42; return u.b; } void main() @safe { import std.stdio; bool b = schrodingersCat(); if (b) writeln("alive"); if (!b) writeln("dead"); }
The master branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>: https://gcc.gnu.org/g:5c9b7408dc578cb2ae142a5c1b724c183497bdb2 commit r12-2597-g5c9b7408dc578cb2ae142a5c1b724c183497bdb2 Author: Iain Buclaw <ibuclaw@gdcproject.org> Date: Mon Jul 26 19:28:02 2021 +0200 d: Ensure casting from bool results in either 0 or 1 (PR96435) If casting from bool, the result is either 0 or 1, any other value violates @safe code, so enforce that it is never invalid. PR d/96435 gcc/d/ChangeLog: * d-convert.cc (convert_for_rvalue): New function. * d-tree.h (convert_for_rvalue): Declare. * expr.cc (ExprVisitor::visit (CastExp *)): Use convert_for_rvalue. (build_return_dtor): Likewise. gcc/testsuite/ChangeLog: * gdc.dg/torture/pr96435.d: New test.
The releases/gcc-11 branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>: https://gcc.gnu.org/g:22589586940439fb5878180a7c586ea753d179f6 commit r11-8816-g22589586940439fb5878180a7c586ea753d179f6 Author: Iain Buclaw <ibuclaw@gdcproject.org> Date: Mon Jul 26 19:28:02 2021 +0200 d: Ensure casting from bool results in either 0 or 1 (PR96435) If casting from bool, the result is either 0 or 1, any other value violates @safe code, so enforce that it is never invalid. PR d/96435 gcc/d/ChangeLog: * d-convert.cc (convert_for_rvalue): New function. * d-tree.h (convert_for_rvalue): Declare. * expr.cc (ExprVisitor::visit (CastExp *)): Use convert_for_rvalue. (build_return_dtor): Likewise. gcc/testsuite/ChangeLog: * gdc.dg/torture/pr96435.d: New test. (cherry picked from commit 5c9b7408dc578cb2ae142a5c1b724c183497bdb2)
The releases/gcc-10 branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>: https://gcc.gnu.org/g:10490b7a453159f9b602b7f273f0aa6b4caebca7 commit r10-10012-g10490b7a453159f9b602b7f273f0aa6b4caebca7 Author: Iain Buclaw <ibuclaw@gdcproject.org> Date: Mon Jul 26 19:28:02 2021 +0200 d: Ensure casting from bool results in either 0 or 1 (PR96435) If casting from bool, the result is either 0 or 1, any other value violates @safe code, so enforce that it is never invalid. PR d/96435 gcc/d/ChangeLog: * d-convert.cc (convert_for_rvalue): New function. * d-tree.h (convert_for_rvalue): Declare. * expr.cc (ExprVisitor::visit (CastExp *)): Use convert_for_rvalue. (build_return_dtor): Likewise. gcc/testsuite/ChangeLog: * gdc.dg/pr96435.d: New test. (cherry picked from commit 5c9b7408dc578cb2ae142a5c1b724c183497bdb2)
The releases/gcc-9 branch has been updated by Iain Buclaw <ibuclaw@gcc.gnu.org>: https://gcc.gnu.org/g:c848c0ec7fab0c0a220683ad234c8842939941cf commit r9-9653-gc848c0ec7fab0c0a220683ad234c8842939941cf Author: Iain Buclaw <ibuclaw@gdcproject.org> Date: Mon Jul 26 19:28:02 2021 +0200 d: Ensure casting from bool results in either 0 or 1 (PR96435) If casting from bool, the result is either 0 or 1, any other value violates @safe code, so enforce that it is never invalid. PR d/96435 gcc/d/ChangeLog: * d-convert.cc (convert_for_rvalue): New function. * d-tree.h (convert_for_rvalue): Declare. * expr.cc (ExprVisitor::visit (CastExp *)): Use convert_for_rvalue. (build_return_dtor): Likewise. gcc/testsuite/ChangeLog: * gdc.dg/pr96435.d: New test. (cherry picked from commit 5c9b7408dc578cb2ae142a5c1b724c183497bdb2)
Fix committed.