-
Notifications
You must be signed in to change notification settings - Fork 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
Null propagation expression does not allow !
as a nested operator form (VS 16.8, .NET 5)
#3393
Comments
I was not aware of this issue and filed dotnet/roslyn#43659. |
It doesn't feel like accepting an optional token after a simple name is sufficient for a case like |
Looks like the NRT Spec needs to be updated. Presently it says
That is not true though cause it changes the manner in which conditional expressions are parsed hence their runtime behavior. Here is a simple sharplab sample that demonstrates this. |
Well, that's how it should be. This is a bug that we need to fix. |
Yes. Also parentheses have runtime behavior for the same reason. |
If nobody has written a syntax proposal for this I would be happy to take a stab at it. |
@RikkiGibson iirc https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#null-conditional-operator is the place we need to adjust: specifically, |
The initial example was not supposed to parse; it's a bug that we accept it at all: var x = a?.b.c!.d.e; The reason is that So in a sense, the fact that we accept this in C# 8.0 is a bug. However, I do agree that we should give it sensible semantics. We could enhance the grammar as follows: null_conditional_expression
: primary_expression null_conditional_operations_no_suppression suppression?
;
null_conditional_operations_no_suppression
: null_conditional_operations? '?' '.' identifier type_argument_list?
| null_conditional_operations? '?' '[' argument_list ']'
| null_conditional_operations '.' identifier type_argument_list?
| null_conditional_operations '[' argument_list ']'
| null_conditional_operations '(' argument_list? ')'
;
null_conditional_operations
: null_conditional_operations_no_suppression suppression?
;
suppression
: '!'
; The semantics then are that the "interior" |
!
as a nested operator form!
as a nested operator form (VS 16.8, .NET 5)
See dotnet/docs#17988
This means that the only correct parse of
is
(a?.b.c)!.d.e
, which prevents usage of null-forgiving in a chain of null-propagating accesses. To remedy this we could accept!
as an optional token after every simple name in the null-propagating non-terminal.cc @jcouv @gafter
Was discussed in LDM 6/17/2020. There is agreement/support, but we'll need to investigate and make a concrete syntax proposal. Also, there is desire to keep terminal
!
parsing as today:a?.b.c!
would still parse as(a?.b.c)!
.For reference, the current syntax.
The text was updated successfully, but these errors were encountered: