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.3 method_body etc. grammar does not allow => ref #825

Closed
KalleOlaviNiemitalo opened this issue Jun 28, 2023 · 3 comments · Fixed by #900
Closed

C# 7.3 method_body etc. grammar does not allow => ref #825

KalleOlaviNiemitalo opened this issue Jun 28, 2023 · 3 comments · Fixed by #900
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 C# 7.3, a method/property/accessor/indexer body => ref variable_reference; should be allowed and mean the same as { return ref variable_reference; }. Roslyn allows this, but the grammar in the C# 7 draft doesn't.

This issue concerns multiple clauses, listed under "Expected behavior" below.

Example

The following should be allowed, but => ref field does not match the syntactic grammar.

class C {
    int field;
    ref int ShortProperty => ref field;
    ref int LongProperty { get => ref field; }
    ref int this[int index] => ref field;
    ref int Method() => ref field;
    void Outer() {
        ref int Inner() => ref field;
    }
}

Expected behavior

In §15.7.1 (Classes / Properties / General), property_body should allow => ref variable_reference ;.

In §15.7.3 (Classes / Properties / Accessors), accessor_body should allow => ref variable_reference ;.

In §15.9 (Classes / Indexers), indexer_body should allow => ref variable_reference ;.

In §15.6.1 (Classes / Methods / General), method_body should allow => ref variable_reference ;.

In §13.6.4 (Statements / Declaration statements / Local function declarations), local_function_body should allow => ref variable_reference ;.

The standard should specify what the => ref syntax means, perhaps by reference to §13.10.5 (Statements / Jump statements / The return statement), which specifies the meaning of return ref.

Additional context

In §12.19.1 (Expressions / Anonymous function expressions / General), anonymous_function_body already allows ref variable_reference. However, I haven't found anything that specifies what ref means in this context.

In §15.10.1 (Classes / Operators / General), operator_body does not allow => ref variable_reference ; but this is OK as operators cannot return by reference; operator_declarator does not allow ref either.

In §15.11.1 (Classes / Instance constructors / General), constructor_body does not allow => ref variable_reference ; but this is OK as instance constructors cannot return values either.

In §15.12 (Classes / Static constructors), static_constructor_body does not allow => ref variable_reference ; but this is OK as static constructors cannot return values either.

In §15.13 (Classes / Finalizers), finalizer_body does not allow => ref variable_reference ; but this is OK as finalizers cannot return values either.

Elaborated from #213 (comment)

@KalleOlaviNiemitalo KalleOlaviNiemitalo changed the title C# 7.3 _method_body_ etc. grammar does not allow => ref C# 7.3 method_body etc. grammar does not allow => ref Jun 28, 2023
@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
@jskeet
Copy link
Contributor

jskeet commented Jul 4, 2023

@Nigel-Ecma: If you've got bandwidth to look at this, that would be great - if not, let me know and I'll see if @gafter can.

@Nigel-Ecma
Copy link
Contributor

Covered by PR #837

@Nigel-Ecma Nigel-Ecma removed their assignment Aug 14, 2023
jskeet pushed a commit to jskeet/csharpstandard that referenced this issue Aug 17, 2023
(This was originally in dotnet#837.)

The grammar has been extended to be as prescriptive as reasonably possible as befits a language specification[1], this means that former statements such as it being illegal to have a set accessor for a ref-valued property are now covered by the grammar. However the usual verbose description of the grammar remains!

The term ref-valued rather than ref-returning is used, a term closer to the language of fields. To distingush the two kinds of properties, which do follow different rules, the corresponding and somewhat ugly term non-ref-valued is used (non-ref-returning would be just as ugly).

[1] The grammar used by a compiler may be less restrictive to support more descriptive error messages, that is of course an implementation issue!

Fixes dotnet#825.
jskeet pushed a commit that referenced this issue Aug 17, 2023
(This was originally in #837.)

The grammar has been extended to be as prescriptive as reasonably possible as befits a language specification[1], this means that former statements such as it being illegal to have a set accessor for a ref-valued property are now covered by the grammar. However the usual verbose description of the grammar remains!

The term ref-valued rather than ref-returning is used, a term closer to the language of fields. To distingush the two kinds of properties, which do follow different rules, the corresponding and somewhat ugly term non-ref-valued is used (non-ref-returning would be just as ugly).

[1] The grammar used by a compiler may be less restrictive to support more descriptive error messages, that is of course an implementation issue!

Fixes #825.
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.

3 participants