diff options
author | Iain Buclaw <ibuclaw@gdcproject.org> | 2021-07-26 19:28:02 +0200 |
---|---|---|
committer | Iain Buclaw <ibuclaw@gdcproject.org> | 2021-07-29 16:16:19 +0200 |
commit | 5c9b7408dc578cb2ae142a5c1b724c183497bdb2 (patch) | |
tree | 9d66585f4b9e8f11d44c05275ea7016577471721 /gcc/d/expr.cc | |
parent | 75f2e3f6cbbb79421b12e399498e9c14241359e7 (diff) | |
download | gcc-5c9b7408dc578cb2ae142a5c1b724c183497bdb2.zip gcc-5c9b7408dc578cb2ae142a5c1b724c183497bdb2.tar.gz gcc-5c9b7408dc578cb2ae142a5c1b724c183497bdb2.tar.bz2 |
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.
Diffstat (limited to 'gcc/d/expr.cc')
-rw-r--r-- | gcc/d/expr.cc | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/gcc/d/expr.cc b/gcc/d/expr.cc index b78778e..99ca958 100644 --- a/gcc/d/expr.cc +++ b/gcc/d/expr.cc @@ -1491,7 +1491,7 @@ public: if (tbtype->ty == Tvoid) this->result_ = build_nop (build_ctype (tbtype), result); else - this->result_ = convert_expr (result, ebtype, tbtype); + this->result_ = convert_for_rvalue (result, ebtype, tbtype); } /* Build a delete expression. */ @@ -3169,11 +3169,14 @@ build_return_dtor (Expression *e, Type *type, TypeFunction *tf) tree result = build_expr (e); /* Convert for initializing the DECL_RESULT. */ - result = convert_expr (result, e->type, type); - - /* If we are returning a reference, take the address. */ if (tf->isref) - result = build_address (result); + { + /* If we are returning a reference, take the address. */ + result = convert_expr (result, e->type, type); + result = build_address (result); + } + else + result = convert_for_rvalue (result, e->type, type); /* The decl to store the return expression. */ tree decl = DECL_RESULT (cfun->decl); |