Bug 705 – Mixins and auto

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2006-12-21T09:22:23Z
Last change time
2019-05-27T12:14:28Z
Keywords
wrong-code
Assigned to
Walter Bright
Creator
Thomas Kühne

Comments

Comment #0 by thomas-dloop — 2006-12-21T09:22:23Z
(Originally posted by Chris Sauls <[email protected]> on 2005-12-10 as news:[email protected]) Might be a bug, or might be me doing something silly, but this doesn't work as expected: # module mix0; # # import std.stdio ; # # class Foo { # this (char[] a_label) { # p_label = a_label; # writefln(" + ", p_label); # } # # ~this () { # writefln(" - ", p_label); # } # # private char[] p_label ; # } # # template MFoo (char[] T_label) { # auto Foo _MFoo_foo = new Foo(T_label); # } # # void main () { # { mixin MFoo!("A"); } # { mixin MFoo!("B"); } # { mixin MFoo!("C"); } # # { auto Foo foo = new Foo("A2"); } # { auto Foo foo = new Foo("B2"); } # { auto Foo foo = new Foo("C2"); } # } The output I expected being: | + A | - A | + B | - B | + C | - C | + A2 | - A2 | + B2 | - B2 | + C2 | - C2 The output I am getting: | + A | + B | + C | + A2 | - A2 | + B2 | - B2 | + C2 | - C2 | - C | - B | - A Eh? Apparently the "auto" variables declared by the mixins aren't getting collected until the end of the entire function, my anonymous blocks be damned, and in reverse order. And in case anyone wonders, no it doesn't change if I place more code in the blocks. I ran across this little beaut looking for a way to conditionally compile in my Timer objects with somewhat cleaner reading code. (I like the look of `mixin MTimer!("void main()");` better than `version (Timer) auto Timer t = new Timer("void main()");` ... I guess I'm crazy.) test cases: http://dstress.kuehne.cn/run/a/auto_13_A.d http://dstress.kuehne.cn/run/a/auto_13_B.d
Comment #1 by razvan.nitu1305 — 2019-05-27T12:14:28Z
In D2 you get an error if you use auto and also provide the type. Closing this as WONTFIX.