Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 0 additions & 4 deletions tests/functional/syntax/modules/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def foo():
"""
input_bundle = make_input_bundle({"lib1.vy": lib1})
with pytest.raises(NamespaceCollision) as e:
# TODO: make the error message reference the export
compile_code(main, contract_path="main.vy", input_bundle=input_bundle)

assert e.value._message == "Member 'foo' already exists in self"
Expand Down Expand Up @@ -235,7 +234,6 @@ def bar():
"""
input_bundle = make_input_bundle({"lib1.vy": lib1})
with pytest.raises(StructureException) as e:
# TODO: make the error message reference the export
compile_code(main, contract_path="main.vy", input_bundle=input_bundle)

assert e.value._message == "already exported!"
Expand Down Expand Up @@ -266,7 +264,6 @@ def bar():
"""
input_bundle = make_input_bundle({"lib1.vy": lib1})
with pytest.raises(StructureException) as e:
# TODO: make the error message reference the export
compile_code(main, contract_path="main.vy", input_bundle=input_bundle)

assert e.value._message == "already exported!"
Expand Down Expand Up @@ -300,7 +297,6 @@ def bar():
"""
input_bundle = make_input_bundle({"lib1.vy": lib1})
with pytest.raises(StructureException) as e:
# TODO: make the error message reference the export
compile_code(main, contract_path="main.vy", input_bundle=input_bundle)

assert e.value._message == "already exported!"
Expand Down
14 changes: 12 additions & 2 deletions vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
InterfaceViolation,
InvalidLiteral,
InvalidType,
NamespaceCollision,
StateAccessViolation,
StructureException,
UndeclaredDefinition,
Expand Down Expand Up @@ -565,7 +566,15 @@ def visit_ExportsDecl(self, node):

self._add_exposed_function(func_t, item, relax=False)
with tag_exceptions(item): # tag exceptions with specific item
self._self_t.typ.add_member(func_t.name, func_t)
try:
self._self_t.typ.add_member(func_t.name, func_t)
except NamespaceCollision as e:
export_name = item.node_source_code
# Re-raise with more specific message mentioning the export
raise NamespaceCollision(
f"Member '{func_t.name}' already exists in self (when exporting `{export_name}`)",
prev_decl=e.prev_decl
) from e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this try/catch block seems a little ad hoc to me. would it be better to fix it at the source (where NamespaceCollision is raised in the first place)?


exported_funcs.append(func_t)

Expand All @@ -588,7 +597,8 @@ def _add_exposed_function(self, func_t, node, relax=True):
# call this before self._self_t.typ.add_member() for exception raising
# priority
if not relax and (prev_decl := self._all_functions.get(func_t)) is not None:
raise StructureException("already exported!", node, prev_decl=prev_decl)
export_name = node.node_source_code
raise StructureException(f"export `{export_name}` already exported!", node, prev_decl=prev_decl)

self._all_functions[func_t] = node

Expand Down