-
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
C# 7.3 ArrayTypeMismatchException in ref initialization/assignment #838
Comments
Other target-typed stuff: C# 7.1 |
When the readonlyness does not match: class C {
int i1 = 1;
readonly int i2 = 2;
ref int R(bool flag) => ref flag ? ref i1 : ref i2;
} .NET SDK 6.0.410 reports error CS8160 and error CS8156 at the expression |
Thanks for catching this. I suspect we'll need to discuss this in the meeting - I'm sufficiently confused by it as to not be able to propose anything else. |
Tagging @jaredpar I can see a few ways to resolve this:
The simplest fix would be (3). Although, I would hope we can define the behavior better. (Edited based on the following comment) |
What do you mean with "arrays of reference variables to reference types"? As a reference variable to a reference type is like string s = "huh";
ref string r = ref s; // r is a reference variable How would you even declare an array of those? |
Clause §13.6.2 (at least) is suffering badly from features, e.g. Trying to fix this array type mismatch without first addressing the existing structural issues is not the way to go here. (I’ll look into suitable changes but won’t assign this to myself at this time.) |
Meeting 2023-09-06: we recognize that this needs fixing, but don't want to actually make the change before the C# 7 spec is cut - it's the sort of change that we want to be done early in a release cycle. Assigning to Nigel to look at this, but also changing the milestone C# 8. |
Describe the bug
In C# 7.3,
ref
initialization (§13.6.2) or assignment (§12.21.3) should do a run-time check for array covariance and possibly throw System.ArrayTypeMismatchException, but the standard omits this check. The behaviour should depend onref
vs.ref readonly
, and this distinction should propagate through the conditional operator (§12.18).Example
SharpLab
Expected behavior
Not sure how you want to model this. Currently, the C# 7.x draft requires a run-time check for array elements even when the variable reference comes from a reference variable (or ref-returning method, property, indexer, or local function) and the run-time type has thus been checked already (unless it came from non-C# code or a C# 11 unsafe pointer cast dotnet/csharplang#6476). The run-time check would be inefficient to implement and would always pass, and Roslyn omits the check. However, standardizing how it actually works could make at least §12.18 (Conditional operator) more complex.
§13.6.2 (Local variable declarations) should specify that, when a
ref
(but notref readonly
) variable is initialized with an array element of reference type, then a run-time check is done andArrayTypeMismatchException
can be thrown.§12.21.3 (Ref assignment) should specify that, when a
ref
(but notref readonly
) variable is assigned, and the right operand is an array element of reference type, then a run-time check is done andArrayTypeMismatchException
can be thrown.§12.18 (Conditional operator) should perhaps specify that, if the expression is used in a context that requires a mutable reference (
ref
, rather thanref readonly
orin
), and thex
ory
operand selected by the condition value is an array element of reference type, then a run-time check is done andArrayTypeMismatchException
can be thrown. Alternatively, this could specify that the element type is not checked here but the value of the expression is "a variable reference with covariance risk", for which a run-time check would then be required in §13.6.2, §12.21.2, §12.21.3, and §12.6.2.3.§12.6.2.3 (Run-time evaluation of argument lists) and §12.21.2 (Simple assignment) should perhaps specify that the run-time check is done only when the expression is syntactically an array element, rather than a reference that can be assumed to have been checked already.
No change to §17.6 (Array covariance).
§21.5 (Common exception classes) should mention references and preferably have a note referencing §12.6.2.3, §12.18, §12.21.3, and §13.6.2 for the details.
Additional context
Noted in #837 (comment).
The text was updated successfully, but these errors were encountered: