-
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
base: features/extensions
Are you sure you want to change the base?
Changes from 2 commits
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 |
|---|---|---|
|
|
@@ -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, scope.Binder, 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()) | ||
|
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.
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. Yes. There are three callers for I'll add a targeted test (
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. Does this mean that a call site at
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. If behavior of |
||
| { | ||
| 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); | ||
|
|
||
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.
Should we use
thisinstead ofscope.Binderhere? #Resolved