Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

Commit 8acff33

Browse files
committed
Just panic on errors
1 parent b1e29ff commit 8acff33

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack-impl
5050

5151
proc_macro_expr_impl! {
5252
/// Add one to an expression.
53-
pub fn add_one_impl(input: &str) -> Result<String, String> {
54-
Ok(format!("1 + {}", input))
53+
pub fn add_one_impl(input: &str) -> String {
54+
format!("1 + {}", input)
5555
}
5656
}
5757

5858
proc_macro_item_impl! {
5959
/// A function that always returns 2.
60-
pub fn two_fn_impl(input: &str) -> Result<String, String> {
61-
Ok(format!("fn {}() -> u8 {{ 2 }}", input))
60+
pub fn two_fn_impl(input: &str) -> String {
61+
format!("fn {}() -> u8 {{ 2 }}", input)
6262
}
6363
}
6464
```
@@ -84,7 +84,7 @@ proc-macro = true
8484

8585
Users of your crate depend on your declaration crate and implementation crate,
8686
then use your procedural macros as though it were magic. They even get
87-
reasonable error messages if your procedural macro returns an error.
87+
reasonable error messages if your procedural macro panics.
8888

8989
https://github.com/dtolnay/proc-macro-hack/tree/master/example
9090

demo-hack-impl/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
proc_macro_expr_impl! {
77
/// Add one to an expression.
8-
pub fn add_one_impl(input: &str) -> Result<String, String> {
9-
Ok(format!("1 + {}", input))
8+
pub fn add_one_impl(input: &str) -> String {
9+
format!("1 + {}", input)
1010
}
1111
}
1212

1313
proc_macro_item_impl! {
1414
/// A function that always returns 2.
15-
pub fn two_fn_impl(input: &str) -> Result<String, String> {
16-
Ok(format!("fn {}() -> u8 {{ 2 }}", input))
15+
pub fn two_fn_impl(input: &str) -> String {
16+
format!("fn {}() -> u8 {{ 2 }}", input)
1717
}
1818
}

src/lib.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! proc_macro_item_decl {
3131
macro_rules! proc_macro_expr_impl {
3232
($(
3333
$( #[$attr:meta] )*
34-
pub fn $func:ident($input:ident: &str ) -> Result<String, String> $body:block
34+
pub fn $func:ident($input:ident: &str) -> String $body:block
3535
)+) => {
3636
$(
3737
$( #[$attr] )*
@@ -47,22 +47,15 @@ macro_rules! proc_macro_expr_impl {
4747

4848
let tokens = &source[prefix.len() .. source.len() - suffix.len()];
4949

50-
fn func($input: &str) -> Result<String, String> $body
50+
fn func($input: &str) -> String $body
5151

52-
let wrap = match func(tokens) {
53-
Ok(expr) => {
54-
format!("
55-
macro_rules! proc_macro_call {{
56-
() => {{
57-
{}
58-
}}
59-
}}
60-
", expr)
61-
}
62-
Err(msg) => panic!(msg)
63-
};
64-
65-
wrap.parse().unwrap()
52+
format!("
53+
macro_rules! proc_macro_call {{
54+
() => {{
55+
{}
56+
}}
57+
}}
58+
", func(tokens)).parse().unwrap()
6659
}
6760
)+
6861
}
@@ -72,7 +65,7 @@ macro_rules! proc_macro_expr_impl {
7265
macro_rules! proc_macro_item_impl {
7366
($(
7467
$( #[$attr:meta] )*
75-
pub fn $func:ident($input:ident: &str ) -> Result<String, String> $body:block
68+
pub fn $func:ident($input:ident: &str) -> String $body:block
7669
)+) => {
7770
$(
7871
$( #[$attr] )*
@@ -88,9 +81,9 @@ macro_rules! proc_macro_item_impl {
8881

8982
let tokens = &source[prefix.len() .. source.len() - suffix.len()];
9083

91-
fn func($input: &str) -> Result<String, String> $body
84+
fn func($input: &str) -> String $body
9285

93-
func(tokens).unwrap_or_else(|msg| panic!(msg)).parse().unwrap()
86+
func(tokens).parse().unwrap()
9487
}
9588
)+
9689
}

0 commit comments

Comments
 (0)