Skip to content

contract self implementation #7030

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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from

Conversation

hey-ewan
Copy link

@hey-ewan hey-ewan commented Mar 23, 2025

Description

close: #5905

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@hey-ewan hey-ewan requested review from a team as code owners March 23, 2025 07:37
@fuel-cla-bot
Copy link

fuel-cla-bot bot commented Mar 23, 2025

Thanks for the contribution! Before we can merge this, we need @hey-ewan to sign the Fuel Labs Contributor License Agreement.

@hey-ewan hey-ewan changed the title Contract self impl contract self implementation Mar 23, 2025
Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

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

This needs some tests to validate that it even works.

Copy link

codspeed-hq bot commented Mar 24, 2025

CodSpeed Performance Report

Merging #7030 will not alter performance

Comparing hey-ewan:contract-self-impl (e03feea) with master (b59e1b6)

Summary

✅ 22 untouched benchmarks

@hey-ewan
Copy link
Author

This needs some tests to validate that it even works.

@IGI-111 I added to the tests in forc-test as this directly tests the self impl feature in sway projects.

the feature generates a unique anonymous ABI with name __AnonymousAbi_N for each impl Contract block, where N is a unique counter.

simply put, the compiler takes an input like this:

impl Contract {
    fn foo() -> u64 { 0 }
}

and generates this equivalent code:

abi __AnonymousAbi_1 {
    fn foo() -> u64;
}

impl __AnonymousAbi_1 for Contract {
    fn foo() -> u64 { 0 }
}

@hey-ewan hey-ewan requested a review from IGI-111 March 26, 2025 06:55
Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

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

I believe you're mistaken about the structure of tests in this repository.

forc-test is a test runner utility similar to cargo-test and has nothing to do with such language changes. Your test should not affect that folder at all, and should live in test/src/e2e_vm_tests/test_programs/should_pass

prevents compiler issue: `Identifiers cannot begin with a double
underscore, as that naming convention is reserved for compiler
intrinsics.`
@hey-ewan
Copy link
Author

I've removed the test from forc-test and added a test contract contract_abi_auto_impl to test/src/e2e_vm_tests/test_programs/should_pass/test_abis, in it there's a unit test to ensure the anonymous abi is usable.

I also renamed __AnonymousAbi_{} to _AnonymousAbi_{} to prevent a compiler issue while trying to access the abi: Identifiers cannot begin with a double underscore, as that naming convention is reserved for compiler intrinsics.

@hey-ewan hey-ewan requested a review from IGI-111 March 31, 2025 09:01
@hey-ewan
Copy link
Author

@IGI-111, after doing some digging with the tests, I figured out that the std dep is needed for this, else the test fails. also there's a test contract should_fail/self_impl_contract that directly opposes this feature, hence I'll be removing that

@xunilrj
Copy link
Contributor

xunilrj commented Apr 8, 2025

Would be possible for the Abi to be named as the project is in Forc.toml? Probably changing the case to upper camel case.

"_Anonymous_Abi" sound strange. We use underscore only for intrinsics.

If we only have one "impl", we don't need a suffix I think.

@xunilrj
Copy link
Contributor

xunilrj commented Apr 8, 2025

Are we propagating all fn attributes like "read", "write" and others? I am seeing some "Attributes::default()" in the changes.

I think we should enable "validate_abi" in the "test.toml", like others tests do.

@hey-ewan
Copy link
Author

hey-ewan commented Apr 8, 2025

Would be possible for the Abi to be named as the project is in Forc.toml? Probably changing the case to upper camel case.

@xunilrj so I tried using the ManifestFile enum to get the name from the manifest, but that can't be imported due to the orphan rule. next I tried getting the current dir through env::current_dir() then parsing the toml file directly, but this falls short when tests aren't run from within any dir of the project being tested. how do you suggest I get the project name?

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

Successfully merging this pull request may close these issues.

Contract self impl
3 participants