List patterns: add more tests and fix nullability analysis#53822
List patterns: add more tests and fix nullability analysis#53822jcouv merged 10 commits intodotnet:features/list-patternsfrom
Conversation
0e1f442 to
232d033
Compare
There was a problem hiding this comment.
This is not testing slice. Did you mean LengthPattern_Exhaustiveness #Resolved
There was a problem hiding this comment.
📝 I'll have a PR out for this next (I'd probably hold that off until after LDM) #Resolved
There was a problem hiding this comment.
Maybe also test for new C<Index>() is { 0 } with class C<T> { this[T] } (should work) #Resolved
There was a problem hiding this comment.
Good idea. It looks like there may be a bug with Index/Range, and the same one for list patterns. See ListPattern_GenericIndexingParameter below.
There was a problem hiding this comment.
I don't think this is intended to be here. #Resolved
There was a problem hiding this comment.
Slice could return an arbitrary type - as far as the analysis goes, the input type of the subpattern is "unrelated" to the list. #Resolved
There was a problem hiding this comment.
Right. I think there's still a question of whether we have some reasonable expectations when a slice returns an indexable/rangeable type. Do we just give up on analysis or do we have some notion of what is "well-behaved"?
There was a problem hiding this comment.
We only do this for tests when the input is exactly the same. In #53891 I had to explicitly add a special case when there's two different but "matching" indexers as the input which is a little unorthodox.
As for slice subpatterns, it wouldn't be that simple. We should match nested subpattern with the outer list. I didn't go through it but I'd expect it's going to be a lot of work which is arguably unlikely to be useful in real code.
There was a problem hiding this comment.
I'd expect it's going to be a lot of work which is arguably unlikely to be useful in real code.
I agree. I'd be comfortable dropping the slice analysis. Just need to confirm with LDM.
There was a problem hiding this comment.
📝 We possibly can support this if we relax range indexers too. #Resolved
There was a problem hiding this comment.
Assuming positive count in the codegen or for subsumption? e.g. Is the match e is [-1] impossible? of course we don't need to worry about this if we drop length patterns.. #Resolved
There was a problem hiding this comment.
LDM agreed to make such assumptions for the length/list pattern. But not generally for property patterns such as Count: <= 0 above.
Not having a good syntax for empty list means causing more pain if I fall back to Count property pattern.
There was a problem hiding this comment.
Very cool. Thanks!
Ping when you want some eyes on that PR.
There was a problem hiding this comment.
I think we should also test for [ImplicitlyConvertibleFromIndex i] (should work) #Resolved
There was a problem hiding this comment.
Good idea. Added ListPattern_ImplicitlyConvertibleFromIndexAndRange which hit an assertion.
There was a problem hiding this comment.
This is expected. we require exactly this[int] as the member for implicit support (vs. only a viable lookup for Index). #Resolved
|
Rebased the change on top of syntax change PR which is now merged. |
|
@333fred @dotnet/roslyn-compiler for review. Thanks |
src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker_Patterns.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests_ListPatterns.cs
Outdated
Show resolved
Hide resolved
|
@dotnet/roslyn-compiler for second review. Thanks |
There was a problem hiding this comment.
Perhaps we should open a tracking issue to make an analogous change for the scenarios @alrz is referring to.
| verify(declarations[0], "item", "System.Int32"); | ||
| verify(declarations[1], "item2", "System.Int32?"); | ||
| verify(declarations[2], "item3", "System.String?"); | ||
| verify(declarations[3], "item4", "System.String?"); |
There was a problem hiding this comment.
nit: assert on declarations.Length? (same comment in the other similar new tests) #Resolved
| } | ||
| } | ||
| "; | ||
| // PROTOTYPE: incorrect exhaustiveness examples from explainer |
There was a problem hiding this comment.
nit: prefer PROTOTYPE(list-patterns) (also for other prototype comments in the file.)
FWIW, I don't see shortcomings in the pattern explainer as blocking.
There was a problem hiding this comment.
Since we disallow the "PROTOTYPE" keyword in main branch, I figured the longer format doesn't help much (and it's more trouble to type). I'll stick with that if that's okay.
Test plan: #51289
Note: the nullability analysis in this PR does not handle exhaustiveness analysis with aliasing of start/end or nested indexes