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

AST validate that a contract does not have a function with contract name #117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ferranbt
Copy link
Contributor

This PR adds an AST validation check similar to this which verifies that a function does not have the same name as the contract.

Note, eventually this can be merged into a specific AST contract level checker.

crates/sema/src/ast_passes.rs Outdated Show resolved Hide resolved
Copy link
Member

@onbjerg onbjerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@onbjerg onbjerg added C-enhancement Category: an issue proposing an enhancement or a PR with one A-sema Area: semantic analysis labels Nov 15, 2024
if func_name.as_str() == contract_name {
self.dcx()
.err("functions are not allowed to have the same name as the contract")
.note("if you intend this to be a constructor, use \"constructor(...) { ... }\" to define it")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.note("if you intend this to be a constructor, use \"constructor(...) { ... }\" to define it")
.note("if you intend this to be a constructor, use `constructor(...) { ... }` to define it")

&item.kind
{
if let Some(func_name) = header.name {
if func_name.as_str() == contract_name {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if func_name.as_str() == contract_name {
if func_name == contract.name {

Compare Symbols not strings, it has the same result but faster, which is the point of Symbol in the first place

if let ast::ItemKind::Function(ast::ItemFunction { kind: _, header, body: _ }) =
&item.kind
{
if let Some(func_name) = header.name {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to also check that kind is Function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sema Area: semantic analysis C-enhancement Category: an issue proposing an enhancement or a PR with one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants