Bug 12486 – Function returning struct isn't called if `enum` of its result is accessed

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-03-28T05:39:45Z
Last change time
2018-04-08T19:57:35Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Denis Shelomovskii
See also
https://issues.dlang.org/show_bug.cgi?id=18560

Comments

Comment #0 by verylonglogin.reg — 2014-03-28T05:39:45Z
This code should run fine: --- struct S { enum a = 1; } // or `const` but not for all types S f(ref int i) { ++i; return S(); } void main() { int i = 2; assert(f(i).a == 1); assert(i == 3); // fails, i = 2 } --- This affects infinite ranges processing e.g. `canFind(repeat(1), 2)` always returns `true` instead of infinite looping.
Comment #1 by yebblies — 2014-07-30T15:04:43Z
I guess this is valid. It works correctly when a is a static var or a static function.
Comment #2 by ag0aep6g — 2018-03-09T21:07:31Z
*** Issue 18562 has been marked as a duplicate of this issue. ***
Comment #3 by ag0aep6g — 2018-03-09T21:24:25Z
Very similar case: ---- struct S {} S f(ref int i) { ++i; return S(); } void main() { int i = 2; assert(f(i) == S()); assert(i == 3); // fails, i = 2 } ---- With the enum, the struct doesn't have to be empty, but here it does.
Comment #4 by default_357-line — 2018-03-13T15:31:25Z
Posted a pull request that's awaiting review: https://github.com/dlang/dmd/pull/8013 I'm not really happy with it, but I don't know a better way to write it.
Comment #5 by github-bugzilla — 2018-04-08T18:59:15Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/0a6d7c437d6ab1a919e35d383516ab3dc6f2bb49 Add test from bug #12486 https://github.com/dlang/dmd/commit/3cf5d36578ff612654fddb8f0bb679257e5e4f3f Merge pull request #8013 from FeepingCreature/fix/12486-dont-discard-expression-on-enum-access fix Issue 12486 - Function returning struct isn't called if `enum` of its result is accessed merged-on-behalf-of: Iain Buclaw <[email protected]>
Comment #6 by ag0aep6g — 2018-04-08T19:54:06Z
The code from comment 3 still fails. Moved to new issue 18746.
Comment #7 by ag0aep6g — 2018-04-08T19:57:35Z
*** Issue 18560 has been marked as a duplicate of this issue. ***