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

C# 7 §13.6.2 ref readonly in local_variable_initializer #828

Closed
KalleOlaviNiemitalo opened this issue Jun 28, 2023 · 1 comment · Fixed by #831
Closed

C# 7 §13.6.2 ref readonly in local_variable_initializer #828

KalleOlaviNiemitalo opened this issue Jun 28, 2023 · 1 comment · Fixed by #831
Labels
type: bug The Standard does not describe the language as intended or implemented
Milestone

Comments

@KalleOlaviNiemitalo
Copy link
Contributor

KalleOlaviNiemitalo commented Jun 28, 2023

Describe the bug

In the C# 7 draft, §13.6.2 (Statements / Declaration statements / Local variable declarations) specifies the following restriction for "implicitly typed local variable declaration":

The local_variable_initializer shall be an expression, optionally preceded by ref or ref readonly.

However, ref readonly at the start of a local_variable_initializer is not allowed by the grammar and not allowed by Roslyn either. The grammar allows ref readonly in local_variable_declaration in front of local_variable_type, but not in front of an expression.

Example

The text of the draft gives the impression that ref readonly i is valid in the following. It is not.

public class C {
    static readonly int i;
    
    void M()
    {
        ref readonly var huh = ref readonly i;
    }
}

Expected behavior

Say just "optionally preceded by ref" and don't mention ref readonly.

Additional context

Declaring and using ref readonly locals in the C# 7.2 proposal shows the initialization ref readonly var r1 = ref M1(); without using ref readonly in the initializer.

@jskeet jskeet added this to the C# 7.x milestone Jul 4, 2023
@jskeet jskeet added the type: bug The Standard does not describe the language as intended or implemented label Jul 4, 2023
@KalleOlaviNiemitalo
Copy link
Contributor Author

If the text allows ref readonly in the initializer but the grammar disallows it, and both are normative, I think that means the standard does not actually allow it. Therefore it would be ok to publish C# 7 with this error even though it is misleading to human readers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug The Standard does not describe the language as intended or implemented
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants