-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Collection expressions: analyze nullability of element assignments #70192
Collection expressions: analyze nullability of element assignments #70192
Conversation
db7004f
to
3c6919c
Compare
src/Compilers/CSharp/Test/Emit2/Semantics/CollectionExpressionTests.cs
Outdated
Show resolved
Hide resolved
b8d0c85
to
121cade
Compare
121cade
to
6517f58
Compare
break; | ||
case BoundCollectionExpressionSpreadElement spread: | ||
// https://github.com/dotnet/roslyn/issues/68786: We should check the spread | ||
Visit(spread); |
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 there anything to do besides visit the spread, and warn if the result may be null? (since we want to enumerate the operand value, it should be non-null.) #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.
I think we may want VisitRvalue here because if a spread puts us into a split state after visiting (like in an error scenario) that could mess things up later on.
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.
FWIW, with #70197, NullableWalker.VisitCollectionExpressionSpreadElement()
relies on base.VisitCollectionExpressionSpreadElement(node)
which simply calls VisitRvalue(node.Expression)
.
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.
I'd be comfortable with this assuming we have a test demonstrating that things don't fall over when an expression inside a spread splits the state. e.g. [..(x is not null)]
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.
Spreads are not in scope for this PR, so I don't want to go too far. I'd be okay with removing this entirely if you prefer.
As things stand, ..(x is null)
hits an assertion: System.InvalidOperationException : Analyzed 11 nodes in NullableWalker, but DebugVerifier expects 10. Example of unverified node: BoundCollectionExpressionSpreadElement ..(x is null)
In release mode, the test produces the expected diagnsotic (binding error). Added a test
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.
Note: a similar assertion is hit if I remove this case, even on simple cases. I still think it's beneficial to keep it in the interim (before we have fully coverage on spreads)
System.InvalidOperationException : Analyzed 33 nodes in NullableWalker, but DebugVerifier expects 27. Example of unverified node: BoundCollectionExpressionSpreadElement ..a
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.
As long as the compiler doesn't blow up in the retail build in these cases, I'm happy. Since it looks like we are actually visiting the spread operand, I think we are just missing the top-level nullable warning for when the operand may be null. That's OK to address later. Thanks!
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.
Chuck pointed out the assertion problem can be solved by undoing the s_skippedExpressions
change. Done
src/Compilers/CSharp/Test/Emit2/Semantics/CollectionExpressionTests.cs
Outdated
Show resolved
Hide resolved
Want to wait until the suggested Visit->VisitRValue change is investigated
Analyzers leg failed src/Compilers/CSharp/Test/Emit2/Semantics/CollectionExpressionTests.cs(18732,1): error IDE0055: (NETCORE_ENGINEERING_TELEMETRY=Build) Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) |
// https://github.com/dotnet/roslyn/issues/68786: We should check the spreads without asserting in DebugVerifier | ||
#if RELEASE | ||
string src = """ | ||
#nullable enable |
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.
Formatting warning is being reported here. I honestly don't know what's wrong. Maybe a bug due to presence of the if-directive above? Might want to use compilation options to enable nullability for this test, or use attributes to only run the test in release mode.
I think @Youssef1313 may have reported something like this recently. #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.
@RikkiGibson Yup, that's the same root cause as dotnet/csharplang#7520.
For "Debug" where the #if RELEASE
is false, the compiler will parse everything in the #if RELEASE
block as disabled trivia, and it will parse #nullable enable
as NullableDirectiveTrivia even if the human thinking is that it's part of a string literal and not a nullable directive trivia.
public void SpreadNullability_SplitExpression() | ||
{ | ||
// https://github.com/dotnet/roslyn/issues/68786: We should check the spreads without asserting in DebugVerifier | ||
#if RELEASE |
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.
Consider using [ConditionalFact(typeof(IsRelease))]
instead. #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.
Yup. Done. Thanks
…o coll-nullability
…o coll-nullability
…o coll-nullability
Addresses parts of #68786
The parts that are not addressed in this PR:
Relates to test plan #66418