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(sol-macro): call proc_macro_error handler manually #866

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ serde = { version = "1.0", default-features = false, features = ["alloc"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

# macros
proc-macro-error2 = "2.0.0"
proc-macro-error2 = ">=2.0.0, <=2.0.1" # We directly use the `entry_point` private API.
proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
Expand Down
12 changes: 7 additions & 5 deletions crates/sol-macro-expander/src/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ const RESOLVE_LIMIT: usize = 32;
///
/// [`sol!`]: https://docs.rs/alloy-sol-macro/latest/alloy_sol_macro/index.html
pub fn expand(ast: File) -> Result<TokenStream> {
ExpCtxt::new(&ast).expand()
utils::pme_compat_result(|| ExpCtxt::new(&ast).expand())
}

/// Expands a Rust type from a Solidity type.
pub fn expand_type(ty: &Type, crates: &ExternCrates) -> TokenStream {
let dummy_file = File { attrs: Vec::new(), items: Vec::new() };
let mut cx = ExpCtxt::new(&dummy_file);
cx.crates = crates.clone();
cx.expand_type(ty)
utils::pme_compat(|| {
let dummy_file = File { attrs: Vec::new(), items: Vec::new() };
let mut cx = ExpCtxt::new(&dummy_file);
cx.crates = crates.clone();
cx.expand_type(ty)
})
}

/// Mapping namespace -> ident -> T
Expand Down
25 changes: 25 additions & 0 deletions crates/sol-macro-expander/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,28 @@ impl<T: ToTokens> ToTokens for ExprArray<T> {
});
}
}

/// Applies [`proc_macro_error2`] programmatically.
pub(crate) fn pme_compat(f: impl FnOnce() -> TokenStream) -> TokenStream {
pme_compat_result(|| Ok(f())).unwrap()
}

/// Applies [`proc_macro_error2`] programmatically.
pub(crate) fn pme_compat_result(
f: impl FnOnce() -> syn::Result<TokenStream>,
) -> syn::Result<TokenStream> {
let mut r = None;
let e = proc_macro_error2::entry_point(
std::panic::AssertUnwindSafe(|| {
r = Some(f());
Default::default()
}),
false,
);
if let Some(r) = r {
if e.is_empty() || r.is_err() {
return r;
}
}
Ok(e.into())
}
2 changes: 1 addition & 1 deletion crates/sol-types/tests/compiletest.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(missing_docs)]

#[rustversion::attr(nightly, ignore = "type.rs depends way too much on compiler internals")]
#[rustversion::attr(not(nightly), ignore)]
#[rustversion::attr(not(nightly), ignore = "should run only on nightly")]
#[cfg_attr(any(target_os = "windows", miri), ignore)]
#[test]
fn ui() {
Expand Down
18 changes: 6 additions & 12 deletions crates/sol-types/tests/ui/collisions.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
error: function selector `0xa9059cbb` collides with `func_2093253501`

= note: other declaration is here

--> tests/ui/collisions.rs:10:14
|
10 | function transfer(address,uint256);
| ^^^^^^^^
|
note: other declaration is here
--> tests/ui/collisions.rs:9:14
|
9 | function func_2093253501(bytes);
| ^^^^^^^^^^^^^^^

error: error selector `0x00000000` is reserved
--> tests/ui/collisions.rs:12:11
Expand All @@ -17,16 +14,13 @@ error: error selector `0x00000000` is reserved
| ^^^^^^^^^^^^^^^^^^^

error: function selector `0xa9059cbb` collides with `func_2093253501`

= note: other declaration is here

--> tests/ui/collisions.rs:19:18
|
19 | function transfer(address,uint256);
| ^^^^^^^^
|
note: other declaration is here
--> tests/ui/collisions.rs:18:18
|
18 | function func_2093253501(bytes);
| ^^^^^^^^^^^^^^^

error: error selector `0x00000000` is reserved
--> tests/ui/collisions.rs:21:15
Expand Down
1 change: 0 additions & 1 deletion crates/sol-types/tests/ui/features/not(json)/abi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ error: the `#[sol(abi)]` attribute requires the `"json"` feature
5 | |
6 | | contract C {
... |
22 | | }
23 | | }
| |_^
|
Expand Down
39 changes: 14 additions & 25 deletions crates/sol-types/tests/ui/keywords.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -554,8 +554,7 @@ error: expected `<`, found `>`
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| |_________^ expected `<`
Expand Down Expand Up @@ -818,8 +817,7 @@ error: expected `{`, found `,`
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
| | - expected `{`
11 | | }
Expand Down Expand Up @@ -853,8 +851,7 @@ error: expected `{`, found `)`
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
| | - expected `{`
11 | | }
Expand Down Expand Up @@ -1540,8 +1537,7 @@ error: expected `(`, found `>`
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| |_________^ unexpected token
Expand Down Expand Up @@ -1700,8 +1696,7 @@ error[E0433]: failed to resolve: there are too many leading `super` keywords
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| |_________^ there are too many leading `super` keywords
Expand All @@ -1718,8 +1713,7 @@ error[E0573]: expected type, found module `crate`
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| |_________^ not a type
Expand All @@ -1741,8 +1735,7 @@ error[E0574]: expected struct, variant or union type, found module `crate`
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| |_________^ not a struct, variant or union type
Expand Down Expand Up @@ -1773,8 +1766,7 @@ error[E0573]: expected type, found module `self`
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| | ^ help: a self type with a similar name exists: `Self`
Expand All @@ -1797,8 +1789,7 @@ error[E0574]: expected struct, variant or union type, found module `self`
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| | ^ help: a self type with a similar name exists: `Self`
Expand Down Expand Up @@ -1836,8 +1827,7 @@ error[E0425]: cannot find value `value` in this scope
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| |_________^ not found in this scope
Expand Down Expand Up @@ -1876,8 +1866,7 @@ error: `Self` is not valid in the self type of an impl block
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| |_________^
Expand All @@ -1899,13 +1888,11 @@ error[E0117]: only traits defined in the current crate can be implemented for ar
5 | / sol! {
6 | | struct $kw {
7 | | uint $kw;
8 | | }
9 | |
... |
10 | | function $kw(uint $kw);
11 | | }
| | ^
| | |
| | impl doesn't use only types from inside the current crate
| |_________this is not defined in the current crate because tuples are always foreign
| this is not defined in the current crate because this is a foreign trait
...
Expand All @@ -1917,6 +1904,8 @@ error[E0117]: only traits defined in the current crate can be implemented for ar
47 | | }
| |_- in this macro invocation
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
= note: this error originates in the macro `sol` which comes from the expansion of the macro `kws` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
90 changes: 30 additions & 60 deletions crates/sol-types/tests/ui/overloads.stderr
Original file line number Diff line number Diff line change
@@ -1,119 +1,89 @@
error: function `overloadTaken` is overloaded, but the generated name `overloadTaken_0` is already in use

= note: other declaration is here

--> tests/ui/overloads.rs:15:18
|
15 | function overloadTaken();
| ^^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:17:18
|
17 | function overloadTaken_0();
| ^^^^^^^^^^^^^^^

error: function `overloadTaken` is overloaded, but the generated name `overloadTaken_1` is already in use

= note: other declaration is here

--> tests/ui/overloads.rs:16:18
|
16 | function overloadTaken(uint256);
| ^^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:18:18
|
18 | function overloadTaken_1();
| ^^^^^^^^^^^^^^^

error: function with same name and parameter types defined twice

= note: other declaration is here

--> tests/ui/overloads.rs:23:18
|
23 | function sameOverload();
| ^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:24:18
|
24 | function sameOverload();
| ^^^^^^^^^^^^

error: function with same name and parameter types defined twice

= note: other declaration is here

--> tests/ui/overloads.rs:28:18
|
28 | function sameTysOverload1(uint256[]memory a);
| ^^^^^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:29:18
|
29 | function sameTysOverload1(uint256[]storage b);
| ^^^^^^^^^^^^^^^^

error: function with same name and parameter types defined twice

= note: other declaration is here

--> tests/ui/overloads.rs:33:18
|
33 | function sameTysOverload2(string memory,string storage);
| ^^^^^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:34:18
|
34 | function sameTysOverload2(string storage b,string calldata);
| ^^^^^^^^^^^^^^^^

error: event `overloadTaken` is overloaded, but the generated name `overloadTaken_0` is already in use

= note: other declaration is here

--> tests/ui/overloads.rs:50:15
|
50 | event overloadTaken();
| ^^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:52:15
|
52 | event overloadTaken_0();
| ^^^^^^^^^^^^^^^

error: event `overloadTaken` is overloaded, but the generated name `overloadTaken_1` is already in use

= note: other declaration is here

--> tests/ui/overloads.rs:51:15
|
51 | event overloadTaken(uint256);
| ^^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:53:15
|
53 | event overloadTaken_1();
| ^^^^^^^^^^^^^^^

error: event with same name and parameter types defined twice

= note: other declaration is here

--> tests/ui/overloads.rs:58:15
|
58 | event sameOverload();
| ^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:59:15
|
59 | event sameOverload();
| ^^^^^^^^^^^^

error: event with same name and parameter types defined twice

= note: other declaration is here

--> tests/ui/overloads.rs:63:15
|
63 | event sameTysOverload1(uint256[] a);
| ^^^^^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:64:15
|
64 | event sameTysOverload1(uint256[] b);
| ^^^^^^^^^^^^^^^^

error: event with same name and parameter types defined twice

= note: other declaration is here

--> tests/ui/overloads.rs:68:15
|
68 | event sameTysOverload2(string, string);
| ^^^^^^^^^^^^^^^^
|
note: other declaration is here
--> tests/ui/overloads.rs:69:15
|
69 | event sameTysOverload2(string, string);
| ^^^^^^^^^^^^^^^^
Loading