aboutsummaryrefslogtreecommitdiff
path: root/java/com
diff options
context:
space:
mode:
authorcushon <cushon@google.com>2018-04-13 11:17:49 -0700
committerLiam Miller-Cushon <cushon@google.com>2018-06-12 19:44:43 -0700
commitc1f59b1b0c4327c186ee84e4087b3926affd3302 (patch)
tree7d3db7582f2fc5c7d20d1e10ae01725fe6f1be14 /java/com
parent822abecd8b08182f75dfb807214b531ba0708bd5 (diff)
downloadturbine-c1f59b1b0c4327c186ee84e4087b3926affd3302.tar.gz
Don't report an error if a static type import cannot be resolved
Single-type imports are allowed to be static, but if they are they cannot be distinguished from member imports until members are resolved. If a static import cannot be processed during type resolution, assume it was a non-type member instead of reporting an error. MOE_MIGRATED_REVID=192795814
Diffstat (limited to 'java/com')
-rw-r--r--java/com/google/turbine/binder/Resolve.java25
-rw-r--r--java/com/google/turbine/binder/lookup/CanonicalSymbolResolver.java8
-rw-r--r--java/com/google/turbine/binder/lookup/ImportIndex.java41
-rw-r--r--java/com/google/turbine/binder/lookup/ImportScope.java20
-rw-r--r--java/com/google/turbine/binder/lookup/MemberImportIndex.java28
5 files changed, 62 insertions, 60 deletions
diff --git a/java/com/google/turbine/binder/Resolve.java b/java/com/google/turbine/binder/Resolve.java
index d19f97d..d722373 100644
--- a/java/com/google/turbine/binder/Resolve.java
+++ b/java/com/google/turbine/binder/Resolve.java
@@ -27,11 +27,7 @@ import com.google.turbine.binder.env.Env;
import com.google.turbine.binder.env.LazyEnv.LazyBindingError;
import com.google.turbine.binder.lookup.CanonicalSymbolResolver;
import com.google.turbine.binder.lookup.ImportScope.ResolveFunction;
-import com.google.turbine.binder.lookup.LookupResult;
import com.google.turbine.binder.sym.ClassSymbol;
-import com.google.turbine.diag.SourceFile;
-import com.google.turbine.diag.TurbineError;
-import com.google.turbine.diag.TurbineError.ErrorKind;
import com.google.turbine.model.TurbineVisibility;
import java.util.Objects;
@@ -104,27 +100,6 @@ public class Resolve {
}
@Override
- public ClassSymbol resolve(SourceFile source, int position, LookupResult result) {
- ClassSymbol sym = (ClassSymbol) result.sym();
- for (String bit : result.remaining()) {
- sym = resolveNext(source, position, sym, bit);
- }
- return sym;
- }
-
- private ClassSymbol resolveNext(SourceFile source, int position, ClassSymbol sym, String bit) {
- ClassSymbol next = resolveOne(sym, bit);
- if (next == null) {
- throw TurbineError.format(
- source,
- position,
- ErrorKind.SYMBOL_NOT_FOUND,
- new ClassSymbol(sym.binaryName() + '$' + bit));
- }
- return next;
- }
-
- @Override
public ClassSymbol resolveOne(ClassSymbol sym, String bit) {
BoundClass ci = env.get(sym);
if (ci == null) {
diff --git a/java/com/google/turbine/binder/lookup/CanonicalSymbolResolver.java b/java/com/google/turbine/binder/lookup/CanonicalSymbolResolver.java
index 64d7c10..1e6eee1 100644
--- a/java/com/google/turbine/binder/lookup/CanonicalSymbolResolver.java
+++ b/java/com/google/turbine/binder/lookup/CanonicalSymbolResolver.java
@@ -17,17 +17,9 @@
package com.google.turbine.binder.lookup;
import com.google.turbine.binder.sym.ClassSymbol;
-import com.google.turbine.diag.SourceFile;
/** Canonical type resolution. Breaks a circular dependency between binding and import handling. */
public interface CanonicalSymbolResolver extends ImportScope.ResolveFunction {
- /**
- * Resolves a type by canonical name (member types must be qualified by the type that declares
- * them, not by types that are inherited into).
- */
- @Override
- ClassSymbol resolve(SourceFile source, int position, LookupResult result);
-
/** Resolves a single member type of the given symbol by canonical name. */
@Override
ClassSymbol resolveOne(ClassSymbol sym, String bit);
diff --git a/java/com/google/turbine/binder/lookup/ImportIndex.java b/java/com/google/turbine/binder/lookup/ImportIndex.java
index d644db2..eb3ac8f 100644
--- a/java/com/google/turbine/binder/lookup/ImportIndex.java
+++ b/java/com/google/turbine/binder/lookup/ImportIndex.java
@@ -90,7 +90,7 @@ public class ImportIndex implements ImportScope {
new Supplier<ImportScope>() {
@Override
public ImportScope get() {
- return staticNamedImport(source, cpi, i);
+ return staticNamedImport(cpi, i);
}
}));
}
@@ -108,15 +108,36 @@ public class ImportIndex implements ImportScope {
ErrorKind.SYMBOL_NOT_FOUND,
new ClassSymbol(Joiner.on('/').join(i.type())));
}
- ClassSymbol sym = resolve.resolve(source, i.position(), result);
+ ClassSymbol sym = (ClassSymbol) result.sym();
+ for (String bit : result.remaining()) {
+ sym = resolveNext(source, i.position(), resolve, sym, bit);
+ }
+ ClassSymbol resolved = sym;
return new ImportScope() {
@Override
public LookupResult lookup(LookupKey lookupKey, ResolveFunction unused) {
- return new LookupResult(sym, lookupKey);
+ return new LookupResult(resolved, lookupKey);
}
};
}
+ private static ClassSymbol resolveNext(
+ SourceFile source,
+ int position,
+ CanonicalSymbolResolver resolve,
+ ClassSymbol sym,
+ String bit) {
+ ClassSymbol next = resolve.resolveOne(sym, bit);
+ if (next == null) {
+ throw TurbineError.format(
+ source,
+ position,
+ ErrorKind.SYMBOL_NOT_FOUND,
+ new ClassSymbol(sym.binaryName() + '$' + bit));
+ }
+ return next;
+ }
+
/**
* Resolve the base class symbol of a possibly non-canonical static named import. For example,
* {@code import static java.util.HashMap.Entry;} is a non-canonical import for {@code
@@ -124,7 +145,7 @@ public class ImportIndex implements ImportScope {
* hierarchy analysis is complete, so for now we resolve the base {@code java.util.HashMap} and
* defer the rest.
*/
- private static ImportScope staticNamedImport(SourceFile source, TopLevelIndex cpi, ImportDecl i) {
+ private static ImportScope staticNamedImport(TopLevelIndex cpi, ImportDecl i) {
LookupResult base = cpi.scope().lookup(new LookupKey(i.type()));
if (base == null) {
return null;
@@ -132,8 +153,16 @@ public class ImportIndex implements ImportScope {
return new ImportScope() {
@Override
public LookupResult lookup(LookupKey lookupKey, ResolveFunction resolve) {
- ClassSymbol result = resolve.resolve(source, i.position(), base);
- return new LookupResult(result, lookupKey);
+ ClassSymbol sym = (ClassSymbol) base.sym();
+ for (String bit : base.remaining()) {
+ sym = resolve.resolveOne(sym, bit);
+ if (sym == null) {
+ // Assume that static imports that don't resolve to types are non-type member imports,
+ // even if the simple name matched what we're looking for.
+ return null;
+ }
+ }
+ return new LookupResult(sym, lookupKey);
}
};
}
diff --git a/java/com/google/turbine/binder/lookup/ImportScope.java b/java/com/google/turbine/binder/lookup/ImportScope.java
index 949b3d1..8b28cb3 100644
--- a/java/com/google/turbine/binder/lookup/ImportScope.java
+++ b/java/com/google/turbine/binder/lookup/ImportScope.java
@@ -17,9 +17,6 @@
package com.google.turbine.binder.lookup;
import com.google.turbine.binder.sym.ClassSymbol;
-import com.google.turbine.diag.SourceFile;
-import com.google.turbine.diag.TurbineError;
-import com.google.turbine.diag.TurbineError.ErrorKind;
/**
* A scope for imports. Non-canonical imports depend on hierarchy analysis, so to break the cycle we
@@ -34,24 +31,7 @@ public interface ImportScope {
*/
@FunctionalInterface
interface ResolveFunction {
-
ClassSymbol resolveOne(ClassSymbol base, String name);
-
- default ClassSymbol resolve(SourceFile source, int position, LookupResult result) {
- ClassSymbol sym = (ClassSymbol) result.sym();
- for (String bit : result.remaining()) {
- ClassSymbol next = resolveOne(sym, bit);
- if (next == null) {
- throw TurbineError.format(
- source,
- position,
- ErrorKind.SYMBOL_NOT_FOUND,
- new ClassSymbol(sym.binaryName() + '$' + bit));
- }
- sym = next;
- }
- return sym;
- }
}
/** See {@link Scope#lookup(LookupKey)}. */
diff --git a/java/com/google/turbine/binder/lookup/MemberImportIndex.java b/java/com/google/turbine/binder/lookup/MemberImportIndex.java
index e2ce5cb..45eefb5 100644
--- a/java/com/google/turbine/binder/lookup/MemberImportIndex.java
+++ b/java/com/google/turbine/binder/lookup/MemberImportIndex.java
@@ -23,6 +23,8 @@ import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.diag.SourceFile;
+import com.google.turbine.diag.TurbineError;
+import com.google.turbine.diag.TurbineError.ErrorKind;
import com.google.turbine.tree.Tree.ImportDecl;
import java.util.Iterator;
import java.util.LinkedHashMap;
@@ -53,7 +55,14 @@ public class MemberImportIndex {
@Override
public ClassSymbol get() {
LookupResult result = tli.scope().lookup(new LookupKey(i.type()));
- return result != null ? resolve.resolve(source, i.position(), result) : null;
+ if (result == null) {
+ return null;
+ }
+ ClassSymbol sym = (ClassSymbol) result.sym();
+ for (String bit : result.remaining()) {
+ sym = resolveNext(resolve, source, i.position(), sym, bit);
+ }
+ return sym;
}
}));
} else {
@@ -79,6 +88,23 @@ public class MemberImportIndex {
this.classes = packageScopes.build();
}
+ private static ClassSymbol resolveNext(
+ CanonicalSymbolResolver resolve,
+ SourceFile source,
+ int position,
+ ClassSymbol sym,
+ String bit) {
+ ClassSymbol next = resolve.resolveOne(sym, bit);
+ if (next == null) {
+ throw TurbineError.format(
+ source,
+ position,
+ ErrorKind.SYMBOL_NOT_FOUND,
+ new ClassSymbol(sym.binaryName() + '$' + bit));
+ }
+ return next;
+ }
+
/** Resolves the owner of a single-member static import of the given simple name. */
public ClassSymbol singleMemberImport(String simpleName) {
Supplier<ClassSymbol> cachedResult = cache.get(simpleName);