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

Update several ExpressionCompiler unit tests for inferred delegate types #58203

Merged
merged 2 commits into from
Dec 9, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1494,8 +1494,10 @@ int WP { set { } }
int this[int x, int y] { set { } }
int this[int x, int y, int z] { get { return 0; } set { } }
int M() { return 0; }
}
int M1() { return 0; }
int M2() { return 0; }
int M2(int i) { return i; }
}
";
var compilation0 = CreateCompilation(
source,
Expand All @@ -1504,7 +1506,7 @@ int WP { set { } }

WithRuntimeInstance(compilation0, runtime =>
{
var context = CreateMethodContext(runtime, "C.M");
var context = CreateMethodContext(runtime, "C.M1");
CheckResultFlags(context, "F", DkmClrCompilationResultFlags.None);
CheckResultFlags(context, "RF", DkmClrCompilationResultFlags.ReadOnlyResult);
Expand All @@ -1522,11 +1524,12 @@ int WP { set { } }
CheckResultFlags(context, "this[1, 2]", DkmClrCompilationResultFlags.None, "error CS0154: The property or indexer 'C.this[int, int]' cannot be used in this context because it lacks the get accessor");
CheckResultFlags(context, "this[1, 2, 3]", DkmClrCompilationResultFlags.None);
CheckResultFlags(context, "M()", DkmClrCompilationResultFlags.PotentialSideEffect | DkmClrCompilationResultFlags.ReadOnlyResult);
CheckResultFlags(context, "M1()", DkmClrCompilationResultFlags.PotentialSideEffect | DkmClrCompilationResultFlags.ReadOnlyResult);
CheckResultFlags(context, "null", DkmClrCompilationResultFlags.ReadOnlyResult);
CheckResultFlags(context, "1", DkmClrCompilationResultFlags.ReadOnlyResult);
CheckResultFlags(context, "M", DkmClrCompilationResultFlags.None, "error CS0428: Cannot convert method group 'M' to non-delegate type 'object'. Did you intend to invoke the method?");
CheckResultFlags(context, "M1", DkmClrCompilationResultFlags.ReadOnlyResult);
CheckResultFlags(context, "M2", DkmClrCompilationResultFlags.None, "error CS8917: The delegate type could not be inferred.");
CheckResultFlags(context, "typeof(C)", DkmClrCompilationResultFlags.ReadOnlyResult);
CheckResultFlags(context, "new C()", DkmClrCompilationResultFlags.ReadOnlyResult);
});
Expand Down Expand Up @@ -1687,20 +1690,43 @@ public void EvaluateMethodGroup()
var source =
@"class C
{
void M()
void M1()
{
}
void M2() { }
void M2(int i) { }
}";
ResultProperties resultProperties;
string error;
var testData = Evaluate(
source,
OutputKind.DynamicallyLinkedLibrary,
methodName: "C.M",
expr: "this.M",
methodName: "C.M1",
expr: "this.M2",
resultProperties: out resultProperties,
error: out error);
Assert.Equal("error CS0428: Cannot convert method group 'M' to non-delegate type 'object'. Did you intend to invoke the method?", error);
Assert.Equal("error CS8917: The delegate type could not be inferred.", error);

testData = Evaluate(
source,
OutputKind.DynamicallyLinkedLibrary,
methodName: "C.M1",
expr: "this.M1",
resultProperties: out resultProperties,
error: out error);
Assert.Equal(DkmClrCompilationResultFlags.ReadOnlyResult, resultProperties.Flags);
var methodData = testData.GetMethodData("<>x.<>m0");
var method = (MethodSymbol)methodData.Method;
Assert.Equal(SpecialType.System_Object, method.ReturnType.SpecialType);
methodData.VerifyIL(
@"{
// Code size 13 (0xd)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldftn ""void C.M1()""
IL_0007: newobj ""System.Action..ctor(object, System.IntPtr)""
IL_000c: ret
}");
}

[Fact]
Expand All @@ -1709,25 +1735,44 @@ public void AssignMethodGroup()
var source =
@"class C
{
static void M()
static void M1()
{
object o;
}
static void M2() { }
static void M2(int i) { }
}";
var compilation0 = CreateCompilation(source, options: TestOptions.DebugDll);
WithRuntimeInstance(compilation0, runtime =>
{
var context = CreateMethodContext(
runtime,
methodName: "C.M");
methodName: "C.M1");
string error;
var testData = new CompilationTestData();
var result = context.CompileAssignment(
target: "o",
expr: "M",
expr: "M2",
error: out error,
testData: testData);
Assert.Equal("error CS0428: Cannot convert method group 'M' to non-delegate type 'object'. Did you intend to invoke the method?", error);
Assert.Equal("error CS8917: The delegate type could not be inferred.", error);
testData = new CompilationTestData();
context.CompileAssignment(
target: "o",
expr: "M1",
error: out error,
testData: testData);
testData.GetMethodData("<>x.<>m0").VerifyIL(
@"{
// Code size 14 (0xe)
.maxstack 2
.locals init (object V_0) //o
IL_0000: ldnull
IL_0001: ldftn ""void C.M1()""
IL_0007: newobj ""System.Action..ctor(object, System.IntPtr)""
IL_000c: stloc.0
IL_000d: ret
}");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ class C
void Test()
{
}
static void F() { }
static void F(int i) { }
}
";
var comp = CreateCompilation(source, options: TestOptions.DebugDll);
Expand All @@ -385,7 +387,7 @@ void Test()
var context = CreateMethodContext(runtime, methodName: "C.Test");
VerifyErrorResultProperties(context, "x => x");
VerifyErrorResultProperties(context, "Test");
VerifyErrorResultProperties(context, "F");
VerifyErrorResultProperties(context, "Missing");
VerifyErrorResultProperties(context, "C");
});
Expand Down