diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/results/ResolutionResultsHandler.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/results/ResolutionResultsHandler.java index b7e5cef0c10..6a539962ed8 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/results/ResolutionResultsHandler.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/results/ResolutionResultsHandler.java @@ -162,39 +162,39 @@ public class ResolutionResultsHandler { @NotNull Set> candidates, boolean discriminateGenerics ) { - if (candidates.size() != 1) { - Set> cleanCandidates = Sets.newLinkedHashSet(candidates); - for (Iterator> iterator = cleanCandidates.iterator(); iterator.hasNext(); ) { - ResolvedCallWithTrace candidate = iterator.next(); - if (candidate.isDirty()) { - iterator.remove(); - } - } - - if (cleanCandidates.isEmpty()) { - cleanCandidates = candidates; - } - ResolvedCallWithTrace maximallySpecific = OverloadingConflictResolver.INSTANCE.findMaximallySpecific(cleanCandidates, false); - if (maximallySpecific != null) { - return OverloadResolutionResultsImpl.success(maximallySpecific); - } - - if (discriminateGenerics) { - ResolvedCallWithTrace maximallySpecificGenericsDiscriminated = OverloadingConflictResolver.INSTANCE.findMaximallySpecific(cleanCandidates, true); - if (maximallySpecificGenericsDiscriminated != null) { - return OverloadResolutionResultsImpl.success(maximallySpecificGenericsDiscriminated); - } - } - - Set> noOverrides = OverridingUtil.filterOverrides(candidates, MAP_TO_RESULT); - - return OverloadResolutionResultsImpl.ambiguity(noOverrides); + if (candidates.size() == 1) { + return OverloadResolutionResultsImpl.success(candidates.iterator().next()); } - else { - ResolvedCallWithTrace result = candidates.iterator().next(); - return OverloadResolutionResultsImpl.success(result); + Set> cleanCandidates = Sets.newLinkedHashSet(candidates); + for (Iterator> iterator = cleanCandidates.iterator(); iterator.hasNext(); ) { + ResolvedCallWithTrace candidate = iterator.next(); + if (candidate.isDirty()) { + iterator.remove(); + } } + + if (cleanCandidates.isEmpty()) { + cleanCandidates = candidates; + } + ResolvedCallWithTrace maximallySpecific = OverloadingConflictResolver.INSTANCE.findMaximallySpecific(cleanCandidates, false); + if (maximallySpecific != null) { + return OverloadResolutionResultsImpl.success(maximallySpecific); + } + + if (discriminateGenerics) { + ResolvedCallWithTrace maximallySpecificGenericsDiscriminated = OverloadingConflictResolver.INSTANCE.findMaximallySpecific(cleanCandidates, true); + if (maximallySpecificGenericsDiscriminated != null) { + return OverloadResolutionResultsImpl.success(maximallySpecificGenericsDiscriminated); + } + } + + Set> noOverrides = OverridingUtil.filterOverrides(candidates, MAP_TO_RESULT); + if (noOverrides.size() == 1) { + return OverloadResolutionResultsImpl.success(noOverrides.iterator().next()); + } + + return OverloadResolutionResultsImpl.ambiguity(noOverrides); } diff --git a/compiler/testData/diagnostics/tests/smartCasts/kt1461.kt b/compiler/testData/diagnostics/tests/smartCasts/kt1461.kt new file mode 100644 index 00000000000..585117a5696 --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/kt1461.kt @@ -0,0 +1,18 @@ +//KT-1461 Front-end complains on overload resolution when superclass property is accessed from string template and implicit type cast is performed +package f + +open class Super(val property : String) {} + +class Sub(str : String) : Super(str) {} + +fun foo(sup : Super, sub : Sub) { + if (sup is Sub) { + println("${sup.property}") + println(sup.property) + } + println("${sub.property}") + println(sub.property) +} + +//from library +fun println(message : Any?) { throw Exception() } \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index a14a3da8af2..6d2e5551ac8 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -33,7 +33,7 @@ import org.jetbrains.jet.checkers.AbstractDiagnosticsTestWithEagerResolve; @InnerTestClasses({JetDiagnosticsTestGenerated.Tests.class, JetDiagnosticsTestGenerated.Script.class}) public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEagerResolve { @TestMetadata("compiler/testData/diagnostics/tests") - @InnerTestClasses({Tests.Annotations.class, Tests.BackingField.class, Tests.Cast.class, Tests.CheckArguments.class, Tests.ControlFlowAnalysis.class, Tests.ControlStructures.class, Tests.DataClasses.class, Tests.DataFlow.class, Tests.DataFlowInfoTraversal.class, Tests.DeclarationChecks.class, Tests.Deparenthesize.class, Tests.Enum.class, Tests.Extensions.class, Tests.FunctionLiterals.class, Tests.Generics.class, Tests.IncompleteCode.class, Tests.Inference.class, Tests.Infos.class, Tests.Inner.class, Tests.J_k.class, Tests.Jdk_annotations.class, Tests.Library.class, Tests.NullabilityAndAutoCasts.class, Tests.NullableTypes.class, Tests.Objects.class, Tests.OperatorsOverloading.class, Tests.Overload.class, Tests.Override.class, Tests.Redeclarations.class, Tests.Regressions.class, Tests.Resolve.class, Tests.Scopes.class, Tests.SenselessComparison.class, Tests.Shadowing.class, Tests.Substitutions.class, Tests.Subtyping.class, Tests.ThisAndSuper.class, Tests.Tuples.class, Tests.Varargs.class}) + @InnerTestClasses({Tests.Annotations.class, Tests.BackingField.class, Tests.Cast.class, Tests.CheckArguments.class, Tests.ControlFlowAnalysis.class, Tests.ControlStructures.class, Tests.DataClasses.class, Tests.DataFlow.class, Tests.DataFlowInfoTraversal.class, Tests.DeclarationChecks.class, Tests.Deparenthesize.class, Tests.Enum.class, Tests.Extensions.class, Tests.FunctionLiterals.class, Tests.Generics.class, Tests.IncompleteCode.class, Tests.Inference.class, Tests.Infos.class, Tests.Inner.class, Tests.J_k.class, Tests.Jdk_annotations.class, Tests.Library.class, Tests.NullabilityAndAutoCasts.class, Tests.NullableTypes.class, Tests.Objects.class, Tests.OperatorsOverloading.class, Tests.Overload.class, Tests.Override.class, Tests.Redeclarations.class, Tests.Regressions.class, Tests.Resolve.class, Tests.Scopes.class, Tests.SenselessComparison.class, Tests.Shadowing.class, Tests.SmartCasts.class, Tests.Substitutions.class, Tests.Subtyping.class, Tests.ThisAndSuper.class, Tests.Tuples.class, Tests.Varargs.class}) public static class Tests extends AbstractDiagnosticsTestWithEagerResolve { @TestMetadata("Abstract.kt") public void testAbstract() throws Exception { @@ -4202,6 +4202,19 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage } + @TestMetadata("compiler/testData/diagnostics/tests/smartCasts") + public static class SmartCasts extends AbstractDiagnosticsTestWithEagerResolve { + public void testAllFilesPresentInSmartCasts() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/smartCasts"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("kt1461.kt") + public void testKt1461() throws Exception { + doTest("compiler/testData/diagnostics/tests/smartCasts/kt1461.kt"); + } + + } + @TestMetadata("compiler/testData/diagnostics/tests/substitutions") public static class Substitutions extends AbstractDiagnosticsTestWithEagerResolve { public void testAllFilesPresentInSubstitutions() throws Exception { @@ -4434,6 +4447,7 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage suite.addTestSuite(Scopes.class); suite.addTestSuite(SenselessComparison.class); suite.addTestSuite(Shadowing.class); + suite.addTestSuite(SmartCasts.class); suite.addTestSuite(Substitutions.class); suite.addTestSuite(Subtyping.class); suite.addTestSuite(ThisAndSuper.class);