Grammar rule *parameter_modifier* disallows valid V7 constructs
[I stumbled on this today while looking at how to spec the addition of scoped to a parameter for a future version.]
The relevant grammar for method parameters in §15.6.2.1 is, as follows:
fixed_parameter
: attributes? parameter_modifier? type identifier default_argument?
;
parameter_modifier
: parameter_mode_modifier
| 'this'
;
parameter_mode_modifier
: 'ref'
| 'out'
| 'in'
;
Rule parameter_modifier allows parameter_mode_modifier or this, but not both. However, two combinations of these are permitted, as follows:
public static class E
{
public static void F1b(this in int p) { }
public static void F1d(this ref int p) { }
}
This possibility is supported by the text in §15.6.10 Extension methods, which states:
When the first parameter of a method includes the
thismodifier, that method is said to be an extension method. Extension methods shall only be declared in non-generic, non-nested static classes. The first parameter of an extension method is restricted, as follows:
- It may only be an input parameter if it has a value type
- It may only be a reference parameter if it has a value type or has a generic type constrained to struct
- It shall not be a pointer type.
I propose the following grammar change:
parameter_modifier
: parameter_mode_modifier
| 'this' parameter_mode_modifier?
;
Now this new grammar also allows this out, which according to my compiler is not permitted [CS8328], and that suggests we also add to the extension bullet list above, the following constraint:
- It shall not be an output parameter.
Re my second suggestion, I just found the following in §15.6.2.1:
If the parameter is a
structtype or a type parameter constrained to astruct, thethismodifier may be combined with either thereforinmodifier, but not theoutmodifier. Extension methods are further described in §15.6.10.
That said, having the new bullet I proposed clarifies it at the parameter end of the call in the case of extension methods, which are not singled out by the above quote.
Can we rename
parameter_modifiertoparameter_modifiers? It doesn't feel likethis refis a single parameter modifier. This is purely a stylistic thing, and I'm happy to be overruled.
I'll vote against on this one – not because I think the current naming in this area is good, it isn’t really, but because making it plural won’t make it any better so stick with the status quo.
Note for the February meeting: this PR has not changed (as far as I can see) since November.
(Closure was accidental.)
closed/reopened to run all checks
@Nigel-Ecma I made no change re your suggestion in https://github.com/dotnet/csharpstandard/pull/1209#pullrequestreview-2446808223. Feel free to add a note in a separate PR.