Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sol-macro): don't panic when encountering functions without names #217

Merged
merged 1 commit into from
Aug 1, 2023
Merged
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
8 changes: 7 additions & 1 deletion crates/sol-macro/src/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ pub(super) fn expand(cx: &ExpCtxt<'_>, function: &ItemFunction) -> Result<TokenS
attrs,
arguments,
returns,
name: Some(_),
..
} = function;
} = function
else {
// ignore functions without names (constructors, modifiers...)
return Ok(quote!())
};

cx.assert_resolved(arguments)?;
if let Some(returns) = returns {
cx.assert_resolved(&returns.returns)?;
Expand Down
12 changes: 7 additions & 5 deletions crates/sol-macro/src/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl ExpCtxt<'_> {
.functions
.values()
.flatten()
.map(|f| f.name().clone())
.flat_map(|f| f.name.clone())
.collect();
let mut overloads_map = std::mem::take(&mut self.function_overloads);

Expand Down Expand Up @@ -236,10 +236,12 @@ impl<'ast> Visit<'ast> for ExpCtxt<'ast> {
}

fn visit_item_function(&mut self, function: &'ast ItemFunction) {
self.functions
.entry(function.name().as_string())
.or_default()
.push(function);
if let Some(name) = &function.name {
self.functions
.entry(name.as_string())
.or_default()
.push(function);
}
ast::visit::visit_item_function(self, function);
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/syn-solidity/src/item/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ impl ItemFunction {
}
}

/// Returns the name of the function.
///
/// # Panics
///
/// Panics if the function has no name. This is the case when `kind` is not
/// `Function`.
pub fn name(&self) -> &SolIdent {
match &self.name {
Some(name) => name,
Expand Down