Skip to content

semantics: decide on the rules of as i.e. casts #15

@feds01

Description

@feds01

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<T> to &T?
      • function pointer casts?

We should also consider the following

  • c-kind casts
    • enums 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?

Metadata

Metadata

Labels

languageLanguage specification related issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions