Bug 1818 – storage class in type tuples is discarded for function and delegate declarations

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2008-02-05T12:12:00Z
Last change time
2015-06-09T01:14:30Z
Keywords
spec, wrong-code
Assigned to
nobody
Creator
kamm-removethis
Blocks
3909, 3106

Comments

Comment #0 by kamm-removethis — 2008-02-05T12:12:36Z
Type tuples do contain storage class information when obtained via is(func argstuple == function) or std.traits.ParameterTypeTuple. However, this information is discarded if the tuple is used for a function declaration even though the ParameterTypeTuple retains the storage class sometimes. void foo(ref int i) { ++i; } void bar(ParameterTypeTuple!(foo) f) { foo(f); } void main() { int i = 0; foo(i); bar(i); writefln(i); // prints 1 } // but incorrectly: pragma(msg, ParameterTypeTuple!(foo).stringof); // prints "(ref int)" pragma(msg, ParameterTypeTuple!(bar).stringof); // prints "(ref int)" I think the frontend code responsible for this misbehaviour is func.c around line 760. There the storage class of the tuple is propagated to each of the tuple members, replacing existing storage classes. Fixing this bug will allow compile-time reflection and manipulation of parameter storage classes. It would still be impossible to create type tuples containing non-default storage classes directly, but using code similar to this will allow indirect manipulation via argument tuples: template appendArg(X, T...) { alias ParameterTypeTuple!(void delegate(T, X)) appendArg; } template appendRefArg(X, T...) { alias ParameterTypeTuple!(void delegate(T, ref X)) appendRefArg; } alias appendRefArg!(int) refint; alias appendArg!(real, refint) refint_real; alias appendRefArg!(long, refint_real) refint_real_outlong; pragma(msg, refint.stringof ~ " " ~ refint_real.stringof ~ " " ~ refint_real_outlong.stringof); // current output: (ref int) (int, real) (int, real, ref long) // desired output: (ref int) (ref int, real) (ref int, real, ref long) Consequently this would also allow the implementation of the identity function template that Andrei recommended as a testcase a while back: ReturnType!(func) ident(alias func)(ParameterTypeTuple!(func) args) { return func(args); } Currently this fails for functions with arguments using non-default storage classes. Finally, this change might break some code that expects storage classes to be lost. It also interacts with bugs 1411 and 1424.
Comment #1 by kamm-removethis — 2008-02-07T13:10:37Z
I have commented out the mentioned code in func.c and tested this with gdc. While it works in the sense that void foo(ref int i) { ++i; } void bar(ParameterTypeTuple!(foo) f) { foo(f); } now yields a bar taking a ref int, there are other issues. Most notably template printT(T...) { pragma(msg, T.stringof); alias T printT; } alias printT!(ParameterTypeTuple!(foo)) printed; prints a mere (int). This seems to happen because tuples are split into types when instantiating a template, losing their extra information. (template.c TemplateInstance::semanticTiargs I think). Looking at the whole issue again, it may be more sensible to remove storage class information from type tuples altogether. After all, they are supposed to hold types and not argument-specifications. As storage class manipulation is essential in some instances, a new kind of tuple could be introduced: ArgumentTuple?
Comment #2 by yebblies — 2011-06-10T10:47:20Z
*** Issue 1424 has been marked as a duplicate of this issue. ***
Comment #3 by bugzilla — 2012-01-21T21:45:58Z
The example now prints: 2