Skip to content

Builtin variable inaccessible once shadowed #16262

@marcvernet31

Description

@marcvernet31

Description

When an event named this is declared in a contract, the compiler incorrectly resolves all uses of this to the event instead of the builtin this keyword, even in contexts where an event type is invalid (e.g., address(this)).

According to the test file test/libsolidity/syntaxTests/events/illegal_names_exception.sol, events are explicitly allowed to shadow builtins like this and super (with warning 2319). However, the current implementation makes the builtin completely inaccessible once shadowed, rather than using context to disambiguate.

Expected: The builtin this should be used in value contexts (like address(this)), while the event should only be used in emit statements.

Actual: All uses of this resolve to the event, making the builtin inaccessible and causing type errors.

Environment

  • Compiler version: 0.8.30
  • Compilation pipeline (legacy, IR, EOF): legacy (default)
  • Target EVM version (as per compiler settings): default (prague)
  • Framework/IDE (e.g. Foundry, Hardhat, Remix): Command line (solc)
  • EVM execution environment / backend / blockchain client: N/A
  • Operating system: macOS 14.2 / Linux (tested on both)

Steps to Reproduce

Create a file test.sol:

pragma solidity ^0.8.30;

contract C {
    event this();
    
    function getAddress() public view returns (address) {
        return address(this);
    }
}

Compile:

solc test.sol

Output:

Warning: This declaration shadows a builtin symbol.
 --> test.sol:5:5:
  |
5 |     event this();
  |     ^^^^^^^^^^^^^

Error: Explicit type conversion not allowed from "event this()" to "address".
 --> test.sol:8:16:
  |
8 |         return address(this);
  |                ^^^^^^^^^^^^^

The compiler resolves this to the event instead of the builtin, even though the context requires a contract instance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions