Auto merge of #43604 - abonander:proc_macro_span_api, r=jseyfried

Improvements to `proc_macro::Span` API

Motivation: https://internals.rust-lang.org/t/better-panic-location-reporting-for-unwrap-and-friends/5042/12?u=logician

TODO:
- [x] Bikeshedding/complete API
- [x] Implement tests/verify return values

cc @jseyfried @nrc
This commit is contained in:
bors 2017-10-06 18:52:30 +00:00
commit 05cbece094
4 changed files with 235 additions and 3 deletions

View file

@ -0,0 +1,45 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// force-host
// no-prefer-dynamic
#![crate_type = "proc-macro"]
#![feature(proc_macro)]
extern crate proc_macro;
use proc_macro::*;
// Re-emits the input tokens by parsing them from strings
#[proc_macro]
pub fn reemit(input: TokenStream) -> TokenStream {
input.to_string().parse().unwrap()
}
#[proc_macro]
pub fn assert_fake_source_file(input: TokenStream) -> TokenStream {
for tk in input {
let source_file = tk.span.source_file();
assert!(!source_file.is_real(), "Source file is real: {:?}", source_file);
}
"".parse().unwrap()
}
#[proc_macro]
pub fn assert_source_file(input: TokenStream) -> TokenStream {
for tk in input {
let source_file = tk.span.source_file();
assert!(source_file.is_real(), "Source file is not real: {:?}", source_file);
}
"".parse().unwrap()
}

View file

@ -0,0 +1,19 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[macro_export]
macro_rules! reemit_legacy {
($($tok:tt)*) => ($($tok)*)
}
#[macro_export]
macro_rules! say_hello_extern {
($macname:ident) => ( $macname! { "Hello, world!" })
}

View file

@ -0,0 +1,43 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// aux-build:span-api-tests.rs
// aux-build:span-test-macros.rs
// ignore-pretty
#![feature(proc_macro)]
#[macro_use]
extern crate span_test_macros;
extern crate span_api_tests;
use span_api_tests::{reemit, assert_fake_source_file, assert_source_file};
macro_rules! say_hello {
($macname:ident) => ( $macname! { "Hello, world!" })
}
assert_source_file! { "Hello, world!" }
say_hello! { assert_source_file }
reemit_legacy! {
assert_source_file! { "Hello, world!" }
}
say_hello_extern! { assert_fake_source_file }
reemit! {
assert_source_file! { "Hello, world!" }
}
fn main() {}