Skip to content
Merged
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
8 changes: 8 additions & 0 deletions changelog/private.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Deprecate the use of selectively imported private members

$(LINK2 $(ROOT_DIR)spec/attribute.html#visibility_attributes, The specification states) that a private member
is visible only from within the same module.
Prior to this release, due to a bug, private members
were visible through selective imports from other modules, violating the specification.
Beginning with this release, accessing private members from outside the module
in which they are declared will result in a deprecation message.
11 changes: 10 additions & 1 deletion src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1148,9 +1148,18 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
bool flag;
AliasDeclaration ad = imp.aliasdecls[i];
//printf("\tImport %s alias %s = %s, scope = %p\n", toPrettyChars(), aliases[i].toChars(), names[i].toChars(), ad._scope);
if (imp.mod.search(imp.loc, imp.names[i]) /*, IgnorePrivateImports*/)
Dsymbol sym = imp.mod.search(imp.loc, imp.names[i] /*, IgnorePrivateImports */);
if (sym)
{
flag = true;

// Deprecated in 2018-01.
// Change to error in 2019-01.
// @@@DEPRECATED_2019-01@@@.
import dmd.access : symbolIsVisible;
if (!symbolIsVisible(sc, sym))
imp.mod.deprecation(imp.loc, "member `%s` is not visible from module `%s`",
imp.names[i].toChars(), sc._module.toChars());
ad.dsymbolSemantic(sc);
// If the import declaration is in non-root module,
// analysis of the aliased symbol is deferred.
Expand Down
18 changes: 18 additions & 0 deletions test/fail_compilation/fail15896.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// PERMUTE_ARGS:
// REQUIRED_ARGS: -de
/*
TEST_OUTPUT:
---
fail_compilation/fail15896.d(11): Deprecation: module `imports.imp15896` member `thebar` is not visible from module `fail15896`
fail_compilation/fail15896.d(11): Deprecation: module `imports.imp15896` member `packagebar` is not visible from module `fail15896`
---
*/

import imports.imp15896 : thebar, packagebar;

int func()
{
thebar +=1;
packagebar += 1;
return 0;
}
4 changes: 4 additions & 0 deletions test/fail_compilation/imports/imp15896.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module imports.imp15896;

private int thebar=4;
package int packagebar=3;