-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Adjust semantic model for method group conversion #75719
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
using Roslyn.Test.Utilities; | ||
using Xunit; | ||
using Basic.Reference.Assemblies; | ||
using Microsoft.CodeAnalysis.Test.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Symbols | ||
{ | ||
|
@@ -425,6 +426,346 @@ static void Main() | |
Assert.True(conversion.IsNumeric); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/36377")] | ||
public void GetSymbolInfo_ExplicitCastOnMethodGroup() | ||
{ | ||
var src = """ | ||
public class C | ||
{ | ||
public static void M() | ||
{ | ||
C x = (C)C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
|
||
public static explicit operator C(System.Func<int> intDelegate) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. There are some existing unexpected behaviors there. Filing issues for those |
||
{ | ||
return new C(); | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics(); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Equal("System.Int32 C.Test()", model.GetSymbolInfo(memberAccess).Symbol.ToTestDisplayString()); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75833")] | ||
public void GetSymbolInfo_ImplicitUserDefinedConversionOnMethodGroup_InLocalDeclaration() | ||
{ | ||
var src = """ | ||
public class C | ||
{ | ||
public static void M() | ||
{ | ||
C x = C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
|
||
public static implicit operator C(System.Func<int> intDelegate) | ||
{ | ||
return new C(); | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics( | ||
// (5,17): error CS0428: Cannot convert method group 'Test' to non-delegate type 'C'. Did you intend to invoke the method? | ||
// C x = C.Test; | ||
Diagnostic(ErrorCode.ERR_MethGrpToNonDel, "Test").WithArguments("Test", "C").WithLocation(5, 17)); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Equal("C C.op_Implicit(System.Func<System.Int32> intDelegate)", model.GetSymbolInfo(memberAccess).Symbol.ToTestDisplayString()); // Unexpected: Should be null | ||
var conversion = model.GetConversion(memberAccess); | ||
Assert.Equal(ConversionKind.ExplicitUserDefined, conversion.Kind); // Unexpected: Should be NoConversion or possibly Identity for error case | ||
Assert.Equal(ConversionKind.MethodGroup, conversion.UserDefinedFromConversion.Kind); | ||
Assert.Equal(ConversionKind.Identity, conversion.UserDefinedToConversion.Kind); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75833")] | ||
public void GetSymbolInfo_ImplicitUserDefinedConversionOnMethodGroup_WithToConversion_InLocalDeclaration() | ||
{ | ||
var src = """ | ||
public struct C | ||
{ | ||
public static void M() | ||
{ | ||
C? x = C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
|
||
public static implicit operator C(System.Func<int> intDelegate) | ||
{ | ||
return new C(); | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics( | ||
// (5,18): error CS0428: Cannot convert method group 'Test' to non-delegate type 'C?'. Did you intend to invoke the method? | ||
// C? x = C.Test; | ||
Diagnostic(ErrorCode.ERR_MethGrpToNonDel, "Test").WithArguments("Test", "C?").WithLocation(5, 18)); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Null(model.GetSymbolInfo(memberAccess).Symbol); | ||
var conversion = model.GetConversion(memberAccess); | ||
Assert.Equal(ConversionKind.ExplicitUserDefined, conversion.Kind); // Unexpected: Should be NoConversion or possibly Identity for error case | ||
Assert.Equal(ConversionKind.MethodGroup, conversion.UserDefinedFromConversion.Kind); | ||
Assert.Equal(ConversionKind.Identity, conversion.UserDefinedToConversion.Kind); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75833")] | ||
public void GetSymbolInfo_ImplicitUserDefinedConversionOnMethodGroup_InAssignemnt() | ||
{ | ||
var src = """ | ||
public class C | ||
{ | ||
public static void M() | ||
{ | ||
C x; | ||
x = C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
|
||
public static implicit operator C(System.Func<int> intDelegate) | ||
{ | ||
return new C(); | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics( | ||
// (6,15): error CS0428: Cannot convert method group 'Test' to non-delegate type 'C'. Did you intend to invoke the method? | ||
// x = C.Test; | ||
Diagnostic(ErrorCode.ERR_MethGrpToNonDel, "Test").WithArguments("Test", "C").WithLocation(6, 15)); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Equal("C C.op_Implicit(System.Func<System.Int32> intDelegate)", model.GetSymbolInfo(memberAccess).Symbol.ToTestDisplayString()); // Unexpected: Should be null | ||
var conversion = model.GetConversion(memberAccess); | ||
Assert.Equal(ConversionKind.ExplicitUserDefined, conversion.Kind); // Unexpected: Should be NoConversion or possibly Identity for error case | ||
Assert.Equal(ConversionKind.MethodGroup, conversion.UserDefinedFromConversion.Kind); | ||
Assert.Equal(ConversionKind.Identity, conversion.UserDefinedToConversion.Kind); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75833")] | ||
public void GetSymbolInfo_ImplicitUserDefinedConversionOnMethodGroup_WithToConversion_InAssignment() | ||
{ | ||
var src = """ | ||
public struct C | ||
{ | ||
public static void M() | ||
{ | ||
C? x; | ||
x = C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
|
||
public static implicit operator C(System.Func<int> intDelegate) | ||
{ | ||
return new C(); | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics( | ||
// (6,15): error CS0428: Cannot convert method group 'Test' to non-delegate type 'C?'. Did you intend to invoke the method? | ||
// x = C.Test; | ||
Diagnostic(ErrorCode.ERR_MethGrpToNonDel, "Test").WithArguments("Test", "C?").WithLocation(6, 15)); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Null(model.GetSymbolInfo(memberAccess).Symbol); // Unexpected: Should be null | ||
var conversion = model.GetConversion(memberAccess); | ||
Assert.Equal(ConversionKind.ExplicitUserDefined, conversion.Kind); // Unexpected: Should be NoConversion or possibly Identity for error case | ||
Assert.Equal(ConversionKind.MethodGroup, conversion.UserDefinedFromConversion.Kind); | ||
Assert.Equal(ConversionKind.Identity, conversion.UserDefinedToConversion.Kind); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75833")] | ||
public void GetSymbolInfo_ImplicitUserDefinedConversionOnMethodGroup_InInvocationArgument() | ||
{ | ||
var src = """ | ||
public class C | ||
{ | ||
public static void M() | ||
{ | ||
M2(C.Test); | ||
} | ||
|
||
public static void M2(C c) { } | ||
|
||
public static int Test() => 1; | ||
|
||
public static implicit operator C(System.Func<int> intDelegate) => throw null; | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics( | ||
// (5,12): error CS1503: Argument 1: cannot convert from 'method group' to 'C' | ||
// M2(C.Test); | ||
Diagnostic(ErrorCode.ERR_BadArgType, "C.Test").WithArguments("1", "method group", "C").WithLocation(5, 12)); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Null(model.GetSymbolInfo(memberAccess).Symbol); | ||
var conversion = model.GetConversion(memberAccess); | ||
Assert.Equal(ConversionKind.Identity, conversion.Kind); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75833")] | ||
public void GetSymbolInfo_ImplicitUserDefinedConversionOnMethodGroup_WithToConversion_InInvocationArgument() | ||
{ | ||
var src = """ | ||
public struct C | ||
{ | ||
public static void M() | ||
{ | ||
M2(C.Test); | ||
} | ||
|
||
public static void M2(C? c) { } | ||
|
||
public static int Test() => 1; | ||
|
||
public static implicit operator C(System.Func<int> intDelegate) | ||
{ | ||
return new C(); | ||
} | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics( | ||
// (5,12): error CS1503: Argument 1: cannot convert from 'method group' to 'C?' | ||
// M2(C.Test); | ||
Diagnostic(ErrorCode.ERR_BadArgType, "C.Test").WithArguments("1", "method group", "C?").WithLocation(5, 12)); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Null(model.GetSymbolInfo(memberAccess).Symbol); | ||
var conversion = model.GetConversion(memberAccess); | ||
Assert.Equal(ConversionKind.Identity, conversion.Kind); | ||
} | ||
|
||
[Fact] | ||
public void GetSymbolInfo_MethodGroupConversionInLocalDeclaration() | ||
{ | ||
var src = """ | ||
public class C | ||
{ | ||
public static void M() | ||
{ | ||
System.Func<int> x = C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics(); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Equal("System.Int32 C.Test()", model.GetSymbolInfo(memberAccess).Symbol.ToTestDisplayString()); | ||
var conversion = model.GetConversion(memberAccess); | ||
Assert.Equal(ConversionKind.MethodGroup, conversion.Kind); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/36377")] | ||
public void GetSymbolInfo_TwoExplicitCastsOnMethodGroup() | ||
{ | ||
var src = """ | ||
public sealed class C | ||
{ | ||
public static void M() | ||
{ | ||
D x = (D)(C)C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
|
||
public static explicit operator C(System.Func<int> intDelegate) => throw null; | ||
} | ||
public sealed class D | ||
{ | ||
public static explicit operator D(C c) => throw null; | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics(); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Equal("System.Int32 C.Test()", model.GetSymbolInfo(memberAccess).Symbol.ToTestDisplayString()); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/36377")] | ||
public void GetSymbolInfo_NoConversion() | ||
{ | ||
var src = """ | ||
public sealed class C | ||
{ | ||
public static void M() | ||
{ | ||
int x = C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics( | ||
// (5,19): error CS0428: Cannot convert method group 'Test' to non-delegate type 'int'. Did you intend to invoke the method? | ||
// int x = C.Test; | ||
Diagnostic(ErrorCode.ERR_MethGrpToNonDel, "Test").WithArguments("Test", "int").WithLocation(5, 19)); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Null(model.GetSymbolInfo(memberAccess).Symbol); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/36377")] | ||
public void GetSymbolInfo_MethodGroupConversion() | ||
{ | ||
var src = """ | ||
public sealed class C | ||
{ | ||
public static void M() | ||
{ | ||
System.Func<int> x = C.Test; | ||
} | ||
|
||
public static int Test() => 1; | ||
} | ||
"""; | ||
var comp = CreateCompilation(src); | ||
comp.VerifyEmitDiagnostics(); | ||
|
||
var tree = comp.SyntaxTrees.Single(); | ||
var model = comp.GetSemanticModel(tree); | ||
var memberAccess = GetSyntax<MemberAccessExpressionSyntax>(tree, "C.Test"); | ||
Assert.Equal("System.Int32 C.Test()", model.GetSymbolInfo(memberAccess).Symbol.ToTestDisplayString()); | ||
} | ||
|
||
#region "Diagnostics" | ||
[Fact] | ||
public void VarianceRelationFail() | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding a similar test for VB #Closed