]> git.proxmox.com Git - rustc.git/blob - src/test/ui-fulldeps/proc-macro/auxiliary/three-equals.rs
New upstream version 1.29.0+dfsg1
[rustc.git] / src / test / ui-fulldeps / proc-macro / auxiliary / three-equals.rs
1 // Copyright 2017 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
11 // no-prefer-dynamic
12
13 #![crate_type = "proc-macro"]
14 #![feature(proc_macro_diagnostic, proc_macro_span)]
15
16 extern crate proc_macro;
17
18 use proc_macro::{TokenStream, TokenTree, Span, Diagnostic};
19
20 fn parse(input: TokenStream) -> Result<(), Diagnostic> {
21 let mut count = 0;
22 let mut last_span = Span::def_site();
23 for tree in input {
24 let span = tree.span();
25 if count >= 3 {
26 return Err(span.error(format!("expected EOF, found `{}`.", tree))
27 .span_note(last_span, "last good input was here")
28 .help("input must be: `===`"))
29 }
30
31 if let TokenTree::Punct(ref tt) = tree {
32 if tt.as_char() == '=' {
33 count += 1;
34 last_span = span;
35 continue
36 }
37 }
38 return Err(span.error(format!("expected `=`, found `{}`.", tree)));
39 }
40
41 if count < 3 {
42 return Err(Span::def_site()
43 .error(format!("found {} equal signs, need exactly 3", count))
44 .help("input must be: `===`"))
45 }
46
47 Ok(())
48 }
49
50 #[proc_macro]
51 pub fn three_equals(input: TokenStream) -> TokenStream {
52 if let Err(diag) = parse(input) {
53 diag.emit();
54 return TokenStream::new();
55 }
56
57 "3".parse().unwrap()
58 }