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: use contract declarer's scope for name resolution #2459

Merged
merged 1 commit into from
May 17, 2024

Conversation

0xalpharush
Copy link
Member

@0xalpharush 0xalpharush commented May 17, 2024

closes #2454
replaces #2455

The scope for FunctionContract was using the inherited contract's scope and not the declarer's so it was find the wrong alias

Summary by CodeRabbit

  • New Features

    • Introduced MainContract and ParentContract Solidity smart contracts with enhanced error handling functions.
    • Added MainErrors and AccessControlErrors Solidity libraries for custom error definitions.
  • Bug Fixes

    • Updated scope determination logic to improve accuracy in variable handling.
  • Tests

    • Added new tests to verify variable scope handling and ensure correct error handling in smart contracts.
    • Enhanced existing tests to improve initialization of the Slither object.

Copy link

coderabbitai bot commented May 17, 2024

Walkthrough

Walkthrough

The changes address a bug in Slither where the scope determination logic for variable assignments was incorrect when using the same alias for different imports. The updates involve modifying the scope assignment logic, adding new Solidity contracts and libraries for testing, and introducing new tests to ensure the correct handling of variable scopes and error handling.

Changes

Files / Paths Change Summary
slither/solc_parsing/expressions/find_variable.py Updated scope assignment in _find_variable_init from underlying_function.contract.file_scope to underlying_function.contract_declarer.file_scope
tests/unit/core/test_data/scope_with_renaming/core/MainContract.sol Added MainContract inheriting from ParentContract with functions handling errors from MainErrors
tests/unit/core/test_data/scope_with_renaming/core/ParentContract.sol Added ParentContract with functions handling access control errors
tests/unit/core/test_data/scope_with_renaming/errors/MainErrors.sol Introduced MainErrors library with custom errors MainError1, MainError2, and MainError3
tests/unit/core/test_data/scope_with_renaming/errors/ParentContractErrors.sol Introduced AccessControlErrors library with custom errors AccessControlErrors1 and AccessControlErrors2
tests/unit/core/test_scope_with_renaming.py Added test test_find_variable_scope_with_renaming to analyze variable scope with renaming using CryticCompile and Slither
tests/unit/core/test_using_for.py Modified Slither instantiation to include disallow_partial=True in test_using_for_global_collision

Assessment against linked issues

Objective (Issue #) Addressed Explanation
Fix scope determination logic when using the same alias for different imports (#2454)
Ensure correct handling of variable scopes and error handling in inherited contracts (#2454)
Add tests to validate the changes and ensure no regression (#2454)
Modify Slither instantiation to prevent partial compilation issues in specific test scenarios (#2454)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@0xalpharush 0xalpharush marked this pull request as ready for review May 17, 2024 20:56
@0xalpharush 0xalpharush merged commit bfa99eb into dev May 17, 2024
83 checks passed
@0xalpharush 0xalpharush deleted the fix/identifier-resolve-with-renaming branch May 17, 2024 20:56
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Out of diff range and nitpick comments (2)
slither/solc_parsing/expressions/find_variable.py (2)

Line range hint 191-191: Replace lambda expressions with function definitions to improve readability and maintainability.

- getter_available = lambda f: f.functions_declared
+ def getter_available(f):
+     return f.functions_declared

- getter_available = lambda m: m.modifiers_declared
+ def getter_available(m):
+     return m.modifiers_declared

Also applies to: 205-205


Line range hint 33-33: Remove the unused import ContractSolc to clean up the code.

- from slither.solc_parsing.declarations.contract import ContractSolc
Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 5793b0d and 22b9cfc.
Files selected for processing (7)
  • slither/solc_parsing/expressions/find_variable.py (1 hunks)
  • tests/unit/core/test_data/scope_with_renaming/core/MainContract.sol (1 hunks)
  • tests/unit/core/test_data/scope_with_renaming/core/ParentContract.sol (1 hunks)
  • tests/unit/core/test_data/scope_with_renaming/errors/MainErrors.sol (1 hunks)
  • tests/unit/core/test_data/scope_with_renaming/errors/ParentContractErrors.sol (1 hunks)
  • tests/unit/core/test_scope_with_renaming.py (1 hunks)
  • tests/unit/core/test_using_for.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • tests/unit/core/test_data/scope_with_renaming/errors/MainErrors.sol
Additional Context Used
Ruff (3)
slither/solc_parsing/expressions/find_variable.py (3)

33-33: slither.solc_parsing.declarations.contract.ContractSolc imported but unused


191-191: Do not assign a lambda expression, use a def


205-205: Do not assign a lambda expression, use a def

Additional comments not posted (6)
tests/unit/core/test_data/scope_with_renaming/errors/ParentContractErrors.sol (1)

4-7: The new custom errors AccessControlErrors1 and AccessControlErrors2 are well-defined and follow Solidity conventions.

tests/unit/core/test_scope_with_renaming.py (1)

11-17: The test function test_find_variable_scope_with_renaming is well-structured and correctly sets up the environment for testing the scope resolution with renaming. The use of disallow_partial=True in the Slither initialization ensures that partial analyses are not allowed, which is crucial for the accuracy of this test.

tests/unit/core/test_data/scope_with_renaming/core/ParentContract.sol (1)

13-27: The functions functionWithAccessControlErrors1 and functionWithAccessControlErrors2 are correctly implemented to handle specific access control errors. The use of custom errors from the Errors alias is consistent with the intended changes in the PR.

tests/unit/core/test_data/scope_with_renaming/core/MainContract.sol (1)

16-38: The functions functionWithMainError1, functionWithMainError2, and functionWithMainError3 in MainContract are implemented correctly to handle specific errors using the Errors alias. This aligns with the PR's objective to ensure correct error handling in inherited contract scenarios.

tests/unit/core/test_using_for.py (1)

20-20: The update to initialize Slither with disallow_partial=True in the test_using_for_global_collision function is a good practice to ensure that the analysis is comprehensive and does not skip any parts.

slither/solc_parsing/expressions/find_variable.py (1)

307-307: The change to use underlying_function.contract_declarer.file_scope instead of underlying_function.contract.file_scope is crucial for ensuring that the scope resolution uses the correct contract's scope, especially in inheritance scenarios. This change directly addresses the issue described in the PR and is a significant improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant