Skip to content
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 @@ -607,6 +607,13 @@ internal override void GenerateMethodBody(TypeCompilationState compilationState,
return;
}

body = ExtensionMethodReferenceRewriter.Rewrite(body);

if (body.HasErrors)
{
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test when there are errors?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible currently. The main thing that could go wrong during this rewriting is that an implementation method would be missing, but we block that upstream (in metadata loading). See PEMethodSymbol_GetUseSiteInfo.
Still, I think we should have this check for robustness.

}

// Variables may have been captured by lambdas in the original method
// or in the expression, and we need to preserve the existing values of
// those variables in the expression. This requires rewriting the variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11872,5 +11872,127 @@ public static int P3

Assert.Equal("error CS0103: The name 'field' does not exist in the current context", error);
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81171")]
public void Extension_01()
{
var source = """
public class C
{
public static void Main()
{
#line 999
var y = 5.Property;
System.Console.WriteLine(y);
}
}

public static class E
{
extension(int i)
{
public int Property => 42;
}
}
""";
var compilation0 = CreateCompilation(source, options: TestOptions.DebugExe);

// First verify the base compilation is valid
compilation0.VerifyEmitDiagnostics();

WithRuntimeInstance(compilation0, runtime =>
{
var context = CreateMethodContext(runtime, "C.Main", atLineNumber: 999);

ResultProperties resultProperties;
string error;
ImmutableArray<AssemblyIdentity> missingAssemblyIdentities;
var testData = new CompilationTestData();

var result = context.CompileExpression(
"5.Property",
DkmEvaluationFlags.TreatAsExpression,
NoAliases,
DebuggerDiagnosticFormatter.Instance,
out resultProperties,
out error,
out missingAssemblyIdentities,
EnsureEnglishUICulture.PreferredOrNull,
testData: testData);

Assert.Null(error);

testData.GetMethodData("<>x.<>m0").VerifyIL("""
{
// Code size 7 (0x7)
.maxstack 1
.locals init (int V_0) //y
IL_0000: ldc.i4.5
IL_0001: call "int E.get_Property(int)"
IL_0006: ret
}
""");
});
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/81171")]
public void Extension_02()
{
var source = """
public class C
{
public static void Main()
{
#line 999
var y = int.Method();
System.Console.WriteLine(y);
}
}

public static class E
{
extension(int)
{
public static int Method() => 42;
}
}
""";
var compilation0 = CreateCompilation(source, options: TestOptions.DebugExe);

// First verify the base compilation is valid
compilation0.VerifyEmitDiagnostics();

WithRuntimeInstance(compilation0, runtime =>
{
var context = CreateMethodContext(runtime, "C.Main", atLineNumber: 999);
ResultProperties resultProperties;
string error;
ImmutableArray<AssemblyIdentity> missingAssemblyIdentities;
var testData = new CompilationTestData();

var result = context.CompileExpression(
"int.Method()",
DkmEvaluationFlags.TreatAsExpression,
NoAliases,
DebuggerDiagnosticFormatter.Instance,
out resultProperties,
out error,
out missingAssemblyIdentities,
EnsureEnglishUICulture.PreferredOrNull,
testData: testData);

Assert.Null(error);

testData.GetMethodData("<>x.<>m0").VerifyIL("""
{
// Code size 6 (0x6)
.maxstack 1
.locals init (int V_0) //y
IL_0000: call "int E.Method()"
IL_0005: ret
}
""");
});
}
}
}
Loading