Skip to content

Commit

Permalink
feat[lang]: reject implements for empty interfaces
Browse files Browse the repository at this point in the history
`implements: <interface>` for an empty interface likely indicates a bug
in user code, reject it.
  • Loading branch information
charles-cooper committed Oct 21, 2024
1 parent b3ea663 commit 12da260
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/functional/syntax/modules/test_implements.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest

from vyper.compiler import compile_code
from vyper.exceptions import StructureException


def test_implements_from_vyi(make_input_bundle):
Expand Down Expand Up @@ -49,3 +52,21 @@ def foo(): # implementation
input_bundle = make_input_bundle({"some_interface.vyi": vyi, "lib1.vy": lib1, "lib2.vy": lib2})

assert compile_code(main, input_bundle=input_bundle) is not None


def test_implements_empty_vyi(make_input_bundle, tmp_path):
vyi = ""
input_bundle = make_input_bundle({"some_interface.vyi": vyi})
main = """
import some_interface
implements: some_interface
"""
with pytest.raises(StructureException) as e:
_ = compile_code(main, input_bundle=input_bundle)

vyi_path = (tmp_path / "some_interface.vyi").as_posix()
assert (
e.value._message
== f"Tried to implement `{vyi_path}`, but it has no functions to implement!"
)
4 changes: 4 additions & 0 deletions vyper/semantics/types/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def _ctor_modifiability_for_call(self, node: vy_ast.Call, modifiability: Modifia
def validate_implements(
self, node: vy_ast.ImplementsDecl, functions: dict[ContractFunctionT, vy_ast.VyperNode]
) -> None:
if len(self.functions) == 0:
msg = f"Tried to implement `{self}`, but it has no functions to implement!"
raise StructureException(msg, node)

# only external functions can implement interfaces
fns_by_name = {fn_t.name: fn_t for fn_t in functions.keys()}

Expand Down

0 comments on commit 12da260

Please sign in to comment.