KT-224 "Overload ambiguity" when calling toString() method

This commit is contained in:
Andrey Breslav
2012-02-17 21:01:06 +04:00
parent bf508ae03e
commit a87c0903f2
20 changed files with 92 additions and 78 deletions
@@ -58,7 +58,7 @@ public class GenerationState {
public GenerationState(Project project, ClassBuilderFactory builderFactory, FileNameTransformer fileNameTransformer) {
this.project = project;
this.standardLibrary = JetStandardLibrary.getJetStandardLibrary(project);
this.standardLibrary = JetStandardLibrary.getInstance();
this.factory = new ClassFileFactory(builderFactory, this);
this.intrinsics = new IntrinsicMethods(project, standardLibrary);
this.fileNameTransformer = fileNameTransformer;
@@ -22,6 +22,7 @@ import com.intellij.mock.MockApplication;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.jet.lang.parsing.JetParserDefinition;
import org.jetbrains.jet.lang.types.JetStandardLibrary;
import org.jetbrains.jet.plugin.JetFileType;
import org.jetbrains.jet.plugin.compiler.PathUtil;
@@ -47,6 +48,8 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
for (VirtualFile root : PathUtil.getAltHeadersRoots()) {
addLibraryRoot(root);
}
JetStandardLibrary.initialize(getProject());
}
public MockApplication getApplication() {
@@ -33,7 +33,7 @@ public class JetSemanticServices {
}
public static JetSemanticServices createSemanticServices(Project project) {
return new JetSemanticServices(JetStandardLibrary.getJetStandardLibrary(project));
return new JetSemanticServices(JetStandardLibrary.getInstance());
}
private final JetStandardLibrary standardLibrary;
@@ -47,29 +47,21 @@ import java.util.*;
*/
public class JetStandardLibrary {
// TODO : consider releasing this memory
private static JetStandardLibrary cachedLibrary = null;
// A temporary try to find a reason of KT-224
private static int wasProcessCanceledException = 0;
// private static final Map<Project, JetStandardLibrary> standardLibraryCache = new HashMap<Project, JetStandardLibrary>();
private static JetStandardLibrary instance = null;
// TODO : double checked locking
synchronized
public static JetStandardLibrary getJetStandardLibrary(@NotNull Project project) {
if (cachedLibrary == null) {
cachedLibrary = new JetStandardLibrary(project);
// This method must be called at least once per application run, on any project
// before any type checking is run
public static synchronized void initialize(@NotNull Project project) {
if (instance == null) {
instance = new JetStandardLibrary(project);
}
return cachedLibrary;
// JetStandardLibrary standardLibrary = standardLibraryCache.get(project);
// if (standardLibrary == null) {
// standardLibrary = new JetStandardLibrary(project);
// standardLibraryCache.put(project, standardLibrary);
// }
// return standardLibrary;
}
private final Project project;
@NotNull // This asserts that initialize() is called before any resolution happens
public static JetStandardLibrary getInstance() {
return instance;
}
private JetScope libraryScope;
private ClassDescriptor numberClass;
@@ -78,7 +70,6 @@ public class JetStandardLibrary {
private ClassDescriptor stringClass;
private ClassDescriptor arrayClass;
private ClassDescriptor iterableClass;
private ClassDescriptor typeInfoClass;
private ClassDescriptor comparableClass;
private ClassDescriptor volatileClass;
@@ -90,10 +81,6 @@ public class JetStandardLibrary {
private JetType nullableTuple0Type;
public JetType getTuple0Type() {
return tuple0Type;
}
private JetType tuple0Type;
private Set<FunctionDescriptor> typeInfoFunction;
@@ -108,8 +95,6 @@ public class JetStandardLibrary {
private Map<JetType, JetType> jetArrayTypeToPrimitiveJetType;
private JetStandardLibrary(@NotNull Project project) {
this.project = project;
// TODO : review
List<String> libraryFiles = Arrays.asList(
"Library.jet",
@@ -134,26 +119,17 @@ public class JetStandardLibrary {
BindingTraceContext bindingTraceContext = new BindingTraceContext();
WritableScopeImpl writableScope = new WritableScopeImpl(JetStandardClasses.STANDARD_CLASSES, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, RedeclarationHandler.THROW_EXCEPTION).setDebugName("Root bootstrap scope");
writableScope.changeLockLevel(WritableScope.LockLevel.BOTH);
// this.libraryScope = bootstrappingTDA.process(JetStandardClasses.STANDARD_CLASSES, file.getRootNamespace().getDeclarations());
// bootstrappingTDA.process(writableScope, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, file.getRootNamespace().getDeclarations());
TopDownAnalyzer.processStandardLibraryNamespace(bootstrappingSemanticServices, bindingTraceContext, writableScope, JetStandardClasses.STANDARD_CLASSES_NAMESPACE, files);
// this.libraryScope = JetStandardClasses.STANDARD_CLASSES_NAMESPACE.getMemberScope();
AnalyzingUtils.throwExceptionOnErrors(bindingTraceContext.getBindingContext());
initStdClasses();
} catch (IOException e) {
throw new IllegalStateException(e);
} catch (ProcessCanceledException e) {
wasProcessCanceledException++;
throw e;
}
}
@NotNull
public Project getProject() {
return project;
}
public JetScope getLibraryScope() {
initStdClasses();
return libraryScope;
@@ -170,8 +146,6 @@ public class JetStandardLibrary {
this.iterableClass = (ClassDescriptor) libraryScope.getClassifier("Iterable");
this.comparableClass = (ClassDescriptor) libraryScope.getClassifier("Comparable");
// typeInfoNamespace = libraryScope.getNamespace("typeinfo");
this.typeInfoClass = (ClassDescriptor) libraryScope.getClassifier("TypeInfo");
this.typeInfoFunction = libraryScope.getFunctions("typeinfo");
this.stringType = new JetTypeImpl(getString());
@@ -296,28 +270,11 @@ public class JetStandardLibrary {
return comparableClass;
}
// public NamespaceDescriptor getTypeInfoNamespace() {
// initStdClasses();
// return typeInfoNamespace;
// }
//
public ClassDescriptor getTypeInfo() {
initStdClasses();
return typeInfoClass;
}
public Set<FunctionDescriptor> getTypeInfoFunctions() {
initStdClasses();
return typeInfoFunction;
}
@NotNull
public JetType getTypeInfoType(@NotNull JetType type) {
TypeProjection typeProjection = new TypeProjection(type);
List<TypeProjection> arguments = Collections.singletonList(typeProjection);
return new JetTypeImpl(Collections.<AnnotationDescriptor>emptyList(), getTypeInfo().getTypeConstructor(), false, arguments, getTypeInfo().getMemberScope(arguments));
}
@NotNull
public JetType getPrimitiveJetType(PrimitiveType primitiveType) {
return primitiveTypeToJetType.get(primitiveType);
@@ -493,4 +450,8 @@ public class JetStandardLibrary {
}
return false;
}
public JetType getTuple0Type() {
return tuple0Type;
}
}
@@ -46,6 +46,7 @@ import java.util.Map;
/*package*/ class ExpressionTypingContext {
@NotNull
public static ExpressionTypingContext newContext(
@NotNull Project project,
@NotNull JetSemanticServices semanticServices,
@NotNull Map<JetPattern, DataFlowInfo> patternsToDataFlowInfo,
@NotNull Map<JetPattern, List<VariableDescriptor>> patternsToBoundVariableLists,
@@ -56,7 +57,7 @@ import java.util.Map;
@NotNull JetType expectedType,
@NotNull JetType expectedReturnType,
boolean namespacesAllowed) {
return new ExpressionTypingContext(semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists,
return new ExpressionTypingContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists,
labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
}
@@ -71,6 +72,7 @@ import java.util.Map;
// return newContext(semanticServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(), trace, scope, dataFlowInfo, expectedType, expectedReturnType);
// }
//
public final Project project;
public final JetSemanticServices semanticServices;
public final BindingTrace trace;
public final JetScope scope;
@@ -93,6 +95,7 @@ import java.util.Map;
private CompileTimeConstantResolver compileTimeConstantResolver;
private ExpressionTypingContext(
@NotNull Project project,
@NotNull JetSemanticServices semanticServices,
@NotNull Map<JetPattern, DataFlowInfo> patternsToDataFlowInfo,
@NotNull Map<JetPattern, List<VariableDescriptor>> patternsToBoundVariableLists,
@@ -103,6 +106,7 @@ import java.util.Map;
@NotNull JetType expectedType,
@NotNull JetType expectedReturnType,
boolean namespacesAllowed) {
this.project = project;
this.trace = trace;
this.patternsToBoundVariableLists = patternsToBoundVariableLists;
this.patternsToDataFlowInfo = patternsToDataFlowInfo;
@@ -118,36 +122,36 @@ import java.util.Map;
@NotNull
public ExpressionTypingContext replaceNamespacesAllowed(boolean namespacesAllowed) {
if (namespacesAllowed == this.namespacesAllowed) return this;
return newContext(semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
}
@NotNull
public ExpressionTypingContext replaceDataFlowInfo(DataFlowInfo newDataFlowInfo) {
if (newDataFlowInfo == dataFlowInfo) return this;
return newContext(semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, newDataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, newDataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
}
public ExpressionTypingContext replaceExpectedType(@Nullable JetType newExpectedType) {
if (newExpectedType == null) return replaceExpectedType(TypeUtils.NO_EXPECTED_TYPE);
if (expectedType == newExpectedType) return this;
return newContext(semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, newExpectedType, expectedReturnType, namespacesAllowed);
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, newExpectedType, expectedReturnType, namespacesAllowed);
}
public ExpressionTypingContext replaceExpectedReturnType(@Nullable JetType newExpectedReturnType) {
if (newExpectedReturnType == null) return replaceExpectedReturnType(TypeUtils.NO_EXPECTED_TYPE);
if (expectedReturnType == newExpectedReturnType) return this;
return newContext(semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, newExpectedReturnType, namespacesAllowed);
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, scope, dataFlowInfo, expectedType, newExpectedReturnType, namespacesAllowed);
}
public ExpressionTypingContext replaceBindingTrace(@NotNull BindingTrace newTrace) {
if (newTrace == trace) return this;
return newContext(semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, newTrace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, newTrace, scope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
}
@NotNull
public ExpressionTypingContext replaceScope(@NotNull JetScope newScope) {
if (newScope == scope) return this;
return newContext(semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, newScope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
return newContext(project, semanticServices, patternsToDataFlowInfo, patternsToBoundVariableLists, labelResolver, trace, newScope, dataFlowInfo, expectedType, expectedReturnType, namespacesAllowed);
}
///////////// LAZY ACCESSORS
@@ -219,6 +223,6 @@ import java.util.Map;
@NotNull
public Project getProject() {
return semanticServices.getStandardLibrary().getProject();
return project;
}
}
@@ -83,6 +83,7 @@ public class ExpressionTypingServices {
@Nullable
public JetType getType(@NotNull final JetScope scope, @NotNull JetExpression expression, @NotNull JetType expectedType, @NotNull DataFlowInfo dataFlowInfo) {
ExpressionTypingContext context = ExpressionTypingContext.newContext(
expression.getProject(),
semanticServices,
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, scope, dataFlowInfo, expectedType, FORBIDDEN, false
@@ -92,6 +93,7 @@ public class ExpressionTypingServices {
public JetType getTypeWithNamespaces(@NotNull final JetScope scope, @NotNull JetExpression expression) {
ExpressionTypingContext context = ExpressionTypingContext.newContext(
expression.getProject(),
semanticServices,
new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, scope, DataFlowInfo.EMPTY, NO_EXPECTED_TYPE, FORBIDDEN,
@@ -131,6 +133,7 @@ public class ExpressionTypingServices {
}
}
checkFunctionReturnType(function, ExpressionTypingContext.newContext(
function.getProject(),
semanticServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, functionInnerScope, dataFlowInfo, NO_EXPECTED_TYPE, expectedReturnType, false
));
@@ -178,7 +181,7 @@ public class ExpressionTypingServices {
JetExpression bodyExpression = function.getBodyExpression();
assert bodyExpression != null;
JetScope functionInnerScope = FunctionDescriptorUtil.getFunctionInnerScope(outerScope, functionDescriptor, trace);
expressionTypingFacade.getType(bodyExpression, ExpressionTypingContext.newContext(semanticServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
expressionTypingFacade.getType(bodyExpression, ExpressionTypingContext.newContext(function.getProject(), semanticServices, new HashMap<JetPattern, DataFlowInfo>(), new HashMap<JetPattern, List<VariableDescriptor>>(), new LabelResolver(),
trace, functionInnerScope, DataFlowInfo.EMPTY, NO_EXPECTED_TYPE, FORBIDDEN, false), !function.hasBlockBody());
//todo function literals
final Collection<JetExpression> returnedExpressions = Lists.newArrayList();
@@ -307,7 +310,7 @@ public class ExpressionTypingServices {
}
private ExpressionTypingContext createContext(ExpressionTypingContext oldContext, BindingTrace trace, WritableScope scope, DataFlowInfo dataFlowInfo, JetType expectedType, JetType expectedReturnType) {
return ExpressionTypingContext.newContext(oldContext.semanticServices, oldContext.patternsToDataFlowInfo, oldContext.patternsToBoundVariableLists, oldContext.labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, oldContext.namespacesAllowed);
return ExpressionTypingContext.newContext(oldContext.project, oldContext.semanticServices, oldContext.patternsToDataFlowInfo, oldContext.patternsToBoundVariableLists, oldContext.labelResolver, trace, scope, dataFlowInfo, expectedType, expectedReturnType, oldContext.namespacesAllowed);
}
private ObservableBindingTrace makeTraceInterceptingTypeMismatch(final BindingTrace trace, final JetExpression expressionToWatch, final boolean[] mismatchFound) {
@@ -155,6 +155,7 @@ public class ExpressionTypingUtils {
JetExpression expression = JetPsiFactory.createExpression(project, "fake");
ExpressionReceiver expressionReceiver = new ExpressionReceiver(expression, variableDescriptor.getOutType());
ExpressionTypingContext context = ExpressionTypingContext.newContext(
project,
JetSemanticServices.createSemanticServices(project),
new HashMap<JetPattern, DataFlowInfo>(),
new HashMap<JetPattern, List<VariableDescriptor>>(),
@@ -62,7 +62,7 @@ public class JetResolveTest extends ExtensibleResolveTestCase {
@Override
protected ExpectedResolveData getExpectedResolveData() {
Project project = getProject();
JetStandardLibrary lib = JetStandardLibrary.getJetStandardLibrary(project);
JetStandardLibrary lib = JetStandardLibrary.getInstance();
Map<String, DeclarationDescriptor> nameToDescriptor = new HashMap<String, DeclarationDescriptor>();
nameToDescriptor.put("std::Int.plus(Int)", standardFunction(lib.getInt(), "plus", lib.getIntType()));
FunctionDescriptor descriptorForGet = standardFunction(lib.getArray(), Collections.singletonList(new TypeProjection(lib.getIntType())), "get", lib.getIntType());
@@ -50,7 +50,7 @@ public class JetDefaultModalityModifiersTest extends JetLiteFixture {
private JetScope scope;
public void setUp() throws Exception {
JetStandardLibrary library = JetStandardLibrary.getJetStandardLibrary(getProject());
JetStandardLibrary library = JetStandardLibrary.getInstance();
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(library);
descriptorResolver = semanticServices.getClassDescriptorResolver(JetTestUtils.DUMMY_EXCEPTION_ON_ERROR_TRACE);
scope = createScope(library.getLibraryScope());
@@ -41,7 +41,7 @@ public class JetOverloadTest extends JetLiteFixture {
@Override
public void setUp() throws Exception {
super.setUp();
library = JetStandardLibrary.getJetStandardLibrary(getProject());
library = JetStandardLibrary.getInstance();
semanticServices = JetSemanticServices.createSemanticServices(library);
descriptorResolver = semanticServices.getClassDescriptorResolver(JetTestUtils.DUMMY_TRACE);
}
@@ -41,7 +41,7 @@ public class JetOverridingTest extends JetLiteFixture {
@Override
public void setUp() throws Exception {
super.setUp();
library = JetStandardLibrary.getJetStandardLibrary(getProject());
library = JetStandardLibrary.getInstance();
semanticServices = JetSemanticServices.createSemanticServices(library);
descriptorResolver = semanticServices.getClassDescriptorResolver(JetTestUtils.DUMMY_TRACE);
}
@@ -59,7 +59,7 @@ public class JetTypeCheckerTest extends JetLiteFixture {
@Override
public void setUp() throws Exception {
super.setUp();
library = JetStandardLibrary.getJetStandardLibrary(getProject());
library = JetStandardLibrary.getInstance();
semanticServices = JetSemanticServices.createSemanticServices(library);
classDefinitions = new ClassDefinitions();
descriptorResolver = semanticServices.getClassDescriptorResolver(JetTestUtils.DUMMY_TRACE);