Skip to content

Commit

Permalink
Merge pull request #36 from mystor/stable_span
Browse files Browse the repository at this point in the history
Support meaningful spans in the stable version of proc-macro2
  • Loading branch information
alexcrichton authored Dec 31, 2017
2 parents 7b7157c + 3aa5bdf commit 92ab1b4
Show file tree
Hide file tree
Showing 7 changed files with 582 additions and 64 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ matrix:
script:
- cargo test
- cargo build --features unstable
- cargo doc --no-deps
- RUSTFLAGS='--cfg procmacro2_unstable' cargo test
- RUSTFLAGS='--cfg procmacro2_unstable' cargo build --features unstable
- RUSTFLAGS='--cfg procmacro2_unstable' cargo doc --no-deps
after_success:
- travis-cargo --only nightly doc-upload

script:
- cargo test
- RUSTFLAGS='--cfg procmacro2_unstable' cargo test
env:
global:
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ proc-macro2 = { version = "0.1", features = ["unstable"] }
```


## Unstable Features

`proc-macro2` supports exporting some methods from `proc_macro` which are
currently highly unstable, and may not be stabilized in the first pass of
`proc_macro` stabilizations. These features are not exported by default.

To export these features, the `procmacro2_unstable` config flag must be passed
to rustc. To pass this flag, run `cargo` with
`RUSTFLAGS='--cfg procmacro2_unstable' cargo build`.


# License

This project is licensed under either of
Expand Down
62 changes: 62 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,46 @@ impl TokenStream {
}
}

// Returned by reference, so we can't easily wrap it.
#[cfg(procmacro2_unstable)]
pub use imp::FileName;

#[cfg(procmacro2_unstable)]
#[derive(Clone, PartialEq, Eq)]
pub struct SourceFile(imp::SourceFile);

#[cfg(procmacro2_unstable)]
impl SourceFile {
/// Get the path to this source file as a string.
pub fn path(&self) -> &FileName {
self.0.path()
}

pub fn is_real(&self) -> bool {
self.0.is_real()
}
}

#[cfg(procmacro2_unstable)]
impl AsRef<FileName> for SourceFile {
fn as_ref(&self) -> &FileName {
self.0.path()
}
}

#[cfg(procmacro2_unstable)]
impl fmt::Debug for SourceFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

#[cfg(procmacro2_unstable)]
pub struct LineColumn {
pub line: usize,
pub column: usize,
}

#[derive(Copy, Clone)]
pub struct Span(imp::Span);

Expand All @@ -121,6 +161,28 @@ impl Span {
pub fn def_site() -> Span {
Span(imp::Span::def_site())
}

#[cfg(procmacro2_unstable)]
pub fn source_file(&self) -> SourceFile {
SourceFile(self.0.source_file())
}

#[cfg(procmacro2_unstable)]
pub fn start(&self) -> LineColumn {
let imp::LineColumn{ line, column } = self.0.start();
LineColumn { line, column }
}

#[cfg(procmacro2_unstable)]
pub fn end(&self) -> LineColumn {
let imp::LineColumn{ line, column } = self.0.end();
LineColumn { line, column }
}

#[cfg(procmacro2_unstable)]
pub fn join(&self, other: Span) -> Option<Span> {
self.0.join(other.0).map(Span)
}
}

#[derive(Clone, Debug)]
Expand Down
Loading

0 comments on commit 92ab1b4

Please sign in to comment.