all: implement comptime if for generic type `$if T is string {` (#7901)
parent
f751271e4e
commit
9025c3528f
|
@ -4122,6 +4122,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
|
|||
mut nbranches_without_return := 0
|
||||
mut should_skip := false // Whether the current branch should be skipped
|
||||
mut found_branch := false // Whether a matching branch was found- skip the rest
|
||||
mut is_comptime_t_is_expr := false // if `$if T is string`
|
||||
for i in 0 .. node.branches.len {
|
||||
mut branch := node.branches[i]
|
||||
if branch.cond is ast.ParExpr {
|
||||
|
@ -4237,10 +4238,16 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
|
|||
// smartcast field type on comptime if
|
||||
if branch.cond is ast.InfixExpr {
|
||||
if branch.cond.op == .key_is {
|
||||
se := branch.cond.left
|
||||
if se is ast.SelectorExpr {
|
||||
left := branch.cond.left
|
||||
got_type := (branch.cond.right as ast.Type).typ
|
||||
c.comptime_fields_type[se.expr.str()] = got_type
|
||||
if left is ast.SelectorExpr {
|
||||
c.comptime_fields_type[left.expr.str()] = got_type
|
||||
} else if left is ast.Type {
|
||||
is_comptime_t_is_expr = true
|
||||
left_type := c.unwrap_generic(left.typ)
|
||||
if left_type != got_type {
|
||||
should_skip = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4255,7 +4262,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
|
|||
}
|
||||
if !c.skip_flags || c.pref.output_cross_c {
|
||||
c.stmts(branch.stmts)
|
||||
} else {
|
||||
} else if !is_comptime_t_is_expr {
|
||||
node.branches[i].stmts = []
|
||||
}
|
||||
c.skip_flags = cur_skip_flags
|
||||
|
@ -4386,7 +4393,9 @@ fn (mut c Checker) comp_if_branch(cond ast.Expr, pos token.Position) bool {
|
|||
return l && r // skip (return true) only if both should be skipped
|
||||
}
|
||||
.key_is, .not_is {
|
||||
if cond.left is ast.SelectorExpr && cond.right is ast.Type {
|
||||
if (cond.left is ast.SelectorExpr ||
|
||||
cond.left is ast.Type) &&
|
||||
cond.right is ast.Type {
|
||||
// $if method.@type is string
|
||||
} else {
|
||||
c.error('invalid `\$if` condition: $cond.left', cond.pos)
|
||||
|
|
|
@ -231,10 +231,18 @@ fn (mut g Gen) comp_if_expr(cond ast.Expr) {
|
|||
g.comp_if_expr(cond.right)
|
||||
}
|
||||
.key_is, .not_is {
|
||||
se := cond.left as ast.SelectorExpr
|
||||
name := '${se.expr}.$se.field_name'
|
||||
exp_type := g.comptime_var_type_map[name]
|
||||
left := cond.left
|
||||
mut name := ''
|
||||
mut exp_type := table.Type(0)
|
||||
got_type := (cond.right as ast.Type).typ
|
||||
if left is ast.SelectorExpr {
|
||||
name = '${left.expr}.$left.field_name'
|
||||
exp_type = g.comptime_var_type_map[name]
|
||||
} else if left is ast.Type {
|
||||
name = left.str()
|
||||
// this is only allowed for generics currently, otherwise blocked by checker
|
||||
exp_type = g.unwrap_generic(left.typ)
|
||||
}
|
||||
g.write('$exp_type == $got_type')
|
||||
}
|
||||
.eq, .ne {
|
||||
|
|
|
@ -31,6 +31,10 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
|
|||
node = p.sql_expr()
|
||||
p.inside_match = false
|
||||
} else {
|
||||
if p.inside_if && p.tok.lit == 'T' {
|
||||
// $if T is string {}
|
||||
p.expecting_type = true
|
||||
}
|
||||
node = p.name_expr()
|
||||
p.is_stmt_ident = is_stmt_ident
|
||||
}
|
||||
|
|
|
@ -28,3 +28,40 @@ fn test_ct_expressions() {
|
|||
assert true
|
||||
}
|
||||
}
|
||||
|
||||
fn generic_t_is<T>() T {
|
||||
$if T is string {
|
||||
return 'It\'s a string!'
|
||||
} $else {
|
||||
return T{}
|
||||
}
|
||||
return T{}
|
||||
}
|
||||
|
||||
struct GenericTIsTest {}
|
||||
|
||||
fn test_generic_t_is() {
|
||||
assert generic_t_is<string>() == 'It\'s a string!'
|
||||
assert generic_t_is<GenericTIsTest>() == GenericTIsTest{}
|
||||
}
|
||||
|
||||
fn generic_t_is2<T>() ?T {
|
||||
$if T is string {
|
||||
return 'It\'s a string!'
|
||||
} $else {
|
||||
return T{}
|
||||
}
|
||||
}
|
||||
|
||||
fn test_generic_t_is2() {
|
||||
res := generic_t_is2<string>() or {
|
||||
assert false
|
||||
''
|
||||
}
|
||||
res2 := generic_t_is2<GenericTIsTest>() or {
|
||||
assert false
|
||||
GenericTIsTest{}
|
||||
}
|
||||
assert res == 'It\'s a string!'
|
||||
assert res2 == GenericTIsTest{}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue