From fccfa0051eabed88547af4ec885480e55338da5d Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Mon, 22 Jul 2013 16:21:00 +0400 Subject: [PATCH] added DoubleValueTypeConstructor no need to store exact value in IntegerValueTypeConstructor, it was removed --- .../CompileTimeConstantResolver.java | 10 +++ .../constants/DoubleValueTypeConstructor.java | 45 ++++++++++++ .../IntegerValueTypeConstructor.java | 68 +++++++++++++++++++ .../constants/NumberValueTypeConstructor.java | 56 +-------------- .../BasicExpressionTypingVisitor.java | 24 +++++-- .../numbers/doublesInSimpleConstraints.kt | 20 ++++++ .../checkers/JetDiagnosticsTestGenerated.java | 5 ++ 7 files changed, 168 insertions(+), 60 deletions(-) create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/DoubleValueTypeConstructor.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/IntegerValueTypeConstructor.java create mode 100644 compiler/testData/diagnostics/tests/numbers/doublesInSimpleConstraints.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java index 13ee1bb0a88..68e02e3d3ef 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/CompileTimeConstantResolver.java @@ -122,6 +122,16 @@ public class CompileTimeConstantResolver { } } + @Nullable + public static Double parseDoubleValue(String text) { + try { + return Double.parseDouble(text); + } + catch (NumberFormatException e) { + return null; + } + } + @NotNull public CompileTimeConstant getFloatValue(@NotNull String text, @NotNull JetType expectedType) { if (noExpectedType(expectedType) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/DoubleValueTypeConstructor.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/DoubleValueTypeConstructor.java new file mode 100644 index 00000000000..307ecf87cb5 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/DoubleValueTypeConstructor.java @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2013 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.resolve.constants; + +import com.google.common.collect.Lists; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; + +import java.util.Collection; +import java.util.List; + +public class DoubleValueTypeConstructor extends NumberValueTypeConstructor { + private static final DoubleValueTypeConstructor INSTANCE = new DoubleValueTypeConstructor(); + + public static DoubleValueTypeConstructor create(double value) { + return INSTANCE; + } + + private final List supertypes; + + private DoubleValueTypeConstructor() { + supertypes = Lists.newArrayList(KotlinBuiltIns.getInstance().getFloatType(), KotlinBuiltIns.getInstance().getDoubleType()); + } + + @NotNull + @Override + public Collection getSupertypes() { + return supertypes; + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/IntegerValueTypeConstructor.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/IntegerValueTypeConstructor.java new file mode 100644 index 00000000000..f0af894da22 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/IntegerValueTypeConstructor.java @@ -0,0 +1,68 @@ +/* + * Copyright 2010-2013 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.resolve.constants; + +import com.google.common.collect.Lists; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; + +import java.util.Collection; + +public class IntegerValueTypeConstructor extends NumberValueTypeConstructor { + private final Collection supertypes = Lists.newArrayList(); + + public static IntegerValueTypeConstructor create(long value) { + return new IntegerValueTypeConstructor(value); + } + + private IntegerValueTypeConstructor(long value) { + checkBoundsAndAddSuperType(value, (long) Byte.MIN_VALUE, (long) Byte.MAX_VALUE, KotlinBuiltIns.getInstance().getByteType()); + checkBoundsAndAddSuperType(value, (long) Short.MIN_VALUE, (long) Short.MAX_VALUE, KotlinBuiltIns.getInstance().getShortType()); + checkBoundsAndAddSuperType(value, (long) Integer.MIN_VALUE, (long) Integer.MAX_VALUE, KotlinBuiltIns.getInstance().getIntType()); + supertypes.add(KotlinBuiltIns.getInstance().getLongType()); + } + + private void checkBoundsAndAddSuperType(long value, long minValue, long maxValue, JetType kotlinType) { + if (value >= minValue && value <= maxValue) { + supertypes.add(kotlinType); + } + } + + @NotNull + @Override + public Collection getSupertypes() { + return supertypes; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + IntegerValueTypeConstructor that = (IntegerValueTypeConstructor) o; + + if (!supertypes.equals(that.supertypes)) return false; + + return true; + } + + @Override + public int hashCode() { + return supertypes.hashCode(); + } +} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/NumberValueTypeConstructor.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/NumberValueTypeConstructor.java index 0346480d483..73fdf2581cf 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/NumberValueTypeConstructor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/constants/NumberValueTypeConstructor.java @@ -16,55 +16,23 @@ package org.jetbrains.jet.lang.resolve.constants; -import com.google.common.collect.Lists; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor; import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; -import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.TypeConstructor; -import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; -import java.util.Collection; import java.util.Collections; import java.util.List; -public class NumberValueTypeConstructor implements TypeConstructor { - private final long value; - private final Collection supertypes = Lists.newArrayList(); - - public NumberValueTypeConstructor(long value) { - this.value = value; - - checkBoundsAndSuperType((long)Byte.MIN_VALUE, (long)Byte.MAX_VALUE, KotlinBuiltIns.getInstance().getByteType()); - checkBoundsAndSuperType((long)Short.MIN_VALUE, (long)Short.MAX_VALUE, KotlinBuiltIns.getInstance().getShortType()); - checkBoundsAndSuperType((long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE, KotlinBuiltIns.getInstance().getIntType()); - supertypes.add(KotlinBuiltIns.getInstance().getLongType()); - } - - private void checkBoundsAndSuperType(long minValue, long maxValue, JetType kotlinType) { - if (value > minValue && value < maxValue) { - supertypes.add(kotlinType); - } - } - - public Long getValue() { - return value; - } - +public abstract class NumberValueTypeConstructor implements TypeConstructor { @NotNull @Override public List getParameters() { return Collections.emptyList(); } - @NotNull - @Override - public Collection getSupertypes() { - return supertypes; - } - @Override public boolean isSealed() { return false; @@ -85,26 +53,4 @@ public class NumberValueTypeConstructor implements TypeConstructor { public List getAnnotations() { return Collections.emptyList(); } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - NumberValueTypeConstructor that = (NumberValueTypeConstructor) o; - - if (value != that.value) return false; - - return true; - } - - @Override - public int hashCode() { - return (int) (value ^ (value >>> 32)); - } - - @Override - public String toString() { - return "NumberValueTypeConstructor(" + value + ')'; - } } diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index 4b0e35cf90c..4be2f0694aa 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -102,6 +102,18 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { return DataFlowUtils.checkType(typeInfo.getType(), expression, context, typeInfo.getDataFlowInfo()); } + private static JetTypeInfo createNumberValueTypeInfo( + @NotNull NumberValueTypeConstructor numberValueTypeConstructor, + @NotNull Number value, + @NotNull DataFlowInfo dataFlowInfo + ) { + return JetTypeInfo.create(new JetTypeImpl( + Collections.emptyList(), numberValueTypeConstructor, + false, Collections.emptyList(), + ErrorUtils.createErrorScope("Scope for number value type (" + value + ")", true)), + dataFlowInfo); + } + @Override public JetTypeInfo visitConstantExpression(JetConstantExpression expression, ExpressionTypingContext context) { ASTNode node = expression.getNode(); @@ -114,11 +126,13 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { if (elementType == JetNodeTypes.INTEGER_CONSTANT) { Long longValue = CompileTimeConstantResolver.parseLongValue(text); if (longValue != null) { - JetTypeImpl numberValueType = new JetTypeImpl( - Collections.emptyList(), new NumberValueTypeConstructor(longValue), false, - Collections.emptyList(), - ErrorUtils.createErrorScope("Scope for number value type (" + longValue + ")", true)); - return JetTypeInfo.create(numberValueType, context.dataFlowInfo); + return createNumberValueTypeInfo(IntegerValueTypeConstructor.create(longValue), longValue, context.dataFlowInfo); + } + } + else if (elementType == JetNodeTypes.FLOAT_CONSTANT) { + Double doubleValue = CompileTimeConstantResolver.parseDoubleValue(text); + if (doubleValue != null) { + return createNumberValueTypeInfo(DoubleValueTypeConstructor.create(doubleValue), doubleValue, context.dataFlowInfo); } } } diff --git a/compiler/testData/diagnostics/tests/numbers/doublesInSimpleConstraints.kt b/compiler/testData/diagnostics/tests/numbers/doublesInSimpleConstraints.kt new file mode 100644 index 00000000000..d3ac9cf14a2 --- /dev/null +++ b/compiler/testData/diagnostics/tests/numbers/doublesInSimpleConstraints.kt @@ -0,0 +1,20 @@ +package a + +fun id(t: T): T = t + +fun either(t1: T, t2: T): T = t1 + +fun test() { + val a: Float = id(2.0) + + val b = id(2.0) + b: Double + + val c = either(1, 2.3) + c: Number + + val d = either(11, 2.3) + d: Any + + val e: Float = id(1) +} \ 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 edb06552586..973bb97dcc9 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -3784,6 +3784,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/diagnostics/tests/numbers"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("doublesInSimpleConstraints.kt") + public void testDoublesInSimpleConstraints() throws Exception { + doTest("compiler/testData/diagnostics/tests/numbers/doublesInSimpleConstraints.kt"); + } + @TestMetadata("numbersInSimpleConstraints.kt") public void testNumbersInSimpleConstraints() throws Exception { doTest("compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt");