Bug 24566 – condition that starts with runtime value and uses compile time array does not short circuit

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-05-25T03:55:36Z
Last change time
2024-06-16T07:36:53Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Steven Schveighoffer
See also
https://issues.dlang.org/show_bug.cgi?id=22646

Comments

Comment #0 by schveiguy — 2024-05-25T03:55:36Z
If I mix a runtime and compile time expression in a condition, short circuiting doesn't prevent a compile-time bounds check for constant folded arrays. ```d enum a = true; bool b = true; enum str = "a"; if(a && str.length > 1 && str[1] == 'a') {} // ok if(b && str.length > 1 && str[1] == 'a') {} // compiler error if(!b && str.length > 1 && str[1] == 'a') {} // compiler error if(str.length > 1 && b && str[1] == 'a') {} // ok ``` The error is: Error: string index 1 is out of bounds [0 .. 1] If the runtime condition is not first, then it compiles. Note that even if the runtime condition is false and should short circuit the whole thing, the compiler still errors. As far as I can tell, this has always been the case, so not a regression. Though a related issue might be issue 22646.
Comment #1 by dlang-bot — 2024-06-10T07:41:31Z
@tgehr updated dlang/dmd pull request #16574 "fix Issue 24566 - condition that starts with runtime value and uses compile time array does not short circuit" fixing this issue: - Fix bugzilla issue 24566 - condition that starts with runtime value and uses compile time array does not short circuit https://github.com/dlang/dmd/pull/16574
Comment #2 by dlang-bot — 2024-06-10T08:21:46Z
dlang/dmd pull request #16574 "Fix bugzilla issue 24566 - condition that starts with runtime value and uses compile time array does not short circuit" was merged into stable: - 057170f9cef94eb94fa4ccabddd84540051c4283 by Timon Gehr: Fix bugzilla issue 24566 - condition that starts with runtime value and uses compile time array does not short circuit https://github.com/dlang/dmd/pull/16574
Comment #3 by dlang-bot — 2024-06-16T07:36:53Z
dlang/dmd pull request #16589 "merge stable" was merged into master: - d92734bdac94e09538911838051f8edb54b25185 by Timon Gehr: Fix bugzilla issue 24566 - condition that starts with runtime value and uses compile time array does not short circuit https://github.com/dlang/dmd/pull/16589