Commit a7efce5
authored
Fix custom attrs incorrectly applied to testutils fns (#1699)
### What
Move attribute filtering before `testutils_only_code` generation in
`derive_fn.rs` so that custom attribute macros on methods in
`#[contractimpl]` blocks are not incorrectly applied to generated
wrapper functions.
### Why
When a method in a `#[contractimpl]` block has a custom attribute macro,
the attribute was being:
- Correctly filtered out of wasm-generated code
- Incorrectly passed to testutils wrapper functions (like
`__invoke_raw_slice`)
This caused `cargo test` to fail while `cargo build --target
wasm32v1-none` succeeded. For example, if an attribute macro injects
code that references `Self`, it would fail when applied to the generated
free functions where `Self` is not valid:
```
error[E0411]: cannot find type Self in this scope
```
This bug affects test-only code, and did not impact the code generation
of code that builds to wasm.
It looks like I introduced this bug in bf73a94 (#1691), which
restructured the generated wrapper functions to be emitted as free
functions instead of inside a nested module. As part of adding
`#[doc(hidden)]` and `#[allow(non_snake_case)]` to the
`testutils_only_code`, `#(#attrs)*` was also added to match the pattern
used for the other generated functions. However, this was done without
noticing that attribute filtering happened *later* in the function and
so `testutils_only_code` received unfiltered attributes while the main
code received filtered attributes:
```rust
// testutils_only_code used UNFILTERED attrs
let testutils_only_code = if cfg!(feature = "testutils") {
Some(quote! {
#(#attrs)* // <-- Added in bf73a94, but unfiltered!
pub fn #invoke_raw_slice(...) { ... }
})
};
// Filtering happened AFTER
let attrs = attrs.iter()
.filter(|attr| pass_through_attr_to_gen_code(attr))
.collect();
```
The fix moves attribute filtering before testutils_only_code generation
so both use consistently filtered attributes.
This bug highlights a gap in the tests for macro composability, and so a
test has been added to close that gap.
Close #1698
### Known limitations
N/A1 parent a3cefc1 commit a7efce5
File tree
4 files changed
+104
-7
lines changed- soroban-sdk-macros/src
- tests/macros
- proc_macros/src
- src
- test_snapshots/test
4 files changed
+104
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
172 | 177 | | |
173 | 178 | | |
174 | 179 | | |
| |||
190 | 195 | | |
191 | 196 | | |
192 | 197 | | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | 198 | | |
200 | 199 | | |
201 | 200 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
17 | 21 | | |
18 | 22 | | |
19 | 23 | | |
| |||
30 | 34 | | |
31 | 35 | | |
32 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
33 | 57 | | |
Lines changed: 61 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
0 commit comments