Skip to content

Commit

Permalink
checker: check error of implementing other module private interface (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Oct 29, 2023
1 parent 0148914 commit 7681a0b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
6 changes: 3 additions & 3 deletions vlib/crypto/cipher/cipher.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module cipher
// using a given key. It provides the capability to encrypt
// or decrypt individual blocks. The mode implementations
// extend that capability to streams of blocks.
interface Block {
pub interface Block {
block_size int // block_size returns the cipher's block size.
encrypt(mut dst []u8, src []u8) // Encrypt encrypts the first block in src into dst.
// Dst and src must overlap entirely or not at all.
Expand All @@ -15,7 +15,7 @@ interface Block {
}

// A Stream represents a stream cipher.
interface Stream {
pub interface Stream {
// xor_key_stream XORs each byte in the given slice with a byte from the
// cipher's key stream. Dst and src must overlap entirely or not at all.
//
Expand All @@ -31,7 +31,7 @@ interface Stream {

// A BlockMode represents a block cipher running in a block-based mode (CBC,
// ECB etc).
interface BlockMode {
pub interface BlockMode {
block_size int // block_size returns the mode's block size.
crypt_blocks(mut dst []u8, src []u8) // crypt_blocks encrypts or decrypts a number of blocks. The length of
// src must be a multiple of the block size. Dst and src must overlap
Expand Down
2 changes: 1 addition & 1 deletion vlib/net/http/server.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum ServerStatus {
stopped
}

interface Handler {
pub interface Handler {
mut:
handle(Request) Response
}
Expand Down
7 changes: 6 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -958,8 +958,14 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
eprintln('> type_implements typ: ${typ.debug()} (`${c.table.type_to_str(typ)}`) | inter_typ: ${interface_type.debug()} (`${c.table.type_to_str(interface_type)}`)')
}
utyp := c.unwrap_generic(typ)
styp := c.table.type_to_str(utyp)
typ_sym := c.table.sym(utyp)
mut inter_sym := c.table.sym(interface_type)
if inter_sym.mod !in [typ_sym.mod, c.mod] && !inter_sym.is_pub && typ_sym.mod != 'builtin' {
c.error('`${styp}` cannot implement private interface `${inter_sym.name}` of other module',
pos)
return false
}

// small hack for JS.Any type. Since `any` in regular V is getting deprecated we have our own JS.Any type for JS backend.
if typ_sym.name == 'JS.Any' {
Expand Down Expand Up @@ -1006,7 +1012,6 @@ fn (mut c Checker) type_implements(typ ast.Type, interface_type ast.Type, pos to
// `none` "implements" the Error interface
return true
}
styp := c.table.type_to_str(utyp)
if typ_sym.kind == .interface_ && inter_sym.kind == .interface_ && !styp.starts_with('JS.')
&& !inter_sym.name.starts_with('JS.') {
c.error('cannot implement interface `${inter_sym.name}` with a different interface `${styp}`',
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/modules/implement_private_interface.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/modules/implement_private_interface/main.v:7:16: error: `Bar` cannot implement private interface `baz.Foo` of other module
5 | fn main() {
6 | b := Bar{10}
7 | baz.print_foo(b) // prints 10
| ^
8 | }
9 |
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/modules/implement_private_interface/baz.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// sub module
module baz

interface Foo {
a int
}

pub fn print_foo(f Foo) {
println(f.a)
}
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/modules/implement_private_interface/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module main

import baz

fn main() {
b := Bar{10}
baz.print_foo(b) // prints 10
}

struct Bar {
a int
}
2 changes: 1 addition & 1 deletion vlib/v/embed_file/decoder.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[has_globals]
module embed_file

interface Decoder {
pub interface Decoder {
decompress([]u8) ![]u8
}

Expand Down

0 comments on commit 7681a0b

Please sign in to comment.