提交 677878c1 编写于 作者: M Manish Vasani

Address PR feedback

上级 6fede951
......@@ -193,7 +193,7 @@ private ImmutableArray<IOperation> GetAnonymousObjectCreationInitializers(BoundA
SyntaxNode syntax = value.Syntax?.Parent ?? expression.Syntax;
ITypeSymbol type = target.Type;
Optional<object> constantValue = value.ConstantValue;
var assignment = new SimpleAssignmentExpression(target, value, _semanticModel, syntax, type, constantValue, isImplicit: false);
var assignment = new SimpleAssignmentExpression(target, value, _semanticModel, syntax, type, constantValue, isImplicit: expression.WasCompilerGenerated);
builder.Add(assignment);
}
......
......@@ -1610,6 +1610,65 @@ public static void Main()
}", additionalRefs: new[] { SystemCoreRef }).VerifyDiagnostics();
}
[CompilerTrait(CompilerFeature.IOperation)]
[Fact]
public void AnonymousTypeSymbols_ErrorCases()
{
string source = @"
class ClassA
{
static void Test1(int x)
{
var obj = /*<bind>*/new { f1 = null, f2 = M, f3 = default }/*</bind>*/;
}
static void M()
{
}
}
";
string expectedOperationTree = @"
IAnonymousObjectCreationOperation (OperationKind.AnonymousObjectCreation, Type: <anonymous type: error f1, error f2, error f3>, IsInvalid) (Syntax: 'new { f1 = ... = default }')
Initializers(3):
ISimpleAssignmentOperation (OperationKind.SimpleAssignment, Type: error, Constant: null, IsInvalid) (Syntax: 'f1 = null')
Left:
IPropertyReferenceOperation: error <anonymous type: error f1, error f2, error f3>.f1 { get; } (OperationKind.PropertyReference, Type: error, IsInvalid) (Syntax: 'f1')
Instance Receiver:
null
Right:
ILiteralOperation (OperationKind.Literal, Type: null, Constant: null, IsInvalid) (Syntax: 'null')
ISimpleAssignmentOperation (OperationKind.SimpleAssignment, Type: error, IsInvalid) (Syntax: 'f2 = M')
Left:
IPropertyReferenceOperation: error <anonymous type: error f1, error f2, error f3>.f2 { get; } (OperationKind.PropertyReference, Type: error, IsInvalid) (Syntax: 'f2')
Instance Receiver:
null
Right:
IOperation: (OperationKind.None, Type: null, IsInvalid) (Syntax: 'M')
Children(1):
IInstanceReferenceOperation (OperationKind.InstanceReference, Type: ClassA, IsInvalid, IsImplicit) (Syntax: 'M')
ISimpleAssignmentOperation (OperationKind.SimpleAssignment, Type: error, IsInvalid) (Syntax: 'f3 = default')
Left:
IPropertyReferenceOperation: error <anonymous type: error f1, error f2, error f3>.f3 { get; } (OperationKind.PropertyReference, Type: error, IsInvalid) (Syntax: 'f3')
Instance Receiver:
null
Right:
IDefaultValueOperation (OperationKind.DefaultValue, Type: null, IsInvalid) (Syntax: 'default')
";
var expectedDiagnostics = new DiagnosticDescription[] {
// file.cs(6,35): error CS0828: Cannot assign '<null>' to anonymous type property
// var obj = /*<bind>*/new { f1 = null, f2 = M, f3 = default }/*</bind>*/;
Diagnostic(ErrorCode.ERR_AnonymousTypePropertyAssignedBadValue, "f1 = null").WithArguments("<null>").WithLocation(6, 35),
// file.cs(6,46): error CS0828: Cannot assign 'method group' to anonymous type property
// var obj = /*<bind>*/new { f1 = null, f2 = M, f3 = default }/*</bind>*/;
Diagnostic(ErrorCode.ERR_AnonymousTypePropertyAssignedBadValue, "f2 = M").WithArguments("method group").WithLocation(6, 46),
// file.cs(6,54): error CS0828: Cannot assign 'default' to anonymous type property
// var obj = /*<bind>*/new { f1 = null, f2 = M, f3 = default }/*</bind>*/;
Diagnostic(ErrorCode.ERR_AnonymousTypePropertyAssignedBadValue, "f3 = default").WithArguments("default").WithLocation(6, 54)
};
VerifyOperationTreeAndDiagnosticsForTest<AnonymousObjectCreationExpressionSyntax>(source, expectedOperationTree, expectedDiagnostics);
}
#region "Utility methods"
private void AssertCannotConstruct(ISymbol type)
......
......@@ -207,7 +207,7 @@ Namespace Microsoft.CodeAnalysis.Operations
Dim syntax As SyntaxNode = If(value.Syntax?.Parent, expression.Syntax)
Dim type As ITypeSymbol = target.Type
Dim constantValue As [Optional](Of Object) = value.ConstantValue
Dim assignment = New SimpleAssignmentExpression(target, value, _semanticModel, syntax, type, constantValue, isImplicit:=False)
Dim assignment = New SimpleAssignmentExpression(target, value, _semanticModel, syntax, type, constantValue, isImplicit:=expression.WasCompilerGenerated)
builder.Add(assignment)
Next i
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册