From 9d7adbe43aabd9e53c531ad2a300139f3fc05ee1 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Wed, 9 Jul 2025 14:51:03 -0700 Subject: [PATCH 1/2] Extensions: add test for function type scenario --- .../Test/Emit3/Semantics/ExtensionTests.cs | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs b/src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs index 12429fe1f8389..3b062a3729425 100644 --- a/src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs +++ b/src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs @@ -35138,6 +35138,87 @@ public void M() { } comp.VerifyDiagnostics(); } + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79309")] + public void FunctionType_InstanceReceiver_12() + { + // static non-extension method vs. extension property + var src = """ +var x = new C().M; + +public static class E +{ + extension(C c) + { + public int M => 42; + } +} + +public class C +{ + public static int M() => throw null; +} +"""; + var comp = CreateCompilation(src); + comp.VerifyEmitDiagnostics( + // (1,9): error CS8917: The delegate type could not be inferred. + // var x = new C().M; + Diagnostic(ErrorCode.ERR_CannotInferDelegateType, "new C().M").WithLocation(1, 9)); + + var tree = comp.SyntaxTrees.First(); + var model = comp.GetSemanticModel(tree); + var localDeclaration = GetSyntax(tree, "var x = new C().M"); + Assert.Equal("?", model.GetTypeInfo(localDeclaration.Type).Type.ToTestDisplayString()); + + // without non-extension method + src = """ +var x = new C().M; +System.Console.Write(x); + +public static class E +{ + extension(C c) + { + public int M => 42; + } +} + +public class C +{ +} +"""; + comp = CreateCompilation(src); + CompileAndVerify(comp, expectedOutput: "42").VerifyDiagnostics(); + + tree = comp.SyntaxTrees.First(); + model = comp.GetSemanticModel(tree); + localDeclaration = GetSyntax(tree, "var x = new C().M"); + Assert.Equal("System.Int32", model.GetTypeInfo(localDeclaration.Type).Type.ToTestDisplayString()); + + // analogous non-extension scenario + src = """ +var x = new C().M; +System.Console.Write(x); + +public class Base +{ + public int M => 42; +} + +public class C : Base +{ + public static int M() => throw null; +} +"""; + comp = CreateCompilation(src); + comp.VerifyEmitDiagnostics( + // (1,9): error CS8917: The delegate type could not be inferred. + // var x = new C().M; + Diagnostic(ErrorCode.ERR_CannotInferDelegateType, "new C().M").WithLocation(1, 9), + // (11,23): warning CS0108: 'C.M()' hides inherited member 'Base.M'. Use the new keyword if hiding was intended. + // public static int M() => throw null; + Diagnostic(ErrorCode.WRN_NewRequired, "M").WithArguments("C.M()", "Base.M").WithLocation(11, 23)); + } + [Fact] public void FunctionType_ColorColorReceiver_01() { From b64b7781e77ddb6b0951e851fe2038eeae6e7615 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Mon, 21 Jul 2025 13:10:21 -0700 Subject: [PATCH 2/2] Add 'new' --- .../CSharp/Test/Emit3/Semantics/ExtensionTests.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs b/src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs index 3b062a3729425..736d4ea587e2c 100644 --- a/src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs +++ b/src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs @@ -35206,17 +35206,14 @@ public class Base public class C : Base { - public static int M() => throw null; + public static new int M() => throw null; } """; comp = CreateCompilation(src); comp.VerifyEmitDiagnostics( // (1,9): error CS8917: The delegate type could not be inferred. // var x = new C().M; - Diagnostic(ErrorCode.ERR_CannotInferDelegateType, "new C().M").WithLocation(1, 9), - // (11,23): warning CS0108: 'C.M()' hides inherited member 'Base.M'. Use the new keyword if hiding was intended. - // public static int M() => throw null; - Diagnostic(ErrorCode.WRN_NewRequired, "M").WithArguments("C.M()", "Base.M").WithLocation(11, 23)); + Diagnostic(ErrorCode.ERR_CannotInferDelegateType, "new C().M").WithLocation(1, 9)); } [Fact]