Issue 3972 - Regarding module with name different from its file name
Summary: Regarding module with name different from its file name
Status: RESOLVED DUPLICATE of issue 4479
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: diagnostic
: 2059 (view as issue list)
Depends on:
Blocks:
 
Reported: 2010-03-15 15:23 UTC by bearophile_hugs
Modified: 2014-02-15 02:44 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 bearophile_hugs 2010-03-15 15:23:57 UTC
Two modules with file names different from the name used in the module statement:

-------------

// File name: foo.d
module bar;
enum int x = 10;

-------------

// File name: spam.d
module test;
import foo: x;
void main() {}

-------------

When I compile the file spam.d I receive no errors from "spam" being different fom "test". But I (think I) receive an error for "foo" being different from "bar":

spam.d(2): Error: module bar is in multiple packages bar

The compiler can try to give a better/more descriptive error message here.


In this situation if you want the compiler can even give two error messages, complaining that inside the "spam.d" file it has a mismatch module name. If you want to enforce this too, then I think in Tango there are modules that have a name with the first letter upper case, but their file name is all lowercase, so I think the comparison to assert that the module name is the same as the file name is better done caseless (and because in Windows file names are essentially caseless).
Comment 1 Don 2010-03-16 13:58:43 UTC
I actually hit this today and got sufficiently irritated to improve the error message a bit. The 'else' clause should also be improved, it's bug 2059 ("Horrible error message"). Perhaps change it to (untested):
error(loc, "has inconsistent naming. It was imported as %s", srcname);


Index: module.c
===================================================================
--- module.c	(revision 416)
+++ module.c	(working copy)
@@ -621,7 +621,10 @@
     if (!dst->insert(this))
     {
 	if (md)
-	    error(loc, "is in multiple packages %s", md->toChars());
+	{
+	    error(loc, "has inconsistent naming.\n"
+	    "It was imported as %s but module declaration is %s", srcname, md->toChars());
+	    }
 	else
 	    error(loc, "is in multiple defined");
     }
Comment 2 bearophile_hugs 2010-03-17 04:23:03 UTC
Thank you.

Can the compiler complain in the other situation too? (I mean when no imports are present, and your program is a file named "foo.d" with written "module bar;" at the top?

If the mismatch name is an error when you import the module, then I think it has to be an error in this case too.
Comment 3 Walter Bright 2010-03-28 17:48:31 UTC
changeset 427
Comment 4 Walter Bright 2010-03-28 17:49:55 UTC
*** Issue 2059 has been marked as a duplicate of this issue. ***
Comment 5 bearophile_hugs 2010-04-09 10:39:50 UTC
In dmd 2.043 now compiling spam.d generates the error:

spam.d(3): Error: module bar from file foo.d conflicts with another module bar from file foo.d

That's not a a good error message, I am not able to understand it.
A better error message is like the following pair:

spam.d(2): Error: module 'test' has mismatched file name 'spam.d'.
foo.d(2): Error: module 'bar' has mismatched file name 'foo.d'.

Note that they are two errors, because both modules have a wrong/mismatched name.
Comment 6 bearophile_hugs 2010-04-09 14:53:02 UTC
After a short discussion with Walter it seems that in D it's OK to have a file named "foo.d" with inside it at the top written "module bar;".
The rationale behind it is "The flexibility comes in handy now and then.".

So probably there's no interest in fixing this small umpteenth hole in the module system.
Comment 7 Andrej Mitrovic 2012-12-20 15:19:33 UTC
pull for 4479 will fix this, with the new message being:

spam.d(2): Error: module bar from file foo.d must be imported as module 'bar'

*** This issue has been marked as a duplicate of issue 4479 ***
Comment 8 bearophile_hugs 2012-12-20 17:28:34 UTC
Some discussions here:

http://forum.dlang.org/thread/iakfgxjlfzrbxerxpria@forum.dlang.org