1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: restrict numeric promotions to cases where no data is lost

This commit is contained in:
Uwe Krüger
2020-05-27 05:42:48 +02:00
committed by GitHub
parent fc67046bac
commit 013fdb8a4b
81 changed files with 510 additions and 247 deletions

View File

@@ -269,6 +269,8 @@ rune // represents a Unicode code point
f32 f64
any_int, any_float // internal intermediate types of number literals
byteptr // these two are mostly used for C interop
voidptr
@@ -277,6 +279,27 @@ any // similar to C's void* and Go's interface{}
Please note that unlike C and Go, `int` is always a 32 bit integer.
There is an exceptions to the rule that all operators
in V must have values of the same type on both sides. A small primitive type
on one side can be automatically promoted if it fits
completely into the data range of the type on the other side, i.e. when
the promotion does not result in any data loss.
These are the allowed possibilities:
```
i8 → i16 → int → i64
↘ ↘
f32 → f64
↗ ↗
byte → u16 → u32 → u64 ⬎
↘ ↘ ↘ ptr
i8 → i16 → int → i64 ⬏
```
An `int` value for example can be automatically promoted to `f64`
or `i64` but not to `f32` or `u32`. (`f32` would mean precission
loss for large values and `u32` would mean loss of the sign for
negative values).
## Strings
```v