Change test layout

This commit is contained in:
CreepySkeleton
2020-01-20 01:16:59 +03:00
parent dccc875f00
commit 2792970024
48 changed files with 262 additions and 300 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
.fuse_hidden*
.fuse_hidden*
+8 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "proc-macro-error"
version = "0.4.4"
version = "0.4.6"
authors = ["CreepySkeleton <creepy-skeleton@yandex.ru>"]
description = "Almost drop-in replacement to panics in proc-macros"
@@ -20,7 +20,13 @@ maintenance = { status = "actively-developed" }
quote = "1"
proc-macro2 = "1"
syn = { version = "1", default-features = false }
proc-macro-error-attr = { path = "./proc-macro-error-attr", version = "0.4.3"}
proc-macro-error-attr = { path = "./proc-macro-error-attr", version = "=0.4.6"}
[build-dependencies]
rustversion = "1.0"
[dev-dependencies]
test-crate = { path = "./test-crate" }
rustversion = "1.0"
trybuild = { version = "1.0.19", features = ["diff"] }
toml = "=0.5.2" # DO NOT BUMP
+6 -6
View File
@@ -1,11 +1,11 @@
#[rustversion::nightly]
fn nightly() {
println!("cargo:rustc-cfg=pme_nightly");
#[rustversion::not(nightly)]
fn check() {
println!("cargo:rustc-cfg=use_fallback");
}
#[rustversion::not(nightly)]
fn nightly() {}
#[rustversion::nightly]
fn check() {}
fn main() {
nightly()
check()
}
+4
View File
@@ -0,0 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
.fuse_hidden*
+3 -3
View File
@@ -1,9 +1,9 @@
[package]
name = "proc-macro-error-attr"
version = "0.4.5"
version = "0.4.6"
authors = ["CreepySkeleton <creepy-skeleton@yandex.ru>"]
edition = "2018"
description = "attribute macro for proc-macro-error crate"
description = "Attribute macro for proc-macro-error crate"
license = "MIT OR Apache-2.0"
repository = "https://gitlab.com/CreepySkeleton/proc-macro-error"
@@ -14,5 +14,5 @@ proc-macro = true
quote = "1"
proc-macro2 = "1"
syn = { version = "1", default-features = false, features = ["derive", "parsing", "proc-macro"] }
syn-mid = "0.4"
syn-mid = "0.5"
rustversion = "1.0"
+9 -25
View File
@@ -1,3 +1,6 @@
//! This is `#[proc_macro_error]` attribute to be used with
//! [`proc-macro-error`](https://docs.rs/proc-macro-error/). There you go.
extern crate proc_macro;
use proc_macro::TokenStream;
@@ -12,8 +15,6 @@ use syn::{
};
use syn_mid::{Block, ItemFn};
//! This is `#[proc_macro_error]` attribute to be used with
//! [`proc-macro-error`](https://docs.rs/proc-macro-error/). There you go.
use self::Setting::*;
@@ -44,8 +45,9 @@ pub fn proc_macro_error(attr: TokenStream, input: TokenStream) -> TokenStream {
if !(settings.is_set(AllowNotMacro) || is_proc_macro) {
return quote!(
#input
compile_error!("#[proc_macro_error] attribute can be used only with a proc-macro\n\n \
hint: if you are really sure that #[proc_macro_error] should be applied \
compile_error!(
"#[proc_macro_error] attribute can be used only with a proc-macro\n\n \
= hint: if you are really sure that #[proc_macro_error] should be applied \
to this exact function use #[proc_macro_error(allow_not_macro)]\n");
)
.into();
@@ -54,34 +56,16 @@ pub fn proc_macro_error(attr: TokenStream, input: TokenStream) -> TokenStream {
let ItemFn {
attrs,
vis,
constness,
asyncness,
unsafety,
abi,
fn_token,
ident,
generics,
inputs,
output,
sig,
block,
..
} = input;
let body = gen_body(block, settings);
let body = gen_body(*block, settings);
quote!(
#(#attrs)*
#vis
#constness
#asyncness
#unsafety
#abi
#fn_token
#ident
#generics
(#inputs)
#output
#sig
{ #body }
)
.into()
View File
+2
View File
@@ -1,3 +1,5 @@
//! This implementation uses [`proc_macro::Diagnostic`]
use std::cell::Cell;
use proc_macro::{Diagnostic as PDiag, Level as PLevel};
View File
+4 -4
View File
@@ -214,12 +214,12 @@ pub mod dummy;
mod macros;
#[cfg(not(any(pme_nightly, nightly_fmt)))]
#[path = "stable.rs"]
#[cfg(use_fallback)]
#[path = "fallback.rs"]
mod imp;
#[cfg(any(pme_nightly, nightly_fmt))]
#[path = "nightly.rs"]
#[cfg(not(use_fallback))]
#[path = "diagnostic.rs"]
mod imp;
/// Represents a diagnostic level
+4
View File
@@ -0,0 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
.fuse_hidden*
+2 -5
View File
@@ -6,15 +6,12 @@ edition = "2018"
publish = false
[lib]
path = "lib.rs"
proc-macro = true
[dependencies]
proc-macro-error = { path = "../proc-macro-error"}
proc-macro-error = { path = "../" }
quote = "1"
proc-macro2 = "1"
syn = {version = "1", default-features = false }
[dev-dependencies]
trybuild = "1.0"
rustversion = "1.0"
toml = "=0.5.2" # DO NOT BUMP
+104
View File
@@ -0,0 +1,104 @@
#[macro_use]
extern crate proc_macro_error;
extern crate proc_macro;
use proc_macro2::{Span, TokenStream};
use proc_macro_error::{set_dummy, Diagnostic, Level, OptionExt, ResultExt};
#[proc_macro]
#[proc_macro_error]
pub fn abort_from(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let span = input.into_iter().next().unwrap().span();
abort!(span, syn::Error::new(Span::call_site(), "abort!(span, from) test"))
}
#[proc_macro]
#[proc_macro_error]
pub fn abort_to_string(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let span = input.into_iter().next().unwrap().span();
abort!(span, "abort!(span, single_expr) test")
}
#[proc_macro]
#[proc_macro_error]
pub fn abort_format(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let span = input.into_iter().next().unwrap().span();
abort!(span, "abort!(span, expr1, {}) test", "expr2")
}
#[proc_macro]
#[proc_macro_error]
pub fn direct_abort(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let span = input.into_iter().next().unwrap().span();
Diagnostic::spanned(span.into(), Level::Error, "Diagnostic::abort() test".into()).abort()
}
#[proc_macro]
#[proc_macro_error]
pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mut spans = input.into_iter().step_by(2).map(|t| t.span());
emit_error!(spans.next().unwrap(), syn::Error::new(Span::call_site(), "emit!(span, from) test"));
emit_error!(spans.next().unwrap(), "emit!(span, expr1, {}) test", "expr2");
emit_error!(spans.next().unwrap(), "emit!(span, single_expr) test");
Diagnostic::spanned(
spans.next().unwrap().into(),
Level::Error,
"Diagnostic::emit() test".into()
).emit();
quote!().into()
}
#[proc_macro]
#[proc_macro_error]
pub fn option_ext(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Option::<syn::Error>::None.expect_or_abort("Option::expect_or_abort() test");
quote!().into()
}
#[proc_macro]
#[proc_macro_error]
pub fn result_unwrap_or_abort(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let span = input.into_iter().next().unwrap().span();
let err = syn::Error::new(span.into(), "Result::unwrap_or_abort() test");
Result::<(), _>::Err(err).unwrap_or_abort();
quote!().into()
}
#[proc_macro]
#[proc_macro_error]
pub fn result_expect_or_abort(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let span = input.into_iter().next().unwrap().span();
let err = syn::Error::new(span.into(), "Result::expect_or_abort() test");
Result::<(), _>::Err(err).expect_or_abort("BOOM");
quote!().into()
}
#[proc_macro]
#[proc_macro_error]
pub fn dummy(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let span = input.into_iter().next().unwrap().span();
set_dummy(quote! {
impl Default for NeedDefault {
fn default() -> Self { NeedDefault::A }
}
});
abort!(span, "set_dummy test")
}
#[proc_macro]
#[proc_macro_error]
pub fn unrelated_panic(_input: proc_macro::TokenStream) -> proc_macro::TokenStream {
panic!("unrelated panic test")
}
#[proc_macro]
#[proc_macro_error]
pub fn ok(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = TokenStream::from(input);
quote!(fn #input() {}).into()
}
-123
View File
@@ -1,123 +0,0 @@
#[macro_use]
extern crate proc_macro_error;
#[macro_use]
extern crate syn;
extern crate proc_macro;
use proc_macro2::Span;
use proc_macro_error::{set_dummy, Level, OptionExt, ResultExt};
use syn::{
parse::{Parse, ParseStream},
punctuated::Punctuated,
spanned::Spanned,
Ident,
};
struct IdentOrUnderscore {
span: Span,
part: String,
}
impl IdentOrUnderscore {
fn new(span: Span, part: String) -> Self {
IdentOrUnderscore { span, part }
}
}
impl Parse for IdentOrUnderscore {
fn parse(input: ParseStream) -> syn::Result<Self> {
let la = input.lookahead1();
if la.peek(Ident) {
let t = input.parse::<Ident>().unwrap();
Ok(IdentOrUnderscore::new(t.span(), t.to_string()))
} else if la.peek(Token![_]) {
let t = input.parse::<Token![_]>().unwrap();
Ok(IdentOrUnderscore::new(t.span(), "_".to_string()))
} else {
Err(la.error())
}
}
}
struct Args(Vec<IdentOrUnderscore>);
impl Parse for Args {
fn parse(input: ParseStream) -> syn::Result<Self> {
let args = Punctuated::<_, Token![,]>::parse_terminated(input)?;
Ok(Args(args.into_iter().collect()))
}
}
#[proc_macro]
#[proc_macro_error]
pub fn make_fn(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let mut name = String::new();
let input = parse_macro_input!(input as Args);
for arg in input.0 {
match &*arg.part {
"abort" => abort!(
arg.span,
"abort! 3{} args {}", "+", "test";
hint = "help {} test", "message"
),
"abort_call_site" => abort_call_site!(
"abort_call_site! 2{} args {}", "+", "test";
help = "help {} test", "message"
),
"direct_abort" => {
diagnostic!(arg.span, Level::Error, "direct MacroError::abort() test").abort()
}
"result_expect" => {
let e = syn::Error::new(arg.span, "error");
Err(e).expect_or_abort("Result::expect_or_abort() test")
}
"result_unwrap" => {
let e = syn::Error::new(arg.span, "Result::unwrap_or_abort() test");
Err(e).unwrap_or_abort()
}
"option_expect" => None.expect_or_abort("Option::expect_or_abort() test"),
"need_default" => {
set_dummy(quote! {
impl Default for NeedDefault {
fn default() -> Self {
NeedDefault::A
}
}
});
abort!(arg.span, "set_dummy test")
}
part if part.starts_with("multi") => {
let no_help: Option<String> = Option::None;
let help = Some("Option help test");
emit_error!(
arg.span,
"multiple error part: {}", part;
note = "help {} test", "message";
hint =? help;
wow = "I see what you did here...";
help =? no_help
)
}
_ => name.push_str(&arg.part),
}
}
// test that unrelated panics are not affected
if name.is_empty() {
panic!("unrelated panic test")
}
let name = Ident::new(&name, Span::call_site());
quote!( fn #name() {} ).into()
}
-9
View File
@@ -1,9 +0,0 @@
extern crate test_crate;
use test_crate::make_fn;
make_fn!(it, _, works);
fn main() {
it_works();
}
-6
View File
@@ -1,6 +0,0 @@
extern crate test_crate;
use test_crate::make_fn;
make_fn!(abort);
fn main() {}
-8
View File
@@ -1,8 +0,0 @@
error: abort! 3+ args test
= help: help message test
--> $DIR/abort.rs:4:10
|
4 | make_fn!(abort);
| ^^^^^
-6
View File
@@ -1,6 +0,0 @@
extern crate test_crate;
use test_crate::make_fn;
make_fn!(abort_call_site);
fn main() {}
-8
View File
@@ -1,8 +0,0 @@
error: abort_call_site! 2+ args test
= help: help message test
--> $DIR/call_site.rs:4:1
|
4 | make_fn!(abort_call_site);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
-6
View File
@@ -1,6 +0,0 @@
extern crate test_crate;
use test_crate::make_fn;
make_fn!(direct_abort);
fn main() {}
-5
View File
@@ -1,5 +0,0 @@
error: direct MacroError::abort() test
--> $DIR/direct_abort.rs:4:10
|
4 | make_fn!(direct_abort);
| ^^^^^^^^^^^^
-5
View File
@@ -1,5 +0,0 @@
error: set_dummy test
--> $DIR/dummy.rs:9:10
|
9 | make_fn!(need_default);
| ^^^^^^^^^^^^
-6
View File
@@ -1,6 +0,0 @@
extern crate test_crate;
use test_crate::make_fn;
make_fn!(multi1, multi2, _, multi3);
fn main() {}
-32
View File
@@ -1,32 +0,0 @@
error: multiple error part: multi1
= note: help message test
= help: Option help test
= note: I see what you did here...
--> $DIR/multi-error.rs:4:10
|
4 | make_fn!(multi1, multi2, _, multi3);
| ^^^^^^
error: multiple error part: multi2
= note: help message test
= help: Option help test
= note: I see what you did here...
--> $DIR/multi-error.rs:4:18
|
4 | make_fn!(multi1, multi2, _, multi3);
| ^^^^^^
error: multiple error part: multi3
= note: help message test
= help: Option help test
= note: I see what you did here...
--> $DIR/multi-error.rs:4:29
|
4 | make_fn!(multi1, multi2, _, multi3);
| ^^^^^^
-6
View File
@@ -1,6 +0,0 @@
extern crate test_crate;
use test_crate::make_fn;
make_fn!(option_expect);
fn main() {}
-5
View File
@@ -1,5 +0,0 @@
error: Option::expect_or_abort() test
--> $DIR/option_expect.rs:4:1
|
4 | make_fn!(option_expect);
| ^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
-6
View File
@@ -1,6 +0,0 @@
extern crate test_crate;
use test_crate::make_fn;
make_fn!(result_expect);
fn main() {}
-5
View File
@@ -1,5 +0,0 @@
error: Result::expect_or_abort() test: error
--> $DIR/result_expect.rs:4:10
|
4 | make_fn!(result_expect);
| ^^^^^^^^^^^^^
-6
View File
@@ -1,6 +0,0 @@
extern crate test_crate;
use test_crate::make_fn;
make_fn!(result_unwrap);
fn main() {}
-5
View File
@@ -1,5 +0,0 @@
error: Result::unwrap_or_abort() test
--> $DIR/result_unwrap.rs:4:10
|
4 | make_fn!(result_unwrap);
| ^^^^^^^^^^^^^
@@ -1,3 +1,6 @@
extern crate trybuild;
extern crate rustversion;
#[rustversion::attr(any(not(stable), before(1.39)), ignore)]
#[test]
fn ui() {
+10
View File
@@ -0,0 +1,10 @@
extern crate test_crate;
use test_crate::*;
ok!(it_works);
#[test]
fn check_it_works() {
it_works();
}
+9
View File
@@ -0,0 +1,9 @@
extern crate test_crate;
use test_crate::*;
abort_from!(one, two);
abort_to_string!(one, two);
abort_format!(one, two);
direct_abort!(one, two);
fn main() {}
+23
View File
@@ -0,0 +1,23 @@
error: abort!(span, from) test
--> $DIR/abort.rs:4:13
|
4 | abort_from!(one, two);
| ^^^
error: abort!(span, single_expr) test
--> $DIR/abort.rs:5:18
|
5 | abort_to_string!(one, two);
| ^^^
error: abort!(span, expr1, expr2) test
--> $DIR/abort.rs:6:15
|
6 | abort_format!(one, two);
| ^^^
error: Diagnostic::abort() test
--> $DIR/abort.rs:7:15
|
7 | direct_abort!(one, two);
| ^^^
@@ -1,12 +1,12 @@
extern crate test_crate;
use test_crate::make_fn;
use test_crate::*;
enum NeedDefault {
A,
B
}
make_fn!(need_default);
dummy!(need_default);
fn main() {
let _ = NeedDefault::default();
+5
View File
@@ -0,0 +1,5 @@
error: set_dummy test
--> $DIR/dummy.rs:9:8
|
9 | dummy!(need_default);
| ^^^^^^^^^^^^
+6
View File
@@ -0,0 +1,6 @@
extern crate test_crate;
use test_crate::*;
emit!(one, two, three, four);
fn main() {}
+23
View File
@@ -0,0 +1,23 @@
error: emit!(span, from) test
--> $DIR/emit.rs:4:7
|
4 | emit!(one, two, three, four);
| ^^^
error: emit!(span, expr1, expr2) test
--> $DIR/emit.rs:4:12
|
4 | emit!(one, two, three, four);
| ^^^
error: emit!(span, single_expr) test
--> $DIR/emit.rs:4:17
|
4 | emit!(one, two, three, four);
| ^^^^^
error: Diagnostic::emit() test
--> $DIR/emit.rs:4:24
|
4 | emit!(one, two, three, four);
| ^^^^
@@ -1,6 +1,6 @@
error: #[proc_macro_error] attribute can be used only with a proc-macro
hint: if you are really sure that #[proc_macro_error] should be applied to this exact function use #[proc_macro_error(allow_not_macro)]
= hint: if you are really sure that #[proc_macro_error] should be applied to this exact function use #[proc_macro_error(allow_not_macro)]
--> $DIR/not_proc_macro.rs:3:1
|
+6
View File
@@ -0,0 +1,6 @@
extern crate test_crate;
use test_crate::*;
option_ext!(one, two);
fn main() {}
+5
View File
@@ -0,0 +1,5 @@
error: Option::expect_or_abort() test
--> $DIR/option_ext.rs:4:1
|
4 | option_ext!(one, two);
| ^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
+7
View File
@@ -0,0 +1,7 @@
extern crate test_crate;
use test_crate::*;
result_unwrap_or_abort!(one, two);
result_expect_or_abort!(one, two);
fn main() {}
+11
View File
@@ -0,0 +1,11 @@
error: Result::unwrap_or_abort() test
--> $DIR/result_ext.rs:4:25
|
4 | result_unwrap_or_abort!(one, two);
| ^^^
error: BOOM: Result::expect_or_abort() test
--> $DIR/result_ext.rs:5:25
|
5 | result_expect_or_abort!(one, two);
| ^^^
@@ -1,6 +1,6 @@
extern crate test_crate;
use test_crate::make_fn;
use test_crate::*;
make_fn!();
unrelated_panic!();
fn main() {}
@@ -1,7 +1,7 @@
error: proc macro panicked
--> $DIR/unrelated_panic.rs:4:1
|
4 | make_fn!();
| ^^^^^^^^^^^
4 | unrelated_panic!();
| ^^^^^^^^^^^^^^^^^^^
|
= help: message: unrelated panic test