Skip to content

Commit

Permalink
Merge pull request #6676 from RazvanN7/Deprecate_Issue_15896
Browse files Browse the repository at this point in the history
Deprecate use of private variables from other modules
merged-on-behalf-of: unknown
  • Loading branch information
dlang-bot committed Feb 2, 2018
2 parents cca2a1e + 1b14a23 commit 15c8da1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions changelog/private.dd
@@ -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
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
@@ -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
@@ -0,0 +1,4 @@
module imports.imp15896;

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

0 comments on commit 15c8da1

Please sign in to comment.