Skip to content

Commit

Permalink
checker: fix option interface member checking when none is passed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed May 20, 2024
1 parent cfd23f9 commit 404f93e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 2 additions & 1 deletion vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,8 @@ pub fn (t Table) does_type_implement_interface(typ Type, inter_typ Type) bool {
}
return false
}
if typ != voidptr_type && typ != nil_type && !inter_sym.info.types.contains(typ) {
if typ != voidptr_type && typ != nil_type && typ != none_type
&& !inter_sym.info.types.contains(typ) {
inter_sym.info.types << typ
}
if !inter_sym.info.types.contains(voidptr_type) {
Expand Down
6 changes: 4 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,8 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
inter_sym.methods
}
// voidptr is an escape hatch, it should be allowed to be passed
if utyp != ast.voidptr_type && utyp != ast.nil_type {
if utyp != ast.voidptr_type && utyp != ast.nil_type && !(interface_type.has_flag(.option)
&& utyp == ast.none_type) {
mut are_methods_implemented := true

// Verify methods
Expand Down Expand Up @@ -1126,7 +1127,8 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
}
}
}
if utyp != ast.voidptr_type && utyp != ast.nil_type && !inter_sym.info.types.contains(utyp) {
if utyp != ast.voidptr_type && utyp != ast.nil_type && utyp != ast.none_type
&& !inter_sym.info.types.contains(utyp) {
inter_sym.info.types << utyp
}
if !inter_sym.info.types.contains(ast.voidptr_type) {
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/tests/option_interface_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interface TestIntf {
test()
}

struct TestStruct {
ti ?TestIntf
}

fn TestStruct.new() TestStruct {
return TestStruct{
ti: none
}
}

fn test_main() {
t := TestStruct.new()
assert t.ti == none
}

0 comments on commit 404f93e

Please sign in to comment.