D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7330 - Local imports are order sensitive
Summary: Local imports are order sensitive
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: spec
Depends on:
Blocks:
 
Reported: 2012-01-20 12:54 UTC by Peter Alexander
Modified: 2012-02-14 06:16 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Peter Alexander 2012-01-20 12:54:07 UTC
According to the language reference: "The order in which ImportDeclarations occur has no significance."

This is true for module scope imports in DMD (2.057 tested), but not for function scope imports.

---
import std.stdio;
void main() {
  writeln("foo"); //ok
}
---
void main() {
  writeln("foo"); //ok
}
import std.stdio;
---
void main() {
  import std.stdio;
  writeln("foo"); // ok
}
---
void main() {
  writeln("foo"); // error, writeln undefined
  import std.stdio;
}
---
Comment 1 timon.gehr 2012-01-20 13:55:42 UTC
I think what is meant in the reference is that the relative order of multiple ImportDeclarations is insignificant.
Comment 2 Peter Alexander 2012-01-20 16:51:52 UTC
(In reply to comment #1)
> I think what is meant in the reference is that the relative order of multiple
> ImportDeclarations is insignificant.

True, it could be interpreted that way.

My understanding with D (in general) is that the ordering of all declarations is supposed to be inconsequential.

If this isn't the case and the code is in error by design then it should be clarified, especially since it is inconsistent with import declarations at module scope.
Comment 3 Jonathan M Davis 2012-01-20 17:21:42 UTC
Yeah. I don't think that this is a bug, but then again, I wouldn't have been surprised if it didn't work to put imports at module scope after the line where you use what you're importing. It would just be so bizarre to put the imports anywhere but at the top that I've never thought about it.

But consider that the order of declarations of module variables doesn't normally matter (it can with static if), but it matters a great deal at local scope. So, for imports to work the same makes sense.

I'm not quite sure how the language spec would be updated though. I don't know how you could have interpreted it to mean that you could put them after the line where you used what you're importing within a function. I would have thought that it was crystal clear as it is.
Comment 4 timon.gehr 2012-01-20 17:54:22 UTC
Also see issue 7329.
Comment 5 Peter Alexander 2012-01-21 03:51:22 UTC
(In reply to comment #3)
> I'm not quite sure how the language spec would be updated though. I don't know
> how you could have interpreted it to mean that you could put them after the
> line where you used what you're importing within a function. I would have
> thought that it was crystal clear as it is.

Well, it can't be crystal clear because you were unaware that you could put imports at module scope after the use of the imported symbols :-)

One way would be to add a statement to the effect of:

"At module scope, import declarations may appear after the use of imported symbols. However, in local scopes, import declarations must appear before the use of any symbols introduced by the import."