Work-in-progress
This commit is contained in:
parent
96de63cb10
commit
85b49cfc74
4 changed files with 18 additions and 13 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -138,6 +138,7 @@ dependencies = [
|
|||
name = "teto_macros"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"teto",
|
||||
|
|
|
@ -10,6 +10,7 @@ proc-macro = true
|
|||
[dependencies]
|
||||
syn = { version = "2.0.77", features = ["full", "extra-traits"] }
|
||||
quote = "1.0.37"
|
||||
proc-macro2 = "1.0.86"
|
||||
|
||||
[dev-dependencies]
|
||||
teto = { version = "*", path = "../teto", features = ["macros"] }
|
||||
|
|
|
@ -6,21 +6,13 @@ use quote::quote;
|
|||
use syn::{FnArg, ItemFn};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn test(_args: TokenStream, tokens: TokenStream) -> TokenStream {
|
||||
pub fn test(args: TokenStream, tokens: TokenStream) -> TokenStream {
|
||||
let ItemFn {
|
||||
attrs,
|
||||
vis,
|
||||
mut sig,
|
||||
block,
|
||||
} = match syn::parse(tokens) {
|
||||
Ok(inner) => inner,
|
||||
Err(e) => {
|
||||
return TokenStream::from(
|
||||
syn::Error::new(e.span(), "teto::test can only be called on functions")
|
||||
.to_compile_error(),
|
||||
)
|
||||
}
|
||||
};
|
||||
} = parse_token(args, tokens)?;
|
||||
|
||||
let inputs = std::mem::take(&mut sig.inputs);
|
||||
// Since this macro replaces #[test], we can assume that the only arg (if
|
||||
|
@ -31,8 +23,8 @@ pub fn test(_args: TokenStream, tokens: TokenStream) -> TokenStream {
|
|||
});
|
||||
let inner_fn_invocation = if let Some(test_token_name) = test_token_name {
|
||||
quote! {
|
||||
let token = const { #test_token_name::__private_teto_macros_only__create_teto_test_token };
|
||||
inner(token())
|
||||
let token = <#test_token_name>::__private_teto_macros_only__create_teto_test_token();
|
||||
inner(token)
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
|
@ -56,6 +48,17 @@ pub fn test(_args: TokenStream, tokens: TokenStream) -> TokenStream {
|
|||
};
|
||||
|
||||
TokenStream::from(wrapped)
|
||||
}q
|
||||
|
||||
fn parse_token(args: TokenStream, tokens: TokenStream) -> syn::Result<ItemFn> {
|
||||
if !args.is_empty() {
|
||||
return Err(syn::Error::new_spanned(
|
||||
proc_macro2::TokenStream::from(args),
|
||||
"teto macros do not accept arguments",
|
||||
));
|
||||
}
|
||||
syn::parse(tokens)
|
||||
.map_err(|e| syn::Error::new(e.span(), "teto::test can only be called on functions"))
|
||||
}
|
||||
|
||||
// #[proc_macro_attribute]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#[test]
|
||||
fn ui() {
|
||||
let t = trybuild::TestCases::new();
|
||||
t.pass("tests/ui/pass/*.rs");
|
||||
t.compile_fail("tests/ui/compile_fail/*.rs");
|
||||
t.pass("tests/ui/pass/*.rs");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue