Skip to content

Commit 2fe2d87

Browse files
author
Jonathan Marler
committed
Unified import mismatch behavior whether or not modules were given on the command line
Fix issue 15086
1 parent f53cabe commit 2fe2d87

18 files changed

Lines changed: 127 additions & 19 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Make imports behave the same whether or not both modules were given on the command line
2+
3+
Before, if an import statement matches a filename but does not match its module name, an error is asserted, but only if both modules have been given on the command line. Now it will be an error even if the modules are not both given on the command line, i.e.
4+
---
5+
// main.d
6+
import foo;
7+
---
8+
---
9+
// foo.d
10+
module bar;
11+
---
12+
Before, this module would fail if compiled like this:
13+
$(CONSOLE dmd -c main.d foo.d)
14+
15+
and pass if compiled like this:
16+
$(CONSOLE
17+
dmd -c main.d
18+
dmd -c foo.d
19+
)
20+
21+
Now it will fail in both cases.

src/dmd/dmodule.d

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,36 @@ extern (C++) final class Module : Package
514514
buf.printf("%s\t(%s)", ident.toChars(), m.srcfile.toChars());
515515
message("import %s", buf.peekString());
516516
}
517-
m = m.parse();
517+
{
518+
auto originalModule = m;
519+
m = m.parse();
520+
521+
// verify the module name matches the imported module name
522+
if (m is originalModule && m.md !is null)
523+
{
524+
bool mismatch = false;
525+
if (!ident.equals(m.md.id))
526+
{
527+
mismatch = true;
528+
}
529+
else
530+
{
531+
auto mdPackageCount = (m.md.packages == null) ? 0 : m.md.packages.dim;
532+
auto packageCount = (packages == null) ? 0 : packages.dim;
533+
for (size_t i = 1; i <= mdPackageCount && i <= packageCount; i++)
534+
{
535+
if (!(*m.md.packages)[mdPackageCount - i].equals((*packages)[packageCount - i]))
536+
{
537+
mismatch = true;
538+
break;
539+
}
540+
}
541+
}
542+
if (mismatch)
543+
m.deprecation(loc, "from file %s must be imported with 'import %s;'",
544+
m.srcfile.name.toChars(), m.toPrettyChars());
545+
}
546+
}
518547

519548
// Call onImport here because if the module is going to be compiled then we
520549
// need to determine it early because it affects semantic analysis. This is

test/compilable/badimport.d

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
C1OMPILED_IMPORTS: imports/wrongpkgname.d
3+
REQUIRED_ARGS: -Ifail_compilation
4+
PERMUTE_ARGS:
5+
TEST_OUTPUT:
6+
---
7+
compilable/badimport.d(10): Deprecation: module `wrongpkg.wrongpkgname` from file compilable/imports/wrongpkgname.d must be imported with 'import wrongpkg.wrongpkgname;'
8+
---
9+
*/
10+
import imports.wrongpkgname;

test/compilable/badimport2.d

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
REQUIRED_ARGS: -Icompilable/imports
3+
PERMUTE_ARGS:
4+
TEST_OUTPUT:
5+
---
6+
compilable/badimport2.d(9): Deprecation: module `wrong_mod_name_bleh` from file compilable/imports/wrong_mod_name.d must be imported with 'import wrong_mod_name_bleh;'
7+
---
8+
*/
9+
import wrong_mod_name;

test/compilable/imports/a15086.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module foo;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module wrong_mod_name_bleh;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module wrongpkg.wrongpkgname;

test/compilable/test15086.d

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
PERMUTE_ARGS:
3+
TEST_OUTPUT:
4+
---
5+
compilable/test15086.d(8): Deprecation: module `foo` from file compilable/imports/a15086.d must be imported with 'import foo;'
6+
---
7+
*/
8+
import imports.a15086;

test/compilable/test313f.d

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// REQUIRED_ARGS: -de
2-
import imports.f313;
2+
// EXTRA_SOURCES: imports/f313.d
3+
import foo.bar;
34

45
void test()
56
{
6-
imports.f313.bug();
7+
foo.bar.bug();
78
}

test/compilable/test314.d

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// REQUIRED_ARGS: -de
2+
// EXTRA_SOURCES: imports/a314.d
23
module imports.test314; // package imports
34

4-
import imports.a314;
5+
import imports.pkg.a314;
56

67
void main()
78
{
8-
imports.a314.bug("This should work.\n");
9+
imports.pkg.a314.bug("This should work.\n");
910
renamed.bug("This should work.\n");
1011
bug("This should work.\n");
1112
}

0 commit comments

Comments
 (0)