Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions changelog/deprecate_compile_seperately_inconsistency.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Make imports behave the same whether or not both modules were given on the command line

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.
---
// main.d
import foo;
---
---
// foo.d
module bar;
---
Before, this module would fail if compiled like this:
$(CONSOLE dmd -c main.d foo.d)

and pass if compiled like this:
$(CONSOLE
dmd -c main.d
dmd -c foo.d
)

Now it will fail in both cases.
31 changes: 30 additions & 1 deletion src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,36 @@ extern (C++) final class Module : Package
buf.printf("%s\t(%s)", ident.toChars(), m.srcfile.toChars());
message("import %s", buf.peekString());
}
m = m.parse();
{
auto originalModule = m;
m = m.parse();

// verify the module name matches the imported module name
if (m is originalModule && m.md !is null)
{
bool mismatch = false;
if (!ident.equals(m.md.id))
{
mismatch = true;
}
else
{
auto mdPackageCount = (m.md.packages == null) ? 0 : m.md.packages.dim;
auto packageCount = (packages == null) ? 0 : packages.dim;
for (size_t i = 1; i <= mdPackageCount && i <= packageCount; i++)
{
if (!(*m.md.packages)[mdPackageCount - i].equals((*packages)[packageCount - i]))
{
mismatch = true;
break;
}
}
}
if (mismatch)
m.deprecation(loc, "from file %s must be imported with 'import %s;'",
m.srcfile.name.toChars(), m.toPrettyChars());
}
}

// Call onImport here because if the module is going to be compiled then we
// need to determine it early because it affects semantic analysis. This is
Expand Down
10 changes: 10 additions & 0 deletions test/compilable/badimport.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
C1OMPILED_IMPORTS: imports/wrongpkgname.d
REQUIRED_ARGS: -Ifail_compilation
PERMUTE_ARGS:
TEST_OUTPUT:
---
compilable/badimport.d(10): Deprecation: module `wrongpkg.wrongpkgname` from file compilable/imports/wrongpkgname.d must be imported with 'import wrongpkg.wrongpkgname;'
---
*/
import imports.wrongpkgname;
9 changes: 9 additions & 0 deletions test/compilable/badimport2.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
REQUIRED_ARGS: -Icompilable/imports
PERMUTE_ARGS:
TEST_OUTPUT:
---
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;'
---
*/
import wrong_mod_name;
1 change: 1 addition & 0 deletions test/compilable/imports/a15086.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module foo;
1 change: 1 addition & 0 deletions test/compilable/imports/wrong_mod_name.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module wrong_mod_name_bleh;
1 change: 1 addition & 0 deletions test/compilable/imports/wrongpkgname.d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module wrongpkg.wrongpkgname;
8 changes: 8 additions & 0 deletions test/compilable/test15086.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
PERMUTE_ARGS:
TEST_OUTPUT:
---
compilable/test15086.d(8): Deprecation: module `foo` from file compilable/imports/a15086.d must be imported with 'import foo;'
---
*/
import imports.a15086;
5 changes: 3 additions & 2 deletions test/compilable/test313f.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// REQUIRED_ARGS: -de
import imports.f313;
// EXTRA_SOURCES: imports/f313.d
import foo.bar;

void test()
{
imports.f313.bug();
foo.bar.bug();
}
5 changes: 3 additions & 2 deletions test/compilable/test314.d
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// REQUIRED_ARGS: -de
// EXTRA_SOURCES: imports/a314.d
module imports.test314; // package imports

import imports.a314;
import imports.pkg.a314;

void main()
{
imports.a314.bug("This should work.\n");
imports.pkg.a314.bug("This should work.\n");
renamed.bug("This should work.\n");
bug("This should work.\n");
}
5 changes: 3 additions & 2 deletions test/compilable/test71.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import imports.test71;
// EXTRA_SOURCES: imports/test71.d
import imports_test71;

void bar()
{
imports.test71.foo();
imports_test71.foo();
}
33 changes: 27 additions & 6 deletions test/d_do_test.d
Original file line number Diff line number Diff line change
Expand Up @@ -459,24 +459,45 @@ bool collectExtraSources (in string input_dir, in string output_dir, in string[]
}

// compare output string to reference string, but ignore places
// marked by $n$ that contain compiler generated unique numbers
// marked by $n$ that contain compiler generated unique numbers and
// also ignore dir seperators '/'
bool compareOutput(string output, string refoutput)
{
import std.ascii : digits;
import std.utf : byCodeUnit;
for ( ; ; )
{
bool skipDirSeparator = false;
auto pos = refoutput.indexOf("$n$");
if (pos < 0)
return refoutput == output;
{
pos = refoutput.indexOf("/");
if (pos < 0)
{
pos = refoutput.indexOf("\\");
if (pos < 0)
return refoutput == output;
}
skipDirSeparator = true;
}
if (output.length < pos)
return false;
if (refoutput[0..pos] != output[0..pos])
return false;
refoutput = refoutput[pos + 3 ..$];
output = output[pos..$];
auto p = output.byCodeUnit.countUntil!(e => !digits.canFind(e));
output = output[p..$];
if (skipDirSeparator)
{
refoutput = refoutput[pos + 1 .. $];
if (output[pos] != '/' && output[pos] != '\\')
return false;
output = output[pos + 1 .. $];
}
else
{
refoutput = refoutput[pos + 3 ..$];
output = output[pos..$];
auto p = output.byCodeUnit.countUntil!(e => !digits.canFind(e));
output = output[p..$];
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/fail_compilation/dip22b.d
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
REQUIRED_ARGS: -de
EXTRA_SOURCES: imports/dip22c.d
TEST_OUTPUT:
---
fail_compilation/dip22b.d(12): Deprecation: `pkg.dip22c.Foo` is not visible from module `dip22`
fail_compilation/dip22b.d(13): Deprecation: `pkg.dip22c.Foo` is not visible from module `dip22`
---
*/
module pkg.dip22;
Expand Down
3 changes: 2 additions & 1 deletion test/fail_compilation/ice11513a.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
EXTRA_SOURCES: imports/ice11513x.d
TEST_OUTPUT:
---
fail_compilation/imports/ice11513x.d(1): Error: package name 'ice11513a' conflicts with usage as a module name in file fail_compilation/ice11513a.d
Expand All @@ -7,4 +8,4 @@ fail_compilation/imports/ice11513x.d(1): Error: package name 'ice11513a' conflic

module ice11513a;

import imports.ice11513x;
import ice11513a.imports;
2 changes: 1 addition & 1 deletion test/fail_compilation/imports/dip22b.d
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module imports.dip22b;
// this public import only exports symbols that are visible within this module
public import imports.dip22c;
public import pkg.dip22c;
1 change: 1 addition & 0 deletions test/fail_compilation/test64.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
TEST_OUTPUT:
---
fail_compilation/imports/test64a.d(1): Error: module `imports` from file fail_compilation/imports/test64a.d conflicts with package name imports
fail_compilation/test64.d(13): Deprecation: module `imports` from file fail_compilation/imports/test64a.d must be imported with 'import imports;'
---
*/

Expand Down
3 changes: 2 additions & 1 deletion test/runnable/link12037.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import imports.a12037;
// EXTRA_SOURCES: imports/a12037.d
import imports.aXXXXX;

alias CustomFloat!(10, 5) Float16;

Expand Down
4 changes: 2 additions & 2 deletions test/runnable/uda.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

// EXTRA_SOURCES: imports/a9741.d
import core.stdc.stdio;

template Tuple(T...)
Expand Down Expand Up @@ -320,7 +320,7 @@ struct Bug9652
/************************************************/
// 9741

import imports.a9741;
import imports.a9741b;

struct A9741 {}

Expand Down