Skip to content
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

List-patterns: add a few tests and add HasValidate flag #57914

Merged
merged 12 commits into from
Nov 24, 2021
2 changes: 1 addition & 1 deletion docs/contributing/Compiler Test Plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ This document provides guidance for thinking about language interactions and tes
- Ref return, ref readonly return, ref ternary, ref readonly local, ref local re-assignment, ref foreach
- `this = e;` in `struct` .ctor
- Stackalloc (including initializers)
- Patterns (constant, declaration, `var`, positional, property and extended property, discard, parenthesized, type, relational, `and`/`or`/`not`)
- Patterns (constant, declaration, `var`, positional, property and extended property, discard, parenthesized, type, relational, `and`/`or`/`not`, list, slice)
- Switch expressions
- With expressions (on record classes and on value types)
- Nullability annotations (`?`, attributes) and analysis
Expand Down
8 changes: 8 additions & 0 deletions eng/test-build-correctness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ try {
}
}

# Verify no TODO2 marker left
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jcouv is this something we expect to be useful on an ongoing basis? should we do something like flip the existing TODO comments over to TODO2 and change this to prevent introduction of new TODO comments to main?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do much about existing TODO comments. I've been using TODO2 comments to track things within a PR and having this helps me ensure I don't let any into the main branch or feature branches.

$prototypes = Get-ChildItem -Path src, eng, scripts -Exclude *.dll,*.exe,*.pdb,*.xlf,test-build-correctness.ps1 -Recurse | Select-String -Pattern 'TODO2' -CaseSensitive -SimpleMatch
if ($prototypes) {
Write-Host "Found TODO2 markers in source:"
Write-Host $prototypes
throw "TODO2 markers disallowed in compiler source"
}

Write-Host "Building Roslyn"
Exec-Block { & (Join-Path $PSScriptRoot "build.ps1") -restore -build -bootstrap -bootstrapConfiguration:Debug -ci:$ci -runAnalyzers:$true -configuration:$configuration -pack -binaryLog -useGlobalNuGetCache:$false -warnAsError:$true -properties "/p:RoslynEnforceCodeStyle=true"}

Expand Down
2 changes: 0 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ private BoundPattern BindSlicePattern(
pattern = BindPattern(node.Pattern, sliceType, GetValEscape(sliceType, inputValEscape), permitDesignations, hasErrors, diagnostics);
}

Debug.Assert(GetIndexerOrImplicitIndexerSymbol(indexerAccess) is var _);
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetIndexerOrImplicitIndexerSymbol

I think we should keep this assert, unless we are removing this method or changing how it is used. I see that this line got moved to Validate method. #Closed

return new BoundSlicePattern(node, pattern, indexerAccess, receiverPlaceholder, argumentPlaceholder, inputType: inputType, narrowedType: inputType, hasErrors);
}

Expand Down Expand Up @@ -317,7 +316,6 @@ private BoundListPattern BindListPattern(
inputValEscape, permitDesignations, typeSyntax: null, diagnostics, ref hasErrors,
out Symbol? variableSymbol, out BoundExpression? variableAccess);

Debug.Assert(GetIndexerOrImplicitIndexerSymbol(indexerAccess) is var _);
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetIndexerOrImplicitIndexerSymbol

Same comment here as well. #Closed

return new BoundListPattern(
syntax: node, subpatterns: subpatterns, hasSlice: sawSlice, lengthAccess: lengthAccess,
indexerAccess: indexerAccess, receiverPlaceholder, argumentPlaceholder, variable: variableSymbol,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ private Tests MakeTestsAndBindingsForListPattern(BoundDagTemp input, BoundListPa
Debug.Assert(slice.ReceiverPlaceholder is not null);
Debug.Assert(slice.ArgumentPlaceholder is not null);

Debug.Assert(slice.IndexerAccess is BoundIndexerAccess or BoundImplicitIndexerAccess or BoundArrayAccess);
var sliceEvaluation = new BoundDagSliceEvaluation(slicePattern.Syntax, slicePattern.InputType, lengthTemp, startIndex: startIndex, endIndex: index,
slice.IndexerAccess, slice.ReceiverPlaceholder, slice.ArgumentPlaceholder, input);

Expand All @@ -76,7 +75,6 @@ private Tests MakeTestsAndBindingsForListPattern(BoundDagTemp input, BoundListPa
Debug.Assert(list.ReceiverPlaceholder is not null);
Debug.Assert(list.ArgumentPlaceholder is not null);

Debug.Assert(list.IndexerAccess is BoundIndexerAccess or BoundImplicitIndexerAccess or BoundArrayAccess);
var indexEvaluation = new BoundDagIndexerEvaluation(subpattern.Syntax, subpattern.InputType, lengthTemp, index++,
list.IndexerAccess, list.ReceiverPlaceholder, list.ArgumentPlaceholder, input);

Expand Down
10 changes: 10 additions & 0 deletions src/Compilers/CSharp/Portable/BoundTree/BoundDagEvaluation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public override bool IsEquivalentTo(BoundDagEvaluation obj)
return base.IsEquivalentTo(obj) &&
this.Index == ((BoundDagIndexerEvaluation)obj).Index;
}

private partial void Validate()
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private partial void Validate()

Consider adding some space between declarations. #Closed

{
Debug.Assert(IndexerAccess is BoundIndexerAccess or BoundImplicitIndexerAccess or BoundArrayAccess);
}
}

partial class BoundDagSliceEvaluation
Expand All @@ -119,6 +124,11 @@ public override bool IsEquivalentTo(BoundDagEvaluation obj)
(BoundDagSliceEvaluation)obj is var e &&
this.StartIndex == e.StartIndex && this.EndIndex == e.EndIndex;
}

private partial void Validate()
{
Debug.Assert(IndexerAccess is BoundIndexerAccess or BoundImplicitIndexerAccess or BoundArrayAccess);
}
}

partial class BoundDagAssignmentEvaluation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ internal BoundExpression GetReceiver()
Debug.Assert(receiver is not null);
return receiver;
}

private partial void Validate()
{
Debug.Assert(LengthOrCountAccess is BoundPropertyAccess or BoundLocal or BoundBadExpression);
Debug.Assert(IndexerOrSliceAccess is BoundIndexerAccess or BoundCall or BoundArrayAccess or BoundBadExpression);
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or BoundBadExpression

The addition of a BoundBadExpression for IndexerOrSliceAccess feels unexpected. We don't call CheckValue on it and we don't handle BoundBadExpression in various helpers. For example in GetReceiver above. #Closed

}
}
}
18 changes: 18 additions & 0 deletions src/Compilers/CSharp/Portable/BoundTree/BoundListPattern.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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.

using System.Diagnostics;

namespace Microsoft.CodeAnalysis.CSharp
{
internal partial class BoundListPattern
{
private partial void Validate()
{
Debug.Assert(LengthAccess is null or BoundPropertyAccess or BoundBadExpression);
Debug.Assert(IndexerAccess is BoundIndexerAccess or BoundImplicitIndexerAccess or BoundArrayAccess or BoundBadExpression or BoundDynamicIndexerAccess);
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IndexerAccess

Can it be null? #Closed

Debug.Assert(Binder.GetIndexerOrImplicitIndexerSymbol(IndexerAccess) is var _);
}
}
}
10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1519,7 +1519,7 @@
<Field Name="Index" Type="int"/>
</Node>

<Node Name="BoundDagIndexerEvaluation" Base="BoundDagEvaluation">
<Node Name="BoundDagIndexerEvaluation" Base="BoundDagEvaluation" HasValidate="true">
<Field Name="IndexerType" Type="TypeSymbol"/>
<Field Name="LengthTemp" Type="BoundDagTemp"/>

Expand All @@ -1539,7 +1539,7 @@
<Field Name="ArgumentPlaceholder" Type="BoundListPatternIndexPlaceholder"/>
</Node>

<Node Name="BoundDagSliceEvaluation" Base="BoundDagEvaluation">
<Node Name="BoundDagSliceEvaluation" Base="BoundDagEvaluation" HasValidate="true">
<Field Name="SliceType" Type="TypeSymbol"/>
<Field Name="LengthTemp" Type="BoundDagTemp"/>
<Field Name="StartIndex" Type="int"/>
Expand Down Expand Up @@ -2066,7 +2066,7 @@
<Field Name="OriginalIndexersOpt" Type="ImmutableArray&lt;PropertySymbol&gt;" Null="allow" SkipInNullabilityRewriter="true"/>
</Node>

<Node Name="BoundImplicitIndexerAccess" Base="BoundExpression" SkipInNullabilityRewriter="true">
<Node Name="BoundImplicitIndexerAccess" Base="BoundExpression" SkipInNullabilityRewriter="true" HasValidate="true">
<Field Name="Type" Type="TypeSymbol" Override="true" Null="disallow" />

<!-- An expression with type Index or Range -->
Expand Down Expand Up @@ -2242,7 +2242,7 @@
<Field Name="IsExplicitNotNullTest" Type="bool"/>
</Node>

<Node Name="BoundListPattern" Base="BoundObjectPattern">
<Node Name="BoundListPattern" Base="BoundObjectPattern" HasValidate="true">
<Field Name="Subpatterns" Type="ImmutableArray&lt;BoundPattern&gt;" Null="disallow"/>
<Field Name="HasSlice" Type="bool"/>
<Field Name="LengthAccess" Type="BoundExpression?" SkipInVisitor="true"/>
Expand All @@ -2260,7 +2260,7 @@
<Field Name="ArgumentPlaceholder" Type="BoundListPatternIndexPlaceholder?" SkipInVisitor="true"/>
</Node>

<Node Name="BoundSlicePattern" Base="BoundPattern">
<Node Name="BoundSlicePattern" Base="BoundPattern" HasValidate="true">
<Field Name="Pattern" Type="BoundPattern?"/>

<!--
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/BoundTree/BoundNodes.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<xs:attribute name="Name" type="xs:string" use="required" />
<xs:attribute name="Base" type="xs:string" use="required" />
<xs:attribute name="SkipInNullabilityRewriter" type="xs:boolean" use="optional" />
<xs:attribute name="HasValidate" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:element>
</xs:choice>
Expand Down
17 changes: 17 additions & 0 deletions src/Compilers/CSharp/Portable/BoundTree/BoundSlicePattern.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.

using System.Diagnostics;

namespace Microsoft.CodeAnalysis.CSharp
{
internal partial class BoundSlicePattern
{
private partial void Validate()
{
Debug.Assert(IndexerAccess is BoundIndexerAccess or BoundImplicitIndexerAccess or BoundArrayAccess or BoundBadExpression or BoundDynamicIndexerAccess);
Copy link
Contributor

@AlekseyTs AlekseyTs Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be null? #Closed

Debug.Assert(Binder.GetIndexerOrImplicitIndexerSymbol(IndexerAccess) is var _);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading