Skip to content

Commit

Permalink
Consider visibility when resolving wildcard imports
Browse files Browse the repository at this point in the history
As reported in bazelbuild/bazel#21509

PiperOrigin-RevId: 611478456
  • Loading branch information
cushon authored and Javac Team committed Feb 29, 2024
1 parent d4e29ef commit 0664571
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 16 deletions.
33 changes: 22 additions & 11 deletions java/com/google/turbine/binder/Resolve.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public static ResolveFunction resolveFunction(
return null;
}
}

@Override
public boolean visible(ClassSymbol sym) {
String packageName = origin != null ? origin.packageName() : null;
return importVisible(env, sym, packageName);
}
};
}

Expand Down Expand Up @@ -131,18 +137,23 @@ public CanonicalResolver(String packagename, CompoundEnv<ClassSymbol, BoundClass

@Override
public boolean visible(ClassSymbol sym) {
TurbineVisibility visibility = TurbineVisibility.fromAccess(env.getNonNull(sym).access());
switch (visibility) {
case PUBLIC:
return true;
case PROTECTED:
case PACKAGE:
return Objects.equals(sym.packageName(), packagename);
case PRIVATE:
return false;
}
throw new AssertionError(visibility);
return importVisible(env, sym, packagename);
}
}

private static boolean importVisible(
Env<ClassSymbol, ? extends BoundClass> env, ClassSymbol sym, @Nullable String packagename) {
TurbineVisibility visibility = TurbineVisibility.fromAccess(env.getNonNull(sym).access());
switch (visibility) {
case PUBLIC:
return true;
case PROTECTED:
case PACKAGE:
return Objects.equals(sym.packageName(), packagename);
case PRIVATE:
return false;
}
throw new AssertionError(visibility);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion java/com/google/turbine/binder/lookup/ImportScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public interface ImportScope {
* A function that performs non-canonical resolution, see {@link
* com.google.turbine.binder.Resolve#resolve}.
*/
@FunctionalInterface
interface ResolveFunction {
@Nullable ClassSymbol resolveOne(ClassSymbol base, Tree.Ident name);

boolean visible(ClassSymbol sym);
}

/** See {@link Scope#lookup(LookupKey)}. */
Expand Down
11 changes: 7 additions & 4 deletions java/com/google/turbine/binder/lookup/WildImportIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
*
* <p>Resolution is lazy, imports are not evaluated until the first request for a matching simple
* name.
*
* <p>Static on-demand imports of types are not supported.
*/
public class WildImportIndex implements ImportScope {

Expand Down Expand Up @@ -160,9 +158,14 @@ public static WildImportIndex create(
continue;
}
LookupResult result = scope.lookup(lookup, resolve);
if (result != null) {
return result;
if (result == null) {
continue;
}
ClassSymbol sym = (ClassSymbol) result.sym();
if (!resolve.visible(sym)) {
continue;
}
return result;
}
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions javatests/com/google/turbine/lower/LowerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ public static Iterable<Object[]> parameters() {
"simplemethod.test",
"source_anno_retention.test",
"source_bootclasspath_order.test",
"star_import_visibility.test",
"star_import_visibility_nested.test",
"static_final_boxed.test",
"static_member_type_import.test",
"static_member_type_import_recursive.test",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== a/Lib.java ===
package a;
class Lib {
}
=== b/Lib.java ===
package b;
public class Lib {
}
=== T.java ===
import a.*; // a.Lib is not visible, b.Lib should be resolved
import b.*;
class T {
Lib x;
}
=== a/SamePackage.java ===
package a;
import a.*; // a.Lib is visible
import b.*;
class SamePackage {
Lib x;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== a/Lib.java ===
package a;
public class Lib {
protected static class Inner {}
}
=== b/Lib.java ===
package b;
public class Lib {
public static class Inner {}
}
=== T.java ===
import a.Lib.*;
import b.Lib.*;
class T {
Inner x;
}
=== S.java ===
import static a.Lib.*;
import static b.Lib.*;
class S {
Inner x;
}

0 comments on commit 0664571

Please sign in to comment.