# Should we make \`nested\_tuple.0.0\` legal syntax?

**URL:** https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804
**Category:** language design
**Created:** [August 17, 2019, 3:18am UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804 "2019-08-17T03:18:02Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [August 17, 2019, 3:18am UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/1 "2019-08-17T03:18:02Z")

</div>

Currently `nested_tuple.0.0` is not legal and gives the following error

```rust
error: unexpected token: `0.0`
 --> src/lib.rs:2:19
  |
2 | let x = nested_tuple.0.0;
  | ------^^^
  | | |
  | | unexpected token
  | help: try parenthesizing the first index: `(tuple.0).0`

error: aborting due to previous error

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

```

Could we change this behavior to parse it as `(nested_tuple.0).0` instead?

I assume that this could only be done if we are fine with not parsing `0.` in a future edition, see this thread for discussion about that

> [@Idea: In the next edition, stop accepting \`0.\` as a valid float literal](https://internals.rust-lang.org/t/idea-in-the-next-edition-stop-accepting-0-as-a-valid-float-literal/10790/):
>
> On reading this post just now, it occurred to me this is exactly the kind of breaking change editions are designed for: purely surface syntax, zero semantic implications, 0. -\> 0.0 seems almost trivial to lint and rustfix, etc. What does everyone else think? Have I missed some glaring reason why it's not so trivial? Or is the potential gain too small to seriously consider this at all?

* * *

This idea was spawned by @jethrogb in the thread about not parsing `0.` float literals

> [@Idea: In the next edition, stop accepting \`0.\` as a valid float literal](https://internals.rust-lang.org/t/idea-in-the-next-edition-stop-accepting-0-as-a-valid-float-literal/10790/6):
>
> Except in the most annoying case of tuple\_tuple\_struct.0.0 which still won't be legal.

---

<div class="post-metadata">

### Author: ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)
#### Post date: [August 17, 2019, 1:49pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/2 "2019-08-17T13:49:50Z")

</div>

I think use of the `.0` syntax should be discouraged in general, so I don't feel a need to improve it. Personally, I try to avoid even single-level anonymous fields, because they make the code hard to follow:

```rust
x.0 = y.1 + 2;

```

I try to use destructuring instead, which allows naming the fields:

```rust
let (width, height) = tuple;

```

and that's clearer than:

```rust
tuple.0, tuple.1

```

So in this case I'd use:

```rust
let ((x, y), ..) = named_tuple;

```

---

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [August 17, 2019, 8:52pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/4 "2019-08-17T20:52:18Z")

</div>

This might be easier as a poll

Should we allow yhis syntax?

_Poll ([view on site](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/4))_

---

<div class="post-metadata">

### Author: ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)
#### Post date: [August 17, 2019, 9:29pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/5 "2019-08-17T21:29:08Z")

</div>

Just to be paranoid: I voted on the assumption that "[only after we rework number literal type inference](https://internals.rust-lang.org/t/idea-in-the-next-edition-stop-accepting-0-as-a-valid-float-literal/10790/11)" counts as a "yes" for your poll.

---

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [August 17, 2019, 10:43pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/6 "2019-08-17T22:43:01Z")

</div>

This doesn't have to do with number type inference, so that doesn't matter. However, I think we should do this after we sort out if we are going to make `0.`-like float literals a hard error in future editions. But even if we don't make it a hard error, I still think we should do this. This is because floating point fields identifiers doesn't make sense because floating point weirdness (like `NaN` and `-0.0 != 0.0`), so this is the only reasonable interpretation of the syntax.

---

<div class="post-metadata">

### Author: ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)
#### Post date: [August 18, 2019, 6:43am UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/7 "2019-08-18T06:43:15Z")

</div>

I don’t see how this has anything to do with whether `0.` is legal.

---

<div class="post-metadata">

### Author: ![Finn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/finn/32/3841_2.png) [@Finn](https://internals.rust-lang.org/u/Finn)
#### Post date: [August 18, 2019, 3:47pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/8 "2019-08-18T15:47:51Z")

</div>

As `s.x.x` is legal syntax, `t.0.0` should be legal syntax for reasons of orthogonality?

---

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [August 18, 2019, 3:58pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/9 "2019-08-18T15:58:24Z")

</div>

Yes, that's the idea

---

<div class="post-metadata">

### Author: ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)
#### Post date: [August 18, 2019, 4:12pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/10 "2019-08-18T16:12:31Z")

</div>

Just to add a bit of technical information:

The lexer sees `name.0.0` and spits out `[(Ident "name") (Punct ".") (Lit "0.0")]`. In order to make this a valid parse, "all" that has to happen is the parser has to learn to accept a float literal as a key for a place (like it does for integer literals already) as a nested tuple access. Alternatively, it can learn to split apart the float literal token in this specific case.

How does `name.0.name` lex and parse today? Naively, I'd expect the `0.` to be lexed as a float currently. (On mobile, can't check.)

---

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [August 18, 2019, 4:24pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/11 "2019-08-18T16:24:29Z")

</div>

`a.0.b` parses without issue, and gives the expected result of accessing field `0` of `a` and then field `b` of `a.0`

---

<div class="post-metadata">

### Author: ![matklad](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/matklad/32/12266_2.png) [@matklad](https://internals.rust-lang.org/u/matklad)
#### Post date: [August 18, 2019, 6:28pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/12 "2019-08-18T18:28:07Z")

</div>

> [@CAD97](#):
>
> How does `name.0.name` lex and parse today? Naively, I'd expect the `0.` to be lexed as a float currently. (On mobile, can't check.)

Special cased in the lexer: [https://github.com/rust-lang/rust/blob/ea52be482ab4945fda63cb65b6a198309a041e3c/src/librustc\_lexer/src/lib.rs#L453-L457](https://github.com/rust-lang/rust/blob/ea52be482ab4945fda63cb65b6a198309a041e3c/src/librustc_lexer/src/lib.rs#L453-L457)

---

<div class="post-metadata">

### Author: ![comex](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/comex/32/2587_2.png) [@comex](https://internals.rust-lang.org/u/comex)
#### Post date: [August 19, 2019, 4:21am UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/13 "2019-08-19T04:21:57Z")

</div>

As long as you don't allow `name.0.1` to be written as `name.1e-1` 😉

---

<div class="post-metadata">

### Author: ![ckaran](https://avatars.discourse-cdn.com/v4/letter/c/f475e1/32.png) [@ckaran](https://internals.rust-lang.org/u/ckaran)
#### Post date: [August 19, 2019, 1:29pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/14 "2019-08-19T13:29:24Z")

</div>

Yes please, make this happen. It's a small, annoying ergonomic bug that I stub my toe on fairly regularly. I agree with @kornel that destructuring is generally a better idea, but there are times when I'm writing masses of tests and just using the number notation is easier. In some cases (niche ones) it is also easier, like when you've got a fixed-size vector/matrix and are treating the numbers as indexes.

---

<div class="post-metadata">

### Author: ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)
#### Post date: [August 19, 2019, 3:37pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/15 "2019-08-19T15:37:29Z")

</div>

Given the overwhelming majority of people wanting this feature as per the poll above, I will see what I can do to make this happen!

---

<div class="post-metadata">

### Author: ![tspiteri](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tspiteri/32/7283_2.png) [@tspiteri](https://internals.rust-lang.org/u/tspiteri)
#### Post date: [August 19, 2019, 10:03pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/16 "2019-08-19T22:03:30Z")

</div>

I prefer your syntax too, but sometimes the compiler generates worse code; I don't have an example at hand but I remember that many times I used to look at the assembly and see that setting `let x = y;` and then using `x` would generate copying instructions even if `y` is not used any more.

---

<div class="post-metadata">

### Author: ![carlomilanesi](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/carlomilanesi/32/3998_2.png) [@carlomilanesi](https://internals.rust-lang.org/u/carlomilanesi)
#### Post date: [August 21, 2019, 9:08pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/17 "2019-08-21T21:08:00Z")

</div>

Rustfmt transforms this code:

```
let a = (((1, 2), 3), 4);
let _ = a . 0 . 0 . 0;

```

to this:

```
let a = (((1, 2), 3), 4);
let _ = a.0 .0 .0;

```

I don't find it particularly annoying.

---

<div class="post-metadata">

### Author: ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)
#### Post date: [August 24, 2019, 11:42pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/18 "2019-08-24T23:42:11Z")

</div>

Although I agree that destructuring is often more elegant, I see no good reason to forbid writing `name.0.0`. It's an inconsistency that adds friction when writing code. I was quite surprised when I found out that it doesn't work.

---

<div class="post-metadata">

### Author: ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)
#### Post date: [November 22, 2019, 11:44pm UTC](https://internals.rust-lang.org/t/should-we-make-nested-tuple-0-0-legal-syntax/10804/19 "2019-11-22T23:44:07Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
