Thread overview
[Issue 3175] New: rejects templated ref return function
Jul 14, 2009
k-foley@onu.edu
Sep 21, 2010
Shin Fujishiro
Sep 26, 2010
Shin Fujishiro
Sep 28, 2010
Walter Bright
Sep 28, 2010
Shin Fujishiro
Nov 23, 2010
Don
July 14, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3175

           Summary: rejects templated ref return function
           Product: D
           Version: 2.031
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: k-foley@onu.edu


import std.stdio;

// This works
//ref int x(int[3] p) { return p[0]; }

// This works
//ref T x(T)(T[3] p) { return p[0]; }

// This does not work
//ref int x(size_t N)(int[N] p) if ( N > 0 ) { return p[0]; }

// This does not work
ref T x(T, size_t N)(T[N] p) if ( N > 0 ) { return p[0]; }

int main()
{
    auto a = [1, 2, 3];

    a.x = 42;

    writeln( a );

    return 0;
}

----

$ rdmd test.d
test.d(12): Error: variable test.N only parameters or foreach declarations can
b
e ref
C:\d\dmd.2.031\dmd2\windows\bin\rdmd.exe: Couldn't compile or execute test.d.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 18, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3175


Luther Tychonievich <lat7h@virginia.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lat7h@virginia.edu
           Severity|normal                      |trivial


--- Comment #1 from Luther Tychonievich <lat7h@virginia.edu> 2009-11-18 10:30:44 PST ---
This is only a problem with template parameter deduction:

    ref int foo(int x)(int[x] v) if (x>0) { return v[0]; }

    foo!(2)([1,2]); // works
    foo([1,2]);     // gives "only parameters or foreach..." error message


It can be sidestepped by explicit template declaration:

    template foo(int x) if (x>0) {
        ref int foo(int[x] v) { return v[0]; }
    }

    foo([1,2]);     // works


While obviously not as nice as function template syntax, this workaround is not difficult to read or write and contains full functionality.


I posit the error message is wrong either way: declarations.c line 908 should
read:
    error("only function return types, parameters, and foreach declarations can
be ref");

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3175


Shin Fujishiro <rsinfu@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rsinfu@gmail.com


--- Comment #2 from Shin Fujishiro <rsinfu@gmail.com> 2010-09-20 22:03:05 PDT ---
*** Issue 4446 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3175


Shin Fujishiro <rsinfu@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           Severity|trivial                     |major


--- Comment #3 from Shin Fujishiro <rsinfu@gmail.com> 2010-09-25 18:16:35 PDT ---
Patch against dmd r687. deduceFunctionTemplateMatch() must reset storage class
in parameter's scope as done in matchWithInstance().

--- a/src/template.c
+++ b/src/template.c
@@ -887,6 +887,7 @@ MATCH
TemplateDeclaration::deduceFunctionTemplateMatch(Scope *sc, Loc loc, Objec
     ScopeDsymbol *paramsym = new ScopeDsymbol();
     paramsym->parent = scope->parent;
     Scope *paramscope = scope->push(paramsym);
+    paramscope->stc = 0;

     TemplateTupleParameter *tp = isVariadic();
     int tp_is_declared = 0;


Increased the severity as the bug has been repeatedly reported: bug 4041, bug 4446, bug 4934 and comment 2 of bug 2460.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 28, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3175


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2010-09-27 19:35:27 PDT ---
Applied the patch http://www.dsource.org/projects/dmd/changeset/695 but the bug remains.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 28, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3175



--- Comment #5 from Shin Fujishiro <rsinfu@gmail.com> 2010-09-27 21:00:38 PDT ---
(In reply to comment #4)
> Applied the patch http://www.dsource.org/projects/dmd/changeset/695 but the bug remains.

Are you saying the bug remains since the reproducing code in comment #0 doesn't compile? It's because the type of [1,2,3] has changed to dynamic int[]. Replacing 'auto' to 'int[3]' would make it compile.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 23, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3175


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |clugdbug@yahoo.com.au
         Resolution|                            |FIXED


--- Comment #6 from Don <clugdbug@yahoo.com.au> 2010-11-23 05:15:10 PST ---
(In reply to comment #5)
> (In reply to comment #4)
> > Applied the patch http://www.dsource.org/projects/dmd/changeset/695 but the bug remains.
> 
> Are you saying the bug remains since the reproducing code in comment #0 doesn't compile? It's because the type of [1,2,3] has changed to dynamic int[]. Replacing 'auto' to 'int[3]' would make it compile.

Indeed it does work in DMD2.050.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------