Skip to content

Commit feb6bb6

Browse files
author
Jonathan Marler
committed
Deprecated full import/module name mismatch
Fix issue 15086
1 parent f53cabe commit feb6bb6

18 files changed

Lines changed: 135 additions & 19 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Deprecated import statements that match a module's filename but completely mismatches its name
2+
3+
Deprecated import statements that match a module's filename but completely mismatches its name, i.e.
4+
---
5+
// main.d
6+
import foo;
7+
---
8+
---
9+
// foo.d
10+
module bar;
11+
---
12+
The above code will now print:
13+
14+
$(CONSOLE Deprecation: module `bar` from file foo.d must be imported with 'import bar;')
15+
16+
Note that no deprecation occurs if the import name matches the module name at least partially, i.e.
17+
---
18+
// main.d
19+
import foo.bar1
20+
import foo.bar2;
21+
---
22+
---
23+
// foo/bar1.d
24+
module pkg.foo.bar1; // OK (module pkg.foo.bar1 partially matches import foo.bar1)
25+
---
26+
---
27+
// foo/bar2.d
28+
module bar2; // OK (module bar2 partially matches import foo.bar2)
29+
---

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)