Skip to content

Make span location information available through a feature #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ matrix:
- cargo test --no-default-features
- RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test
- RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo test --no-default-features
- cargo test --features span-location-info
- cargo test --features span-location-info --no-default-features
- cargo update -Z minimal-versions && cargo build
- rust: nightly
name: WebAssembly
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ quote = "0.6"
proc-macro = []
default = ["proc-macro"]

# turns on location info on spans
span-location-info = []

# This feature no longer means anything.
nightly = []

Expand Down
9 changes: 8 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,21 @@ fn main() {
}
println!("cargo:rustc-cfg=use_proc_macro");

let mut span_location_info = cfg!(feature = "span-location-info");

let semver_exempt = cfg!(procmacro2_semver_exempt);
if semver_exempt {
span_location_info = true;
// https://github.com/alexcrichton/proc-macro2/issues/147
println!("cargo:rustc-cfg=procmacro2_semver_exempt");
}

if span_location_info {
println!("cargo:rustc-cfg=span_location_info");
}

// Rust 1.29 stabilized the necessary APIs in the `proc_macro` crate
if version.nightly || version.minor >= 29 && !semver_exempt {
if version.nightly || version.minor >= 29 && !(semver_exempt || span_location_info) {
println!("cargo:rustc-cfg=wrap_proc_macro");
}

Expand Down
60 changes: 31 additions & 29 deletions src/fallback.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![cfg_attr(not(procmacro2_semver_exempt), allow(dead_code))]
#![cfg_attr(not(all(procmacro2_semver_exempt, span_location_info)), allow(dead_code))]

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
use std::cell::RefCell;
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
use std::cmp;
use std::fmt;
use std::iter;
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
Expand Down Expand Up @@ -35,7 +35,7 @@ impl TokenStream {
}
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
fn get_cursor(src: &str) -> Cursor {
// Create a dummy file & add it to the codemap
CODEMAP.with(|cm| {
Expand All @@ -49,7 +49,7 @@ fn get_cursor(src: &str) -> Cursor {
})
}

#[cfg(not(procmacro2_semver_exempt))]
#[cfg(not(span_location_info))]
fn get_cursor(src: &str) -> Cursor {
Cursor { rest: src }
}
Expand Down Expand Up @@ -225,7 +225,7 @@ pub struct LineColumn {
pub column: usize,
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
thread_local! {
static CODEMAP: RefCell<Codemap> = RefCell::new(Codemap {
// NOTE: We start with a single dummy file which all call_site() and
Expand All @@ -238,14 +238,14 @@ thread_local! {
});
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
struct FileInfo {
name: String,
span: Span,
lines: Vec<usize>,
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
impl FileInfo {
fn offset_line_column(&self, offset: usize) -> LineColumn {
assert!(self.span_within(Span {
Expand All @@ -271,7 +271,7 @@ impl FileInfo {
}

/// Computesthe offsets of each line in the given source string.
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
fn lines_offsets(s: &str) -> Vec<usize> {
let mut lines = vec![0];
let mut prev = 0;
Expand All @@ -282,12 +282,12 @@ fn lines_offsets(s: &str) -> Vec<usize> {
lines
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
struct Codemap {
files: Vec<FileInfo>,
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
impl Codemap {
fn next_start_pos(&self) -> u32 {
// Add 1 so there's always space between files.
Expand Down Expand Up @@ -327,19 +327,19 @@ impl Codemap {

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Span {
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
lo: u32,
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
hi: u32,
}

impl Span {
#[cfg(not(procmacro2_semver_exempt))]
#[cfg(not(span_location_info))]
pub fn call_site() -> Span {
Span {}
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
pub fn call_site() -> Span {
Span { lo: 0, hi: 0 }
}
Expand All @@ -359,7 +359,7 @@ impl Span {
other
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
pub fn source_file(&self) -> SourceFile {
CODEMAP.with(|cm| {
let cm = cm.borrow();
Expand All @@ -370,7 +370,7 @@ impl Span {
})
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
pub fn start(&self) -> LineColumn {
CODEMAP.with(|cm| {
let cm = cm.borrow();
Expand All @@ -379,7 +379,7 @@ impl Span {
})
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
pub fn end(&self) -> LineColumn {
CODEMAP.with(|cm| {
let cm = cm.borrow();
Expand All @@ -388,7 +388,7 @@ impl Span {
})
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
pub fn join(&self, other: Span) -> Option<Span> {
CODEMAP.with(|cm| {
let cm = cm.borrow();
Expand All @@ -406,16 +406,16 @@ impl Span {

impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
return write!(f, "bytes({}..{})", self.lo, self.hi);

#[cfg(not(procmacro2_semver_exempt))]
#[cfg(not(span_location_info))]
write!(f, "Span")
}
}

pub fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) {
if cfg!(procmacro2_semver_exempt) {
if cfg!(span_location_info) {
debug.field("span", &span);
}
}
Expand Down Expand Up @@ -448,10 +448,12 @@ impl Group {
self.span
}

#[cfg(span_location_info)]
pub fn span_open(&self) -> Span {
self.span
}

#[cfg(span_location_info)]
pub fn span_close(&self) -> Span {
self.span
}
Expand Down Expand Up @@ -483,7 +485,7 @@ impl fmt::Debug for Group {
let mut debug = fmt.debug_struct("Group");
debug.field("delimiter", &self.delimiter);
debug.field("stream", &self.stream);
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
debug.field("span", &self.span);
debug.finish()
}
Expand Down Expand Up @@ -601,7 +603,7 @@ impl fmt::Display for Ident {

impl fmt::Debug for Ident {
// Ident(proc_macro), Ident(r#union)
#[cfg(not(procmacro2_semver_exempt))]
#[cfg(not(span_location_info))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut debug = f.debug_tuple("Ident");
debug.field(&format_args!("{}", self));
Expand All @@ -612,7 +614,7 @@ impl fmt::Debug for Ident {
// sym: proc_macro,
// span: bytes(128..138)
// }
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut debug = f.debug_struct("Ident");
debug.field("sym", &format_args!("{}", self));
Expand Down Expand Up @@ -759,7 +761,7 @@ impl fmt::Debug for Literal {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut debug = fmt.debug_struct("Literal");
debug.field("lit", &format_args!("{}", self.text));
#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
debug.field("span", &self.span);
debug.finish()
}
Expand Down Expand Up @@ -788,7 +790,7 @@ fn token_stream(mut input: Cursor) -> PResult<TokenStream> {
Ok((input, TokenStream { inner: trees }))
}

#[cfg(not(procmacro2_semver_exempt))]
#[cfg(not(span_location_info))]
fn spanned<'a, T>(
input: Cursor<'a>,
f: fn(Cursor<'a>) -> PResult<'a, T>,
Expand All @@ -797,7 +799,7 @@ fn spanned<'a, T>(
Ok((a, ((b, ::Span::_new_stable(Span {})))))
}

#[cfg(procmacro2_semver_exempt)]
#[cfg(span_location_info)]
fn spanned<'a, T>(
input: Cursor<'a>,
f: fn(Cursor<'a>) -> PResult<'a, T>,
Expand Down
Loading