Skip to content

Commit b4f24b6

Browse files
add trybuild test output
1 parent b346869 commit b4f24b6

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#[derive(num_enum::ConstTryFromPrimitive)]
2+
#[num_enum(method_names(const_try = f))]
3+
#[repr(u8)]
4+
enum MissingConstructor {
5+
Zero,
6+
One,
7+
Two,
8+
}
9+
10+
#[derive(num_enum::ConstTryFromPrimitive)]
11+
#[num_enum(method_names(const_try_from = f, extra = something))]
12+
#[repr(u8)]
13+
enum MissingConstructor1 {
14+
Zero,
15+
One,
16+
Two,
17+
}
18+
19+
#[derive(num_enum::ConstTryFromPrimitive)]
20+
#[num_enum(method_names(const_try_from = f, const_into = g, const_try_from = something))]
21+
#[repr(u8)]
22+
enum MissingConstructor2 {
23+
Zero,
24+
One,
25+
Two,
26+
}
27+
28+
#[derive(num_enum::ConstTryFromPrimitive)]
29+
#[num_enum(method_names(const_try_from = f, const_into = g))]
30+
#[num_enum(method_names(const_try_from = f1, const_into = g1))]
31+
#[repr(u8)]
32+
enum MissingConstructor3 {
33+
Zero,
34+
One,
35+
Two,
36+
}
37+
38+
fn main() {
39+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: expected one of: `const_into`, `const_from`, `const_try_from`, `const_default`
2+
--> tests/try_build/compile_fail/custom_method_name_parsing.rs:2:25
3+
|
4+
2 | #[num_enum(method_names(const_try = f))]
5+
| ^^^^^^^^^
6+
7+
error: expected one of: `const_into`, `const_from`, `const_try_from`, `const_default`
8+
--> tests/try_build/compile_fail/custom_method_name_parsing.rs:11:45
9+
|
10+
11 | #[num_enum(method_names(const_try_from = f, extra = something))]
11+
| ^^^^^
12+
13+
error: At most one num_enum const_try_from method name may be specified
14+
--> tests/try_build/compile_fail/custom_method_name_parsing.rs:20:61
15+
|
16+
20 | #[num_enum(method_names(const_try_from = f, const_into = g, const_try_from = something))]
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: At most one num_enum method_names attribute may be specified
20+
--> tests/try_build/compile_fail/custom_method_name_parsing.rs:30:1
21+
|
22+
30 | #[num_enum(method_names(const_try_from = f1, const_into = g1))]
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

num_enum_derive/src/enum_attributes.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::utils::die;
2-
use core::marker::PhantomData;
32
use proc_macro2::{Span, TokenStream};
43
use quote::ToTokens;
54
use syn::{
@@ -253,30 +252,23 @@ impl Parse for GeneratedMethodNameArgument {
253252
#[derive(Clone)]
254253
#[cfg_attr(test, derive(Debug))]
255254
pub(crate) struct GeneratedMethodNameAttribute<KW> {
255+
pub(crate) kw: KW,
256256
pub(crate) id: syn::Ident,
257-
_ph: PhantomData<KW>,
258-
}
259-
260-
impl<KW> GeneratedMethodNameAttribute<KW> {
261-
pub(crate) fn new(id: syn::Ident) -> Self {
262-
Self {
263-
id,
264-
_ph: PhantomData,
265-
}
266-
}
267257
}
268258

269259
impl<KW: Parse> Parse for GeneratedMethodNameAttribute<KW> {
270260
fn parse(input: ParseStream) -> Result<Self> {
271-
input.parse::<KW>()?;
261+
let kw = input.parse::<KW>()?;
272262
input.parse::<syn::Token![=]>()?;
273263
let id = input.parse()?;
274-
Ok(Self::new(id))
264+
265+
Ok(Self { kw, id })
275266
}
276267
}
277268

278-
impl<KW> ToTokens for GeneratedMethodNameAttribute<KW> {
269+
impl<KW: ToTokens> ToTokens for GeneratedMethodNameAttribute<KW> {
279270
fn to_tokens(&self, tokens: &mut TokenStream) {
271+
self.kw.to_tokens(tokens);
280272
self.id.to_tokens(tokens);
281273
}
282274
}

0 commit comments

Comments
 (0)