Skip to content

Commit 6b01de3

Browse files
usefahmed07thewilsonator
authored andcommitted
Fix issue 24632 - Fully qualified package name in package requires import
checkAccess(Scope*, Package) only treated a package as self-accessible when sc._module == p. When the fully qualified reference is to the enclosing package.d's own name, the resolved symbol is the semantic Package object, which is a distinct instance from the Module object for package.d even though p.isPackageMod() == sc._module. That equality was already checked in the sibling function hasPackageAccess but missing here, so a package.d module referencing itself by fully qualified name was wrongly told to add static import. https://issues.dlang.org/show_bug.cgi?id=24632
1 parent 2ed21d0 commit 6b01de3

7 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Fully qualified self-reference inside `package.d` no longer requires `static import`
2+
3+
Previously, referring to a package's own fully qualified name from
4+
inside its `package.d` module incorrectly required adding a
5+
`static import` of itself, even though unqualified access already
6+
worked without it.
7+
8+
---
9+
// bug/buggier/package.d
10+
module bug.buggier;
11+
12+
void fn() {}
13+
14+
void main()
15+
{
16+
bug.buggier.fn(); // used to require `static import bug.buggier;`
17+
}
18+
---

compiler/src/dmd/access.d

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ bool checkAccess(Scope* sc, Package p)
228228
{
229229
if (sc._module == p)
230230
return false;
231+
232+
// https://issues.dlang.org/show_bug.cgi?id=24632
233+
// A `package.d` module referring to its own fully qualified package
234+
// name resolves to the semantic `Package` symbol, not the `Module`
235+
// symbol for `package.d` itself, so the check above misses this case.
236+
// Treat that as a self-reference too.
237+
if (auto m = p.isPackageMod())
238+
if (sc._module == m)
239+
return false;
240+
231241
for (; sc; sc = sc.enclosing)
232242
{
233243
if (sc.scopesym && sc.scopesym.isPackageAccessible(p, Visibility(Visibility.Kind.private_)))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module bug.buggier;
2+
3+
void fn() {}
4+
5+
void self()
6+
{
7+
// https://issues.dlang.org/show_bug.cgi?id=24632
8+
// Fully qualifying a package.d module's own name from inside itself
9+
// used to require `static import bug.buggier;` -- it shouldn't.
10+
alias thisModule = bug.buggier;
11+
bug.buggier.fn();
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// https://issues.dlang.org/show_bug.cgi?id=24632
2+
// EXTRA_SOURCES: imports/test20476/bug/buggier/package.d
3+
4+
module test20476;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// https://issues.dlang.org/show_bug.cgi?id=24632
2+
// EXTRA_SOURCES: imports/fail20476/bug/buggier/package.d imports/fail20476/bug/other/package.d
3+
/*
4+
TEST_OUTPUT:
5+
---
6+
fail_compilation/imports/fail20476/bug/other/package.d(3): Error: undefined identifier `buggier` in package `bug`, perhaps add `static import bug.buggier;`
7+
---
8+
*/
9+
10+
module fail20476;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module bug.buggier;
2+
3+
void pub() {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module bug.other;
2+
3+
void main() { bug.buggier.pub(); }

0 commit comments

Comments
 (0)