Bug 22937 – identity comparison of strings / struct literals not constant folded

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-03-25T21:44:59Z
Last change time
2024-12-13T19:21:44Z
Assigned to
No Owner
Creator
Dennis
Moved to GitHub: dmd#20074 →

Comments

Comment #0 by dkorpel — 2022-03-25T21:44:59Z
I encountered this while fixing issue 3632: https://github.com/dlang/dmd/pull/13780 ``` struct S { int f; } void main() { assert(S(0) is S(0)); assert("a" is "a"); } ``` They are not constant folded to `true`, as evidenced by inspecting with -vcg-ast or -vasm. `visitIdentity` in `optimize.d` has this condition: ``` if ((e.e1.isConst() && e.e2.isConst()) || (e.e1.op == EXP.null_ && e.e2.op == EXP.null_)) ``` isConst checks for number literals, but not strings or struct literals.
Comment #1 by maxhaton — 2022-05-21T01:08:57Z
Is it a bug though? The frontend should be doing less constant folding not more.
Comment #2 by dkorpel — 2022-05-21T09:22:57Z
(In reply to mhh from comment #1) > The frontend should be doing less constant folding not more. Which operator/type combinations should be constant folded, and why? To me, it would make sense to constant fold all built in operators on literal values. I don't expect the compiler to ever generate a runtime addition for '3 + 4', even in debug builds.
Comment #3 by robert.schadek — 2024-12-13T19:21:44Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20074 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB