Bug 13336 – auto ref return deduced to be ref despite return value coercion

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-08-19T21:31:00Z
Last change time
2015-02-18T03:40:26Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
peter.alexander.au

Comments

Comment #0 by peter.alexander.au — 2014-08-19T21:31:11Z
------------------------ int sx; double sy; auto ref foo() { if (1) return sx; return sy; } ------------------------ foo is determined to return a 'ref double', even though sx is not a double. It is cast to a double and a ref to the temporary is returned. Expected behaviour: When common-type coercions are involved, auto ref should deduce to be non-ref otherwise the code becomes unsafe and surprising. The spec doesn't say anything about this, it just says "The lexically first ReturnStatement determines the ref-ness of a function".
Comment #1 by peter.alexander.au — 2014-08-19T21:59:29Z
You can break the type system using this: class Animal {} class Cat : Animal {} class Dog : Animal {} Animal animal; Cat cat; auto ref choose() { return cat; return animal; } void main() { import std.stdio; choose() = new Dog(); writeln("cat is a ", typeid(cat)); } Prints "cat is a Dog"
Comment #2 by k.hara.pg — 2014-09-12T17:09:02Z
Comment #3 by bugzilla — 2014-09-12T22:10:07Z
Note that Kenji's pull request implements the "expected behavior", so the spec needs updating.
Comment #4 by github-bugzilla — 2014-09-12T22:10:24Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7e7e4814799b145c518ccc77191b2c11cda59d18 fix Issue 13336 - auto ref return deduced to be ref despite return value coercion https://github.com/D-Programming-Language/dmd/commit/d7b38383f4b19c211e1210bc00091788d2d74887 Merge pull request #3979 from 9rnsr/fix13336 Issue 13336 - auto ref return deduced to be ref despite return value coercion
Comment #5 by k.hara.pg — 2014-09-13T04:47:44Z
Comment #6 by github-bugzilla — 2014-11-27T08:18:59Z
Commits pushed to master at https://github.com/D-Programming-Language/dlang.org https://github.com/D-Programming-Language/dlang.org/commit/7961228c7acafc7b96ef463256b5adc48e161598 fix Issue 13336 - auto ref return deduced to be ref despite return value coercion https://github.com/D-Programming-Language/dlang.org/commit/88d0c0f258f7d78c7729137da83c57cc27d95607 Merge pull request #651 from 9rnsr/fix13336 Issue 8307 and 13336 - fix documentation around return type inference and ref deduction
Comment #7 by github-bugzilla — 2015-02-18T03:36:46Z
Commits pushed to 2.067 at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7e7e4814799b145c518ccc77191b2c11cda59d18 fix Issue 13336 - auto ref return deduced to be ref despite return value coercion https://github.com/D-Programming-Language/dmd/commit/d7b38383f4b19c211e1210bc00091788d2d74887 Merge pull request #3979 from 9rnsr/fix13336
Comment #8 by github-bugzilla — 2015-02-18T03:40:26Z