Permalink
Browse files

Fixed resolving "-2147483648".

  • Loading branch information...
dgrunwald committed Dec 9, 2011
1 parent dd59e41 commit f70a726495e6b39ab768b50b1f826ab10c840cf2
@@ -415,12 +415,17 @@ public ResolveResult ResolveUnaryOperator(UnaryOperatorType op, ResolveResult ex
CSharpOperators.UnaryOperatorMethod m = (CSharpOperators.UnaryOperatorMethod)builtinOperatorOR.BestCandidate;
IType resultType = m.ReturnType;
if (builtinOperatorOR.BestCandidateErrors != OverloadResolutionErrors.None) {
- // If there are any user-defined operators, prefer those over the built-in operators.
- // It'll be a more informative error.
- if (userDefinedOperatorOR.BestCandidate != null)
+ if (userDefinedOperatorOR.BestCandidate != null) {
+ // If there are any user-defined operators, prefer those over the built-in operators.
+ // It'll be a more informative error.
return CreateResolveResultForUserDefinedOperator(userDefinedOperatorOR);
- else
+ } else if (builtinOperatorOR.BestCandidateAmbiguousWith != null) {
+ // If the best candidate is ambiguous, just use the input type instead
+ // of picking one of the ambiguous overloads.
+ return new ErrorResolveResult(expression.Type);
+ } else {
return new ErrorResolveResult(resultType);
+ }
} else if (expression.IsCompileTimeConstant && m.CanEvaluateAtCompileTime) {
object val;
try {
@@ -1394,6 +1394,15 @@ void HandleObjectInitializer(IType type, ArrayInitializerExpression initializer)
if (resolverEnabled) {
Expression expr = unaryOperatorExpression.Expression;
ResolveResult input = Resolve(expr);
+ ITypeDefinition inputTypeDef = input.Type.GetDefinition();
+ if (input.IsCompileTimeConstant && expr is PrimitiveExpression && inputTypeDef != null) {
+ // Special cases for int.MinValue and long.MinValue
+ if (inputTypeDef.KnownTypeCode == KnownTypeCode.UInt32 && 2147483648.Equals(input.ConstantValue)) {
+ return new ConstantResolveResult(resolver.Compilation.FindType(KnownTypeCode.Int32), -2147483648);
+ } else if (inputTypeDef.KnownTypeCode == KnownTypeCode.UInt64 && 9223372036854775808.Equals(input.ConstantValue)) {
+ return new ConstantResolveResult(resolver.Compilation.FindType(KnownTypeCode.Int64), -9223372036854775808);
+ }
+ }
ResolveResult rr = resolver.ResolveUnaryOperator(unaryOperatorExpression.Operator, input);
OperatorResolveResult uorr = rr as OperatorResolveResult;
if (uorr != null && uorr.Operands.Count == 1) {
@@ -3082,6 +3091,8 @@ static IType GetElementTypeFromIEnumerable(IType collectionType, ICompilation co
var typeArguments = GetTypeArguments(simpleType.TypeArguments);
Identifier identifier = simpleType.IdentifierToken;
+ if (string.IsNullOrEmpty(identifier.Name))
+ return new TypeResolveResult(SpecialType.UnboundTypeArgument);
ResolveResult rr = resolver.LookupSimpleNameOrTypeName(identifier.Name, typeArguments, currentTypeLookupMode);
if (simpleType.Parent is Attribute && !identifier.IsVerbatim) {
var withSuffix = resolver.LookupSimpleNameOrTypeName(identifier.Name + "Attribute", typeArguments, currentTypeLookupMode);
@@ -67,10 +67,12 @@ public void CharTest1()
CheckLiteral("'\\u0356'", '\u0356');
}
- [Test, Ignore("this special case isn't implemented yet")]
+ [Test]
public void IntMinValueTest()
{
- CheckLiteral("-2147483648", -2147483648);
+ ParseUtilCSharp.AssertExpression(
+ "-2147483648",
+ new UnaryOperatorExpression(UnaryOperatorType.Minus, new PrimitiveExpression(-2147483648)));
}
[Test]
@@ -80,10 +82,12 @@ public void IntMaxValueTest()
CheckLiteral("2147483648", 2147483648); // uint
}
- [Test, Ignore("this special case isn't implemented yet")]
+ [Test]
public void LongMinValueTest()
{
- CheckLiteral("-9223372036854775808", -9223372036854775808);
+ ParseUtilCSharp.AssertExpression(
+ "-9223372036854775808",
+ new UnaryOperatorExpression(UnaryOperatorType.Minus, new PrimitiveExpression(9223372036854775808)));
}
[Test]
@@ -17,6 +17,7 @@
// DEALINGS IN THE SOFTWARE.
using System;
+using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
using NUnit.Framework;
@@ -211,5 +212,25 @@ public void TestBitwiseNotOnEnum()
AssertType(typeof(StringComparison), resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeResult(typeof(StringComparison))));
AssertType(typeof(StringComparison?), resolver.ResolveUnaryOperator(UnaryOperatorType.BitNot, MakeResult(typeof(StringComparison?))));
}
+
+ [Test]
+ public void IntMinValue()
+ {
+ // int:
+ AssertConstant(-2147483648, Resolve("class A { object x = $-2147483648$; }"));
+ AssertConstant(-/**/2147483648, Resolve("class A { object x = $-/**/2147483648$; }"));
+ // long:
+ AssertConstant(-2147483648L, Resolve("class A { object x = $-2147483648L$; }"));
+ AssertConstant(-(2147483648), Resolve("class A { object x = $-(2147483648)$; }"));
+ }
+
+ [Test]
+ public void LongMinValue()
+ {
+ // long:
+ AssertConstant(-9223372036854775808, Resolve("class A { object x = $-9223372036854775808$; }"));
+ // compiler error:
+ AssertError(typeof(ulong), Resolve("class A { object x = $-(9223372036854775808)$; }"));
+ }
}
}

0 comments on commit f70a726

Please sign in to comment.