-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Handle ref returning receivers in dynamic calls #70676
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10deff8
Handle ref returning receivers in dynamic calls
jjonescz 049704f
Add more tests
jjonescz 2b6e2d3
Use `HasHome` helper to decide dynamic receiver ref kind
jjonescz 0c7d3bd
Fix desktop tests
jjonescz 1381cff
Simplify
jjonescz 16608ce
Merge branch 'main' into 68063-DynamicOpFactoryRefKind
jjonescz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1510,6 +1510,217 @@ public void Goo() | |
VerifyOperationTreeAndDiagnosticsForTest<InvocationExpressionSyntax>(source, expectedOperationTree, expectedDiagnostics); | ||
} | ||
|
||
[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/roslyn/issues/68063")] | ||
public void RefReturn_Method([CombinatorialValues("int", "dynamic")] string type) | ||
{ | ||
var source = $$""" | ||
{{type}} d = 1; | ||
S s = default; | ||
GetS(ref s).M(d); | ||
System.Console.Write(s.X); | ||
|
||
static ref S GetS(ref S s) => ref s; | ||
|
||
struct S | ||
{ | ||
public int X { get; private set; } | ||
|
||
public void M(int x) => X = x; | ||
} | ||
"""; | ||
CompileAndVerify(source, new[] { CSharpRef }, | ||
expectedOutput: "1", verify: Verification.Fails).VerifyDiagnostics(); | ||
} | ||
|
||
[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/roslyn/issues/68063")] | ||
public void RefReturn_Property([CombinatorialValues("int", "dynamic")] string type) | ||
{ | ||
var source = $$""" | ||
{{type}} d = 1; | ||
S s = default; | ||
s.Self.M(d); | ||
System.Console.Write(s.X); | ||
|
||
struct S | ||
{ | ||
[System.Diagnostics.CodeAnalysis.UnscopedRef] | ||
public ref S Self => ref this; | ||
public int X { get; private set; } | ||
|
||
public void M(int x) => X = x; | ||
} | ||
"""; | ||
CompileAndVerify(new[] { source, UnscopedRefAttributeDefinition }, new[] { CSharpRef }, | ||
expectedOutput: "1", verify: Verification.Fails).VerifyDiagnostics(); | ||
} | ||
|
||
[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/roslyn/issues/68063")] | ||
public void RefReturn_Indexer([CombinatorialValues("int", "dynamic")] string type) | ||
{ | ||
var source = $$""" | ||
{{type}} d = 1; | ||
S s = default; | ||
s[0].M(d); | ||
System.Console.Write(s.X); | ||
|
||
struct S | ||
{ | ||
[System.Diagnostics.CodeAnalysis.UnscopedRef] | ||
public ref S this[int _] => ref this; | ||
public int X { get; private set; } | ||
|
||
public void M(int x) => X = x; | ||
} | ||
"""; | ||
CompileAndVerify(new[] { source, UnscopedRefAttributeDefinition }, new[] { CSharpRef }, | ||
expectedOutput: "1", verify: Verification.Fails).VerifyDiagnostics(); | ||
} | ||
|
||
[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/roslyn/issues/68063")] | ||
public void RefReturn_Local([CombinatorialValues("int", "dynamic")] string type) | ||
{ | ||
var source = $$""" | ||
{{type}} d = 1; | ||
S s = default; | ||
ref S r = ref s; | ||
r.M(d); | ||
System.Console.Write(s.X); | ||
|
||
struct S | ||
{ | ||
public int X { get; private set; } | ||
public void M(int x) => X = x; | ||
} | ||
"""; | ||
CompileAndVerify(new[] { source, UnscopedRefAttributeDefinition }, new[] { CSharpRef }, | ||
expectedOutput: "1").VerifyDiagnostics(); | ||
} | ||
|
||
[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/roslyn/issues/68063")] | ||
public void RefReturn_Field([CombinatorialValues("int", "dynamic")] string type) | ||
{ | ||
var source = $$""" | ||
{{type}} d = 1; | ||
S s = default; | ||
P p = new P(ref s); | ||
p.S.M(d); | ||
System.Console.Write(s.X); | ||
|
||
ref struct P(ref S s) | ||
{ | ||
public ref S S = ref s; | ||
} | ||
|
||
struct S | ||
{ | ||
public int X { get; private set; } | ||
public void M(int x) => X = x; | ||
} | ||
"""; | ||
CompileAndVerify(new[] { source, UnscopedRefAttributeDefinition }, targetFramework: TargetFramework.Net70, | ||
expectedOutput: RefFieldTests.IncludeExpectedOutput("1"), verify: Verification.FailsPEVerify).VerifyDiagnostics(); | ||
} | ||
|
||
[Theory, CombinatorialData, WorkItem("https://github.com/dotnet/roslyn/issues/68063")] | ||
public void StructReceiver_Field([CombinatorialValues("int", "dynamic")] string type, bool ro) | ||
{ | ||
var source = $$""" | ||
new C(default).Run(); | ||
|
||
class C(S s) | ||
{ | ||
private {{(ro ? "readonly" : "")}} S s = s; | ||
|
||
public void Run() | ||
{ | ||
{{type}} d = 1; | ||
s.M(d); | ||
System.Console.Write(s.X); | ||
} | ||
} | ||
|
||
struct S | ||
{ | ||
public int X { get; private set; } | ||
public void M(int x) => X = x; | ||
} | ||
"""; | ||
CompileAndVerify(new[] { source, UnscopedRefAttributeDefinition }, new[] { CSharpRef }, | ||
expectedOutput: ro ? "0" : "1", verify: ro ? Verification.FailsPEVerify : default).VerifyDiagnostics(); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/68063")] | ||
public void StructReceiver_Rvalue() | ||
{ | ||
var source = """ | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
dynamic d = 1; | ||
GetS().M(d); | ||
System.Console.Write(GetS().X); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
static S GetS() => default; | ||
} | ||
|
||
struct S | ||
{ | ||
public int X { get; private set; } | ||
public void M(int x) => X = x; | ||
} | ||
"""; | ||
var verifier = CompileAndVerify(source, new[] { CSharpRef }, expectedOutput: "0").VerifyDiagnostics(); | ||
verifier.VerifyIL("Program.Main", """ | ||
{ | ||
// Code size 121 (0x79) | ||
.maxstack 9 | ||
.locals init (object V_0, //d | ||
S V_1) | ||
IL_0000: ldc.i4.1 | ||
IL_0001: box "int" | ||
IL_0006: stloc.0 | ||
IL_0007: ldsfld "System.Runtime.CompilerServices.CallSite<System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic>> Program.<>o__0.<>p__0" | ||
IL_000c: brtrue.s IL_004c | ||
IL_000e: ldc.i4 0x100 | ||
IL_0013: ldstr "M" | ||
IL_0018: ldnull | ||
IL_0019: ldtoken "Program" | ||
IL_001e: call "System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)" | ||
IL_0023: ldc.i4.2 | ||
IL_0024: newarr "Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo" | ||
IL_0029: dup | ||
IL_002a: ldc.i4.0 | ||
IL_002b: ldc.i4.1 | ||
IL_002c: ldnull | ||
IL_002d: call "Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, string)" | ||
IL_0032: stelem.ref | ||
IL_0033: dup | ||
IL_0034: ldc.i4.1 | ||
IL_0035: ldc.i4.0 | ||
IL_0036: ldnull | ||
IL_0037: call "Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, string)" | ||
IL_003c: stelem.ref | ||
IL_003d: call "System.Runtime.CompilerServices.CallSiteBinder Microsoft.CSharp.RuntimeBinder.Binder.InvokeMember(Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, string, System.Collections.Generic.IEnumerable<System.Type>, System.Type, System.Collections.Generic.IEnumerable<Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo>)" | ||
IL_0042: call "System.Runtime.CompilerServices.CallSite<System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic>> System.Runtime.CompilerServices.CallSite<System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic>>.Create(System.Runtime.CompilerServices.CallSiteBinder)" | ||
IL_0047: stsfld "System.Runtime.CompilerServices.CallSite<System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic>> Program.<>o__0.<>p__0" | ||
IL_004c: ldsfld "System.Runtime.CompilerServices.CallSite<System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic>> Program.<>o__0.<>p__0" | ||
IL_0051: ldfld "System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic> System.Runtime.CompilerServices.CallSite<System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic>>.Target" | ||
IL_0056: ldsfld "System.Runtime.CompilerServices.CallSite<System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic>> Program.<>o__0.<>p__0" | ||
IL_005b: call "S Program.GetS()" | ||
IL_0060: ldloc.0 | ||
IL_0061: callvirt "void System.Action<System.Runtime.CompilerServices.CallSite, S, dynamic>.Invoke(System.Runtime.CompilerServices.CallSite, S, dynamic)" | ||
IL_0066: call "S Program.GetS()" | ||
IL_006b: stloc.1 | ||
IL_006c: ldloca.s V_1 | ||
IL_006e: call "readonly int S.X.get" | ||
IL_0073: call "void System.Console.Write(int)" | ||
IL_0078: ret | ||
} | ||
"""); | ||
} | ||
|
||
#endregion | ||
|
||
#region Type Inference | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need to depend on this setting here. The most permissive value is probably going to be the closest match with the previous behavior. #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, at least, with intent.