-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Extensions: account for new extensions in function type scenarios #77755
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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
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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| using System.Linq; | ||
| using Microsoft.CodeAnalysis.CSharp.Symbols; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.PooledObjects; | ||
| using Microsoft.CodeAnalysis.Text; | ||
| using Roslyn.Utilities; | ||
|
|
||
|
|
@@ -154,6 +155,87 @@ internal static TMember ConstructIncludingExtension<TMember>(this TMember member | |
| throw ExceptionUtilities.UnexpectedValue(member); | ||
| } | ||
|
|
||
| // For lookup APIs in the semantic model, we can return symbols that aren't fully inferred. | ||
| // But for function type inference, if the symbol isn't fully inferred with the information we have (the receiver and any explicit type arguments) | ||
| // then we won't return it. | ||
| internal static Symbol? GetReducedAndFilteredSymbol(this Symbol member, ImmutableArray<TypeWithAnnotations> typeArguments, TypeSymbol receiverType, CSharpCompilation compilation, bool checkFullyInferred) | ||
|
Member
Author
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. 📝 This logic is extracted from |
||
| { | ||
| if (member is MethodSymbol method) | ||
| { | ||
| // 1. construct with explicit type arguments if provided | ||
| MethodSymbol? constructed; | ||
| if (!typeArguments.IsDefaultOrEmpty && method.GetMemberArityIncludingExtension() == typeArguments.Length) | ||
| { | ||
| constructed = method.ConstructIncludingExtension(typeArguments); | ||
| Debug.Assert((object)constructed != null); | ||
|
|
||
| if (!checkConstraintsIncludingExtension(constructed, compilation, method.ContainingAssembly.CorLibrary.TypeConversions)) | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| constructed = method; | ||
| } | ||
|
|
||
| // 2. infer type arguments based on the receiver type if needed, check applicability, reduce symbol (for classic extension methods), check whether fully inferred | ||
| if ((object)receiverType != null) | ||
| { | ||
| if (method.IsExtensionMethod) | ||
| { | ||
| constructed = constructed.ReduceExtensionMethod(receiverType, compilation, out bool wasFullyInferred); | ||
|
|
||
| if (checkFullyInferred && !wasFullyInferred) | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Debug.Assert(method.GetIsNewExtensionMember()); | ||
| constructed = (MethodSymbol?)SourceNamedTypeSymbol.GetCompatibleSubstitutedMember(compilation, constructed, receiverType); | ||
|
|
||
| if (checkFullyInferred && constructed?.IsGenericMethod == true && typeArguments.IsDefaultOrEmpty) | ||
| { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return constructed; | ||
| } | ||
| else if (member is PropertySymbol property) | ||
| { | ||
| // infer type arguments based off the receiver type if needed, check applicability | ||
| Debug.Assert(receiverType is not null); | ||
| Debug.Assert(property.GetIsNewExtensionMember()); | ||
| return (PropertySymbol?)SourceNamedTypeSymbol.GetCompatibleSubstitutedMember(compilation, property, receiverType); | ||
| } | ||
|
|
||
| throw ExceptionUtilities.UnexpectedValue(member.Kind); | ||
|
|
||
| static bool checkConstraintsIncludingExtension(MethodSymbol symbol, CSharpCompilation compilation, TypeConversions conversions) | ||
| { | ||
| var constraintArgs = new ConstraintsHelper.CheckConstraintsArgs(compilation, conversions, includeNullability: false, | ||
| NoLocation.Singleton, diagnostics: BindingDiagnosticBag.Discarded, template: CompoundUseSiteInfo<AssemblySymbol>.Discarded); | ||
|
|
||
| bool success = true; | ||
|
|
||
| if (symbol.GetIsNewExtensionMember()) | ||
| { | ||
| NamedTypeSymbol extensionDeclaration = symbol.ContainingType; | ||
| success = extensionDeclaration.CheckConstraints(constraintArgs); | ||
| } | ||
|
|
||
| if (success) | ||
| { | ||
| success = symbol.CheckConstraints(constraintArgs); | ||
| } | ||
|
|
||
| return success; | ||
| } | ||
| } | ||
| #nullable disable | ||
|
|
||
| /// <summary> | ||
|
|
||
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.
📝 Fixed a bug here. The relevant test is
FunctionType_ColorColorReceiver_04.