Repository: dtolnay/proc-macro2 (Rust)

Issue:
Remove proc_macro2::FileName
This type was removed upstream by https://github.com/rust-lang/rust/commit/e5e29d1a1976ff7d0b39ade8706ca0e3ddebe7a9 in favor of PathBuf.


Discussion:

Patch:
diff --git a/src/lib.rs b/src/lib.rs
index 784e456a..1204c6c2 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -58,6 +58,8 @@ use std::fmt;
 use std::hash::{Hash, Hasher};
 use std::iter::FromIterator;
 use std::marker;
+#[cfg(procmacro2_semver_exempt)]
+use std::path::PathBuf;
 use std::rc::Rc;
 use std::str::FromStr;
 
@@ -211,10 +213,6 @@ impl fmt::Debug for LexError {
     }
 }
 
-// Returned by reference, so we can't easily wrap it.
-#[cfg(procmacro2_semver_exempt)]
-pub use imp::FileName;
-
 /// The source file of a given `Span`.
 ///
 /// This type is semver exempt and not exposed by default.
@@ -247,7 +245,7 @@ impl SourceFile {
     /// may not actually be valid.
     ///
     /// [`is_real`]: #method.is_real
-    pub fn path(&self) -> &FileName {
+    pub fn path(&self) -> PathBuf {
         self.inner.path()
     }
 
@@ -258,13 +256,6 @@ impl SourceFile {
     }
 }
 
-#[cfg(procmacro2_semver_exempt)]
-impl AsRef<FileName> for SourceFile {
-    fn as_ref(&self) -> &FileName {
-        self.inner.path()
-    }
-}
-
 #[cfg(procmacro2_semver_exempt)]
 impl fmt::Debug for SourceFile {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
diff --git a/src/stable.rs b/src/stable.rs
index 0d8aac9d..baeed69a 100644
--- a/src/stable.rs
+++ b/src/stable.rs
@@ -6,6 +6,9 @@ use std::cell::RefCell;
 use std::cmp;
 use std::fmt;
 use std::iter;
+#[cfg(procmacro2_semver_exempt)]
+use std::path::Path;
+use std::path::PathBuf;
 use std::str::FromStr;
 use std::vec;
 
@@ -190,29 +193,15 @@ impl IntoIterator for TokenStream {
     }
 }
 
-#[derive(Clone, PartialEq, Eq, Debug)]
-pub struct FileName(String);
-
-#[allow(dead_code)]
-pub fn file_name(s: String) -> FileName {
-    FileName(s)
-}
-
-impl fmt::Display for FileName {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        self.0.fmt(f)
-    }
-}
-
 #[derive(Clone, PartialEq, Eq)]
 pub struct SourceFile {
-    name: FileName,
+    path: PathBuf,
 }
 
 impl SourceFile {
     /// Get the path to this source file as a string.
-    pub fn path(&self) -> &FileName {
-        &self.name
+    pub fn path(&self) -> PathBuf {
+        self.path.clone()
     }
 
     pub fn is_real(&self) -> bool {
@@ -221,12 +210,6 @@ impl SourceFile {
     }
 }
 
-impl AsRef<FileName> for SourceFile {
-    fn as_ref(&self) -> &FileName {
-        self.path()
-    }
-}
-
 impl fmt::Debug for SourceFile {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         f.debug_struct("SourceFile")
@@ -382,7 +365,7 @@ impl Span {
             let cm = cm.borrow();
             let fi = cm.fileinfo(*self);
             SourceFile {
-                name: FileName(fi.name.clone()),
+                path: Path::new(&fi.name).to_owned(),
             }
         })
     }
diff --git a/src/unstable.rs b/src/unstable.rs
index d15b9da6..c4cf11a8 100644
--- a/src/unstable.rs
+++ b/src/unstable.rs
@@ -3,6 +3,8 @@
 use std::fmt;
 use std::iter;
 use std::panic::{self, PanicInfo};
+#[cfg(super_unstable)]
+use std::path::PathBuf;
 use std::str::FromStr;
 
 use proc_macro;
@@ -382,52 +384,40 @@ impl fmt::Debug for TokenTreeIter {
     }
 }
 
-pub use stable::FileName;
-
-// NOTE: We have to generate our own filename object here because we can't wrap
-// the one provided by proc_macro.
 #[derive(Clone, PartialEq, Eq)]
 #[cfg(super_unstable)]
 pub enum SourceFile {
-    Nightly(proc_macro::SourceFile, FileName),
+    Nightly(proc_macro::SourceFile),
     Stable(stable::SourceFile),
 }
 
 #[cfg(super_unstable)]
 impl SourceFile {
     fn nightly(sf: proc_macro::SourceFile) -> Self {
-        let filename = stable::file_name(sf.path().display().to_string());
-        SourceFile::Nightly(sf, filename)
+        SourceFile::Nightly(sf)
     }
 
     /// Get the path to this source file as a string.
-    pub fn path(&self) -> &FileName {
+    pub fn path(&self) -> PathBuf {
         match self {
-            SourceFile::Nightly(_, f) => f,
+            SourceFile::Nightly(a) => a.path(),
             SourceFile::Stable(a) => a.path(),
         }
     }
 
     pub fn is_real(&self) -> bool {
         match self {
-            SourceFile::Nightly(a, _) => a.is_real(),
+            SourceFile::Nightly(a) => a.is_real(),
             SourceFile::Stable(a) => a.is_real(),
         }
     }
 }
 
-#[cfg(super_unstable)]
-impl AsRef<FileName> for SourceFile {
-    fn as_ref(&self) -> &FileName {
-        self.path()
-    }
-}
-
 #[cfg(super_unstable)]
 impl fmt::Debug for SourceFile {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match self {
-            SourceFile::Nightly(a, _) => a.fmt(f),
+            SourceFile::Nightly(a) => a.fmt(f),
             SourceFile::Stable(a) => a.fmt(f),
         }
     }


Test patch:
diff --git a/tests/test.rs b/tests/test.rs
index 5d2fb854..9d37cf37 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -203,7 +203,7 @@ fn default_span() {
     assert_eq!(end.line, 1);
     assert_eq!(end.column, 0);
     let source_file = Span::call_site().source_file();
-    assert_eq!(source_file.path().to_string(), "<unspecified>");
+    assert_eq!(source_file.path().to_string_lossy(), "<unspecified>");
     assert!(!source_file.is_real());
 }