提交 93356495 编写于 作者: E Evan Hauck

Merge pull request #11256 from khyperia/fix10463

Generalize dynamic static-only restriction to include field initializers
......@@ -270,11 +270,13 @@ private BoundExpression BindArgListOperator(InvocationExpressionSyntax node, Dia
break;
case BoundKind.ThisReference:
if (InConstructorInitializer && receiver.WasCompilerGenerated)
// Can't call the HasThis method due to EE doing odd things with containing member and its containing type.
if ((InConstructorInitializer || InFieldInitializer) && receiver.WasCompilerGenerated)
{
// Only a static method can be called in a constructor initializer. If we were not in a ctor initializer
// the runtime binder would ignore the receiver, but in a ctor initializer we can't read "this" before
// the base constructor is called. We need to handle this as a type qualified static method call.
// Also applicable to things like field initializers, which run before the ctor initializer.
expression = methodGroup.Update(
methodGroup.TypeArgumentsOpt,
methodGroup.Name,
......
......@@ -14833,6 +14833,73 @@ .maxstack 4
IL_007f: callvirt ""int System.Collections.Generic.List<Program.TextSpan>.Count.get""
IL_0084: blt.s IL_002d
IL_0086: ret
}");
}
[WorkItem(10463, "https://github.com/dotnet/roslyn/issues/10463")]
[Fact]
public void FieldInitializerDynamic()
{
string source = @"
using System;
class M
{
object a = Test((dynamic)2);
static object Test(object obj) => obj;
static void Main()
{
Console.Write(new M().a);
}
}
";
var compilation = CompileAndVerify(source, new[] { SystemCoreRef, CSharpRef }, expectedOutput: "2");
// the main point of this test is to have it PEVerify/run correctly, although checking IL too can't hurt.
compilation.VerifyIL("M..ctor",
@"{
// Code size 115 (0x73)
.maxstack 10
IL_0000: ldarg.0
IL_0001: ldsfld ""System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic>> M.<>o__3.<>p__0""
IL_0006: brtrue.s IL_0043
IL_0008: ldc.i4.0
IL_0009: ldstr ""Test""
IL_000e: ldnull
IL_000f: ldtoken ""M""
IL_0014: call ""System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)""
IL_0019: ldc.i4.2
IL_001a: newarr ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo""
IL_001f: dup
IL_0020: ldc.i4.0
IL_0021: ldc.i4.s 33
IL_0023: ldnull
IL_0024: call ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, string)""
IL_0029: stelem.ref
IL_002a: dup
IL_002b: ldc.i4.1
IL_002c: ldc.i4.0
IL_002d: ldnull
IL_002e: call ""Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create(Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags, string)""
IL_0033: stelem.ref
IL_0034: 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_0039: call ""System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic>> System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic>>.Create(System.Runtime.CompilerServices.CallSiteBinder)""
IL_003e: stsfld ""System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic>> M.<>o__3.<>p__0""
IL_0043: ldsfld ""System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic>> M.<>o__3.<>p__0""
IL_0048: ldfld ""System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic> System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic>>.Target""
IL_004d: ldsfld ""System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic>> M.<>o__3.<>p__0""
IL_0052: ldtoken ""M""
IL_0057: call ""System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)""
IL_005c: ldc.i4.2
IL_005d: box ""int""
IL_0062: callvirt ""dynamic System.Func<System.Runtime.CompilerServices.CallSite, System.Type, dynamic, dynamic>.Invoke(System.Runtime.CompilerServices.CallSite, System.Type, dynamic)""
IL_0067: stfld ""object M.a""
IL_006c: ldarg.0
IL_006d: call ""object..ctor()""
IL_0072: ret
}");
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册