-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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,99 @@ 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) | ||||||||
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; | ||||||||
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. Shouldn't this be nullable? We seem to be comparing
Suggested change
|
||||||||
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)!; | ||||||||
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. Should we be also checking this result for 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. Good catch. Thanks. Added a test that hits that |
||||||||
|
||||||||
if (checkFullyInferred && constructed.IsGenericMethod && typeArguments.IsDefaultOrEmpty) | ||||||||
{ | ||||||||
return null; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
if ((object)constructed == null) | ||||||||
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 looks unnecessary since we |
||||||||
{ | ||||||||
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()); | ||||||||
var constructedProperty = (PropertySymbol)SourceNamedTypeSymbol.GetCompatibleSubstitutedMember(compilation, property, receiverType)!; | ||||||||
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. Why do we suppress nullability here but compare with |
||||||||
|
||||||||
if (constructedProperty is null) | ||||||||
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. Similar comment. #Resolved |
||||||||
{ | ||||||||
return null; | ||||||||
} | ||||||||
|
||||||||
return constructedProperty; | ||||||||
} | ||||||||
|
||||||||
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> | ||||||||
|
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
.