Skip to content

Commit 92ab1b4

Browse files
authored
Merge pull request #36 from mystor/stable_span
Support meaningful spans in the stable version of proc-macro2
2 parents 7b7157c + 3aa5bdf commit 92ab1b4

File tree

7 files changed

+582
-64
lines changed

7 files changed

+582
-64
lines changed

Diff for: .travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ matrix:
1212
script:
1313
- cargo test
1414
- cargo build --features unstable
15-
- cargo doc --no-deps
15+
- RUSTFLAGS='--cfg procmacro2_unstable' cargo test
16+
- RUSTFLAGS='--cfg procmacro2_unstable' cargo build --features unstable
17+
- RUSTFLAGS='--cfg procmacro2_unstable' cargo doc --no-deps
1618
after_success:
1719
- travis-cargo --only nightly doc-upload
1820

1921
script:
2022
- cargo test
23+
- RUSTFLAGS='--cfg procmacro2_unstable' cargo test
2124
env:
2225
global:
2326
- TRAVIS_CARGO_NIGHTLY_FEATURE=""

Diff for: README.md

+11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ proc-macro2 = { version = "0.1", features = ["unstable"] }
5959
```
6060

6161

62+
## Unstable Features
63+
64+
`proc-macro2` supports exporting some methods from `proc_macro` which are
65+
currently highly unstable, and may not be stabilized in the first pass of
66+
`proc_macro` stabilizations. These features are not exported by default.
67+
68+
To export these features, the `procmacro2_unstable` config flag must be passed
69+
to rustc. To pass this flag, run `cargo` with
70+
`RUSTFLAGS='--cfg procmacro2_unstable' cargo build`.
71+
72+
6273
# License
6374

6475
This project is licensed under either of

Diff for: src/lib.rs

+62
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,46 @@ impl TokenStream {
103103
}
104104
}
105105

106+
// Returned by reference, so we can't easily wrap it.
107+
#[cfg(procmacro2_unstable)]
108+
pub use imp::FileName;
109+
110+
#[cfg(procmacro2_unstable)]
111+
#[derive(Clone, PartialEq, Eq)]
112+
pub struct SourceFile(imp::SourceFile);
113+
114+
#[cfg(procmacro2_unstable)]
115+
impl SourceFile {
116+
/// Get the path to this source file as a string.
117+
pub fn path(&self) -> &FileName {
118+
self.0.path()
119+
}
120+
121+
pub fn is_real(&self) -> bool {
122+
self.0.is_real()
123+
}
124+
}
125+
126+
#[cfg(procmacro2_unstable)]
127+
impl AsRef<FileName> for SourceFile {
128+
fn as_ref(&self) -> &FileName {
129+
self.0.path()
130+
}
131+
}
132+
133+
#[cfg(procmacro2_unstable)]
134+
impl fmt::Debug for SourceFile {
135+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
136+
self.0.fmt(f)
137+
}
138+
}
139+
140+
#[cfg(procmacro2_unstable)]
141+
pub struct LineColumn {
142+
pub line: usize,
143+
pub column: usize,
144+
}
145+
106146
#[derive(Copy, Clone)]
107147
pub struct Span(imp::Span);
108148

@@ -121,6 +161,28 @@ impl Span {
121161
pub fn def_site() -> Span {
122162
Span(imp::Span::def_site())
123163
}
164+
165+
#[cfg(procmacro2_unstable)]
166+
pub fn source_file(&self) -> SourceFile {
167+
SourceFile(self.0.source_file())
168+
}
169+
170+
#[cfg(procmacro2_unstable)]
171+
pub fn start(&self) -> LineColumn {
172+
let imp::LineColumn{ line, column } = self.0.start();
173+
LineColumn { line, column }
174+
}
175+
176+
#[cfg(procmacro2_unstable)]
177+
pub fn end(&self) -> LineColumn {
178+
let imp::LineColumn{ line, column } = self.0.end();
179+
LineColumn { line, column }
180+
}
181+
182+
#[cfg(procmacro2_unstable)]
183+
pub fn join(&self, other: Span) -> Option<Span> {
184+
self.0.join(other.0).map(Span)
185+
}
124186
}
125187

126188
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)