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

checker/cgen: small generic fixes (mut arg return)

This commit is contained in:
joe-conigliaro 2020-06-06 12:24:27 +10:00
parent de76ac583f
commit 31d03bb113
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
3 changed files with 8 additions and 15 deletions

View File

@ -1226,7 +1226,7 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
if return_stmt.exprs.len == 0 {
return
}
expected_type := c.expected_type
expected_type := c.unwrap_generic(c.expected_type)
expected_type_sym := c.table.get_type_symbol(expected_type)
exp_is_optional := expected_type.has_flag(.optional)
mut expected_types := [expected_type]
@ -1257,23 +1257,16 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
return
}
for i, exp_type in expected_types {
got_typ := got_types[i]
got_typ := c.unwrap_generic(got_types[i])
if got_typ.has_flag(.optional) &&
(!exp_type.has_flag(.optional) || c.table.type_to_str(got_typ) != c.table.type_to_str(exp_type)) {
pos := return_stmt.exprs[i].position()
c.error('cannot use `${c.table.type_to_str(got_typ)}` as type `${c.table.type_to_str(exp_type)}` in return argument', pos)
}
is_generic := exp_type == table.t_type
ok := if is_generic { c.check_types(got_typ, c.cur_generic_type) || got_typ == exp_type } else { c.check_types(got_typ,
exp_type) }
// ok := c.check_types(got_typ, exp_type)
if !ok { // !c.table.check(got_typ, exp_typ) {
if !c.check_types(got_typ, exp_type) {
got_typ_sym := c.table.get_type_symbol(got_typ)
mut exp_typ_sym := c.table.get_type_symbol(exp_type)
pos := return_stmt.exprs[i].position()
if is_generic {
exp_typ_sym = c.table.get_type_symbol(c.cur_generic_type)
}
if exp_typ_sym.kind == .interface_ {
c.type_implements(got_typ, exp_type, return_stmt.pos)
continue
@ -1747,7 +1740,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) {
c.expected_type = table.void_type
}
pub fn (mut c Checker) unwrap_generic(typ table.Type) table.Type {
pub fn (c &Checker) unwrap_generic(typ table.Type) table.Type {
if typ.idx() == table.t_type_idx {
// return c.cur_generic_type
// its more efficient to set the id rather than to copy flags/nr_muls

View File

@ -328,7 +328,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
}
}
pub fn (mut g Gen) unwrap_generic(typ table.Type) table.Type {
pub fn (g &Gen) unwrap_generic(typ table.Type) table.Type {
if typ.idx() == table.t_type_idx {
// return g.cur_generic_type
// its more efficient to set the id rather than to copy flags/nr_muls

View File

@ -68,18 +68,18 @@ fn mut_arg<T>(mut x T) {
println(x.foo) // = 'foo'
}
/*
fn mut_arg2<T>(mut x T) T {
println(x.foo) // = 'foo'
return x
}
*/
fn test_create() {
create<User>()
create<City>()
u := User{}
mut_arg<User>(mut u)
// mut_arg2<User>(mut u)
mut_arg2<User>(mut u)
}
/*