提交 d9569c8c 编写于 作者: J jaredpar

Responding to CR feedback (changeset 1390324)

上级 3dd6ae9d
......@@ -1527,10 +1527,10 @@ private BoundExpression BindCast(CastExpressionSyntax node, DiagnosticBag diagno
return BindExplicitNullableCastFromNonNullable(node, operand, targetType, diagnostics);
}
return BindCastCore(node, operand, targetType, diagnostics);
return BindCastCore(node, operand, targetType, wasCompilerGenerated: operand.WasCompilerGenerated, diagnostics: diagnostics);
}
private BoundExpression BindCastCore(ExpressionSyntax node, BoundExpression operand, TypeSymbol targetType, DiagnosticBag diagnostics)
private BoundExpression BindCastCore(ExpressionSyntax node, BoundExpression operand, TypeSymbol targetType, bool wasCompilerGenerated, DiagnosticBag diagnostics)
{
HashSet<DiagnosticInfo> useSiteDiagnostics = null;
Conversion conversion = this.Conversions.ClassifyConversionForCast(operand, targetType, ref useSiteDiagnostics);
......@@ -1601,7 +1601,7 @@ private BoundExpression BindCastCore(ExpressionSyntax node, BoundExpression oper
hasErrors: true);
}
return CreateConversion(node, operand, conversion, isCast: true, destination: targetType, diagnostics: diagnostics);
return CreateConversion(node, operand, conversion, isCast: true, wasCompilerGenerated: wasCompilerGenerated, destination: targetType, diagnostics: diagnostics);
}
/// <summary>
......@@ -1625,13 +1625,13 @@ private BoundExpression BindExplicitNullableCastFromNonNullable(ExpressionSyntax
var underlyingConversion = Conversions.ClassifyConversion(operand.Type, underlyingTargetType, ref unused, builtinOnly: true);
if (!underlyingConversion.Exists)
{
return BindCastCore(node, operand, targetType, diagnostics);
return BindCastCore(node, operand, targetType, wasCompilerGenerated: operand.WasCompilerGenerated, diagnostics: diagnostics);
}
var bag = DiagnosticBag.GetInstance();
try
{
var underlyingExpr = BindCastCore(node, operand, targetType.GetNullableUnderlyingType(), bag);
var underlyingExpr = BindCastCore(node, operand, targetType.GetNullableUnderlyingType(), wasCompilerGenerated: false, diagnostics: bag);
if (underlyingExpr.HasErrors || bag.HasAnyErrors())
{
Error(diagnostics, ErrorCode.ERR_NoExplicitConv, node, operand.Type, targetType);
......@@ -1653,10 +1653,10 @@ private BoundExpression BindExplicitNullableCastFromNonNullable(ExpressionSyntax
if (underlyingExpr.ConstantValue != null)
{
underlyingExpr.WasCompilerGenerated = true;
return BindCastCore(node, underlyingExpr, targetType, diagnostics);
return BindCastCore(node, underlyingExpr, targetType, wasCompilerGenerated: operand.WasCompilerGenerated, diagnostics: diagnostics);
}
return BindCastCore(node, operand, targetType, diagnostics);
return BindCastCore(node, operand, targetType, wasCompilerGenerated: operand.WasCompilerGenerated, diagnostics: diagnostics);
}
finally
{
......
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
......@@ -7,7 +8,6 @@
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Xunit;
using System.Collections.Generic;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
......@@ -693,6 +693,22 @@ void Test()
i = (int?)float.PositiveInfinity;
Use(i);
unchecked {
// double checks
i = (int?)3.5d;
i = (int?)double.MaxValue;
i = (int?)double.NaN;
i = (int?)double.NegativeInfinity;
i = (int?)double.PositiveInfinity;
// float checks
i = (int?)3.5d;
i = (int?)float.MaxValue;
i = (int?)float.NaN;
i = (int?)float.NegativeInfinity;
i = (int?)float.PositiveInfinity;
}
}
}
";
......
......@@ -3221,6 +3221,8 @@ static void Main()
Console.WriteLine(case1);
int? case2 = (int?)5.5;
Console.WriteLine(case2);
int? case3 = (int?)5;
Console.WriteLine(case3);
}
}
";
......@@ -3232,17 +3234,30 @@ static void Main()
var method = tree.GetRoot().DescendantNodes().OfType<MethodDeclarationSyntax>().Single();
var init0 = method.Body.Statements[0].DescendantNodes().OfType<VariableDeclaratorSyntax>().Single().Initializer.Value;
var value0 = model.GetConstantValue(init0);
var typeInfo0 = model.GetTypeInfo(init0);
Assert.True(value0.HasValue);
Assert.Equal(-1, (short)value0.Value);
Assert.True(typeInfo0.Type != null && typeInfo0.Type.SpecialType == SpecialType.System_Int16);
// The CodePlex bug indicates this should return a constant value of 5. While 'case2' should
// have that value it is not constant because of the nullable cast
var init1 = method.Body.Statements[2].DescendantNodes().OfType<VariableDeclaratorSyntax>().Single().Initializer.Value;
var value1 = model.GetConstantValue(init1);
var typeInfo1 = model.GetTypeInfo(init1);
var type1 = comp.GetSpecialType(SpecialType.System_Nullable_T).Construct(comp.GetSpecialType(SpecialType.System_Int32));
Assert.False(value1.HasValue);
Assert.True(typeInfo1.Type != null && typeInfo1.Type.Equals(type1));
var init2 = method.Body.Statements[4].DescendantNodes().OfType<VariableDeclaratorSyntax>().Single().Initializer.Value;
var value2 = model.GetConstantValue(init2);
var typeInfo2 = model.GetTypeInfo(init2);
var type2 = comp.GetSpecialType(SpecialType.System_Nullable_T).Construct(comp.GetSpecialType(SpecialType.System_Int32));
Assert.False(value2.HasValue);
Assert.True(typeInfo2.Type != null && typeInfo2.Type.Equals(type2));
var output = @"
-1
5
5";
CompileAndVerify(compilation: comp, expectedOutput: output);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册