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

Operator precedence for x?.y seems to be listed wrong #26112

Closed
initram opened this issue Sep 15, 2021 · 5 comments
Closed

Operator precedence for x?.y seems to be listed wrong #26112

initram opened this issue Sep 15, 2021 · 5 comments
Assignees
Labels
doc-bug Problem with the content; needs to be fixed [org][type][category] dotnet-csharp/svc lang-reference/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. Pri1 High priority, do before Pri2 and Pri3 📌 seQUESTered Identifies that an issue has been imported into Quest.

Comments

@initram
Copy link
Contributor

initram commented Sep 15, 2021

Under the section "Operator precedence" the following are listed as having equal precedence: "x.y" and "x?.y"
Also none of them are listed as being right associative.

This would then mean that the following two lines should be semantically identical:

var x = a.b?.c.d;
var x = (a.b?.c).d;

This how ever is not how the language works!
The first example will never try to access d on c if b is null. The final result will simply be null if b is null.
in the second example it will try to access d on the result of the expression in parentheses, even if b is null. (resulting in a null reference exception if b is null)

I am not sure exactly where in the precedence hierarchy "x?.y" belongs, but at least lower than primary expressions.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.


Associated WorkItem - 123095

@dotnet-bot dotnet-bot added the ⌚ Not Triaged Not triaged label Sep 15, 2021
@PRMerger8 PRMerger8 added lang-reference/subsvc dotnet-csharp/svc Pri1 High priority, do before Pri2 and Pri3 labels Sep 15, 2021
@BillWagner
Copy link
Member

Thanks for noting this @initram

Ill add this to our backlog pending work currently in progress in the ECMA standards committee. The committee is evaluating two different strategies for the normative language in this feature:

Once one of those is merged, we'll update this article to match.

@BillWagner BillWagner added the doc-bug Problem with the content; needs to be fixed [org][type][category] label Sep 15, 2021
@dotnet-bot dotnet-bot removed the ⌚ Not Triaged Not triaged label Sep 15, 2021
@pkulikov
Copy link
Contributor

@initram @BillWagner there is some history about the precedence of the null-conditional operator (once it was moved away from the primary row but then returned back):
#18037
#18001 (that one contains a discussion about the operator precedence)

@gafter
Copy link
Member

gafter commented Sep 21, 2021

Your assertion that var x = a.b?.c.d; should mean the same thing as var x = (a.b?.c).d; only works if a.b?.c is a subexpression of the former. But it isn't a subexpression of the former (even though it is a substring of the former).

Having said that, the OP does have a point. The . "operator" and the ?. "operator" do not take an expression on their right-hand-side, so the usual concept of precedence does not apply.

@ghost ghost added the okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. label Sep 7, 2022
@BillWagner BillWagner added the 🗺️ reQUEST Triggers an issue to be imported into Quest. label Jun 30, 2023
@BillWagner BillWagner self-assigned this Jun 30, 2023
@BillWagner
Copy link
Member

Adding this to the sprint as I'm making other changes in the same file.

@BillWagner
Copy link
Member

Already fixed in #34945.

Closing.

@github-actions github-actions bot added 📌 seQUESTered Identifies that an issue has been imported into Quest. and removed 🗺️ reQUEST Triggers an issue to be imported into Quest. labels Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-bug Problem with the content; needs to be fixed [org][type][category] dotnet-csharp/svc lang-reference/subsvc okr-quality Content-quality KR: Concerns article defects (bugs), freshness, or build warnings. Pri1 High priority, do before Pri2 and Pri3 📌 seQUESTered Identifies that an issue has been imported into Quest.
Projects
No open projects
Status: Done
Development

No branches or pull requests

6 participants