From 89ce2c78194585ff05022474aaf54f776eed5963 Mon Sep 17 00:00:00 2001 From: bscuron Date: Mon, 2 Mar 2026 22:32:50 -0500 Subject: [PATCH 1/6] Normalize bool bitfield to 0 or 1 during logical not optimizations --- compiler/src/dmd/backend/cgelem.d | 6 +++--- compiler/test/runnable/issue22663.d | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 compiler/test/runnable/issue22663.d diff --git a/compiler/src/dmd/backend/cgelem.d b/compiler/src/dmd/backend/cgelem.d index 56768622749f..47e23d2d8f37 100644 --- a/compiler/src/dmd/backend/cgelem.d +++ b/compiler/src/dmd/backend/cgelem.d @@ -2037,9 +2037,9 @@ private elem* elnot(elem* e, Goal goal) } else if (tybasic(e1.Ety) == TYbool && tysize(e.Ety) == 1) { - // !e1 => (e1 ^ 1) - e.Eoper = OPxor; - e.E2 = el_long(e1.Ety,1); + // !e1 => (e1 & 1) ^ 1 + elem* normalized = el_bin(OPand, e1.Ety, e1, el_long(e1.Ety, 1)); + e = el_bin(OPxor, e1.Ety, normalized, el_long(e1.Ety, 1)); e = optelem(e, goal); } else diff --git a/compiler/test/runnable/issue22663.d b/compiler/test/runnable/issue22663.d new file mode 100644 index 000000000000..537c3cf05112 --- /dev/null +++ b/compiler/test/runnable/issue22663.d @@ -0,0 +1,22 @@ +// https://issues.dlang.org/show_bug.cgi?id=22663 + +void main() +{ + struct S + { + bool b: 1; + } + S s = S(true); + + assert(s.b == true); + assert(s.b == 1); + + assert(!s.b == false); + assert(!s.b == 0); + + assert(s.b ? true : false); + assert(!s.b ? false: true); + + if (s.b) assert(1); + if (!s.b) assert(0); +} From 17c3e73dadadd6a6ce0f34077a5006285dca66ec Mon Sep 17 00:00:00 2001 From: bscuron Date: Mon, 2 Mar 2026 22:56:21 -0500 Subject: [PATCH 2/6] Update issue link in test --- compiler/test/runnable/issue22663.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/test/runnable/issue22663.d b/compiler/test/runnable/issue22663.d index 537c3cf05112..12248f61f6d9 100644 --- a/compiler/test/runnable/issue22663.d +++ b/compiler/test/runnable/issue22663.d @@ -1,4 +1,4 @@ -// https://issues.dlang.org/show_bug.cgi?id=22663 +// https://github.com/dlang/dmd/issues/22663 void main() { From dc9a7d4f5553e849bd3d674d5ca938b17b272f3f Mon Sep 17 00:00:00 2001 From: bscuron Date: Wed, 4 Mar 2026 19:59:09 -0500 Subject: [PATCH 3/6] Use OPshr instead of OPashr for bool bitfield sign extension --- compiler/src/dmd/backend/cgelem.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dmd/backend/cgelem.d b/compiler/src/dmd/backend/cgelem.d index 47e23d2d8f37..f25b405f1ba0 100644 --- a/compiler/src/dmd/backend/cgelem.d +++ b/compiler/src/dmd/backend/cgelem.d @@ -3296,7 +3296,7 @@ private elem* elbit(elem* e, Goal goal) e2.Ety = e.Ety; OPER shift = OPshr; - if (!tyuns(tym1)) + if (!tyuns(tym1) && tybasic(tym1) != TYbool) shift = OPashr; e.E1 = el_bin(shift,tym1, el_bin(OPshl,tym1,e.E1,el_long(TYint,c)), From 91e80ef1afc0b240c45b71da07fae471b40e0fcf Mon Sep 17 00:00:00 2001 From: bscuron Date: Wed, 4 Mar 2026 20:02:34 -0500 Subject: [PATCH 4/6] Revert "Update issue link in test" This reverts commit 17c3e73dadadd6a6ce0f34077a5006285dca66ec. --- compiler/test/runnable/issue22663.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/test/runnable/issue22663.d b/compiler/test/runnable/issue22663.d index 12248f61f6d9..537c3cf05112 100644 --- a/compiler/test/runnable/issue22663.d +++ b/compiler/test/runnable/issue22663.d @@ -1,4 +1,4 @@ -// https://github.com/dlang/dmd/issues/22663 +// https://issues.dlang.org/show_bug.cgi?id=22663 void main() { From a6c4b223ad539724f0e13810035c89a46fbc6829 Mon Sep 17 00:00:00 2001 From: bscuron Date: Wed, 4 Mar 2026 20:02:43 -0500 Subject: [PATCH 5/6] Revert "Normalize bool bitfield to 0 or 1 during logical not optimizations" This reverts commit 89ce2c78194585ff05022474aaf54f776eed5963. --- compiler/src/dmd/backend/cgelem.d | 6 +++--- compiler/test/runnable/issue22663.d | 22 ---------------------- 2 files changed, 3 insertions(+), 25 deletions(-) delete mode 100644 compiler/test/runnable/issue22663.d diff --git a/compiler/src/dmd/backend/cgelem.d b/compiler/src/dmd/backend/cgelem.d index f25b405f1ba0..a670e27f0e44 100644 --- a/compiler/src/dmd/backend/cgelem.d +++ b/compiler/src/dmd/backend/cgelem.d @@ -2037,9 +2037,9 @@ private elem* elnot(elem* e, Goal goal) } else if (tybasic(e1.Ety) == TYbool && tysize(e.Ety) == 1) { - // !e1 => (e1 & 1) ^ 1 - elem* normalized = el_bin(OPand, e1.Ety, e1, el_long(e1.Ety, 1)); - e = el_bin(OPxor, e1.Ety, normalized, el_long(e1.Ety, 1)); + // !e1 => (e1 ^ 1) + e.Eoper = OPxor; + e.E2 = el_long(e1.Ety,1); e = optelem(e, goal); } else diff --git a/compiler/test/runnable/issue22663.d b/compiler/test/runnable/issue22663.d deleted file mode 100644 index 537c3cf05112..000000000000 --- a/compiler/test/runnable/issue22663.d +++ /dev/null @@ -1,22 +0,0 @@ -// https://issues.dlang.org/show_bug.cgi?id=22663 - -void main() -{ - struct S - { - bool b: 1; - } - S s = S(true); - - assert(s.b == true); - assert(s.b == 1); - - assert(!s.b == false); - assert(!s.b == 0); - - assert(s.b ? true : false); - assert(!s.b ? false: true); - - if (s.b) assert(1); - if (!s.b) assert(0); -} From 2b445a18da277a8a29ee647a34ee2b3384fd43b2 Mon Sep 17 00:00:00 2001 From: bscuron Date: Thu, 5 Mar 2026 18:37:00 -0500 Subject: [PATCH 6/6] Add test --- compiler/test/runnable/issue22663.d | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 compiler/test/runnable/issue22663.d diff --git a/compiler/test/runnable/issue22663.d b/compiler/test/runnable/issue22663.d new file mode 100644 index 000000000000..12248f61f6d9 --- /dev/null +++ b/compiler/test/runnable/issue22663.d @@ -0,0 +1,22 @@ +// https://github.com/dlang/dmd/issues/22663 + +void main() +{ + struct S + { + bool b: 1; + } + S s = S(true); + + assert(s.b == true); + assert(s.b == 1); + + assert(!s.b == false); + assert(!s.b == 0); + + assert(s.b ? true : false); + assert(!s.b ? false: true); + + if (s.b) assert(1); + if (!s.b) assert(0); +}