checker: implement implicit conversions/promotions to `rune` (#6249)

pull/6262/head
Uwe Krüger 2020-08-29 01:59:07 +02:00 committed by GitHub
parent 7bd2804ce9
commit fe8286c53c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 28 deletions

View File

@ -217,8 +217,8 @@ fn (c &Checker) promote_num(left_type, right_type table.Type) table.Type {
}
} else if idx_lo >= table.byte_type_idx { // both operands are unsigned
return type_hi
} else if idx_lo >= table.i8_type_idx && idx_hi <= table.i64_type_idx { // both signed
return type_hi
} else if idx_lo >= table.i8_type_idx && (idx_hi <= table.i64_type_idx || idx_hi == table.rune_type_idx) { // both signed
return if idx_lo == table.i64_type_idx { type_lo } else { type_hi }
} else if idx_hi - idx_lo < (table.byte_type_idx - table.i8_type_idx) {
return type_lo // conversion unsigned -> signed if signed type is larger
} else {

View File

@ -255,12 +255,11 @@ pub const (
array_type_idx = 20
map_type_idx = 21
chan_type_idx = 22
any_type_idx = 23
// t_type_idx = 23
any_flt_type_idx = 24
any_int_type_idx = 25
sizet_type_idx = 26
rune_type_idx = 27
sizet_type_idx = 23
rune_type_idx = 24
any_type_idx = 25
any_flt_type_idx = 26
any_int_type_idx = 27
)
pub const (
@ -300,11 +299,10 @@ pub const (
array_type = new_type(array_type_idx)
map_type = new_type(map_type_idx)
chan_type = new_type(chan_type_idx)
rune_type = new_type(rune_type_idx)
any_type = new_type(any_type_idx)
// t_type = new_type(t_type_idx)
any_flt_type = new_type(any_flt_type_idx)
any_int_type = new_type(any_int_type_idx)
rune_type = new_type(rune_type_idx)
)
pub const (
@ -569,18 +567,24 @@ pub fn (mut t Table) register_builtin_type_symbols() {
source_name: 'chan'
mod: 'builtin'
})
t.register_type_symbol({
kind: .size_t
name: 'size_t'
source_name: 'size_t'
mod: 'builtin'
})
t.register_type_symbol({
kind: .rune
name: 'rune'
source_name: 'rune'
mod: 'builtin'
})
t.register_type_symbol({
kind: .any
name: 'any'
source_name: 'any'
mod: 'builtin'
})
// t.register_type_symbol({
// kind: .any
// name: 'T'
// mod: 'builtin'
// is_public: true
// })
t.register_type_symbol({
kind: .any_float
name: 'any_float'
@ -593,18 +597,6 @@ pub fn (mut t Table) register_builtin_type_symbols() {
source_name: 'any_int'
mod: 'builtin'
})
t.register_type_symbol({
kind: .size_t
name: 'size_t'
source_name: 'size_t'
mod: 'builtin'
})
t.register_type_symbol({
kind: .size_t
name: 'rune'
source_name: 'rune'
mod: 'builtin'
})
// TODO: remove. for v1 map compatibility
map_string_string_idx := t.find_or_register_map(string_type, string_type)
map_string_int_idx := t.find_or_register_map(string_type, int_type)

View File

@ -80,3 +80,21 @@ fn test_f32_int() {
d := u64(300)
assert d + c == 316.75
}
fn test_rune() {
a := 3
mut b := rune(67)
b = a
assert b == rune(3)
b = rune(67)
mut x := 5
x = int(b)
assert x == 67
c := b + a
assert c == rune(70)
assert typeof(c) == 'rune'
d := i64(12)
e := b + d
assert e == i64(79)
assert typeof(e) == 'i64'
}