Skip to content

Commit

Permalink
feat(parser): recover old-style fallbacks (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Nov 21, 2024
1 parent df50ff0 commit 2e1c94e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/ast/src/ast/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ impl Type<'_> {
pub fn is_custom(&self) -> bool {
self.kind.is_custom()
}

/// Returns `true` if the type is a function.
#[inline]
pub fn is_function(&self) -> bool {
matches!(self.kind, TypeKind::Function(_))
}
}

/// The kind of a type.
Expand Down
21 changes: 21 additions & 0 deletions crates/parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,27 @@ impl<'sess, 'ast> Parser<'sess, 'ast> {
}
};

if ty.is_function()
&& flags == VarFlags::STATE_VAR
&& self.check_noexpect(&TokenKind::OpenDelim(Delimiter::Brace))
{
let msg = "expected a state variable declaration";
let note = "this style of fallback function has been removed; use the `fallback` or `receive` keywords instead";
self.dcx().err(msg).span(self.token.span).note(note).emit();
let _ = self.parse_block()?;
return Ok(VariableDefinition {
span: lo.to(self.prev_token.span),
ty,
visibility: None,
mutability: None,
data_location: None,
override_: None,
indexed: false,
name: None,
initializer: None,
});
}

let mut data_location = None;
let mut visibility = None;
let mut mutability = None;
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/parser/old_fallback.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// https://github.com/foundry-rs/foundry/issues/9349

pragma solidity >=0.4.22 <0.6;

contract BugReport {
function() external payable { //~ ERROR: expected a state variable declaration
deposit();
uint
} //~ ERROR: expected
function deposit() public payable {}
}
19 changes: 19 additions & 0 deletions tests/ui/parser/old_fallback.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: expected a state variable declaration
--> ROOT/tests/ui/parser/old_fallback.sol:LL:CC
|
LL | function() external payable {
| ^
|
= note: this style of fallback function has been removed; use the `fallback` or `receive` keywords instead

error: expected one of `(`, `.`, `;`, `?`, `[`, `payable`, `pure`, `view`, or `{`, found `}`
--> ROOT/tests/ui/parser/old_fallback.sol:LL:CC
|
LL | uint
| ^ expected one of 9 possible tokens
LL | }
| ^ unexpected token
|

error: aborting due to 2 previous errors

0 comments on commit 2e1c94e

Please sign in to comment.