Browse Source

Fixed resolving "condition ? byte : 0".

newNRvisualizers
Daniel Grunwald 14 years ago
parent
commit
9f5f18eeb2
  1. 14
      ICSharpCode.NRefactory.CSharp/Resolver/CSharpResolver.cs
  2. 2
      ICSharpCode.NRefactory.ConsistencyCheck/ResolverTest.cs
  3. 14
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/ConditionalOperatorTests.cs

14
ICSharpCode.NRefactory.CSharp/Resolver/CSharpResolver.cs

@ -2091,11 +2091,11 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -2091,11 +2091,11 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
// The operator is valid:
// a) if there's a conversion in one direction but not the other
// b) if there are conversions in both directions, and the types are equivalent
if (t2f.IsValid && !f2t.IsValid) {
if (IsBetterConditionalConversion(t2f, f2t)) {
resultType = falseExpression.Type;
isValid = true;
trueExpression = Convert(trueExpression, resultType, t2f);
} else if (f2t.IsValid && !t2f.IsValid) {
} else if (IsBetterConditionalConversion(f2t, t2f)) {
resultType = trueExpression.Type;
isValid = true;
falseExpression = Convert(falseExpression, resultType, f2t);
@ -2128,6 +2128,16 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -2128,6 +2128,16 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
}
}
bool IsBetterConditionalConversion(Conversion c1, Conversion c2)
{
// Valid is better than ImplicitConstantExpressionConversion is better than invalid
if (!c1.IsValid)
return false;
if (c1 != Conversion.ImplicitConstantExpressionConversion && c2 == Conversion.ImplicitConstantExpressionConversion)
return true;
return !c2.IsValid;
}
bool HasType(ResolveResult r)
{
return r.Type.Kind != TypeKind.Unknown && r.Type.Kind != TypeKind.Null;

2
ICSharpCode.NRefactory.ConsistencyCheck/ResolverTest.cs

@ -51,7 +51,7 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck @@ -51,7 +51,7 @@ namespace ICSharpCode.NRefactory.ConsistencyCheck
this.fileName = fileName;
// We allow errors in XAML codebehind because we're currently not adding the XAML-generated
// members to the type system.
this.allowErrors = (fileName.Contains(".xaml") || File.Exists(Path.ChangeExtension(fileName, ".xaml")) || fileName.EndsWith("AvalonDockLayout.cs"));
this.allowErrors = (fileName.Contains(".xaml") || File.Exists(Path.ChangeExtension(fileName, ".xaml")) || fileName.EndsWith("AvalonDockLayout.cs") || fileName.EndsWith("ResourcesFileTreeNode.cs"));
}
HashSet<AstNode> resolvedNodes = new HashSet<AstNode>();

14
ICSharpCode.NRefactory.Tests/CSharp/Resolver/ConditionalOperatorTests.cs

@ -145,6 +145,20 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -145,6 +145,20 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
MakeConstant(true), MakeConstant(1), MakeResult(typeof(long))));
}
[Test]
public void ByteAndZeroLiteral()
{
AssertType(typeof(int), resolver.ResolveConditional(
MakeResult(typeof(bool)), MakeResult(typeof(byte)), MakeConstant(0)));
}
[Test]
public void ByteAndUShort()
{
AssertType(typeof(ushort), resolver.ResolveConditional(
MakeResult(typeof(bool)), MakeResult(typeof(byte)), MakeResult(typeof(ushort))));
}
[Test]
public void EnumAndZeroLiteral()
{

Loading…
Cancel
Save