From 420a7c9ad43970e59e85cd7efa9cd7bb0a01531f Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Fri, 11 May 2012 18:00:09 +0400 Subject: [PATCH] KT-1961 Incorrect override error in functions with type parameter with two upper bounds #KT-1961 Fixed --- .../types/IntersectionTypeConstructor.java | 100 ++++++++++++++++++ .../jetbrains/jet/lang/types/TypeUtils.java | 20 +--- .../override/EqualityOfIntersectionTypes.jet | 15 +++ 3 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/types/IntersectionTypeConstructor.java create mode 100644 compiler/testData/diagnostics/tests/override/EqualityOfIntersectionTypes.jet diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/IntersectionTypeConstructor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/IntersectionTypeConstructor.java new file mode 100644 index 00000000000..2a9136302ae --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/IntersectionTypeConstructor.java @@ -0,0 +1,100 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.lang.types; + + +import com.google.common.collect.Sets; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; +import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; +import org.jetbrains.jet.lang.descriptors.annotations.AnnotatedImpl; +import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; + +import java.util.*; + +/** + * @author abreslav + */ +public class IntersectionTypeConstructor extends AnnotatedImpl implements TypeConstructor { + private final Set intersectedTypes; + private final int hashCode; + + public IntersectionTypeConstructor(List annotations, Collection typesToIntersect) { + super(annotations); + this.intersectedTypes = Sets.newLinkedHashSet(typesToIntersect); + this.hashCode = intersectedTypes.hashCode(); + } + + @NotNull + @Override + public List getParameters() { + return Collections.emptyList(); + } + + @NotNull + @Override + public Collection getSupertypes() { + return intersectedTypes; + } + + @Override + public boolean isSealed() { + return false; + } + + @Override + public ClassifierDescriptor getDeclarationDescriptor() { + return null; + } + + @Override + public String toString() { + return makeDebugNameForIntersectionType(intersectedTypes); + } + + private static String makeDebugNameForIntersectionType(Iterable resultingTypes) { + StringBuilder debugName = new StringBuilder("{"); + for (Iterator iterator = resultingTypes.iterator(); iterator.hasNext(); ) { + JetType type = iterator.next(); + + debugName.append(type.toString()); + if (iterator.hasNext()) { + debugName.append(" & "); + } + } + debugName.append("}"); + return debugName.toString(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + IntersectionTypeConstructor that = (IntersectionTypeConstructor) o; + + if (intersectedTypes != null ? !intersectedTypes.equals(that.intersectedTypes) : that.intersectedTypes != null) return false; + + return true; + } + + @Override + public int hashCode() { + return hashCode; + } + +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java index 3e2c231da4c..bf9eabc071d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/TypeUtils.java @@ -193,12 +193,8 @@ public class TypeUtils { List noAnnotations = Collections.emptyList(); - TypeConstructor constructor = new TypeConstructorImpl( - null, + TypeConstructor constructor = new IntersectionTypeConstructor( noAnnotations, - false, - makeDebugNameForIntersectionType(resultingTypes).toString(), - Collections.emptyList(), resultingTypes); JetScope[] scopes = new JetScope[resultingTypes.size()]; @@ -269,20 +265,6 @@ public class TypeUtils { } } - private static StringBuilder makeDebugNameForIntersectionType(Iterable resultingTypes) { - StringBuilder debugName = new StringBuilder("{"); - for (Iterator iterator = resultingTypes.iterator(); iterator.hasNext(); ) { - JetType type = iterator.next(); - - debugName.append(type.toString()); - if (iterator.hasNext()) { - debugName.append(" & "); - } - } - debugName.append("}"); - return debugName; - } - public static boolean canHaveSubtypes(JetTypeChecker typeChecker, JetType type) { if (type.isNullable()) { return true; diff --git a/compiler/testData/diagnostics/tests/override/EqualityOfIntersectionTypes.jet b/compiler/testData/diagnostics/tests/override/EqualityOfIntersectionTypes.jet new file mode 100644 index 00000000000..33c380bc350 --- /dev/null +++ b/compiler/testData/diagnostics/tests/override/EqualityOfIntersectionTypes.jet @@ -0,0 +1,15 @@ +trait Foo +trait Bar + +trait A { + fun foo() + where T : Foo, T : Bar + = #() +} + +class B : A { + override fun foo() + where T : Foo, T : Bar + = #() + +} \ No newline at end of file