D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 900 - changing import order causes type mismatch
Summary: changing import order causes type mismatch
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2007-01-28 10:47 UTC by torhu
Modified: 2014-02-16 15:24 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 torhu 2007-01-28 10:47:45 UTC
Tested with DMD 1.0 and 1.004 on winxp.

There are 4 files involved:

// palette.d
module palette;

struct RGB { }

=========================================
// color_inl.d
module color_inl;

//uncommenting this line will fix the problem
//import palette : RGB:

import color;

void _set_color(RGB* p) { }

=========================================
// color.d
module color;

// swapping the order of these two lines will also fix the problem
public import color_inl;
import palette : RGB;

=========================================
// dtest.d
import color;

void fade()
{
   RGB rgb;
   _set_color(&rgb);
}

void main() {}

========================================

C:\prog\test\D\bugtest>dmd test.d
test.d(6): function color_inl._set_color (int,RGB*) does not match parameter types (int,RGB *)
test.d(6): Error: cannot implicitly convert expression (& rgb) of type RGB * to RGB*
Comment 1 torhu 2007-01-29 02:34:47 UTC
Are selective imports private by default or not?  The docs state that imports are public by default, but also says that selective imports are "bound into the current namespace".

I take it they are supposed to be private, which means this bug is really about the compiler accepting invalid code, since color_inl.d and dtest.d are able to use RGB without importing palette.d.
Comment 2 Lars Ivar Igesund 2007-01-29 03:59:27 UTC
Imports are private by default, if the spec says anything else, create a bug report for it.
Comment 3 torhu 2007-01-29 17:42:52 UTC
(In reply to comment #2)
The problem is that I don't know if "bound into the current namespace" implies that it's also automatically exported as part of the importing module.

Probably it shouldn't imply that, since the point of selective imports seems to be to avoid name conflicts in the importing module.

But this still confuses me, so I think there should be a clarification in the docs.  At least so we know what is correct when the compiler is somewhere in between.
Comment 4 torhu 2007-04-03 13:00:33 UTC
(In reply to comment #2)
 A couple of bug reports here that suggest that selective, static, and renamed imports are meant to be be private.

http://d.puremagic.com/issues/show_bug.cgi?id=604
http://d.puremagic.com/issues/show_bug.cgi?id=314

The question remains whether this is intended, or not.  In DMD, they are currently public.  Were they supposed to be private?  The docs don't say.
Comment 5 Leandro Lucarella 2009-11-13 15:50:57 UTC
I think the spec is quite clear:

  [...]
  Public Imports

  By default, imports are private. [...]

http://www.digitalmars.com/d/1.0/module.html#ImportDeclaration

Static, renamed and selective import are all *import*, so it looks like they all should be private by default (for the same reason basic imports are).

This bug is old and doesn't look very useful, I think it should be closed.
Comment 6 Jesse Phillips 2010-03-29 16:42:17 UTC
I seem to have run across this in DMD 2.042. Here a static assert fails with the error:

=====================
C:\opt\dmd\windows\bin\..\..\src\phobos\std\typecons.d(424): Error: static assert  (is(Tuple!(string,float) == Tuple!(string,float))) is false

C:\opt\dmd\windows\bin\..\..\src\phobos\std\typecons.d(413): 
instantiated from here: Tuple!(string,float)

C:\opt\dmd\windows\bin\..\..\src\phobos\std\typecons.d(423): 
instantiated from here: slice!(1,3)

C:\opt\dmd\windows\bin\..\..\src\phobos\std\typecons.d(420):
instantiated from here: Tuple!(int,string,float,double)

C:\opt\dmd\windows\bin\..\..\src\phobos\std\regex.d(260):
instantiated from here: Tuple!(string,string)

C:\opt\dmd\windows\bin\..\..\src\phobos\std\regex.d(280):
instantiated from here: opCall!(string)

C:\opt\dmd\windows\bin\..\..\src\phobos\std\regex.d(1519):
instantiated from here: Regex!(char).\test2.d(7):
instantiated from here: regex!(string)
===================

--------------------------
// Place string after import test2 to make this work
import std.string;
import test2;

void main() {
    new TestMe();
}
------------------------------

--------------------
module test2;

import std.regex;

class TestMe {
    void test(string input) {
        auto foo = !match(input, regex("fish")).empty;
    }
}
Comment 7 Jesse Phillips 2010-04-15 14:23:31 UTC
My import issue is no longer in DMD 2.043, but the original submission's code still doesn't work.
Comment 8 Martin Nowak 2012-02-14 05:03:30 UTC
This is invalid because 'import palette : RGB;' introduces RGB as private alias.
You cannot access it from color_inl.
Changing that to 'public import palette : RGB' will fix your problem.
Comment 9 Martin Nowak 2012-02-14 05:29:54 UTC
Sorry, I was using a wrong dmd version.
Changing it to 'public import palette : RGB' will fail because
of a forward reference error. This happens due to the cyclic import of color and color_inc.
Comment 10 Andrei Alexandrescu 2013-11-15 20:34:29 UTC
This seems to have been fixed, possibly a while ago.