Skip to content

Fix tuple assignment #1366

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

Draft
wants to merge 1 commit into
base: draft-v8
Choose a base branch
from

Conversation

BillWagner
Copy link
Member

Fixes #1155

This PR follows the process outlined in #1155 (comment)

This replaces the first commit in #1159.

See #1359 (review) for the status of this PR.

Fixes dotnet#1155

This PR follows the process outlined in dotnet#1155 (comment)
@Nigel-Ecma
Copy link
Contributor

Carried over from #1159:

My possible fix to #1155 doesn’t deal with the case of a tuple_expression not having a type (RIP null type), and one without a type is not representable directly as a ValueTuple<…> as there is no type for one or more of the type params…

A re-think is needed, till then I’ll mark this PR as draft

This PR is for that rethink…

@Nigel-Ecma Nigel-Ecma added this to the C# 8.0 milestone Jul 3, 2025
@jnm2
Copy link
Contributor

jnm2 commented Jul 3, 2025

@Nigel-Ecma If a tuple expression doesn't have a type, then I think we don't get to the part where ValueTuple<...> comes into play, because of this text:

The value of a tuple expression is the value obtained by evaluating the tuple expression (§12.8.6). It is an error to obtain the value of a tuple expression that does not have a type.

Thus a tuple expression is only evaluated when it has a type, and thus a tuple expression can be evaluated by constructing the corresponding ValueTuple<...> type.

@@ -1611,7 +1611,7 @@ A tuple expression has a type if and only if each of its element expressions `Ei

A tuple expression is evaluated by evaluating each of its element expressions in order from left to right.

A tuple value can be obtained from a tuple expression by converting it to a tuple type ([§10.2.13](conversions.md#10213-implicit-tuple-conversions)), by reclassifying it as a value ([§12.2.2](expressions.md#1222-values-of-expressions))) or by making it the target of a deconstructing assignment ([§12.21.2](expressions.md#12212-simple-assignment)).
A tuple value is obtained from a tuple expression by evaluating it and storing the result in corresponding `System.ValueTuple<...>` type, and initializing each of its fields in order from left to right by evaluating the corresponding tuple element expression of `E`, converting it to the corresponding element type of `T` using the implicit conversion found, and initializing the field with the result.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System.ValueTuple isn't mentioned anywhere else in this file. Shouldn't we be a bit more specific? There are wrinkles in how tuples map to the ValueTuple types. For instance, a tuple type (T1, T2, T3, T4, T5, T6, T7, T8) must be represented as nested ValueTuples, because the TRest type argument of a tuple must always itself be a nested tuple: ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that conversions.md does mention ValueTuple<...> a few times, but it doesn't explain the required recursive encoding.

@@ -33,12 +33,12 @@ An ***instance accessor*** is a property access on an instance, an event access

### 12.2.2 Values of expressions

Most of the constructs that involve an expression ultimately require the expression to denote a ***value***. In such cases, if the actual expression denotes a namespace, a type, a method group, or nothing, a compile-time error occurs. However, if the expression denotes a property access, an indexer access, or a variable, the value of the property, indexer, or variable is implicitly substituted:
Most of the constructs that involve an expression ultimately require the expression to denote a ***value***. In such cases, if the actual expression denotes a namespace, a type, a method group, or nothing, a compile-time error occurs. However, if the expression denotes a property access, an indexer access, a tuple, or a variable, the value of the property, indexer, tuple, or variable is implicitly substituted:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we mention tuple expressions here, but we don't mention other kinds of expressions such as object creation expressions, array creation expressions, lambda expressions, and so on?

@@ -345,7 +345,7 @@ In all cases, the rules ensure that a conversion is executed as a boxing convers

### 10.2.13 Implicit tuple conversions

An implicit conversion exists from a tuple expression `E` to a tuple type `T` if `E` has the same arity as `T` and an implicit conversion exists from each element in `E` to the corresponding element type in `T`. The conversion is performed by creating an instance of `T`’s corresponding `System.ValueTuple<...>` type, and initializing each of its fields in order from left to right by evaluating the corresponding tuple element expression of `E`, converting it to the corresponding element type of `T` using the implicit conversion found, and initializing the field with the result.
An implicit conversion exists from an expression `E` with a tuple type `S` to a tuple type `T` if `S` has the same arity as `T` and an implicit conversion exists from each element type in `S` to the corresponding element type in `T`.
Copy link
Contributor

@jnm2 jnm2 Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm following, but just checking. Is there a tuple type S for every tuple expression? For instance, does (default, default) have a tuple type S in:

(object, object) x = (default, default);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tuple conversion are incorrectly specified
3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy