From fd7abde114a14413ddaf9c68e0c95850c2ff20c1 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 17 Mar 2011 12:12:41 +0300 Subject: [PATCH] Unary + and - for numbers --- idea/src/jet/lang/Library.jet | 14 +++++++++++++ .../lang/parsing/JetExpressionParsing.java | 2 +- .../jet/lang/types/JetTypeInferrer.java | 20 ++++++++++++++----- .../resolve/ResolveOfInfixExpressions.jet | 8 ++++++-- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/idea/src/jet/lang/Library.jet b/idea/src/jet/lang/Library.jet index 212dbd82d92..e79780810b4 100644 --- a/idea/src/jet/lang/Library.jet +++ b/idea/src/jet/lang/Library.jet @@ -104,6 +104,8 @@ class Double : Number, Comparable { fun inc() : Double fun dec() : Double + fun plus() : Double + fun minus() : Double } class Float : Number, Comparable { @@ -165,6 +167,8 @@ class Float : Number, Comparable { fun inc() : Float fun dec() : Float + fun plus() : Float + fun minus() : Float } class Long : Number, Comparable { @@ -226,6 +230,8 @@ class Long : Number, Comparable { fun inc() : Long fun dec() : Long + fun plus() : Long + fun minus() : Long } class Int : Number, Comparable { @@ -287,6 +293,8 @@ class Int : Number, Comparable { fun inc() : Int fun dec() : Int + fun plus() : Int + fun minus() : Int } class Char : Number, Comparable { @@ -348,6 +356,8 @@ class Char : Number, Comparable { fun inc() : Char fun dec() : Char + fun plus() : Int + fun minus() : Int } class Short : Number, Comparable { @@ -409,6 +419,8 @@ class Short : Number, Comparable { fun inc() : Short fun dec() : Short + fun plus() : Int + fun minus() : Int } class Byte : Number, Comparable { @@ -470,4 +482,6 @@ class Byte : Number, Comparable { fun inc() : Byte fun dec() : Byte + fun plus() : Int + fun minus() : Int } \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java b/idea/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java index 971b240b7ec..040aafea8f9 100644 --- a/idea/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java +++ b/idea/src/org/jetbrains/jet/lang/parsing/JetExpressionParsing.java @@ -1358,7 +1358,7 @@ public class JetExpressionParsing extends AbstractJetParsing { } /* - * "new" constructorInvocation // identical to new nunctionCall + * "new" constructorInvocation // identical to new functionCall * * constructorInvocation * : userType valueArguments diff --git a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java index 9da268d9306..2a92bed8cee 100644 --- a/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java +++ b/idea/src/org/jetbrains/jet/lang/types/JetTypeInferrer.java @@ -653,16 +653,26 @@ public class JetTypeInferrer { @Override public void visitUnaryExpression(JetUnaryExpression expression) { JetSimpleNameExpression operationSign = expression.getOperationSign(); - String name = unaryOperationNames.get(operationSign.getReferencedNameElementType()); + IElementType operationType = operationSign.getReferencedNameElementType(); + String name = unaryOperationNames.get(operationType); if (name == null) { semanticServices.getErrorHandler().genericError(operationSign.getNode(), "Unknown unary operation"); } else { - JetType type = getType(scope, expression.getBaseExpression(), false); - if (type != null) { - FunctionDescriptor functionDescriptor = lookupFunction(scope, expression.getOperationSign(), name, type, Collections.emptyList(), true); + JetType receiverType = getType(scope, expression.getBaseExpression(), false); + if (receiverType != null) { + FunctionDescriptor functionDescriptor = lookupFunction(scope, expression.getOperationSign(), name, receiverType, Collections.emptyList(), true); if (functionDescriptor != null) { - result = functionDescriptor.getUnsubstitutedReturnType(); + JetType returnType = functionDescriptor.getUnsubstitutedReturnType(); + if (operationType == JetTokens.PLUSPLUS || operationType == JetTokens.MINUSMINUS) { + if (!semanticServices.getTypeChecker().isSubtypeOf(returnType, receiverType)) { + semanticServices.getErrorHandler().genericError(operationSign.getNode(), name + " must return " + receiverType + " but returns " + returnType); + } + // TODO : Maybe returnType? + result = receiverType; + } else { + result = returnType; + } } } } diff --git a/idea/testData/resolve/ResolveOfInfixExpressions.jet b/idea/testData/resolve/ResolveOfInfixExpressions.jet index 77d50f12b84..0719bac4554 100644 --- a/idea/testData/resolve/ResolveOfInfixExpressions.jet +++ b/idea/testData/resolve/ResolveOfInfixExpressions.jet @@ -44,8 +44,10 @@ import util.* ~Bar~class Bar : Foo { ~not~fun not() : String {} - ~inc~fun inc() : Unit - ~dec~fun dec() : Unit + ~inc~fun inc() : Bar + ~dec~fun dec() : Bar + ~-~fun minus() : Bar + ~+~fun plus() : Bar } fun tt(t : T) : T { @@ -68,6 +70,8 @@ fun tt(t : T) : T { `dec`--y y`dec`-- y `+=`+= 1 + `+`+y + `-`-y 1 `std::Int.compareTo(Double)`> 2.0 1 `std::Int.compareTo(Double)`< 2.0 1 `std::Int.compareTo(Double)`>= 2.0