-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Extension indexers: implicit indexers in list-patterns #82757
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
jcouv
wants to merge
5
commits into
dotnet:features/extensions
Choose a base branch
from
jcouv:indexers_11
base: features/extensions
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.
+5,413
−2,405
Open
Changes from 3 commits
Commits
Show all changes
5 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5422,7 +5422,7 @@ static BoundNode bindSpreadElement(SpreadElementSyntax syntax, BindingDiagnostic | |
| Debug.Assert(length > 0); | ||
| lengthOrCount = new BoundLiteral(expression.Syntax, ConstantValue.Create(length), @this.GetSpecialType(SpecialType.System_Int32, diagnostics, expression.Syntax)) { WasCompilerGenerated = true }; | ||
| } | ||
| else if (!@this.TryBindNonExtensionLengthOrCount(syntax.Expression, expressionPlaceholder, out lengthOrCount, ref useSiteInfo, diagnostics)) // PROTOTYPE should extension Length/Count count? | ||
| else if (!@this.TryBindNonExtensionLengthOrCount(syntax.Expression, expressionPlaceholder, out lengthOrCount, ref useSiteInfo, diagnostics)) | ||
| { | ||
| lengthOrCount = null; | ||
| } | ||
|
|
@@ -9268,17 +9268,15 @@ private bool TryBindImplicitIndexerInAnyScope(SyntaxNode syntax, BoundExpression | |
|
|
||
| AnalyzedArguments? analyzedIntIndexerOrSliceArguments = null; | ||
| ImmutableArray<BoundImplicitIndexerValuePlaceholder> intIndexerOrSliceArgumentPlaceholders = default; | ||
| AnalyzedArguments? actualExtensionLengthOrCountArguments = null; | ||
| AnalyzedArguments? actualExtensionIntIndexerOrSliceArguments = null; | ||
|
|
||
| bool result = tryBindImplicitIndexerInAnyScope( | ||
| syntax, left, analyzedArguments, binder: this, ref useSiteInfo, diagnostics, | ||
| ref analyzedIntIndexerOrSliceArguments, ref intIndexerOrSliceArgumentPlaceholders, | ||
| ref actualExtensionLengthOrCountArguments, ref actualExtensionIntIndexerOrSliceArguments, | ||
| ref actualExtensionIntIndexerOrSliceArguments, | ||
| out extensionIndexerAccess); | ||
|
|
||
| analyzedIntIndexerOrSliceArguments?.Free(); | ||
| actualExtensionLengthOrCountArguments?.Free(); | ||
| actualExtensionIntIndexerOrSliceArguments?.Free(); | ||
|
|
||
| return result; | ||
|
|
@@ -9293,14 +9291,13 @@ static bool tryBindImplicitIndexerInAnyScope( | |
| BindingDiagnosticBag diagnostics, | ||
| ref AnalyzedArguments? analyzedIntIndexerOrSliceArguments, | ||
| ref ImmutableArray<BoundImplicitIndexerValuePlaceholder> argumentPlaceholders, | ||
| ref AnalyzedArguments? actualExtensionLengthOrCountArguments, | ||
| ref AnalyzedArguments? actualExtensionIntIndexerOrSliceArguments, | ||
| out BoundExpression? indexerAccess) | ||
| { | ||
| indexerAccess = null; | ||
| IndexOrRangeArgKind argKind = GetIndexOrRangeArgKind(arguments, binder.Compilation); | ||
| if (argKind == IndexOrRangeArgKind.None) | ||
| { | ||
| indexerAccess = null; | ||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -9312,33 +9309,9 @@ static bool tryBindImplicitIndexerInAnyScope( | |
| BoundExpression? indexerOrSliceAccess = null; | ||
| BoundExpression? lengthOrCountAccess = null; | ||
|
|
||
| bool foundApplicableLengthOrCount = false; | ||
| if (binder.TryBindNonExtensionLengthOrCount(syntax, receiverPlaceholder, out var instanceLengthOrCountAccess, ref useSiteInfo, implicitIndexerDiagnostics)) | ||
| { | ||
| foundApplicableLengthOrCount = true; | ||
| lengthOrCountAccess = instanceLengthOrCountAccess; | ||
| } | ||
|
|
||
| if (!foundApplicableLengthOrCount) | ||
| { | ||
| foreach (var scope in new ExtensionScopes(binder)) | ||
| { | ||
| if (tryLookupExtensionLengthOrCount(syntax, receiverPlaceholder, binder, scope, | ||
| ref actualExtensionLengthOrCountArguments, out PropertySymbol? extensionLengthOrCountProperty, ref useSiteInfo, implicitIndexerDiagnostics)) | ||
| { | ||
| foundApplicableLengthOrCount = true; | ||
| if (extensionLengthOrCountProperty is not null) | ||
| { | ||
| Debug.Assert(extensionLengthOrCountProperty.ContainingType.ExtensionParameter is not null); | ||
| diagnostics.ReportUseSite(extensionLengthOrCountProperty, syntax); | ||
| lengthOrCountAccess = binder.GetExtensionMemberAccess(syntax, receiver, extensionLengthOrCountProperty, implicitIndexerDiagnostics).MakeCompilerGenerated(); | ||
| lengthOrCountAccess = binder.CheckValue(lengthOrCountAccess, BindValueKind.RValue, implicitIndexerDiagnostics); | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
| // Check for Length/Count property in both instance and extension scopes | ||
| bool foundApplicableLengthOrCount = binder.TryBindLengthOrCountInAnyScope( | ||
| syntax, receiverPlaceholder, ref useSiteInfo, implicitIndexerDiagnostics, out lengthOrCountAccess); | ||
|
|
||
| bool foundApplicableIndexerOrSlice = false; | ||
| if (foundApplicableLengthOrCount) | ||
|
|
@@ -9363,17 +9336,13 @@ static bool tryBindImplicitIndexerInAnyScope( | |
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (lengthOrCountAccess is not null && indexerOrSliceAccess is not null) | ||
| { | ||
| Debug.Assert(!argumentPlaceholders.IsDefault); | ||
| indexerAccess = binder.MakeImplicitIndexerAccess(syntax, receiver, arguments, receiverPlaceholder, | ||
| lengthOrCountAccess, indexerOrSliceAccess, argumentPlaceholders, argKind, implicitIndexerDiagnostics); | ||
| } | ||
| else | ||
| { | ||
| indexerAccess = null; | ||
| if (lengthOrCountAccess is not null && indexerOrSliceAccess is not null) | ||
| { | ||
| Debug.Assert(!argumentPlaceholders.IsDefault); | ||
| indexerAccess = binder.MakeImplicitIndexerAccess(syntax, receiver, arguments, receiverPlaceholder, | ||
| lengthOrCountAccess, indexerOrSliceAccess, argumentPlaceholders, argKind, implicitIndexerDiagnostics); | ||
| } | ||
| } | ||
|
|
||
| if (foundApplicableLengthOrCount && foundApplicableIndexerOrSlice) | ||
|
|
@@ -9387,12 +9356,40 @@ static bool tryBindImplicitIndexerInAnyScope( | |
| // (the Length/Count and the this[int]/Slice) when each part is searched independently across extension scopes. | ||
| return foundApplicableLengthOrCount && foundApplicableIndexerOrSlice; | ||
| } | ||
| } | ||
|
|
||
| // Returns true if any applicable candidates | ||
| // The caller is responsible to free actualExtensionLengthOrCountArguments | ||
| static bool TryBindExtensionLengthOrCountInScope( | ||
| SyntaxNode syntax, | ||
| BoundValuePlaceholderBase receiver, | ||
| Binder binder, | ||
| ExtensionScope scope, | ||
| ref AnalyzedArguments? actualExtensionLengthOrCountArguments, | ||
| out BoundExpression? lengthOrCountAccess, | ||
| ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo, | ||
| BindingDiagnosticBag diagnostics) | ||
| { | ||
| lengthOrCountAccess = null; | ||
| bool foundApplicable = tryLookupExtensionLengthOrCount(syntax, receiver, binder, scope, ref actualExtensionLengthOrCountArguments, out var lengthOrCountProperty, ref useSiteInfo, diagnostics); | ||
| if (foundApplicable) | ||
| { | ||
| if (lengthOrCountProperty is not null) | ||
| { | ||
| lengthOrCountProperty.AddUseSiteInfo(ref useSiteInfo); | ||
| Debug.Assert(lengthOrCountProperty.ContainingType.ExtensionParameter is not null); | ||
| lengthOrCountAccess = binder.GetExtensionMemberAccess(syntax, receiver, lengthOrCountProperty, diagnostics).MakeCompilerGenerated(); | ||
| lengthOrCountAccess = binder.CheckValue(lengthOrCountAccess, BindValueKind.RValue, diagnostics); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| // Returns true if any applicable candidates | ||
| // The caller is responsible to free actualExtensionLengthOrCountArguments | ||
| static bool tryLookupExtensionLengthOrCount( | ||
| SyntaxNode syntax, | ||
| BoundImplicitIndexerReceiverPlaceholder receiver, | ||
| BoundValuePlaceholderBase receiver, | ||
| Binder binder, | ||
| ExtensionScope scope, | ||
| ref AnalyzedArguments? actualExtensionLengthOrCountArguments, | ||
|
|
@@ -9464,7 +9461,46 @@ static bool tryResolveLengthOrCount(BoundExpression receiver, ArrayBuilder<Prope | |
| result.Free(); | ||
| return foundApplicable; | ||
| } | ||
| } | ||
|
|
||
| private bool TryBindLengthOrCountInAnyScope( | ||
| SyntaxNode node, | ||
| BoundValuePlaceholderBase receiverPlaceholder, | ||
| ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo, | ||
| BindingDiagnosticBag diagnostics, | ||
| out BoundExpression? lengthAccess) | ||
| { | ||
| var instanceDiagnostics = BindingDiagnosticBag.GetInstance(diagnostics); | ||
| bool foundApplicable = TryBindNonExtensionLengthOrCount(node, receiverPlaceholder, out lengthAccess, ref useSiteInfo, instanceDiagnostics); | ||
|
|
||
| if (foundApplicable) | ||
| { | ||
| diagnostics.AddRangeAndFree(instanceDiagnostics); | ||
| return true; | ||
| } | ||
|
|
||
| AnalyzedArguments? actualExtensionLengthOrCountArguments = null; | ||
| foreach (var scope in new ExtensionScopes(this)) | ||
| { | ||
| foundApplicable = TryBindExtensionLengthOrCountInScope(node, receiverPlaceholder, binder: this, scope, | ||
| ref actualExtensionLengthOrCountArguments, out lengthAccess, ref useSiteInfo, diagnostics); | ||
|
|
||
| if (foundApplicable) | ||
| { | ||
| if (lengthAccess is null) | ||
| { | ||
| break; | ||
| } | ||
|
|
||
| actualExtensionLengthOrCountArguments?.Free(); | ||
| instanceDiagnostics.Free(); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| actualExtensionLengthOrCountArguments?.Free(); | ||
| diagnostics.AddRangeAndFree(instanceDiagnostics); | ||
| return false; | ||
| } | ||
|
|
||
| // Returns true if any applicable candidates | ||
|
|
@@ -11309,6 +11345,8 @@ private bool TryBindNonExtensionImplicitIndexerParts( | |
| actualExtensionIntIndexerOrSliceArguments?.Free(); | ||
| } | ||
|
|
||
| // We consider this scope to have an applicable implicit indexer if we found applicable candidates for both parts (the Length/Count and the this[int]/Slice). | ||
| // If only one parts or no parts are applicable, we'll continue searching further scopes. | ||
| return lengthOrCountAccess?.HasErrors == false && indexerOrSliceAccess?.HasErrors == false; | ||
| } | ||
|
|
||
|
|
@@ -11408,13 +11446,30 @@ private static bool IsValidImplicitIndexIndexer(PropertySymbol property) | |
| original.Parameters[0] is { Type.SpecialType: SpecialType.System_Int32, RefKind: RefKind.None }; | ||
| } | ||
|
|
||
| // Returns true if any applicable candidates | ||
| private bool TryBindNonExtensionLengthOrCount( | ||
| SyntaxNode syntax, | ||
| BoundValuePlaceholderBase receiverPlaceholder, | ||
| out BoundExpression lengthOrCountAccess, | ||
| ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo, | ||
| BindingDiagnosticBag diagnostics) | ||
| { | ||
| Debug.Assert(receiverPlaceholder.Type is not null); | ||
| if (receiverPlaceholder.Type.IsSZArray()) | ||
| { | ||
| bool foundApplicable = TryGetSpecialTypeMember(Compilation, SpecialMember.System_Array__Length, syntax, diagnostics, out PropertySymbol lengthProperty); | ||
| if (lengthProperty is not null) | ||
|
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. |
||
| { | ||
| lengthOrCountAccess = new BoundPropertyAccess(syntax, receiverPlaceholder, initialBindingReceiverIsSubjectToCloning: ThreeState.False, lengthProperty, autoPropertyAccessorKind: AccessorKind.Unknown, LookupResultKind.Viable, lengthProperty.Type) { WasCompilerGenerated = true }; | ||
| } | ||
| else | ||
| { | ||
| lengthOrCountAccess = new BoundBadExpression(syntax, LookupResultKind.Empty, ImmutableArray<Symbol?>.Empty, ImmutableArray<BoundExpression>.Empty, CreateErrorType(), hasErrors: true) { WasCompilerGenerated = true }; | ||
| } | ||
|
|
||
| return foundApplicable; | ||
| } | ||
|
|
||
| var lookupResult = LookupResult.GetInstance(); | ||
|
|
||
| Debug.Assert(receiverPlaceholder.Type is not null); | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Is this ever true for non-list-pattern scenarios? #Resolved
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.
Yes. There are three callers for
TryBindNonExtensionLengthOrCount: 1. element access, 2. list-patterns, 3. spreads in collection expressions.For indexer access, we cannot reach this because array access is treated separately (routed to
BindArrayAccess).But other scenarios can.
I'll add a targeted test (
MissingMember_ArrayLength)