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

Splitting contract impls across modules leads to errors #1321

Closed
leighmcculloch opened this issue Aug 19, 2024 · 2 comments · Fixed by #1322
Closed

Splitting contract impls across modules leads to errors #1321

leighmcculloch opened this issue Aug 19, 2024 · 2 comments · Fixed by #1322
Assignees
Labels
bug Something isn't working

Comments

@leighmcculloch
Copy link
Member

leighmcculloch commented Aug 19, 2024

What version are you using?

v21.5.1

What did you do?

Cargo.toml:

[package]
name = "test_modular"
version.workspace = true
authors = ["Stellar Development Foundation <info@stellar.org>"]
license = "Apache-2.0"
edition = "2021"
publish = false
rust-version.workspace = true

[lib]
crate-type = ["cdylib"]
doctest = false

[dependencies]
soroban-sdk = {path = "../../soroban-sdk"}

[dev-dependencies]
soroban-sdk = {path = "../../soroban-sdk", features = ["testutils"]}

src/lib.rs:

#![no_std]
use soroban_sdk::{contract, contractimpl};

mod feat1;
mod feat2;

#[contract]
pub struct Contract;

#[contractimpl]
impl Contract {
    pub fn base() -> u32 {
        0
    }
}

src/feat1.rs:

use soroban_sdk::contractimpl;

use crate::Contract;
use crate::ContractClient;

#[contractimpl]
impl Contract {
    pub fn one() -> u32 {
        1
    }
}

src/feat2.rs:

use soroban_sdk::contractimpl;

use crate::Contract;

#[contractimpl]
impl Contract {
    pub fn two() -> u32 {
        2
    }
}

What did you expect to see?

Compiled successfully, and test successfully.

What did you see instead?

$ soroban contract build
cargo rustc --manifest-path=Cargo.toml --crate-type=cdylib --target=wasm32-unknown-unknown --release
   Compiling test_modular v21.5.1 (/Users/leighmcculloch/Code/rs-soroban-sdk/tests/modular)
error[E0412]: cannot find type `ContractClient` in this scope
 --> tests/modular/src/feat1.rs:6:1
  |
6 | #[contractimpl]
  | ^^^^^^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in the attribute macro `soroban_sdk::contractclient` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this struct
  |
1 + use crate::ContractClient;
  |

error[E0412]: cannot find type `ContractClient` in this scope
 --> tests/modular/src/feat2.rs:6:1
  |
6 | #[contractimpl]
  | ^^^^^^^^^^^^^^^ not found in this scope
  |
  = note: this error originates in the attribute macro `soroban_sdk::contractclient` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this struct
  |
1 + use crate::ContractClient;
  |

For more information about this error, try `rustc --explain E0412`.
error: could not compile `test_modular` (lib) due to 2 previous errors

After adding in these lines to feat1.rs and feat2.rs:

use crate::ContractClient;

Build worked fine, but cargo test failed:

❯ cargo test
   Compiling test_modular v21.5.1 (/Users/leighmcculloch/Code/rs-soroban-sdk/tests/modular)
error[E0433]: failed to resolve: use of undeclared crate or module `__Contract_fn_set_registry`
 --> tests/modular/src/feat1.rs:6:1
  |
6 | #[contractimpl]
  | ^^^^^^^^^^^^^^^ use of undeclared crate or module `__Contract_fn_set_registry`
  |
  = note: this error originates in the attribute macro `contractimpl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this module
  |
1 + use crate::__Contract_fn_set_registry;
  |

error[E0433]: failed to resolve: use of undeclared crate or module `__Contract_fn_set_registry`
 --> tests/modular/src/feat2.rs:6:1
  |
6 | #[contractimpl]
  | ^^^^^^^^^^^^^^^ use of undeclared crate or module `__Contract_fn_set_registry`
  |
  = note: this error originates in the attribute macro `contractimpl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this module
  |
1 + use crate::__Contract_fn_set_registry;
  |

For more information about this error, try `rustc --explain E0433`.
error: could not compile `test_modular` (lib test) due to 2 previous errors
@leighmcculloch
Copy link
Member Author

I believe I have a fix that requires no breaking changes:

@leighmcculloch
Copy link
Member Author

I still have an issue to resolve on how to resolve the contract client. So change is still a WIP.

github-merge-queue bot pushed a commit that referenced this issue Sep 10, 2024
### What
Add a new trait `ContractFunctionRegister` that wraps the existing
`register` function that all contracts use to create a table of all
their functions.

### Why

When a contract is registered with the test environment it uses the
table of functions to know what functions a contract has defined.

Today contract functions get added to the table by using the ctor crate
to call the `#mod::register` function where `#mod` is a generated module
at the same location as where the `#[contract]` macro gets used.

The problem with this, and that was reported in #1321, is that it's
valid in Rust to place type implementations anywhere in a crate that is
able to reference the type, but there's no way for the generated code to
know what relative or absolute path to use to reference that `#mod`.

The generated code assumes that the `#mod` is imported into the current
scope, which it never will be because users have no reason to manually
do it because it is hidden from them and they don't know it exists.

This situation leads to the following error that makes little sense to a
developer because it refers to hidden generated code:
```
use of undeclared crate or module `__Contract_fn_set_registry`
```

Most of the time when folks add additional `impl` blocks to a type they
will import the type into the current module so as to reference it, and
so we can improve the existing situation by shifting the call to
`#mod::register` to be a call to `Contract::register`.

To ensure that the `register` function does not clash with a contract
function that may also be named `register` the function is defined on a
hidden trait, `ContractFunctionRegister`, and is only used in the
context of that trait.

There are some other minor changes included that remove assumptions
about the type name of a contract being a simple identifier, so that
when it is more than an identifier, such as a path (e.g.
`path::Identifier`), the full type name is preserved everywhere it is
needed.

Close #1321
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant