-
Notifications
You must be signed in to change notification settings - Fork 86
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
Local variable initialization is not always equivalent to immediate assignment after declaration #899
Comments
@jskeet, do you intend this issue to also cover that many of the ref safety rules only apply to "reference variables" and not to variables of ref-like type? |
There's also this in 13.6.2 (Local variable declarations):
So you cannot transform a declaration with an initialiser, to a declaration without an initialiser, followed by an assignment. |
I'd only expected it to cover the issue you brought up on #837. I'm not sure whether that answers your question or not, but I basically wanted to avoid that new issue from blocking the PR. |
@jskeet, it was a misunderstanding on my part. Elaborated in #837 (comment). |
@KalleOlaviNiemitalo: Some aspects may have been a misunderstanding, but I still thing 13.6.2 needs clarification, as your example points out that the "exact correspondence" isn't the case. Or maybe I've misunderstood too :) |
The statement quoted at the start:
Needs to be updated to cope with:
If the above sounds wrong to you please comment. I haven’t wordsmithed a PR, yet… Here is the output from a demo app (yes, it prints the code, the failing statements show its not code itself!):
|
There's also a minor complication with an array initializer, as |
@KalleOlaviNiemitalo – I think array initializers are a “minor complication”, from §17.7:
So once the shorthand is expanded the equivalence holds. However there may well be other cases, both now and in the future, when the equivalence does not hold. Wordsmithing the exceptions is just messy. There is one obvious solution worth considering… |
Oops, fixed the title, its now:
|
I was thinking that one could even add a note like "the initialiser can affect the safe-context of the local variable (16.4.12.3)". However, 13.6.2 already has this that can be a sufficient hint:
But… now I don't understand what kind of code would trigger the compile-time error that is specified here. |
@KalleOlaviNiemitalo wrote:
If you are referring to “CS8353: A result of a stackalloc expression of type 'Span' cannot be used in this context because it may be exposed outside of the containing method” as you originally reported and shown in the sample output above then:
|
@Nigel-Ecma, I understand a compile-time error due to ref-safe-context mismatch in ref assignment, but the quoted paragraph is for a local_variable_initializer. |
In other words, for a local variable declaration ref T local_variable = ref variable_reference; What kind of variable_reference would trigger a compile-time error because "the scope of the local variable is wider than the ref-safe-context of the variable_reference"? Does the sentence mean a reference to a variable that has not been declared yet: ref int a = ref b;
int b = 0; IIRC there is some other text that disallows this case so it does not seem the purpose of the sentence. |
@KalleOlaviNiemitalo – I see what you're talking about now. This isn't the same thing as this issue and as there is now PR #909 this issue may be closed as done which will lose your comment. I suggest you put it in another issue.
|
This addresses #899. As explained there the statement “a variable initializer in a local variable declaration corresponds exactly to an assignment statement that is inserted immediately after the declaration” is not strictly true. However describing the exceptions would at best make a simple statement rather more complicated. This begs the question “What is the value of the statement?” This PR answers that one way – by just deleting it. Agree or disagree in your reviews (so yes, this is a “poll” which if passed automatically gets implemented).
@KalleOlaviNiemitalo: Is #909 enough to close this, or is there more needed, as far as you're aware? |
It looks sufficient to me. |
From #837, in statements.md:
@KalleOlaviNiemitalo wrote:
The text was updated successfully, but these errors were encountered: