-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Extensions: relax inferrability rule #78758
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 7 commits
ace0477
46636d7
4996484
39dd249
129813b
3c27068
f00db0c
d158ed3
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 |
|---|---|---|
|
|
@@ -77,8 +77,6 @@ protected override (TypeWithAnnotations ReturnType, ImmutableArray<ParameterSymb | |
|
|
||
| if (parameter is { }) | ||
| { | ||
| checkUnderspecifiedGenericExtension(parameter, ContainingType.TypeParameters, diagnostics); | ||
|
|
||
| TypeSymbol parameterType = parameter.TypeWithAnnotations.Type; | ||
| RefKind parameterRefKind = parameter.RefKind; | ||
| SyntaxNode? parameterTypeSyntax = parameterList.Parameters[0].Type; | ||
|
|
@@ -112,34 +110,6 @@ protected override (TypeWithAnnotations ReturnType, ImmutableArray<ParameterSymb | |
|
|
||
| return parameter; | ||
| } | ||
|
|
||
| static void checkUnderspecifiedGenericExtension(ParameterSymbol parameter, ImmutableArray<TypeParameterSymbol> typeParameters, BindingDiagnosticBag diagnostics) | ||
|
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. 📝 |
||
| { | ||
| var underlyingType = parameter.Type; | ||
| var usedTypeParameters = PooledHashSet<TypeParameterSymbol>.GetInstance(); | ||
| underlyingType.VisitType(collectTypeParameters, arg: usedTypeParameters); | ||
|
|
||
| foreach (var typeParameter in typeParameters) | ||
| { | ||
| if (!usedTypeParameters.Contains(typeParameter)) | ||
| { | ||
| diagnostics.Add(ErrorCode.ERR_UnderspecifiedExtension, parameter.GetFirstLocation(), underlyingType, typeParameter); | ||
| } | ||
| } | ||
|
|
||
| usedTypeParameters.Free(); | ||
| } | ||
|
|
||
| static bool collectTypeParameters(TypeSymbol type, PooledHashSet<TypeParameterSymbol> typeParameters, bool ignored) | ||
| { | ||
| if (type is TypeParameterSymbol typeParameter) | ||
| { | ||
| typeParameters.Add(typeParameter); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -519,6 +519,71 @@ private static void EnsureNullableAttributeExists(CSharpCompilation compilation, | |
| } | ||
| } | ||
|
|
||
| internal static void CheckUnderspecifiedGenericExtension(Symbol extensionMember, ImmutableArray<ParameterSymbol> parameters, BindingDiagnosticBag diagnostics) | ||
| { | ||
| Debug.Assert(extensionMember.GetIsNewExtensionMember()); | ||
|
|
||
| NamedTypeSymbol extension = extensionMember.ContainingType; | ||
| if (extension.ExtensionParameter is not { } extensionParameter || extension.ContainingType.Arity != 0) | ||
| { | ||
| // error cases, already reported elsewhere | ||
| return; | ||
| } | ||
|
|
||
| if (extension.Arity == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var usedTypeParameters = PooledHashSet<TypeParameterSymbol>.GetInstance(); | ||
|
Member
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. Does this need some kind of comparison flags? Do we have tests where the type parameter differs by nullability? #Resolved
Contributor
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.
We are dealing with declarations, therefore, we don't need to worry about any substitution/alpha renaming going on. |
||
| reportUnusedExtensionTypeParameters(extensionMember, parameters, diagnostics, extension, extensionParameter, usedTypeParameters); | ||
|
|
||
| usedTypeParameters.Free(); | ||
| return; | ||
|
|
||
| static void reportUnusedExtensionTypeParameters(Symbol extensionMember, ImmutableArray<ParameterSymbol> parameters, BindingDiagnosticBag diagnostics, NamedTypeSymbol extension, ParameterSymbol extensionParameter, PooledHashSet<TypeParameterSymbol> usedTypeParameters) | ||
| { | ||
| int extensionArity = extension.Arity; | ||
| int extensionMemberArity = extensionMember.GetMemberArity(); | ||
| Debug.Assert(extensionMemberArity == 0); // we currently only apply the check for members which may not be generic | ||
|
|
||
| extensionParameter.Type.VisitType(collectTypeParameters, arg: usedTypeParameters); | ||
|
|
||
| if (usedTypeParameters.Count == extensionArity && extensionMemberArity == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var parameter in parameters) | ||
| { | ||
| parameter.Type.VisitType(collectTypeParameters, arg: usedTypeParameters); | ||
|
|
||
| if (usedTypeParameters.Count == extensionArity && extensionMemberArity == 0) | ||
| { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| foreach (var typeParameter in extension.TypeParameters) | ||
| { | ||
| if (!usedTypeParameters.Contains(typeParameter)) | ||
| { | ||
| diagnostics.Add(ErrorCode.ERR_UnderspecifiedExtension, extensionMember.GetFirstLocation(), typeParameter); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static bool collectTypeParameters(TypeSymbol type, PooledHashSet<TypeParameterSymbol> typeParameters, bool ignored) | ||
| { | ||
| if (type is TypeParameterSymbol typeParameter) | ||
| { | ||
| typeParameters.Add(typeParameter); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| internal static Location GetParameterLocation(ParameterSymbol parameter) => parameter.GetNonNullSyntaxNode().Location; | ||
|
|
||
| internal static void CheckParameterModifiers( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -561,6 +561,7 @@ public bool IsExtension | |
|
|
||
| /// <summary> | ||
| /// For the type representing an extension declaration, returns the receiver parameter symbol. | ||
| /// Note: this may be null even if <see cref="IsExtension"/> is true, in error cases. | ||
|
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. 📝 I'd already added that comment to the corresponding public API, but it seems useful here too
Member
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. Think you should add some remarks to indicate that it will not be |
||
| /// </summary> | ||
| internal abstract ParameterSymbol? ExtensionParameter { get; } | ||
| #nullable disable | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
I think this is far too wordy.