Bug 96435 - d: Returning boolean in a union violates @safe code
Summary: d: Returning boolean in a union violates @safe code
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: d (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: ---
Assignee: Iain Buclaw
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-08-03 15:49 UTC by Iain Buclaw
Modified: 2021-07-29 15:27 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Iain Buclaw 2020-08-03 15:49:42 UTC
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");
}
Comment 1 GCC Commits 2021-07-29 15:12:55 UTC
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.
Comment 2 GCC Commits 2021-07-29 15:20:04 UTC
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)
Comment 3 GCC Commits 2021-07-29 15:21:08 UTC
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)
Comment 4 GCC Commits 2021-07-29 15:22:07 UTC
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)
Comment 5 Iain Buclaw 2021-07-29 15:27:39 UTC
Fix committed.