semantics: decide on the rules of `as` i.e. casts
- Dominant language
- Python
- Stars
- 1
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
The language currently does not define any strict rules about `as` casts. This should be dedicated so that it can be:
- formally implemented in the IR and lowered
- supported by whichever backend (if it involves runtime conversions)
## Cast kinds
Some clear requirements from casts are:
- Numeric casts:
- integer type to another integer type cast (for example `8u8 as u64`)
- integer to float cast
- float to integer cast
- pointer casts
- what are the valid pointer casts?
- array to pointer?
- decide on how different pointer kinds interact with one another
- `Ref` to `&T`?
- function pointer casts?
We should also consider the following
- c-kind casts
- `enum`s to integer casts - so long as the enum is compatible with a `c`-like enum
- `bool` or `char` to integer type cast
- `u8` to `char cast
## Numeric cast semantics
We should define some clear semantics on numeric casting, what is valid, what isn't, if there is a runtime cost, etc.
- casting between an integer kind of the same size is a no-op
- downcasting between integers results in truncation
- upcasting integers will result:
- zero-extend if the source integer type is unsigned
- sign-extend if the source integer type is signed
- casting between a float to an integer will...?
- casting between an integer to a float will...?
- upcasting floats is a perfect and lossless operation.
- downcasting floats tries to get the nearest approximation of float value (how?)
- should we follow a formal specification?
## Enum cast
Cast from an `enum` value to its discriminant value, and then uses a numeric cast.
## `c`-kind casts
Boolean to integer cast adheres to the following rules (basically an `enum` cast):
- `false` casts to `0`
- `true` casts to `1`
`u8` to `char` cast converts the value into the corresponding `char` code point.
## pointer casts
An area for discussion!
What are the rules for pointer casts... can pointers be cast into an integral type, how do different reference types interact with one another when it comes to casting, etc?
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.