Bug 9047 – Expression requiring std.math fails with function-local import
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2012-11-19T14:43:00Z
Last change time
2014-08-26T07:31:03Z
Keywords
pull, rejects-valid
Assigned to
andrej.mitrovich
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-11-19T14:43:53Z
void main() {
import std.math;
auto f = (double a, double b) => a ^^ b;
}
DMD 2.061alpha gives:
test.d(3): Error: undefined identifier 'std'
Comment #1 by andrej.mitrovich — 2013-01-07T10:51:03Z
I don't think this is a diagnostic bug, it's a problem where the compiler rewrites the call to:
void main() {
import std.math;
auto f = (double a, double b) => .std.math.pow(a, b);
}
This is why you get a "undefined identifier std" message.
Using the .dot won't work here because the import is function-local.
Comment #2 by bearophile_hugs — 2013-01-07T11:14:42Z
(In reply to comment #1)
> I don't think this is a diagnostic bug, it's a problem where the compiler
> rewrites the call to:
>
> void main() {
> import std.math;
> auto f = (double a, double b) => .std.math.pow(a, b);
> }
>
> This is why you get a "undefined identifier std" message.
>
> Using the .dot won't work here because the import is function-local.
The code the performs that rewriting should verify that the correctness conditions are satisfied, and otherwise possibly rewrite the ^^ with something that shows a good error message.
Comment #3 by andrej.mitrovich — 2013-01-07T14:59:29Z
Comment #4 by andrej.mitrovich — 2013-01-07T15:13:35Z
*** Issue 4845 has been marked as a duplicate of this issue. ***
Comment #5 by bearophile_hugs — 2013-01-07T15:15:29Z
(In reply to comment #3)
> https://github.com/D-Programming-Language/dmd/pull/1438
Thank you Andrej. The usability of a language comes also from little forward steps like this :-) It's a small step, but hundreds of such steps make a difference.
Comment #6 by andrej.mitrovich — 2013-01-10T15:07:59Z
*** Issue 8354 has been marked as a duplicate of this issue. ***
Comment #7 by bearophile_hugs — 2013-03-23T07:54:19Z
void main() {
import std.stdio;
int x = 3;
immutable j = 2 ^^ x;
}
Error received:
temp.d(4): Error: undefined identifier 'std'
Expected error:
temp.d(4): Error: must import std.math to use ^^ operator
That problem causes curious cascades of errors like:
void main() {
import std.stdio;
enum size_t N = 5;
char[N][N] mat = '0';
int e = 0;
foreach (immutable i, ref row; mat) {
immutable j = (i + (2 ^^ e)) % N;
row[j] = '1';
}
writeln(mat);
}
temp.d(7): Error: undefined identifier 'std'
...\dmd2\src\phobos\std\range.d(611): Error: static assert "Cannot put a const(dchar) into a Appender!(char[])"
...\dmd2\src\phobos\std\format.d(2163): instantiated from here: put!(Appender!(char[]), const(dchar))
...\dmd2\src\phobos\std\format.d(2210): instantiated from here: formatChar!(Appender!(char[]))
...\dmd2\src\phobos\std\format.d(2113): instantiated from here: formatElement!(LockingTextWriter, char[5u], char)
...\dmd2\src\phobos\std\format.d(1827): ... (5 instantiations, -v to show) ...
...\dmd2\src\phobos\std\stdio.d(1622): instantiated from here: write!(char[5u][5u],char)
temp.d(10): instantiated from here: writeln!(char[5u][5u])
Comment #8 by andrej.mitrovich — 2013-03-23T08:05:25Z
(In reply to comment #7)
> temp.d(7): Error: undefined identifier 'std'
> ...\dmd2\src\phobos\std\range.d(611): Error: static assert "Cannot put a
> const(dchar) into a Appender!(char[])"
Looks like another case of http://d.puremagic.com/issues/show_bug.cgi?id=9549 which was accidentally fixed by another pull.
However your test-case still creates this cascade of errors, so maybe you should put your test-case into Issue 9549 and reopen it.
My pull here fixes the first error message (to "must import std.math"), but the cascade of errors still appear.
Comment #9 by bearophile_hugs — 2013-03-23T12:41:29Z
(In reply to comment #8)
> However your test-case still creates this cascade of errors, so maybe you
> should put your test-case into Issue 9549 and reopen it.
I don't understand Issue 9549 enough, and it was opened by you, so probably I think it's better for you to copy that stuff there :-)
> My pull here fixes the first error message (to "must import std.math"), but the
> cascade of errors still appear.
OK.
Comment #10 by yebblies — 2013-11-24T08:17:08Z
*** Issue 8587 has been marked as a duplicate of this issue. ***
Comment #11 by k.hara.pg — 2014-08-26T07:31:03Z
Froom 2.065, dmd implicitly imports std.math module in order to resolve PowExpression. So the issue does not occur anymore.