D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7707 - Purity inference fails when template used before it's defined
Summary: Purity inference fails when template used before it's defined
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-03-13 21:28 UTC by Jonathan M Davis
Modified: 2017-06-25 11:05 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 Jonathan M Davis 2012-03-13 21:28:03 UTC
2.059 HEAD

If you change std.utf.toUTFz to

const(wchar)* toUTF16z(C)(const(C)[] str) @system
    if(isSomeChar!C)
{
    return toUTFz!(const(wchar)*)(str);
}

unittest
{
    toUTF16z("hello world");
}

then compilation fails with this error:

std/utf.d(1373): Error: pure function 'toUTF16z' cannot call impure function 'toUTFz'
std/utf.d(1378): Error: template instance std.utf.toUTF16z!(char) error instantiating
make[1]: *** [generated/linux/debug/64/unittest/std/utf] Error 1
make: *** [unittest] Error 2

toUTFz is _not_ marked as pure, and neither is toUTF16z. I don't believe that toUTFz _can_ be pure (certainly, it _shouldn't_ be given what it does), so it should always infer as impure, which would cause toUTF16z be inferred as impure if it's changed to call toUTFz as is done above.

However, if you move toUTF16z to after the definition of toUTFz, then it compiles. So, it appears to be a forward declaration issue.
Comment 1 Vladimir Panteleev 2017-06-25 11:05:22 UTC
This needs a self-contained test case.

Please reopen if you consider this issue still valid and can provide a self-contained test case.