[Issue 15086] import doesn't verify module declaration

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 14 13:30:56 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=15086

--- Comment #25 from ag0aep6g at gmail.com ---
(In reply to Walter Bright from comment #21)
> I suspect the core of a fix
> can be along the lines of detecting that an explicit import of bar.d is done
> using two different names in the same module.

That would fix a problem, but not this Bugzilla issue.

If you want to attack the issue at hand in the spec, then I suggest we get to
that before fixing related-but-not-identical cases. An attempt at amending the
spec is likely to reveal more interesting cases.

Also, so far we focused on accepts-invalid cases where we'd like DMD to throw
an error instead, but the lack of verification can also lead to wrong code when
the wrong module is accepted before the right one is attempted.

Example:


pkg/hello_world.d:
----
module pkg.hello_world;
import std.stdio; /* Should import Phobos's std.stdio. */
void main()
{
    writeln("Hello, world!"); /* Should print "Hello, world!". */
}
----

pkg/std/stdio.d:
----
module pkg.std.stdio; /* NOTE: Not std.stdio. */
void writeln()(string s) /* Template makes for a shorter example. */
{
    import core.stdc.stdio;
    printf("Goodbye, cruel world!\n");
}
----

`cd pkg; dmd hello_world.d && ./hello_world; cd ..`:
----
Goodbye, cruel world!
----


Note that I have no custom module std.stdio. DMD incorrectly picks up
pkg.std.stdio as std.stdio, because it ignores the module declaration. Instead,
it should look at the module declaration, see that pkg.std.stdio is not
std.stdio, and then continue the search for the real std.stdio.

Maybe this too can be fixed without addressing the core problem of missing
verification (e.g. with a "best match" kind of thing). But I'm afraid this way
leads down a rabbit hole of special cases. In my opinion, it would be better to
have a clean cut, maybe with a deprecation period.

--


More information about the Digitalmars-d-bugs mailing list