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

https://gitlab.com/jianglu/rust · Rust · 51 lines · 31 code · 10 blank · 10 comment · 3 complexity · a187da163b96907fbda806c44c71bff0 MD5 · raw file

  1. // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. // no-prefer-dynamic
  11. #![feature(proc_macro)]
  12. #![crate_type = "proc-macro"]
  13. extern crate proc_macro;
  14. use proc_macro::{TokenStream, TokenTree, Span};
  15. fn lit_span(tt: TokenTree) -> (Span, String) {
  16. match tt {
  17. TokenTree::Literal(..) |
  18. TokenTree::Group(..) => (tt.span(), tt.to_string().trim().into()),
  19. _ => panic!("expected a literal in token tree, got: {:?}", tt)
  20. }
  21. }
  22. #[proc_macro]
  23. pub fn parent_source_spans(input: TokenStream) -> TokenStream {
  24. let mut tokens = input.into_iter();
  25. let (sp1, str1) = lit_span(tokens.next().expect("first string"));
  26. let _ = tokens.next();
  27. let (sp2, str2) = lit_span(tokens.next().expect("second string"));
  28. sp1.error(format!("first final: {}", str1)).emit();
  29. sp2.error(format!("second final: {}", str2)).emit();
  30. if let (Some(p1), Some(p2)) = (sp1.parent(), sp2.parent()) {
  31. p1.error(format!("first parent: {}", str1)).emit();
  32. p2.error(format!("second parent: {}", str2)).emit();
  33. if let (Some(gp1), Some(gp2)) = (p1.parent(), p2.parent()) {
  34. gp1.error(format!("first grandparent: {}", str1)).emit();
  35. gp2.error(format!("second grandparent: {}", str2)).emit();
  36. }
  37. }
  38. sp1.source().error(format!("first source: {}", str1)).emit();
  39. sp2.source().error(format!("second source: {}", str2)).emit();
  40. "ok".parse().unwrap()
  41. }