checker: check optional interface type mismatch (#10617)

pull/10619/head
yuyi 2021-06-30 13:54:28 +08:00 committed by GitHub
parent d2f19ac494
commit 1e896c7020
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 1 deletions

View File

@ -304,6 +304,15 @@ pub fn (mut c Checker) check_types(got ast.Type, expected ast.Type) bool {
if expected == ast.charptr_type && got == ast.char_type.to_ptr() {
return true
}
if expected.has_flag(.optional) {
sym := c.table.get_type_symbol(got)
if (sym.kind == .interface_ && sym.name == 'IError')
|| got in [ast.none_type, ast.error_type] {
return true
} else if !c.check_basic(got, expected.clear_flag(.optional)) {
return false
}
}
if !c.check_basic(got, expected) { // TODO: this should go away...
return false
}

View File

@ -6170,7 +6170,6 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
node.is_expr = true
node.typ = c.expected_type
}
continue
}
if c.expected_type.has_flag(.generic) {
if node.typ == ast.void_type {

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/optional_interface_mismatch.vv:11:9: error: mismatched types `?MObject` and `string`
9 |
10 | fn give_string(line string) ?MObject {
11 | return if true { 'string' } else { 'string' }
| ~~
12 | }

View File

@ -0,0 +1,12 @@
fn main() {
le_string := give_string('string') or { return }
le_string.unimplemented()
}
interface MObject {
unimplemented() string
}
fn give_string(line string) ?MObject {
return if true { 'string' } else { 'string' }
}