checker: fix comptime if T is interface (#12365)

pull/12358/head
yuyi 2021-11-02 15:35:01 +08:00 committed by GitHub
parent 4ed6fb0e9b
commit 0952af606c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -6678,6 +6678,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
is_comptime_type_is_expr = true
} else if branch.cond.right is ast.TypeNode && left is ast.TypeNode
&& sym.kind == .interface_ {
is_comptime_type_is_expr = true
// is interface
checked_type := c.unwrap_generic(left.typ)
should_skip = !c.table.does_type_implement_interface(checked_type,

View File

@ -0,0 +1,29 @@
interface Something {
i int
}
struct Some {
i int
}
struct App {
mut:
count u8
}
fn (mut self App) next<T>(input T) string {
$if T is Something {
return 'Something'
} $else $if T is f64 {
return 'f64'
} $else {
panic('${typeof(T.typ).name} is not supported')
}
panic('Unreachable')
}
fn test_comptime_if_is_interface() {
mut app := App{}
assert app.next(Something(Some{1})) == 'Something'
assert app.next(1.0) == 'f64'
}