Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve exception for invalid use of dynamically sized struct #2189

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/parser/exceptions/test_argument_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ def foo():
for i in range(1, 2, 3, 4):
pass
""",
"""
struct Foo:
a: Bytes[32]

@external
def foo(a: Foo):
pass
""",
"""
struct Foo:
a: String[32]

@external
def foo(a: Foo):
pass
""",
"""
struct Foo:
b: uint256
a: String[32]

@external
def foo(a: Foo):
pass
""",
]


Expand Down
2 changes: 2 additions & 0 deletions vyper/context/types/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class BaseTypeDefinition:
If `True`, the value of this object cannot be modified after assignment.
"""

is_dynamic_size = False

def __init__(
self,
location: DataLocation = DataLocation.UNSET,
Expand Down
8 changes: 8 additions & 0 deletions vyper/context/types/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from vyper.context.namespace import get_namespace
from vyper.context.types.bases import BaseTypeDefinition, DataLocation
from vyper.context.types.indexable.sequence import TupleDefinition
from vyper.context.types.meta.struct import StructDefinition
from vyper.context.types.utils import (
StringEnum,
check_constant,
Expand Down Expand Up @@ -290,6 +291,13 @@ def from_FunctionDef(
type_definition = get_type_from_annotation(
arg.annotation, location=DataLocation.CALLDATA, is_immutable=True
)
if isinstance(type_definition, StructDefinition) and type_definition.is_dynamic_size:
# this is a temporary restriction and should be removed once support for dynamically
# sized structs is implemented - https://github.com/vyperlang/vyper/issues/2190
raise ArgumentException(
fubuloubu marked this conversation as resolved.
Show resolved Hide resolved
"Struct with dynamically sized data cannot be used as a function input", arg
)

if value is not None:
if not check_constant(value):
raise StateAccessViolation(
Expand Down
8 changes: 8 additions & 0 deletions vyper/context/types/indexable/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def __init__(
def __repr__(self):
return f"{self.value_type}[{self.length}]"

@property
def is_dynamic_size(self):
return self.value_type.is_dynamic_size

def get_index_type(self, node):
if isinstance(node, vy_ast.Int):
if node.value < 0:
Expand Down Expand Up @@ -107,6 +111,10 @@ def __init__(self, value_type: Tuple[BaseTypeDefinition, ...]) -> None:
def __repr__(self):
return self._id

@property
def is_dynamic_size(self):
return any(i for i in self.self.value_type if i.is_dynamic_size)

def get_index_type(self, node):
if not isinstance(node, vy_ast.Int):
raise InvalidType("Tuple indexes must be literals", node)
Expand Down
4 changes: 4 additions & 0 deletions vyper/context/types/meta/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def __init__(
for key, type_ in members.items():
self.add_member(key, type_)

@property
def is_dynamic_size(self):
return any(i for i in self.members.values() if i.is_dynamic_size)

def compare_type(self, other):
return super().compare_type(other) and self._id == other._id

Expand Down
2 changes: 2 additions & 0 deletions vyper/context/types/value/array_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class _ArrayValueDefinition(ValueTypeDefinition):
is applied to a literal definition.
"""

is_dynamic_size = True

def __repr__(self):
return f"{self._id}[{self.length}]"

Expand Down