Skip to content

Commit

Permalink
parser: allow embedding interfaces from other modules (#13385)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzopalazzo committed Feb 6, 2022
1 parent 1dc2392 commit 10dcb2e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions vlib/v/parser/struct.v
Expand Up @@ -519,6 +519,32 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
}
continue
}

// Check embedded interface from external module
if p.tok.kind == .name && p.peek_tok.kind == .dot {
if p.tok.lit !in p.imports {
p.error_with_pos('mod `$p.tok.lit` not imported', p.tok.pos())
break
}
mod_name := p.tok.lit
from_mod_typ := p.parse_type()
from_mod_name := '${mod_name}.$p.prev_tok.lit'
if from_mod_name.is_lower() {
p.error_with_pos('The interface name need to have the pascal case', p.prev_tok.pos())
break
}
comments := p.eat_comments()
ifaces << ast.InterfaceEmbedding{
name: from_mod_name
typ: from_mod_typ
pos: p.prev_tok.pos()
comments: comments
}
if p.tok.kind == .rcbr {
break
}
}

if p.tok.kind == .key_mut {
if is_mut {
p.error_with_pos('redefinition of `mut` section', p.tok.pos())
Expand Down
15 changes: 15 additions & 0 deletions vlib/v/tests/modules/interface_from_another_module/main_test.v
@@ -0,0 +1,15 @@
module main

import interface_from_another_module.mod

interface IBar {
mod.IFoo
}

struct Abc {}

fn test_interface() {
a := IBar(Abc{})
dump(a)
assert true
}
3 changes: 3 additions & 0 deletions vlib/v/tests/modules/interface_from_another_module/mod/mod.v
@@ -0,0 +1,3 @@
module mod

pub interface IFoo {}

0 comments on commit 10dcb2e

Please sign in to comment.