Skip to content

Support meaningful spans in the stable version of proc-macro2 #36

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

Merged
merged 11 commits into from
Dec 31, 2017
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