checker: fix error for cast to alias of reference struct (#13278)

pull/13285/head
yuyi 2022-01-26 18:32:14 +08:00 committed by GitHub
parent 867056dafb
commit d71fc0d13f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -88,7 +88,7 @@ pub fn open(name string, level CompressionLevel, mode OpenMode) ?&Zip {
if name.len == 0 {
return error('szip: name of file empty')
}
p_zip := &Zip(C.zip_open(&char(name.str), int(level), char(mode.to_byte())))
p_zip := unsafe { &Zip(C.zip_open(&char(name.str), int(level), char(mode.to_byte()))) }
if isnil(p_zip) {
return error('szip: cannot open/create/append new zip archive')
}

View File

@ -2661,7 +2661,7 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
if !c.table.sumtype_has_variant(to_type, from_type, false) && !to_type.has_flag(.optional) {
c.error('cannot cast `$from_sym.name` to `$to_sym.name`', node.pos)
}
} else if mut to_sym.info is ast.Alias {
} else if mut to_sym.info is ast.Alias && !(final_to_sym.kind == .struct_ && to_type.is_ptr()) {
if !c.check_types(from_type, to_sym.info.parent_type) && !(final_to_sym.is_int()
&& final_from_sym.kind in [.enum_, .bool, .i8, .char]) {
c.error('cannot convert type `$from_sym.name` to `$to_sym.name` (alias to `$final_to_sym.name`)',

View File

@ -34,3 +34,20 @@ fn test_cast_to_alias() {
println(ret_str)
assert ret_str == '1'
}
struct Foo {
x int
y string
}
type Alias = Foo
fn test_cast_to_alias_of_ref_struct() {
foo := &Foo(0)
println(typeof(foo).name)
assert typeof(foo).name == '&Foo'
bar := &Alias(0)
println(typeof(bar).name)
assert typeof(bar).name == '&Alias'
}