D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7452 - Function using enforce() cannot be inferred as @safe because of anonymous function due to lazy argument
Summary: Function using enforce() cannot be inferred as @safe because of anonymous fun...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: pull, rejects-valid
Depends on:
Blocks:
 
Reported: 2012-02-06 13:22 UTC by kennytm
Modified: 2012-02-19 16:28 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description kennytm 2012-02-06 13:22:40 UTC
Test case:

----------------------------------
import std.exception;
int f7452()(int x)
{
   enforce(x > 0);
   return x;
}
void g7452() @safe pure
{
   assert(4 == f7452(4));
}
----------------------------------

This caused error:

    Error: safe function 'g7452' cannot call system function 'f7452'



This is because of the lazy argument, as shown with this minimal D-only test case:

----------------------------------
void e7452b(int, lazy int) pure nothrow @safe {}
int f7452b()(int x)
{
   e7452b(x, 0);
   return x;
}
void g7452b() pure nothrow @safe
{
   assert(4 == f7452b(4));
}
----------------------------------
Comment 1 kennytm 2012-02-07 12:10:39 UTC
Further reduced test case, showing the cause is the function/delegate type, not 'lazy'.

-----------------
int f7452c()(int x)
{
   auto y = function int() { return 0; };
   return x;
}
void g7452c() pure nothrow @safe
{
   assert(4 == f7452c(4));
}
-----------------

Note that 'pure' and 'nothrow' are correctly inferred.
Comment 3 github-bugzilla 2012-02-19 15:00:39 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/b9938baed4f3a0d3606d40ad435ca0e18176980e
Merge pull request #700 from kennytm/bug7452_lazy_safe

Bug 7452 (@safe inference failed with a function literal inside)