Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IOperation of byref member initializer #73243

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,9 @@ private IOperation CreateBoundObjectInitializerMemberOperation(BoundObjectInitia
{
// In nested member initializers, the property is not actually set. Instead, it is retrieved for a series of Add method calls or nested property setter calls,
// so we need to use the getter for this property
MethodSymbol? accessor = isObjectOrCollectionInitializer ? property.GetOwnOrInheritedGetMethod() : property.GetOwnOrInheritedSetMethod();
MethodSymbol? accessor = isObjectOrCollectionInitializer || property.RefKind != RefKind.None
? property.GetOwnOrInheritedGetMethod()
: property.GetOwnOrInheritedSetMethod();
if (accessor == null || boundObjectInitializerMember.ResultKind == LookupResultKind.OverloadResolutionFailure || accessor.OriginalDefinition is ErrorMethodSymbol)
{
var children = CreateFromArray<BoundNode, IOperation>(((IBoundInvalidNode)boundObjectInitializerMember).InvalidNodeChildren);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9475,10 +9475,8 @@ public static void Main()
AssertEx.Equal("System.Int32", typeInfo.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", typeInfo.ConvertedType.ToTestDisplayString());

// IInvalidOperation is pre-existing condition - https://github.com/dotnet/roslyn/issues/72916
var propertyRef = (IInvalidOperation)model.GetOperation(elementAccess);
//var propertyRef = (IPropertyReferenceOperation)model.GetOperation(elementAccess);
//AssertEx.Equal(symbolInfo.Symbol.ToTestDisplayString(), propertyRef.Property.ToTestDisplayString());
var propertyRef = (IPropertyReferenceOperation)model.GetOperation(elementAccess);
AssertEx.Equal(symbolInfo.Symbol.ToTestDisplayString(), propertyRef.Property.ToTestDisplayString());
Assert.Equal(typeInfo.Type, propertyRef.Type);

var assignment = (AssignmentExpressionSyntax)elementAccess.Parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
Expand Down Expand Up @@ -4186,5 +4188,113 @@ public void DynamicInvocationOnRefStructs()
Diagnostic(ErrorCode.ERR_RefStructInterfaceImpl, "IEnumerable<int>").WithArguments("S").WithLocation(7, 16)
);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/72916")]
public void RefReturning_Indexer()
{
var source = """
public class C
{
public static void Main()
{
var c = new C() { [1] = 2 };
System.Console.WriteLine(c[1]);
}

int _test1 = 0;
ref int this[int x]
{
get => ref _test1;
}
}
""";

var comp = CreateCompilation(source, options: TestOptions.DebugExe, targetFramework: TargetFramework.StandardAndCSharp);

CompileAndVerify(comp, expectedOutput: "2").VerifyDiagnostics();

var tree = comp.SyntaxTrees.Single();
var model = comp.GetSemanticModel(tree);

var elementAccess = tree.GetRoot().DescendantNodes().OfType<ImplicitElementAccessSyntax>().Single();
var symbolInfo = model.GetSymbolInfo(elementAccess);
AssertEx.Equal("ref System.Int32 C.this[System.Int32 x] { get; }", symbolInfo.Symbol.ToTestDisplayString());
var typeInfo = model.GetTypeInfo(elementAccess);
AssertEx.Equal("System.Int32", typeInfo.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", typeInfo.ConvertedType.ToTestDisplayString());

var propertyRef = (IPropertyReferenceOperation)model.GetOperation(elementAccess);
AssertEx.Equal(symbolInfo.Symbol.ToTestDisplayString(), propertyRef.Property.ToTestDisplayString());
Assert.Equal(typeInfo.Type, propertyRef.Type);

var assignment = (AssignmentExpressionSyntax)elementAccess.Parent;
typeInfo = model.GetTypeInfo(assignment);
AssertEx.Equal("System.Int32", typeInfo.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", typeInfo.ConvertedType.ToTestDisplayString());

var operation = (IAssignmentOperation)model.GetOperation(assignment);
AssertEx.Equal("System.Int32", operation.Target.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", operation.Value.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", operation.Type.ToTestDisplayString());

var right = assignment.Right;
typeInfo = model.GetTypeInfo(right);
AssertEx.Equal("System.Int32", typeInfo.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", typeInfo.ConvertedType.ToTestDisplayString());
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/72916")]
public void RefReturning_Property()
{
var source = """
public class C
{
public static void Main()
{
var c = new C() { P = 2 };
System.Console.WriteLine(c.P);
}

int _test1 = 0;
ref int P
{
get => ref _test1;
}
}
""";

var comp = CreateCompilation(source, options: TestOptions.DebugExe, targetFramework: TargetFramework.StandardAndCSharp);

CompileAndVerify(comp, expectedOutput: "2").VerifyDiagnostics();

var tree = comp.SyntaxTrees.Single();
var model = comp.GetSemanticModel(tree);

var propertyAccess = tree.GetRoot().DescendantNodes().OfType<AssignmentExpressionSyntax>().First().Left;
var symbolInfo = model.GetSymbolInfo(propertyAccess);
AssertEx.Equal("ref System.Int32 C.P { get; }", symbolInfo.Symbol.ToTestDisplayString());
var typeInfo = model.GetTypeInfo(propertyAccess);
AssertEx.Equal("System.Int32", typeInfo.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", typeInfo.ConvertedType.ToTestDisplayString());

var propertyRef = (IPropertyReferenceOperation)model.GetOperation(propertyAccess);
AssertEx.Equal(symbolInfo.Symbol.ToTestDisplayString(), propertyRef.Property.ToTestDisplayString());
Assert.Equal(typeInfo.Type, propertyRef.Type);

var assignment = (AssignmentExpressionSyntax)propertyAccess.Parent;
typeInfo = model.GetTypeInfo(assignment);
AssertEx.Equal("System.Int32", typeInfo.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", typeInfo.ConvertedType.ToTestDisplayString());

var operation = (IAssignmentOperation)model.GetOperation(assignment);
AssertEx.Equal("System.Int32", operation.Target.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", operation.Value.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", operation.Type.ToTestDisplayString());

var right = assignment.Right;
typeInfo = model.GetTypeInfo(right);
AssertEx.Equal("System.Int32", typeInfo.Type.ToTestDisplayString());
AssertEx.Equal("System.Int32", typeInfo.ConvertedType.ToTestDisplayString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,52 @@ Console.writeline( cust2.e.ToString)
CompileAndVerify(compilation)
End Sub

<Fact, CompilerTrait(CompilerFeature.IOperation), WorkItem("https://github.com/dotnet/roslyn/issues/72916")>
Public Sub RefReturningProperty()
Dim cSharpSource = <![CDATA[
public class C
{
int _f = 0;
public ref int P
{
get => ref _f;
}
}]]>.Value
Dim cSharpCompilation = CreateCSharpCompilation(cSharpSource).VerifyDiagnostics()
Dim cSharpRef = cSharpCompilation.EmitToPortableExecutableReference()

Dim source = <![CDATA[
Imports System

Class Program
Public Shared Sub Main()
Dim c As C = New C() With { .P = 123 }'BIND:"New C() With { .P = 123 }"
Console.WriteLine(c.P)
End Sub
End Class]]>.Value

Dim expectedOperationTree = <![CDATA[
IObjectCreationOperation (Constructor: Sub C..ctor()) (OperationKind.ObjectCreation, Type: C) (Syntax: 'New C() Wit ... .P = 123 }')
Arguments(0)
Initializer:
IObjectOrCollectionInitializerOperation (OperationKind.ObjectOrCollectionInitializer, Type: C) (Syntax: 'With { .P = 123 }')
Initializers(1):
ISimpleAssignmentOperation (OperationKind.SimpleAssignment, Type: System.Void) (Syntax: '.P = 123')
Left:
IPropertyReferenceOperation: ReadOnly ByRef Property C.P As System.Int32 (OperationKind.PropertyReference, Type: System.Int32) (Syntax: 'P')
Instance Receiver:
IInstanceReferenceOperation (ReferenceKind: ImplicitReceiver) (OperationKind.InstanceReference, Type: C, IsImplicit) (Syntax: 'New C() Wit ... .P = 123 }')
Right:
ILiteralOperation (OperationKind.Literal, Type: System.Int32, Constant: 123) (Syntax: '123')
]]>.Value

Dim expectedDiagnostics = String.Empty

VerifyOperationTreeAndDiagnosticsForTest(Of ObjectCreationExpressionSyntax)(source, expectedOperationTree, expectedDiagnostics, references:={cSharpRef})

CompileAndVerify(CreateCompilation(source, {cSharpRef}, TestOptions.ReleaseExe), expectedOutput:=<![CDATA[123]]>).VerifyDiagnostics()
End Sub

End Class
End Namespace