/src/test/ui/proc-macro/auxiliary/parent-source-spans.rs

https://gitlab.com/rust-lang/rust · Rust · 43 lines · 31 code · 10 blank · 2 comment · 3 complexity · ed6ee9dae2b075b87bb73446d4849b28 MD5 · raw file

  1. // force-host
  2. // no-prefer-dynamic
  3. #![feature(proc_macro_diagnostic, proc_macro_span)]
  4. #![crate_type = "proc-macro"]
  5. extern crate proc_macro;
  6. use proc_macro::{TokenStream, TokenTree, Span};
  7. fn lit_span(tt: TokenTree) -> (Span, String) {
  8. match tt {
  9. TokenTree::Literal(..) |
  10. TokenTree::Group(..) => (tt.span(), tt.to_string().trim().into()),
  11. _ => panic!("expected a literal in token tree, got: {:?}", tt)
  12. }
  13. }
  14. #[proc_macro]
  15. pub fn parent_source_spans(input: TokenStream) -> TokenStream {
  16. let mut tokens = input.into_iter();
  17. let (sp1, str1) = lit_span(tokens.next().expect("first string"));
  18. let _ = tokens.next();
  19. let (sp2, str2) = lit_span(tokens.next().expect("second string"));
  20. sp1.error(format!("first final: {}", str1)).emit();
  21. sp2.error(format!("second final: {}", str2)).emit();
  22. if let (Some(p1), Some(p2)) = (sp1.parent(), sp2.parent()) {
  23. p1.error(format!("first parent: {}", str1)).emit();
  24. p2.error(format!("second parent: {}", str2)).emit();
  25. if let (Some(gp1), Some(gp2)) = (p1.parent(), p2.parent()) {
  26. gp1.error(format!("first grandparent: {}", str1)).emit();
  27. gp2.error(format!("second grandparent: {}", str2)).emit();
  28. }
  29. }
  30. sp1.source().error(format!("first source: {}", str1)).emit();
  31. sp2.source().error(format!("second source: {}", str2)).emit();
  32. "ok".parse().unwrap()
  33. }