checker: implement implicit conversions/promotions to `rune` (#6249)
parent
7bd2804ce9
commit
fe8286c53c
|
@ -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
|
} else if idx_lo >= table.byte_type_idx { // both operands are unsigned
|
||||||
return type_hi
|
return type_hi
|
||||||
} else if idx_lo >= table.i8_type_idx && idx_hi <= table.i64_type_idx { // both signed
|
} else if idx_lo >= table.i8_type_idx && (idx_hi <= table.i64_type_idx || idx_hi == table.rune_type_idx) { // both signed
|
||||||
return type_hi
|
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) {
|
} 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
|
return type_lo // conversion unsigned -> signed if signed type is larger
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -255,12 +255,11 @@ pub const (
|
||||||
array_type_idx = 20
|
array_type_idx = 20
|
||||||
map_type_idx = 21
|
map_type_idx = 21
|
||||||
chan_type_idx = 22
|
chan_type_idx = 22
|
||||||
any_type_idx = 23
|
sizet_type_idx = 23
|
||||||
// t_type_idx = 23
|
rune_type_idx = 24
|
||||||
any_flt_type_idx = 24
|
any_type_idx = 25
|
||||||
any_int_type_idx = 25
|
any_flt_type_idx = 26
|
||||||
sizet_type_idx = 26
|
any_int_type_idx = 27
|
||||||
rune_type_idx = 27
|
|
||||||
)
|
)
|
||||||
|
|
||||||
pub const (
|
pub const (
|
||||||
|
@ -300,11 +299,10 @@ pub const (
|
||||||
array_type = new_type(array_type_idx)
|
array_type = new_type(array_type_idx)
|
||||||
map_type = new_type(map_type_idx)
|
map_type = new_type(map_type_idx)
|
||||||
chan_type = new_type(chan_type_idx)
|
chan_type = new_type(chan_type_idx)
|
||||||
|
rune_type = new_type(rune_type_idx)
|
||||||
any_type = new_type(any_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_flt_type = new_type(any_flt_type_idx)
|
||||||
any_int_type = new_type(any_int_type_idx)
|
any_int_type = new_type(any_int_type_idx)
|
||||||
rune_type = new_type(rune_type_idx)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
pub const (
|
pub const (
|
||||||
|
@ -569,18 +567,24 @@ pub fn (mut t Table) register_builtin_type_symbols() {
|
||||||
source_name: 'chan'
|
source_name: 'chan'
|
||||||
mod: 'builtin'
|
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({
|
t.register_type_symbol({
|
||||||
kind: .any
|
kind: .any
|
||||||
name: 'any'
|
name: 'any'
|
||||||
source_name: 'any'
|
source_name: 'any'
|
||||||
mod: 'builtin'
|
mod: 'builtin'
|
||||||
})
|
})
|
||||||
// t.register_type_symbol({
|
|
||||||
// kind: .any
|
|
||||||
// name: 'T'
|
|
||||||
// mod: 'builtin'
|
|
||||||
// is_public: true
|
|
||||||
// })
|
|
||||||
t.register_type_symbol({
|
t.register_type_symbol({
|
||||||
kind: .any_float
|
kind: .any_float
|
||||||
name: 'any_float'
|
name: 'any_float'
|
||||||
|
@ -593,18 +597,6 @@ pub fn (mut t Table) register_builtin_type_symbols() {
|
||||||
source_name: 'any_int'
|
source_name: 'any_int'
|
||||||
mod: 'builtin'
|
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
|
// TODO: remove. for v1 map compatibility
|
||||||
map_string_string_idx := t.find_or_register_map(string_type, string_type)
|
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)
|
map_string_int_idx := t.find_or_register_map(string_type, int_type)
|
||||||
|
|
|
@ -80,3 +80,21 @@ fn test_f32_int() {
|
||||||
d := u64(300)
|
d := u64(300)
|
||||||
assert d + c == 316.75
|
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'
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue