-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Improve arity error message to prefer generic type when both generic and non-generic exist #80707
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
Open
Copilot
wants to merge
11
commits into
main
Choose a base branch
from
copilot/fix-inaccurate-error-message
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−3
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ea3af0c
Initial plan
Copilot 1cc3b72
Improve arity error message to prefer generic type when both generic …
Copilot f3ff1c2
Update test expectation for improved arity error message
Copilot eb87740
Update src/Compilers/CSharp/Portable/Binder/LookupResult.cs
CyrusNajmabadi f8f611b
Move PreferGenericOverNonGeneric to static local function
Copilot e08335c
Convert test strings to raw string literals and add WorkItem attributes
Copilot 6a16886
Add tests
CyrusNajmabadi 01d1c78
Update src/Compilers/CSharp/Portable/Binder/LookupResult.cs
CyrusNajmabadi 8b1aa80
Remove
CyrusNajmabadi 565efa9
Merge branch 'copilot/fix-inaccurate-error-message' of https://github…
CyrusNajmabadi 849d606
Update test
CyrusNajmabadi 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 hidden or 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 hidden or 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
122 changes: 122 additions & 0 deletions
122
src/Compilers/CSharp/Test/Semantic/Semantics/ImprovedArityErrorTests.cs
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using Microsoft.CodeAnalysis.CSharp.Test.Utilities; | ||
using Roslyn.Test.Utilities; | ||
using Xunit; | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp.UnitTests | ||
{ | ||
public class ImprovedArityErrorTests : CSharpTestBase | ||
{ | ||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/24406")] | ||
public void TestGenericAndNonGenericType() | ||
{ | ||
var text = """ | ||
class MyExpression { } | ||
class MyExpression<T> { } | ||
|
||
class Test | ||
{ | ||
void M() | ||
{ | ||
MyExpression<int, string> x; | ||
} | ||
} | ||
"""; | ||
|
||
CreateCompilation(text).VerifyDiagnostics( | ||
// (8,9): error CS0305: Using the generic type 'MyExpression<T>' requires 1 type arguments | ||
Diagnostic(ErrorCode.ERR_BadArity, "MyExpression<int, string>").WithArguments("MyExpression<T>", "type", "1").WithLocation(8, 9), | ||
// (8,35): warning CS0168: The variable 'x' is declared but never used | ||
Diagnostic(ErrorCode.WRN_UnreferencedVar, "x").WithArguments("x").WithLocation(8, 35)); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/24406")] | ||
public void TestGenericAndNonGenericType_SingleTypeArgument() | ||
{ | ||
var text = """ | ||
class MyExpression { } | ||
class MyExpression<T> { } | ||
|
||
class Test | ||
{ | ||
void M() | ||
{ | ||
MyExpression<int> x = null; | ||
} | ||
} | ||
"""; | ||
|
||
CreateCompilation(text).VerifyDiagnostics( | ||
// (8,27): warning CS0219: The variable 'x' is assigned but its value is never used | ||
Diagnostic(ErrorCode.WRN_UnreferencedVarAssg, "x").WithArguments("x").WithLocation(8, 27)); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/24406")] | ||
public void TestNonGenericTypeOnly() | ||
{ | ||
var text = """ | ||
class MyExpression { } | ||
|
||
class Test | ||
{ | ||
void M() | ||
{ | ||
MyExpression<int> x; | ||
} | ||
} | ||
"""; | ||
|
||
CreateCompilation(text).VerifyDiagnostics( | ||
// (7,9): error CS0308: The non-generic type 'MyExpression' cannot be used with type arguments | ||
Diagnostic(ErrorCode.ERR_HasNoTypeVars, "MyExpression<int>").WithArguments("MyExpression", "type").WithLocation(7, 9), | ||
// (7,27): warning CS0168: The variable 'x' is declared but never used | ||
Diagnostic(ErrorCode.WRN_UnreferencedVar, "x").WithArguments("x").WithLocation(7, 27)); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/24406")] | ||
public void TestGenericTypeOnly() | ||
{ | ||
var text = """ | ||
class MyExpression<T> { } | ||
|
||
class Test | ||
{ | ||
void M() | ||
{ | ||
MyExpression<int, string> x; | ||
} | ||
} | ||
"""; | ||
|
||
CreateCompilation(text).VerifyDiagnostics( | ||
// (7,9): error CS0305: Using the generic type 'MyExpression<T>' requires 1 type arguments | ||
Diagnostic(ErrorCode.ERR_BadArity, "MyExpression<int, string>").WithArguments("MyExpression<T>", "type", "1").WithLocation(7, 9), | ||
// (7,35): warning CS0168: The variable 'x' is declared but never used | ||
Diagnostic(ErrorCode.WRN_UnreferencedVar, "x").WithArguments("x").WithLocation(7, 35)); | ||
} | ||
|
||
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/24406")] | ||
public void TestMultipleGenericTypes() | ||
{ | ||
var text = """ | ||
class MyExpression { } | ||
class MyExpression<T> { } | ||
class MyExpression<T1, T2> { } | ||
|
||
class Test | ||
{ | ||
void M() | ||
{ | ||
MyExpression<int, string, bool> x; | ||
} | ||
} | ||
"""; | ||
// With multiple generic types and non-generic, should prefer the closest match | ||
// In this case, we look for arity 3, find generic with arity 2, that's better than non-generic | ||
CreateCompilation(text).VerifyDiagnostics( | ||
// (9,9): error CS0305: Using the generic type 'MyExpression<T>' requires 1 type arguments | ||
Diagnostic(ErrorCode.ERR_BadArity, "MyExpression<int, string, bool>").WithArguments("MyExpression<T>", "type", "1").WithLocation(9, 9), | ||
// (9,41): warning CS0168: The variable 'x' is declared but never used | ||
Diagnostic(ErrorCode.WRN_UnreferencedVar, "x").WithArguments("x").WithLocation(9, 41)); | ||
} | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.