about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorest31 <MTest31@outlook.com>2021-08-03 02:41:39 +0200
committerest31 <MTest31@outlook.com>2021-08-03 03:10:10 +0200
commit5d7b6595ceed4edb77a558a340f73120610d76fe (patch)
tree37ea537cc1bf68656014704044d0c5fa9658d8d3 /src
parent50fcd454c71084cb682f4d908b06cdd3eddacaf9 (diff)
downloadrust-5d7b6595ceed4edb77a558a340f73120610d76fe.tar.gz
rust-5d7b6595ceed4edb77a558a340f73120610d76fe.zip
Add tests
Diffstat (limited to 'src')
-rw-r--r--src/test/ui/proc-macro/auxiliary/assert-span-pos.rs37
-rw-r--r--src/test/ui/proc-macro/span-absolute-posititions.rs16
-rw-r--r--src/test/ui/proc-macro/span-absolute-posititions.stderr14
3 files changed, 67 insertions, 0 deletions
diff --git a/src/test/ui/proc-macro/auxiliary/assert-span-pos.rs b/src/test/ui/proc-macro/auxiliary/assert-span-pos.rs
new file mode 100644
index 00000000000..455c5c7c380
--- /dev/null
+++ b/src/test/ui/proc-macro/auxiliary/assert-span-pos.rs
@@ -0,0 +1,37 @@
+// force-host
+// no-prefer-dynamic
+
+#![feature(proc_macro_diagnostic, proc_macro_span)]
+#![crate_type = "proc-macro"]
+
+extern crate proc_macro;
+
+use proc_macro::{TokenStream, TokenTree, Span};
+
+fn lit_span(tt: TokenTree) -> (Span, String) {
+    match tt {
+        TokenTree::Literal(..) |
+        TokenTree::Group(..) => (tt.span(), tt.to_string().trim().into()),
+        _ => panic!("expected a literal in token tree, got: {:?}", tt)
+    }
+}
+
+#[proc_macro]
+pub fn assert_span_pos(input: TokenStream) -> TokenStream {
+    let mut tokens = input.into_iter();
+    let (sp1, str1) = lit_span(tokens.next().expect("first argument"));
+    let _ = tokens.next();
+    let (_sp2, str2) = lit_span(tokens.next().expect("second argument"));
+
+    let line: usize = str1.parse().unwrap();
+    let col: usize = str2.parse().unwrap();
+
+    let sp1s = sp1.start();
+    if (line, col) != (sp1s.line, sp1s.column) {
+        let msg = format!("line/column mismatch: ({}, {}) != ({}, {})", line, col,
+            sp1s.line, sp1s.column);
+        sp1.error(msg).emit();
+    }
+
+    "".parse().unwrap()
+}
diff --git a/src/test/ui/proc-macro/span-absolute-posititions.rs b/src/test/ui/proc-macro/span-absolute-posititions.rs
new file mode 100644
index 00000000000..7fc3f6b5793
--- /dev/null
+++ b/src/test/ui/proc-macro/span-absolute-posititions.rs
@@ -0,0 +1,16 @@
+// aux-build:assert-span-pos.rs
+// ignore-tidy-tab
+extern crate assert_span_pos;
+
+assert_span_pos::assert_span_pos!(5, 35);
+
+// Test space indentation
+    assert_span_pos::assert_span_pos!(8, 39);
+// Test tab indentation
+	assert_span_pos::assert_span_pos!(10, 36);
+
+// Test that the macro actually emits an error on a mismatch:
+assert_span_pos::assert_span_pos!(0, 35); //~ ERROR line/column mismatch: (0, 35) != (13, 35)
+assert_span_pos::assert_span_pos!(14, 0); //~ ERROR line/column mismatch: (14, 0) != (14, 35)
+
+fn main() {}
diff --git a/src/test/ui/proc-macro/span-absolute-posititions.stderr b/src/test/ui/proc-macro/span-absolute-posititions.stderr
new file mode 100644
index 00000000000..ade66417520
--- /dev/null
+++ b/src/test/ui/proc-macro/span-absolute-posititions.stderr
@@ -0,0 +1,14 @@
+error: line/column mismatch: (0, 35) != (13, 35)
+  --> $DIR/span-absolute-posititions.rs:13:35
+   |
+LL | assert_span_pos::assert_span_pos!(0, 35);
+   |                                   ^
+
+error: line/column mismatch: (14, 0) != (14, 35)
+  --> $DIR/span-absolute-posititions.rs:14:35
+   |
+LL | assert_span_pos::assert_span_pos!(14, 0);
+   |                                   ^^
+
+error: aborting due to 2 previous errors
+