rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Allow `const`/`static` destructuring

Open vimirage opened this issue 3 years ago • 6 comments

I'll primarily defer to existing discussions requesting the same, see the following at internals.rust-lang: allow destructuring patterns in const and static destructuring declarations for const

It simply seems that it hasn't been considered to allow this yet? If so, may it be introduced?

vimirage avatar Jul 06 '22 19:07 vimirage

I would like to voice a modest objection to doing this for static: People often imagine const and static are "basically the same thing". But they aren't. A static is not like a let binding, and it denotes a "place in memory" in a more absolute sense, compared to the more nebulous sense of let.

// The reason we allow destructuring is that this,
let (x, y) = (5, 5);
let z = x + y;

// has, as far as a programmer is concerned, the same semantics as this:
let xy = (5, 5);
let z = xy.0 + xy.1;

// But in the case of static...
static XY: (i32, i32) = (5,5);
static (X: i32, Y: i32) = (5, 5);
// ...these are very different, in a way that is *immediately* relevant.

I think allowing this is allowing too much programmer confusion. But it seems eminently permissible for const declarations, which are nebulously located like let is. However, if people believe that to allow it for one must be to allow it for either, because they would like const and static to remain similar, then it would be preferable to allow it for neither.

workingjubilee avatar Aug 05 '22 19:08 workingjubilee

// But in the case of static...
static XY: (i32, i32) = (5,5);
static (X: i32, Y: i32) = (5, 5);
// ...these are very different, in a way that is *immediately* relevant.

Do you intend to say that it's different because the memory locations are disconnected now? If so, this is odd, because the tuple memory representation isn't allowed to be relied upon by user code anyways?

Does anyone rely on such behaviour? I would see it permissable that X and Y alias the same memory location.

vimirage avatar Aug 05 '22 19:08 vimirage

Yes. And the tuple memory representation is under such constraints that programmers will rely on the fact that XY.0 and XY.1 are, at furthest, adjacent in memory. They could certainly not alias if they were different values, and people do rely on the equivalence between homogenous tuples and homogenous arrays of equivalent length, especially when they don't need the order to be perfectly preserved, for various historical reasons that I am disinclined to chatter extensively about. Replace the parentheses with [] if you prefer.

Destructuring, however, allows them to be in wildly different regions of memory, putting entire gigabytes of empty addressing between them.

workingjubilee avatar Aug 05 '22 19:08 workingjubilee

Destructuring, however, allows them to be in wildly different regions of memory, putting entire gigabytes of empty addressing between them.

Ah, this (although somewhat exaggerated) number definitely helps understand the issue. I agree that, that's worth keeping in mind. It's quite imaginable that this is a footgun.

vimirage avatar Aug 05 '22 20:08 vimirage

I would like to voice a modest objection to doing this for static: People often imagine const and static are "basically the same thing". But they aren't. A static is not like a let binding, and it denotes a "place in memory" in a more absolute sense, compared to the more nebulous sense of let.

// The reason we allow destructuring is that this,
let (x, y) = (5, 5);
let z = x + y;

// has, as far as a programmer is concerned, the same semantics as this:
let xy = (5, 5);
let z = xy.0 + xy.1;

// But in the case of static...
static XY: (i32, i32) = (5,5);
static (X: i32, Y: i32) = (5, 5);
// ...these are very different, in a way that is *immediately* relevant.

I think allowing this is allowing too much programmer confusion. But it seems eminently permissible for const declarations, which are nebulously located like let is. However, if people believe that to allow it for one must be to allow it for either, because they would like const and static to remain similar, then it would be preferable to allow it for neither.

do only for const then

igotfr avatar Feb 14 '23 03:02 igotfr

do only for const then

+1

schneiderfelipe avatar Mar 13 '24 22:03 schneiderfelipe