Skip to content
Merged
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
78 changes: 78 additions & 0 deletions src/Compilers/CSharp/Test/Emit3/Semantics/ExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35138,6 +35138,84 @@ 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<VariableDeclarationSyntax>(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<VariableDeclarationSyntax>(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 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));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

// (11,23): warning CS0108: 'C.M()' hides inherited member 'Base.M'. Use the new keyword if hiding was intended.

Consider suppressing this warning by doing what it suggests


[Fact]
public void FunctionType_ColorColorReceiver_01()
{
Expand Down