www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7554] New: Immutable function pointer arguments too

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

           Summary: Immutable function pointer arguments too
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This problem may be correlated to Issue 7500

This code doesn't compile because 'foo' is mutable:


T outer(T)(T function(in T) pure foo) pure {
     pure int inner() {
         return foo(5);
     }
     return inner();
}
int sqr(in int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}


test.d(3): Error: pure nested function 'inner' cannot access mutable data 'foo'
test.d(11): Error: template instance test.outer!(int) error instantiating


But it doesn't work if you add an 'immutable':


T outer(T)(immutable T function(in T) pure foo) pure {
     pure int inner() {
         return foo(5);
     }
     return inner();
}
int sqr(in int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}


test.d(11): Error: template test.outer(T) cannot deduce template function from
argument types !()(int function(const(int) x) pure)



This compiles, but it's not nice:


int sqr(in int x) pure {
     return x * x;
}
immutable sqrPtr =&sqr;
auto outer(typeof(sqrPtr) foo) pure {
     pure int inner() {
         return foo(5);
     }
     return inner();
}
void main() {
     assert(outer(sqrPtr) == 25);
}



A better workaround, found by Timon Gehr:

T outer(T)(T function(in T) pure foo) pure {
     immutable fooTick = foo;
     pure int inner() {
         return fooTick(5);
     }
     return inner();
}
int sqr(in int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 20 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7554




I think making inner() pure isn't necessary.
Following raises no error and works as expected.

T outer(T)(T function(in T) pure foo) pure {
     int inner() {
         return foo(5);
     }
     return inner();
}
int sqr(in int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7554





 I think making inner() pure isn't necessary.
In this program I want inner() to be pure. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7554




This code compiles, but I don't remember if this used to compile even before
fixing bug 7500 :


int outer(immutable int function(in int) pure foo) pure {
     pure int inner() {
         return foo(5);
     }
     return inner();
}
int sqr(in int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}



This doesn't compile still:

T outer(T)(immutable T function(T) pure foo) pure {
     pure int inner() {
         return foo(5);
     }
     return inner();
}
int sqr(int x) pure {
     return x * x;
}
void main() {
     assert(outer(&sqr) == 25);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7554




OK. Reduced test case.

int sqr(int x) pure {
     return x * x;
}
void main()
{
    immutable(int function(int) pure) ifp = &sqr;
    // Error: cannot implicitly convert expression (& sqr) of type int
function(int x) pure to immutable(int function(int) pure)
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7554


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull



https://github.com/D-Programming-Language/dmd/pull/772

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7554




Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/c1906e09d7b773bc5ff0a2477b3139c98d8e60ea
fix Issue 7554 - Immutable function pointer arguments too

Protect function type from inappropriate modifier transition.

https://github.com/D-Programming-Language/dmd/commit/461649be7975faf119188674f296a7fac7c38a96


Issue 7554 - Immutable function pointer arguments too

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 28 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7554


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


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