-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Unify representation of an implicit Index
indexer over an array and non-array types.
#57918
Changes from all 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Microsoft.CodeAnalysis.CSharp | ||
{ | ||
internal partial class BoundArrayAccess | ||
{ | ||
internal BoundArrayAccess WithReceiver(BoundExpression receiver) | ||
{ | ||
return this.Update(receiver, this.Indices, this.Type); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -222,7 +222,7 @@ private BoundExpression VisitImplicitIndexerAccess(BoundImplicitIndexerAccess no | |
private BoundExpression VisitIndexPatternIndexerAccess(BoundImplicitIndexerAccess node, bool isLeftOfAssignment) | ||
{ | ||
Debug.Assert(node.ArgumentPlaceholders.Length == 1); | ||
Debug.Assert(node.IndexerOrSliceAccess is BoundIndexerAccess); | ||
Debug.Assert(node.IndexerOrSliceAccess is BoundIndexerAccess or BoundArrayAccess); | ||
|
||
Debug.Assert(TypeSymbol.Equals( | ||
node.Argument.Type, | ||
|
@@ -259,15 +259,17 @@ private BoundExpression VisitIndexPatternIndexerAccess(BoundImplicitIndexerAcces | |
switch (strategy) | ||
{ | ||
case PatternIndexOffsetLoweringStrategy.SubtractFromLength: | ||
// ensure we evaluate the input before accessing length | ||
if (makeOffsetInput.ConstantValue is null) | ||
BoundExpression lengthAccess = RewriteLengthAccess(node, receiver); | ||
|
||
// ensure we evaluate the input before accessing length, unless it is an array length | ||
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 is array length different in terms of ordering? #Resolved 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.
Because it is an IL instruction, not a property access. I.e. not observable. There is no change in behavior here, we are doing what we used to do. |
||
if (makeOffsetInput.ConstantValue is null && lengthAccess.Kind is not BoundKind.ArrayLength) | ||
{ | ||
makeOffsetInput = F.StoreToTemp(makeOffsetInput, out BoundAssignmentOperator inputStore); | ||
locals.Add(((BoundLocal)makeOffsetInput).LocalSymbol); | ||
sideeffects.Add(inputStore); | ||
} | ||
|
||
integerArgument = MakePatternIndexOffsetExpression(makeOffsetInput, RewriteLengthAccess(node, receiver), strategy); | ||
integerArgument = MakePatternIndexOffsetExpression(makeOffsetInput, lengthAccess, strategy); | ||
break; | ||
|
||
case PatternIndexOffsetLoweringStrategy.UseAsIs: | ||
|
@@ -282,40 +284,46 @@ private BoundExpression VisitIndexPatternIndexerAccess(BoundImplicitIndexerAcces | |
throw ExceptionUtilities.UnexpectedValue(strategy); | ||
} | ||
|
||
var indexerAccess = (BoundIndexerAccess)node.IndexerOrSliceAccess; | ||
Debug.Assert(node.ArgumentPlaceholders.Length == 1); | ||
var argumentPlaceholder = node.ArgumentPlaceholders[0]; | ||
AddPlaceholderReplacement(argumentPlaceholder, integerArgument); | ||
|
||
BoundExpression rewrittenIndexerAccess; | ||
|
||
if (isLeftOfAssignment && indexerAccess.Indexer.RefKind == RefKind.None) | ||
if (node.IndexerOrSliceAccess is BoundIndexerAccess indexerAccess) | ||
{ | ||
ImmutableArray<BoundExpression> rewrittenArguments = VisitArguments( | ||
indexerAccess.Arguments, | ||
indexerAccess.Indexer, | ||
indexerAccess.ArgsToParamsOpt, | ||
indexerAccess.ArgumentRefKindsOpt, | ||
ref receiver, | ||
out ArrayBuilder<LocalSymbol>? temps); | ||
if (isLeftOfAssignment && indexerAccess.Indexer.RefKind == RefKind.None) | ||
{ | ||
ImmutableArray<BoundExpression> rewrittenArguments = VisitArguments( | ||
indexerAccess.Arguments, | ||
indexerAccess.Indexer, | ||
indexerAccess.ArgsToParamsOpt, | ||
indexerAccess.ArgumentRefKindsOpt, | ||
ref receiver, | ||
out ArrayBuilder<LocalSymbol>? temps); | ||
|
||
if (temps is not null) | ||
{ | ||
locals.AddRange(temps); | ||
temps.Free(); | ||
} | ||
|
||
if (temps is not null) | ||
rewrittenIndexerAccess = indexerAccess.Update( | ||
receiver, indexerAccess.Indexer, rewrittenArguments, | ||
indexerAccess.ArgumentNamesOpt, indexerAccess.ArgumentRefKindsOpt, | ||
indexerAccess.Expanded, | ||
indexerAccess.ArgsToParamsOpt, | ||
indexerAccess.DefaultArguments, | ||
indexerAccess.Type); | ||
} | ||
else | ||
{ | ||
locals.AddRange(temps); | ||
temps.Free(); | ||
rewrittenIndexerAccess = VisitIndexerAccess(indexerAccess.WithReceiver(receiver), isLeftOfAssignment); | ||
} | ||
|
||
rewrittenIndexerAccess = indexerAccess.Update( | ||
receiver, indexerAccess.Indexer, rewrittenArguments, | ||
indexerAccess.ArgumentNamesOpt, indexerAccess.ArgumentRefKindsOpt, | ||
indexerAccess.Expanded, | ||
indexerAccess.ArgsToParamsOpt, | ||
indexerAccess.DefaultArguments, | ||
indexerAccess.Type); | ||
} | ||
else | ||
{ | ||
rewrittenIndexerAccess = VisitIndexerAccess(indexerAccess.WithReceiver(receiver), isLeftOfAssignment); | ||
rewrittenIndexerAccess = (BoundExpression)VisitArrayAccess(((BoundArrayAccess)node.IndexerOrSliceAccess).WithReceiver(receiver)); | ||
} | ||
|
||
RemovePlaceholderReplacement(argumentPlaceholder); | ||
|
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.
nit: Consider commenting: this case is
array[int]
and the one above isarray[Index/Range]
#Pending