diff --git a/tests/parser/features/decorators/test_pure.py b/tests/parser/features/decorators/test_pure.py index ad8d475ba1..a587bc5856 100644 --- a/tests/parser/features/decorators/test_pure.py +++ b/tests/parser/features/decorators/test_pure.py @@ -83,6 +83,20 @@ def foo() -> uint256: ) +def test_invalid_self_access(get_contract, assert_compile_failed): + assert_compile_failed( + lambda: get_contract( + """ +@pure +@external +def foo() -> address: + return self + """ + ), + StateAccessViolation, + ) + + def test_invalid_call(get_contract, assert_compile_failed): assert_compile_failed( lambda: get_contract( diff --git a/vyper/semantics/validation/local.py b/vyper/semantics/validation/local.py index 7df016cad8..0217c28cc8 100644 --- a/vyper/semantics/validation/local.py +++ b/vyper/semantics/validation/local.py @@ -194,6 +194,14 @@ def __init__( ) }, ) + + # Add references to `self` as standalone address + self_references = fn_node.get_descendants(vy_ast.Name, {"id": "self"}) + standalone_self = [ + n for n in self_references if not isinstance(n.get_ancestor(), vy_ast.Attribute) + ] + node_list.extend(standalone_self) # type: ignore + for node in node_list: t = node._metadata.get("type") if isinstance(t, ContractFunction) and t.mutability == StateMutability.PURE: