Bug 8354 – Some missing "import std.math to use ^^ operator" error messages
Status
RESOLVED
Resolution
DUPLICATE
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-06T17:44:00Z
Last change time
2013-01-10T15:07:59Z
Keywords
diagnostic
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-07-06T17:44:32Z
A wrong D2 program:
void main() {
int x1 = 10;
auto y1 = x1 ^^ 5;
}
It gives a correct error message:
test.d(3): Error: must import std.math to use ^^ operator
------------------------
But adding a second power it gives a wrong error message (dmd 2.060alpha):
void main() {
int x1 = 10;
auto y1 = x1 ^^ 5;
double x2 = 10.5;
auto y2 = x2 ^^ 5;
}
test.d(3): Error: must import std.math to use ^^ operator
test.d(5): Error: undefined identifier 'std'
Here I'd like the second power to give an error message similar to the first one.
------------------------
Something similar happens if you import pow, this time for both powers:
import std.math: pow;
void main() {
int x1 = 10;
auto y1 = x1 ^^ 5;
double x2 = 10.5;
auto y2 = x2 ^^ 5;
}
test.d(4): Error: undefined identifier 'std'
test.d(6): Error: undefined identifier 'std'
Comment #1 by repeatedly — 2012-07-14T14:11:22Z
(In reply to comment #0)
> A wrong D2 program:
>
> void main() {
> int x1 = 10;
> auto y1 = x1 ^^ 5;
> }
>
>
> It gives a correct error message:
> test.d(3): Error: must import std.math to use ^^ operator
>
> ------------------------
>
> But adding a second power it gives a wrong error message (dmd 2.060alpha):
>
> void main() {
> int x1 = 10;
> auto y1 = x1 ^^ 5;
> double x2 = 10.5;
> auto y2 = x2 ^^ 5;
> }
>
>
> test.d(3): Error: must import std.math to use ^^ operator
> test.d(5): Error: undefined identifier 'std'
>
>
> Here I'd like the second power to give an error message similar to the first
> one.
Following patch fixes this problem:
-----
diff --git a/src/expression.c b/src/expression.c
index 392ca06..9916fbe 100644
--- a/src/expression.c
+++ b/src/expression.c
@@ -11472,6 +11472,7 @@ Expression *PowExp::semantic(Scope *sc)
}
static int importMathChecked = 0;
+ static bool importMath = false;
if (!importMathChecked)
{
importMathChecked = 1;
@@ -11480,13 +11481,20 @@ Expression *PowExp::semantic(Scope *sc)
//printf("\t[%d] %s\n", i, mi->toChars());
if (mi->ident == Id::math &&
mi->parent->ident == Id::std &&
- !mi->parent->parent)
+ !mi->parent->parent) {
+ importMath = true;
goto L1;
+ }
}
error("must import std.math to use ^^ operator");
return new ErrorExp();
L1: ;
+ } else {
+ if (!importMath) {
+ error("must import std.math to use ^^ operator");
+ return new ErrorExp();
+ }
}
e = new IdentifierExp(loc, Id::empty);
-----
> ------------------------
>
> Something similar happens if you import pow, this time for both powers:
>
>
> import std.math: pow;
> void main() {
> int x1 = 10;
> auto y1 = x1 ^^ 5;
> double x2 = 10.5;
> auto y2 = x2 ^^ 5;
> }
>
>
> test.d(4): Error: undefined identifier 'std'
> test.d(6): Error: undefined identifier 'std'
I can't judge the this problem is bug or spec.
'import std.math: pow' does not import 'std' namespace,
but dmd replaces ^^ with std.math.sqrt or std.math.pow.
This is the same problem below:
-----
import std.math : sqrt;
void main()
{
real x1 = 10;
auto y1 = std.math.sqrt(x1);
}
m.d(6): Error: undefined identifier std
-----
Comment #2 by github-bugzilla — 2012-08-06T06:33:44Z
If std.math is imported in one module and power (^^) expression is used in another module and both the modules are compiled together, we again see the same confusing error:
bar.d(1): Error: undefined identifier 'std'
Here is the testcase (compile with -- dmd foo.d bar.d):
// File foo.d
import std.math;
void foo() { }
// File bar.d
void bar(int i) { 2^^i; }
Comment #5 by andrej.mitrovich — 2013-01-10T15:07:59Z
(In reply to comment #4)
> If std.math is imported in one module and power (^^) expression is used in
> another module and both the modules are compiled together, we again see the
> same confusing error:
>
> bar.d(1): Error: undefined identifier 'std'
>
>
> Here is the testcase (compile with -- dmd foo.d bar.d):
>
> // File foo.d
> import std.math;
> void foo() { }
>
> // File bar.d
> void bar(int i) { 2^^i; }
This will be fixed with pull for Issue 9047.
*** This issue has been marked as a duplicate of issue 9047 ***