Skip to content

Commit

Permalink
Merge pull request #2397 from charles-cooper/fix_interface_types
Browse files Browse the repository at this point in the history
fix: allow interfaces in lists,structs,maps
  • Loading branch information
charles-cooper authored Jul 28, 2021
2 parents cbc9e65 + 6f13e2b commit 0a389e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 3 additions & 5 deletions vyper/old_codegen/global_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ def make_struct(self, node: "vy_ast.StructDef") -> list:
# A struct must be defined before it is referenced.
# This feels like a semantic step and maybe should be pushed
# to a later compilation stage.
parse_type(
member_type, "storage", custom_structs=self._structs,
)
self.parse_type( member_type, "storage")
members.append((member_name, member_type))
else:
raise StructureException("Structs can only contain variables", item)
Expand Down Expand Up @@ -206,7 +204,7 @@ def add_globals_and_events(self, item):
if isinstance(item.annotation.args[0], vy_ast.Name) and item_name in self._contracts:
typ = InterfaceType(item_name)
else:
typ = parse_type(item.annotation.args[0], "storage", custom_structs=self._structs,)
typ = self.parse_type(item.annotation.args[0], "storage")
self._globals[item.target.id] = VariableRecord(
item.target.id, len(self._globals), typ, True,
)
Expand All @@ -215,7 +213,7 @@ def add_globals_and_events(self, item):
self._globals[item.target.id] = VariableRecord(
item.target.id,
len(self._globals),
parse_type(item.annotation, "storage", custom_structs=self._structs,),
self.parse_type(item.annotation, "storage"),
True,
)
else:
Expand Down
16 changes: 9 additions & 7 deletions vyper/old_codegen/types/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ def canonicalize_type(t, is_indexed=False):
raise InvalidType(f"Invalid or unsupported type: {repr(t)}")


def make_struct_type(name, location, members, custom_structs):
def make_struct_type(name, location, sigs, members, custom_structs):
o = OrderedDict()

for key, value in members:
if not isinstance(key, vy_ast.Name):
raise InvalidType(
f"Invalid member variable for struct {key.id}, expected a name.", key,
)
o[key.id] = parse_type(value, location, custom_structs=custom_structs)
o[key.id] = parse_type(value, location, sigs=sigs, custom_structs=custom_structs)

return StructType(o, name)

Expand All @@ -201,8 +201,10 @@ def parse_type(item, location, sigs=None, custom_structs=None):
if isinstance(item, vy_ast.Name):
if item.id in BASE_TYPES:
return BaseType(item.id)
elif (sigs is not None) and item.id in sigs:
return InterfaceType(item.id)
elif (custom_structs is not None) and (item.id in custom_structs):
return make_struct_type(item.id, location, custom_structs[item.id], custom_structs,)
return make_struct_type(item.id, location, sigs, custom_structs[item.id], custom_structs,)
else:
raise InvalidType("Invalid base type: " + item.id, item)
# Units, e.g. num (1/sec) or contracts
Expand All @@ -213,7 +215,7 @@ def parse_type(item, location, sigs=None, custom_structs=None):
return InterfaceType(item.args[0].id)
# Struct types
if (custom_structs is not None) and (item.func.id in custom_structs):
return make_struct_type(item.id, location, custom_structs[item.id], custom_structs,)
return make_struct_type(item.id, location, sigs, custom_structs[item.id], custom_structs,)
raise InvalidType("Units are no longer supported", item)
# Subscripts
elif isinstance(item, vy_ast.Subscript):
Expand All @@ -233,13 +235,13 @@ def parse_type(item, location, sigs=None, custom_structs=None):
# List
else:
return ListType(
parse_type(item.value, location, custom_structs=custom_structs,), n_val,
parse_type(item.value, location, sigs, custom_structs=custom_structs,), n_val,
)
elif item.value.id in ("HashMap",) and isinstance(item.slice.value, vy_ast.Tuple):
keytype = parse_type(item.slice.value.elements[0], None, custom_structs=custom_structs,)
keytype = parse_type(item.slice.value.elements[0], None, sigs, custom_structs=custom_structs,)
return MappingType(
keytype,
parse_type(item.slice.value.elements[1], location, custom_structs=custom_structs,),
parse_type(item.slice.value.elements[1], location, sigs, custom_structs=custom_structs,),
)
# Mappings, e.g. num[address]
else:
Expand Down

0 comments on commit 0a389e3

Please sign in to comment.