added DoubleValueTypeConstructor

no need to store exact value in IntegerValueTypeConstructor, it was removed
This commit is contained in:
Svetlana Isakova
2013-07-22 16:21:00 +04:00
parent 8b7585af66
commit fccfa0051e
7 changed files with 168 additions and 60 deletions
@@ -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)
@@ -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<JetType> supertypes;
private DoubleValueTypeConstructor() {
supertypes = Lists.newArrayList(KotlinBuiltIns.getInstance().getFloatType(), KotlinBuiltIns.getInstance().getDoubleType());
}
@NotNull
@Override
public Collection<JetType> getSupertypes() {
return supertypes;
}
}
@@ -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<JetType> 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<JetType> 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();
}
}
@@ -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<JetType> 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<TypeParameterDescriptor> getParameters() {
return Collections.emptyList();
}
@NotNull
@Override
public Collection<JetType> getSupertypes() {
return supertypes;
}
@Override
public boolean isSealed() {
return false;
@@ -85,26 +53,4 @@ public class NumberValueTypeConstructor implements TypeConstructor {
public List<AnnotationDescriptor> 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 + ')';
}
}
@@ -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.<AnnotationDescriptor>emptyList(), numberValueTypeConstructor,
false, Collections.<TypeProjection>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.<AnnotationDescriptor>emptyList(), new NumberValueTypeConstructor(longValue), false,
Collections.<TypeProjection>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);
}
}
}
@@ -0,0 +1,20 @@
package a
fun <T> id(t: T): T = t
fun <T> either(t1: T, <!UNUSED_PARAMETER!>t2<!>: T): T = t1
fun test() {
val <!UNUSED_VARIABLE!>a<!>: Float = id(2.0)
val b = id(2.0)
b: Double
val c = either<Number>(1, 2.3)
c: Number
val d = either(11, 2.3)
d: Any
val <!UNUSED_VARIABLE!>e<!>: Float = <!TYPE_INFERENCE_EXPECTED_TYPE_MISMATCH!>id<!>(1)
}
@@ -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");