Odin icon indicating copy to clipboard operation
Odin copied to clipboard

Expressions with transmute should be constant when the thing they are transmuting is also constant.

Open Sanian-Creations opened this issue 3 years ago • 1 comments

Suggestion

Expressions with transmute should be constant when the thing they are transmuting is also constant.

Context

Values that are in theory constant, cannot be assigned to constants if they use transmute in their definition.

For example, this snippet from the Overview page which explains how transmute works:

f := f32(123)
u := transmute(u32)f

All values involved are known at compile time and do not call to any procedures. So, making both constant should work in theory.

f :: f32(123)
u :: transmute(u32)f

We can't however, because 'transmute(u32)f' is not a constant.

u should by all means be constant, because f is constant, and transmute by definition does not change the bits of f, it only assigns them a different type (quote: "The transmute operator is a bit cast conversion between two types of the same size").

Another example it should work for, is to assign hex values to signed integers, even if those hex values write to the sign bit

// store 0x80000000 as a constant i32
x80000000 ::                i32(0x80000000);  // X  - Cannot cast '0x80000000' as 'i32' from 'untyped integer'
x80000000 ::      cast(i32) u32(0x80000000);  // X  - Cannot cast 'u32(0x80000000)' as 'i32' from 'u32'
x80000000 :: transmute(i32) u32(0x80000000);  // X  - 'transmute(i32)u32(0x80000000)' is not a constant
x80000000 ::              ~ i32(0x7FFFFFFF);  // OK - Forced to write this, completely changing the hex,
x80000000 ::          ~i32(~u32(0x80000000)); // OK - or this, both making the expression more convoluted and hard to read,
                                              // all just to make sure the sign bit isn't 1 when casting

Sanian-Creations avatar Oct 19 '22 15:10 Sanian-Creations

We now support this for integers but not for floats yet.

gingerBill avatar Nov 01 '22 00:11 gingerBill