Skip to content
Open
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
11 changes: 11 additions & 0 deletions crates/sema/src/ast_lowering/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,14 @@ impl Declarations {
name: Symbol,
decl: Declaration,
) -> Result<(), ErrorGuaranteed> {
// Check if we're shadowing a builtin before declaring
if let hir::Res::Item(hir::ItemId::Event(_)) = decl.res
&& let Some(existing) = self.declarations.get(&name)
&& existing.iter().any(|d| matches!(d.res, hir::Res::Builtin(_)))
{
sess.dcx.warn("this declaration shadows a builtin symbol").span(decl.span).emit();
}

self.try_declare(hir, name, decl)
.map_err(|conflict| report_conflict(hir, sess, name, decl, conflict))
}
Expand Down Expand Up @@ -1549,6 +1557,9 @@ impl Declarations {
let same_kind = |decl2: &Declaration| match decl2.res {
Item(Variable(v)) => hir.variable(v).getter == getter,
Item(Function(f)) => hir.function(f).kind.is_ordinary(),
// Events are allowed to shadow builtins (this, super, _)
// See: https://github.com/argotorg/solidity/blob/develop/test/libsolidity/syntaxTests/events/illegal_names_exception.sol
Builtin(_) if matches!(decl.res, Item(Event(_))) => true,
ref k => k.matches(&decl.res),
};
declarations.iter().find(|&decl2| !same_kind(decl2)).copied()
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/resolve/buildin_shadow.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//https://github.com/argotorg/solidity/blob/develop/test/libsolidity/syntaxTests/events/illegal_names_exception.sol

contract C {
event this();
event super();
event _();
}
12 changes: 12 additions & 0 deletions tests/ui/resolve/buildin_shadow.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
warning: this declaration shadows a builtin symbol
╭▸ ROOT/tests/ui/resolve/buildin_shadow.sol:LL:CC
LL │ event this();
╰╴ ━━━━

warning: this declaration shadows a builtin symbol
╭▸ ROOT/tests/ui/resolve/buildin_shadow.sol:LL:CC
LL │ event super();
╰╴ ━━━━━

Loading