-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
235 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/test/run-pass-fulldeps/proc-macro/auxiliary/span-api-tests.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/run-pass-fulldeps/proc-macro/auxiliary/span-test-macros.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} |