Skip to content

Commit 857acc5

Browse files
authored
Merge pull request dtolnay#151 from dtolnay/path
Remove proc_macro2::FileName in favor of PathBuf
2 parents 7c8bf4b + d981303 commit 857acc5

File tree

4 files changed

+19
-55
lines changed

4 files changed

+19
-55
lines changed

src/lib.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ use std::fmt;
5858
use std::hash::{Hash, Hasher};
5959
use std::iter::FromIterator;
6060
use std::marker;
61+
#[cfg(procmacro2_semver_exempt)]
62+
use std::path::PathBuf;
6163
use std::rc::Rc;
6264
use std::str::FromStr;
6365

@@ -211,10 +213,6 @@ impl fmt::Debug for LexError {
211213
}
212214
}
213215

214-
// Returned by reference, so we can't easily wrap it.
215-
#[cfg(procmacro2_semver_exempt)]
216-
pub use imp::FileName;
217-
218216
/// The source file of a given `Span`.
219217
///
220218
/// This type is semver exempt and not exposed by default.
@@ -247,7 +245,7 @@ impl SourceFile {
247245
/// may not actually be valid.
248246
///
249247
/// [`is_real`]: #method.is_real
250-
pub fn path(&self) -> &FileName {
248+
pub fn path(&self) -> PathBuf {
251249
self.inner.path()
252250
}
253251

@@ -258,13 +256,6 @@ impl SourceFile {
258256
}
259257
}
260258

261-
#[cfg(procmacro2_semver_exempt)]
262-
impl AsRef<FileName> for SourceFile {
263-
fn as_ref(&self) -> &FileName {
264-
self.inner.path()
265-
}
266-
}
267-
268259
#[cfg(procmacro2_semver_exempt)]
269260
impl fmt::Debug for SourceFile {
270261
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/stable.rs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use std::cell::RefCell;
66
use std::cmp;
77
use std::fmt;
88
use std::iter;
9+
#[cfg(procmacro2_semver_exempt)]
10+
use std::path::Path;
11+
use std::path::PathBuf;
912
use std::str::FromStr;
1013
use std::vec;
1114

@@ -190,29 +193,15 @@ impl IntoIterator for TokenStream {
190193
}
191194
}
192195

193-
#[derive(Clone, PartialEq, Eq, Debug)]
194-
pub struct FileName(String);
195-
196-
#[allow(dead_code)]
197-
pub fn file_name(s: String) -> FileName {
198-
FileName(s)
199-
}
200-
201-
impl fmt::Display for FileName {
202-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
203-
self.0.fmt(f)
204-
}
205-
}
206-
207196
#[derive(Clone, PartialEq, Eq)]
208197
pub struct SourceFile {
209-
name: FileName,
198+
path: PathBuf,
210199
}
211200

212201
impl SourceFile {
213202
/// Get the path to this source file as a string.
214-
pub fn path(&self) -> &FileName {
215-
&self.name
203+
pub fn path(&self) -> PathBuf {
204+
self.path.clone()
216205
}
217206

218207
pub fn is_real(&self) -> bool {
@@ -221,12 +210,6 @@ impl SourceFile {
221210
}
222211
}
223212

224-
impl AsRef<FileName> for SourceFile {
225-
fn as_ref(&self) -> &FileName {
226-
self.path()
227-
}
228-
}
229-
230213
impl fmt::Debug for SourceFile {
231214
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
232215
f.debug_struct("SourceFile")
@@ -382,7 +365,7 @@ impl Span {
382365
let cm = cm.borrow();
383366
let fi = cm.fileinfo(*self);
384367
SourceFile {
385-
name: FileName(fi.name.clone()),
368+
path: Path::new(&fi.name).to_owned(),
386369
}
387370
})
388371
}

src/unstable.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
use std::fmt;
44
use std::iter;
55
use std::panic::{self, PanicInfo};
6+
#[cfg(super_unstable)]
7+
use std::path::PathBuf;
68
use std::str::FromStr;
79

810
use proc_macro;
@@ -382,52 +384,40 @@ impl fmt::Debug for TokenTreeIter {
382384
}
383385
}
384386

385-
pub use stable::FileName;
386-
387-
// NOTE: We have to generate our own filename object here because we can't wrap
388-
// the one provided by proc_macro.
389387
#[derive(Clone, PartialEq, Eq)]
390388
#[cfg(super_unstable)]
391389
pub enum SourceFile {
392-
Nightly(proc_macro::SourceFile, FileName),
390+
Nightly(proc_macro::SourceFile),
393391
Stable(stable::SourceFile),
394392
}
395393

396394
#[cfg(super_unstable)]
397395
impl SourceFile {
398396
fn nightly(sf: proc_macro::SourceFile) -> Self {
399-
let filename = stable::file_name(sf.path().display().to_string());
400-
SourceFile::Nightly(sf, filename)
397+
SourceFile::Nightly(sf)
401398
}
402399

403400
/// Get the path to this source file as a string.
404-
pub fn path(&self) -> &FileName {
401+
pub fn path(&self) -> PathBuf {
405402
match self {
406-
SourceFile::Nightly(_, f) => f,
403+
SourceFile::Nightly(a) => a.path(),
407404
SourceFile::Stable(a) => a.path(),
408405
}
409406
}
410407

411408
pub fn is_real(&self) -> bool {
412409
match self {
413-
SourceFile::Nightly(a, _) => a.is_real(),
410+
SourceFile::Nightly(a) => a.is_real(),
414411
SourceFile::Stable(a) => a.is_real(),
415412
}
416413
}
417414
}
418415

419-
#[cfg(super_unstable)]
420-
impl AsRef<FileName> for SourceFile {
421-
fn as_ref(&self) -> &FileName {
422-
self.path()
423-
}
424-
}
425-
426416
#[cfg(super_unstable)]
427417
impl fmt::Debug for SourceFile {
428418
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
429419
match self {
430-
SourceFile::Nightly(a, _) => a.fmt(f),
420+
SourceFile::Nightly(a) => a.fmt(f),
431421
SourceFile::Stable(a) => a.fmt(f),
432422
}
433423
}

tests/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn default_span() {
203203
assert_eq!(end.line, 1);
204204
assert_eq!(end.column, 0);
205205
let source_file = Span::call_site().source_file();
206-
assert_eq!(source_file.path().to_string(), "<unspecified>");
206+
assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
207207
assert!(!source_file.is_real());
208208
}
209209

0 commit comments

Comments
 (0)