Skip to content

Commit

Permalink
Expand parse_meta compiletests
Browse files Browse the repository at this point in the history
  • Loading branch information
TedDriggs committed Mar 1, 2024
1 parent adae089 commit 5fd3989
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
17 changes: 17 additions & 0 deletions tests/compile-fail/parse_meta.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0277]: the trait bound `NotMacroArgs: FromMeta` is not satisfied
--> tests/compile-fail/parse_meta.rs:8:16
|
8 | let args = parse_meta!(args as NotMacroArgs);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromMeta` is not implemented for `NotMacroArgs`
|
= help: the following other types implement trait `FromMeta`:
()
Arc<T>
AtomicBool
ExprArray
ExprPath
Flag
HashMap<proc_macro2::Ident, V, S>
HashMap<std::string::String, V, S>
and $N others
= note: this error originates in the macro `parse_meta` (in Nightly builds, run with -Z macro-backtrace for more info)
13 changes: 13 additions & 0 deletions tests/compile-fail/parse_meta_invalid_receiver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
extern crate proc_macro;
use darling::parse_meta;
use proc_macro::TokenStream;

struct NotMacroArgs;

pub fn invalid_receiver_type(args: TokenStream, _input: TokenStream) -> TokenStream {
let args = parse_meta!(args as NotMacroArgs);

unimplemented!()
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/compile-fail/parse_meta_invalid_receiver.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error[E0277]: the trait bound `NotMacroArgs: FromMeta` is not satisfied
--> tests/compile-fail/parse_meta_invalid_receiver.rs:8:16
|
8 | let args = parse_meta!(args as NotMacroArgs);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromMeta` is not implemented for `NotMacroArgs`
|
= help: the following other types implement trait `FromMeta`:
()
Arc<T>
AtomicBool
ExprArray
ExprPath
Flag
HashMap<proc_macro2::Ident, V, S>
HashMap<std::string::String, V, S>
and $N others
= note: this error originates in the macro `parse_meta` (in Nightly builds, run with -Z macro-backtrace for more info)
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ struct MacroArgs {
}

pub fn your_attr(args: TokenStream, input: TokenStream) -> TokenStream {
let _args = parse_meta!(args as MacroArgs);
let args = parse_meta!(args);
let _input = syn::parse_macro_input!(input as ItemFn);

// do things with `args`
println!("{:?}", args.timeout_ms);
unimplemented!()
}

Expand Down
13 changes: 13 additions & 0 deletions tests/compile-fail/parse_meta_no_type.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0282]: type annotations needed
--> tests/compile-fail/parse_meta_no_type.rs:14:9
|
14 | let args = parse_meta!(args);
| ^^^^
...
17 | println!("{:?}", args.timeout_ms);
| ---- type must be known at this point
|
help: consider giving `args` an explicit type
|
14 | let args: _ = parse_meta!(args);
| +++
31 changes: 31 additions & 0 deletions tests/compile-pass/parse_meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extern crate proc_macro;
use darling::{parse_meta, FromMeta};
use proc_macro::TokenStream;
use syn::ItemFn;

#[derive(Debug, Clone, FromMeta)]
struct MacroArgs {
#[darling(default)]
timeout_ms: Option<u16>,
path: String,
}

pub fn with_explicit_type(args: TokenStream, input: TokenStream) -> TokenStream {
let _args = parse_meta!(args as MacroArgs);
let _input = syn::parse_macro_input!(input as ItemFn);

// do things with `args`
unimplemented!()
}

pub fn example_with_inferred_type(args: TokenStream, _input: TokenStream) -> TokenStream {
fn takes_macro_args(args: MacroArgs) -> Option<u16> {
args.timeout_ms
}

let _timeout = takes_macro_args(parse_meta!(args));

unimplemented!()
}

fn main() {}
2 changes: 1 addition & 1 deletion tests/compiletests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
fn compile_test() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/compile-fail/*.rs");
t.pass("tests/compile-success/*.rs");
t.pass("tests/compile-pass/*.rs");
}

#[rustversion::not(stable(1.65))]
Expand Down

0 comments on commit 5fd3989

Please sign in to comment.