Bug 17992 – auto return type and null for classes

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-11-19T07:36:25Z
Last change time
2020-03-21T03:56:41Z
Assigned to
No Owner
Creator
Temtaime

Comments

Comment #0 by temtaime — 2017-11-19T07:36:25Z
class C { int v; C parent; C[] childs; auto foo(int a) { a -= v; if(parent && a < 0) { return null; } foreach(c; childs) { if(auto r = c.foo(a)) { pragma(msg, typeof(r)); return r; } } if(parent) { return this; } return null; } } void main() { auto a = new C, b = new C; a.v = 12; b.parent = a; a.childs ~= b; assert(a.foo(123) is b); } It passes, but prints typeof(null) It should print C or give an error with pragma or doesn't compile at all. LDC team says it's impossible to generate code from frontend data so this example doesn't work with ldc.
Comment #1 by razvan.nitu1305 — 2018-06-14T10:29:43Z
Reduced test case: class V {} auto foo(int a) { if (a == 1) return null; pragma(msg, typeof(return)); return new V(); } void main() {} prints: typeof(null) I'm not sure if this bug is valid. When semantic analysis is performed at the point where typeof is evaluated the inference of the return type of foo (in both examples) has only seen return statements of null. This makes inference deduce that the type of foo is typeof(null) and this is true for that particular point. Changing this would require moving the typeof logic from semantic1 to semantic2 or semantic3 so that when typeof is encountered, the whole foo function has been typechecked.