提交 e4d141a3 编写于 作者: N nmgafter

1099252-Check that alignment is a 'good' constant (changeset 1394370)

上级 3abbc44d
......@@ -39,16 +39,17 @@ private BoundExpression BindInterpolatedString(InterpolatedStringExpressionSynta
if (interpolation.AlignmentClause != null)
{
alignment = GenerateConversionForAssignment(intType, BindValue(interpolation.AlignmentClause.Value, diagnostics, Binder.BindValueKind.RValue), diagnostics);
if (alignment.ConstantValue != null)
var alignmentConstant = alignment.ConstantValue;
if (alignmentConstant != null && !alignmentConstant.IsBad)
{
const int magnitudeLimit = 32767;
// check that the magnitude of the alignment is "in range".
int alignmentValue = alignment.ConstantValue.Int32Value;
int alignmentValue = alignmentConstant.Int32Value;
// We do the arithmetic using negative numbers because the largest negative int has no corresponding positive (absolute) value.
alignmentValue = (alignmentValue > 0) ? -alignmentValue : alignmentValue;
if (alignmentValue < -magnitudeLimit)
{
diagnostics.Add(ErrorCode.WRN_AlignmentMagnitude, alignment.Syntax.Location, alignment.ConstantValue.Int32Value, magnitudeLimit);
diagnostics.Add(ErrorCode.WRN_AlignmentMagnitude, alignment.Syntax.Location, alignmentConstant.Int32Value, magnitudeLimit);
}
}
else if (!alignment.HasErrors)
......
......@@ -1083,5 +1083,28 @@ public static void Main(string[] args)
-System.String[]-");
}
[WorkItem(1097386, "DevDiv")]
[Fact]
public void Dynamic01()
{
var text =
@"class C
{
const dynamic a = a;
string s = $""{0,a}"";
}";
CreateCompilationWithMscorlibAndSystemCore(text).VerifyDiagnostics(
// (3,19): error CS0110: The evaluation of the constant value for 'C.a' involves a circular definition
// const dynamic a = a;
Diagnostic(ErrorCode.ERR_CircConstValue, "a").WithArguments("C.a").WithLocation(3, 19),
// (3,23): error CS0134: 'C.a' is of type 'dynamic'. A const field of a reference type other than string can only be initialized with null.
// const dynamic a = a;
Diagnostic(ErrorCode.ERR_NotNullConstRefField, "a").WithArguments("C.a", "dynamic").WithLocation(3, 23),
// (4,21): error CS0150: A constant value is expected
// string s = $"{0,a}";
Diagnostic(ErrorCode.ERR_ConstantExpected, "a").WithLocation(4, 21)
);
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册