www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3316] New: Functions nested in a pure templated function cannot reference its local variables

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3316

           Summary: Functions nested in a pure templated function cannot
                    reference its local variables
           Product: D
           Version: 2.032
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: peng2cheng2 yahoo.com


--- Comment #0 from pc <peng2cheng2 yahoo.com> 2009-09-13 16:17:31 PDT ---
import std.stdio;

/*
  ATTEMPT TO USE NESTED "PURE" FUNCTIONS IN A TEMPLATE.

  All works fine unless you uncomment the third line in main. If you
  do, dmd 2.032 yeilds:

  pure.d(35): Error: pure nested function 'bar' cannot access mutable
  data 'fooState'

  pure.d(36): Error: pure nested function 'bar' cannot access mutable
  data 'y'

  pure.d(47): Error: template instance pure.fooPT!(char) error
  instantiating
*/


//"pure" inner function, with concrete types - ok
pure string foo(string x, string y){

  string fooState;

  string bar(string x){
    fooState = "hello ";
    return x ~ y;
  }

  return fooState ~ bar(x);
}

//potentially pure (?) templated version not labled as pure - ok
immutable(T)[] fooT(T)(immutable(T)[] x, immutable(T)[] y){

  immutable(T)[] fooState;

  immutable(T)[] bar(immutable(T)[] x){
    fooState = "hello ";
    return x ~ y;
  }

  return fooState ~ bar(x);

}

//attempt to make templated version pure - no dice
pure immutable(T)[] fooPT(T)(immutable(T)[] x, immutable(T)[] y){

  immutable(T)[] fooState;

  immutable(T)[] bar(immutable(T)[] x){
    fooState = "hello ";
    return x ~ y;
  }

  return fooState ~ bar(x);

}


void main(){
  writeln(foo("p", "c"));
  writeln(fooT("p", "c"));
  //writeln(fooPT("p", "c"));

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 13 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3316


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #1 from Don <clugdbug yahoo.com.au> 2009-09-29 23:56:55 PDT ---
This is happening because the nested template is being marked as pure. This 
happens in mtype.c, in TypeFunction::semantic, line 4038:
 the template function gets marked as pure/nothrow because it's taken from the 
parent scope. This is wrong, because pure/nothrow shouldn't be inherited by 
members.

PATCH: In DeclarationExp::semantic(Scope *sc), pure (and nothrow) should not be 
passed on to members. Turn it off while running semantic on those functions.

Index: expression.c
===================================================================
--- expression.c    (revision 196)
+++ expression.c    (working copy)
\   -4505,8 +4505,12   
     }
     if (!s->isVarDeclaration())
     {
+    // 'pure nothrow' is not inherited by member declarations
+        int scopePureNothrow = sc->stc & (STCpure | STCnothrow);
+    sc->stc ^= scopePureNothrow;
     declaration->semantic(sc);
     s->parent = sc->parent;
+    sc->stc ^= scopePureNothrow;
     }
     if (!global.errors)
     {

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 29 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3316


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #2 from Don <clugdbug yahoo.com.au> 2009-10-21 06:41:44 PDT ---
Fixed DMD2.035.

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