From 20e50bfab7718f2a7a44e805a8aae0e6eb063ad2 Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Sun, 16 Aug 2020 23:02:35 -0700 Subject: [PATCH 01/11] Start of marking remapping with an enum This commit is rather large, as simply moving from boolean to enum marking results in many places in the crate changing. This commit does not compile as the `Name` would have to be `pub`, which may result in even more changes required and is not part of the originally proposed cleanup. --- src/librustc_span/lib.rs | 108 +++++++++++++++++++++++++++----- src/librustc_span/source_map.rs | 21 +++---- 2 files changed, 99 insertions(+), 30 deletions(-) diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index c654dade2abd4..ae9e824ca30b8 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1074,15 +1074,94 @@ impl SourceFileHash { } } -/// A single source in the `SourceMap`. -#[derive(Clone)] -pub struct SourceFile { +#[derive(Clone, Debug, PartialEq, Eq)] +enum Name { /// The name of the file that the source came from. Source that doesn't /// originate from files has names between angle brackets by convention /// (e.g., ``). - pub name: FileName, - /// `true` if the `name` field above has been modified by `--remap-path-prefix`. - pub name_was_remapped: bool, + Normal(FileName), + /// FileName modified by `--remap-path-prefix`. + Remapped(FileName), +} + +impl Name { + pub fn new(filename: FileName, was_remapped: bool) -> Self { + if was_remapped { Name::Remapped(filename) } else { Name::Normal(filename) } + } + + fn is_real(&self) -> bool { + use Name::*; + match *self { + Normal(ref name) => name.is_real(), + Remapped(ref name) => name.is_real(), + } + } + + fn is_remapped(&self) -> bool { + use Name::*; + match *self { + Normal(_) => false, + Remapped(_) => true, + } + } + + fn name_and_remapped(&self) -> (FileName, bool) { + use Name::*; + let name = match *self { + Normal(ref name) => name, + Remapped(ref name) => name, + }; + (name.clone(), self.is_remapped()) + } +} + +/// Does a comparison with the filename, ignoring any remapping. +impl PartialEq for Name { + fn eq(&self, other: &FileName) -> bool { + use Name::*; + match *self { + Normal(ref name) => name == other, + Remapped(ref name) => name == other, + } + } +} + +impl std::fmt::Display for Name { + fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + use Name::*; + match *self { + Normal(ref name) => write!(fmt, "{}", name), + Remapped(ref name) => write!(fmt, "remapped {}", name), + } + } +} + +impl Encodable for Name { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + s.emit_enum("Name", |s| match self { + Name::Normal(name) => s.emit_enum_variant("Normal", 0, 1, |s| name.encode(s)), + Name::Remapped(name) => s.emit_enum_variant("Remapped", 1, 1, |s| name.encode(s)), + }) + } +} + +impl Decodable for Name { + fn decode(d: &mut D) -> Result { + let names = ["Normal", "Remapped"]; + d.read_enum("Name", |d| { + d.read_enum_variant(&names, |d, id| match id { + 0 => Ok(Name::Normal(FileName::decode(d)?)), + 1 => Ok(Name::Remapped(FileName::decode(d)?)), + _ => Err(d.error("Name enum variant could not be found")), + }) + }) + } +} + +/// A single source in the `SourceMap`. +#[derive(Clone)] +pub struct SourceFile { + pub name: Name, /// The unmapped path of the file that the source came from. /// Set to `None` if the `SourceFile` was imported from an external crate. pub unmapped_path: Option, @@ -1115,7 +1194,6 @@ impl Encodable for SourceFile { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_struct("SourceFile", 8, |s| { s.emit_struct_field("name", 0, |s| self.name.encode(s))?; - s.emit_struct_field("name_was_remapped", 1, |s| self.name_was_remapped.encode(s))?; s.emit_struct_field("src_hash", 2, |s| self.src_hash.encode(s))?; s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?; s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?; @@ -1184,9 +1262,7 @@ impl Encodable for SourceFile { impl Decodable for SourceFile { fn decode(d: &mut D) -> Result { d.read_struct("SourceFile", 8, |d| { - let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?; - let name_was_remapped: bool = - d.read_struct_field("name_was_remapped", 1, |d| Decodable::decode(d))?; + let name: Name = d.read_struct_field("name", 0, |d| Decodable::decode(d))?; let src_hash: SourceFileHash = d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?; let start_pos: BytePos = @@ -1230,7 +1306,6 @@ impl Decodable for SourceFile { let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?; Ok(SourceFile { name, - name_was_remapped, unmapped_path: None, start_pos, end_pos, @@ -1281,8 +1356,7 @@ impl SourceFile { analyze_source_file::analyze_source_file(&src[..], start_pos); SourceFile { - name, - name_was_remapped, + name: if name_was_remapped { Name::Remapped(name) } else { Name::Normal(name) }, unmapped_path: Some(unmapped_path), src: Some(Lrc::new(src)), src_hash, @@ -1700,18 +1774,18 @@ pub enum SpanSnippetError { IllFormedSpan(Span), DistinctSources(DistinctSources), MalformedForSourcemap(MalformedSourceMapPositions), - SourceNotAvailable { filename: FileName }, + SourceNotAvailable { filename: Name }, } #[derive(Clone, PartialEq, Eq, Debug)] pub struct DistinctSources { - pub begin: (FileName, BytePos), - pub end: (FileName, BytePos), + pub begin: (Name, BytePos), + pub end: (Name, BytePos), } #[derive(Clone, PartialEq, Eq, Debug)] pub struct MalformedSourceMapPositions { - pub name: FileName, + pub name: Name, pub source_len: usize, pub begin_pos: BytePos, pub end_pos: BytePos, diff --git a/src/librustc_span/source_map.rs b/src/librustc_span/source_map.rs index 7c656db22ed8c..18ee94b8d9758 100644 --- a/src/librustc_span/source_map.rs +++ b/src/librustc_span/source_map.rs @@ -125,9 +125,10 @@ pub struct StableSourceFileId(u128); // StableSourceFileId, perhaps built atop source_file.name_hash. impl StableSourceFileId { pub fn new(source_file: &SourceFile) -> StableSourceFileId { + let (source_file_name, source_file_was_remapped) = source_file.name.name_and_remapped(); StableSourceFileId::new_from_pieces( - &source_file.name, - source_file.name_was_remapped, + &source_file_name, + source_file_was_remapped, source_file.unmapped_path.as_ref(), ) } @@ -379,8 +380,7 @@ impl SourceMap { } let source_file = Lrc::new(SourceFile { - name: filename, - name_was_remapped, + name: Name::new(filename, name_was_remapped), unmapped_path: None, src: None, src_hash, @@ -540,7 +540,7 @@ impl SourceMap { } pub fn span_to_filename(&self, sp: Span) -> FileName { - self.lookup_char_pos(sp.lo()).file.name.clone() + self.lookup_char_pos(sp.lo()).file.name.name_and_remapped().0 } pub fn span_to_unmapped_path(&self, sp: Span) -> FileName { @@ -906,12 +906,7 @@ impl SourceMap { } pub fn get_source_file(&self, filename: &FileName) -> Option> { - for sf in self.files.borrow().source_files.iter() { - if *filename == sf.name { - return Some(sf.clone()); - } - } - None + self.files.borrow().source_files.iter().find(|sf| sf.name == *filename).cloned() } /// For a global `BytePos`, computes the local offset within the containing `SourceFile`. @@ -1052,8 +1047,8 @@ impl SourceMap { None } pub fn ensure_source_file_source_present(&self, source_file: Lrc) -> bool { - source_file.add_external_src(|| match source_file.name { - FileName::Real(ref name) => self.file_loader.read_file(name.local_path()).ok(), + source_file.add_external_src(|| match source_file.name.name_and_remapped() { + (FileName::Real(ref name), _) => self.file_loader.read_file(name.local_path()).ok(), _ => None, }) } From c146be02ccd35d04a2cc5a08d57beaff24b0bc55 Mon Sep 17 00:00:00 2001 From: Nicholas Baron Date: Sun, 16 Aug 2020 23:12:56 -0700 Subject: [PATCH 02/11] Update src/librustc_span/lib.rs Use the `new` function instead of an if --- src/librustc_span/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index ae9e824ca30b8..8303a7e1a959d 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1356,7 +1356,7 @@ impl SourceFile { analyze_source_file::analyze_source_file(&src[..], start_pos); SourceFile { - name: if name_was_remapped { Name::Remapped(name) } else { Name::Normal(name) }, + name: Name::new(name, name_was_remapped), unmapped_path: Some(unmapped_path), src: Some(Lrc::new(src)), src_hash, From c2dc7aaa46062a0970747e999908b813f92ae820 Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Mon, 17 Aug 2020 12:38:26 -0700 Subject: [PATCH 03/11] Moved the unmapped_name into the new struct This commit has the rustc_span crate compiling, but its dependents need to be updated. It seems that the was_remapped bool is unused. It will probably be removed before the branch lands. --- src/librustc_span/lib.rs | 116 ++++++++++++-------------------- src/librustc_span/source_map.rs | 25 ++++--- 2 files changed, 56 insertions(+), 85 deletions(-) diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index 8303a7e1a959d..e5f41e91a1386 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1074,97 +1074,71 @@ impl SourceFileHash { } } -#[derive(Clone, Debug, PartialEq, Eq)] -enum Name { +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd)] +pub struct SourceFileName { /// The name of the file that the source came from. Source that doesn't /// originate from files has names between angle brackets by convention /// (e.g., ``). - Normal(FileName), - /// FileName modified by `--remap-path-prefix`. - Remapped(FileName), + name: FileName, + /// `true` if the name field was modified by `--remap-path-prefix`. + was_remapped: bool, + /// The unmapped path of the file that the source came from. + /// Set to `None` if the `SourceFile` was imported from an external crate. + unmapped_name: Option, } -impl Name { - pub fn new(filename: FileName, was_remapped: bool) -> Self { - if was_remapped { Name::Remapped(filename) } else { Name::Normal(filename) } - } - - fn is_real(&self) -> bool { - use Name::*; - match *self { - Normal(ref name) => name.is_real(), - Remapped(ref name) => name.is_real(), - } - } - - fn is_remapped(&self) -> bool { - use Name::*; - match *self { - Normal(_) => false, - Remapped(_) => true, - } +impl SourceFileName { + pub fn new(name: FileName, was_remapped: bool, unmapped_name: Option) -> Self { + Self { name, was_remapped, unmapped_name } } - fn name_and_remapped(&self) -> (FileName, bool) { - use Name::*; - let name = match *self { - Normal(ref name) => name, - Remapped(ref name) => name, - }; - (name.clone(), self.is_remapped()) + pub fn name(&self) -> &FileName { + &self.name } -} -/// Does a comparison with the filename, ignoring any remapping. -impl PartialEq for Name { - fn eq(&self, other: &FileName) -> bool { - use Name::*; - match *self { - Normal(ref name) => name == other, - Remapped(ref name) => name == other, - } + pub fn unmapped_path(&self) -> &FileName { + self.unmapped_name.as_ref().unwrap_or(self.name()) } -} -impl std::fmt::Display for Name { - fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - use Name::*; - match *self { - Normal(ref name) => write!(fmt, "{}", name), - Remapped(ref name) => write!(fmt, "remapped {}", name), - } + fn is_real(&self) -> bool { + self.name.is_real() } } -impl Encodable for Name { +impl Encodable for SourceFileName { fn encode(&self, s: &mut S) -> Result<(), S::Error> { - s.emit_enum("Name", |s| match self { - Name::Normal(name) => s.emit_enum_variant("Normal", 0, 1, |s| name.encode(s)), - Name::Remapped(name) => s.emit_enum_variant("Remapped", 1, 1, |s| name.encode(s)), + s.emit_struct("SourceFileName", 3, |s| { + s.emit_struct_field("name", 0, |s| self.name.encode(s))?; + s.emit_struct_field("was_remapped", 1, |s| self.was_remapped.encode(s))?; + s.emit_struct_field("unmapped_name", 2, |s| self.unmapped_name.encode(s))?; + Ok(()) }) } } -impl Decodable for Name { +impl Decodable for SourceFileName { fn decode(d: &mut D) -> Result { - let names = ["Normal", "Remapped"]; - d.read_enum("Name", |d| { - d.read_enum_variant(&names, |d, id| match id { - 0 => Ok(Name::Normal(FileName::decode(d)?)), - 1 => Ok(Name::Remapped(FileName::decode(d)?)), - _ => Err(d.error("Name enum variant could not be found")), - }) + d.read_struct("SourceFileName", 3, |d| { + let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?; + let was_remapped: bool = + d.read_struct_field("was_remapped", 1, |d| Decodable::decode(d))?; + let unmapped_name: Option = + d.read_struct_field("unmapped_name", 2, |d| Decodable::decode(d))?; + Ok(Self::new(name, was_remapped, unmapped_name)) }) } } +impl std::fmt::Display for SourceFileName { + fn fmt(&self, _fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + todo!() + } +} + /// A single source in the `SourceMap`. #[derive(Clone)] pub struct SourceFile { - pub name: Name, - /// The unmapped path of the file that the source came from. - /// Set to `None` if the `SourceFile` was imported from an external crate. - pub unmapped_path: Option, + pub name: SourceFileName, /// The complete source code. pub src: Option>, /// The source code's hash. @@ -1262,7 +1236,7 @@ impl Encodable for SourceFile { impl Decodable for SourceFile { fn decode(d: &mut D) -> Result { d.read_struct("SourceFile", 8, |d| { - let name: Name = d.read_struct_field("name", 0, |d| Decodable::decode(d))?; + let name: SourceFileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?; let src_hash: SourceFileHash = d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?; let start_pos: BytePos = @@ -1306,7 +1280,6 @@ impl Decodable for SourceFile { let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?; Ok(SourceFile { name, - unmapped_path: None, start_pos, end_pos, src: None, @@ -1356,8 +1329,7 @@ impl SourceFile { analyze_source_file::analyze_source_file(&src[..], start_pos); SourceFile { - name: Name::new(name, name_was_remapped), - unmapped_path: Some(unmapped_path), + name: SourceFileName::new(name, name_was_remapped, Some(unmapped_path)), src: Some(Lrc::new(src)), src_hash, external_src: Lock::new(ExternalSource::Unneeded), @@ -1774,18 +1746,18 @@ pub enum SpanSnippetError { IllFormedSpan(Span), DistinctSources(DistinctSources), MalformedForSourcemap(MalformedSourceMapPositions), - SourceNotAvailable { filename: Name }, + SourceNotAvailable { filename: SourceFileName }, } #[derive(Clone, PartialEq, Eq, Debug)] pub struct DistinctSources { - pub begin: (Name, BytePos), - pub end: (Name, BytePos), + pub begin: (SourceFileName, BytePos), + pub end: (SourceFileName, BytePos), } #[derive(Clone, PartialEq, Eq, Debug)] pub struct MalformedSourceMapPositions { - pub name: Name, + pub name: SourceFileName, pub source_len: usize, pub begin_pos: BytePos, pub end_pos: BytePos, diff --git a/src/librustc_span/source_map.rs b/src/librustc_span/source_map.rs index 18ee94b8d9758..54936e0e0d579 100644 --- a/src/librustc_span/source_map.rs +++ b/src/librustc_span/source_map.rs @@ -125,11 +125,15 @@ pub struct StableSourceFileId(u128); // StableSourceFileId, perhaps built atop source_file.name_hash. impl StableSourceFileId { pub fn new(source_file: &SourceFile) -> StableSourceFileId { - let (source_file_name, source_file_was_remapped) = source_file.name.name_and_remapped(); + let SourceFileName { + name: source_file_name, + was_remapped: source_file_was_remapped, + unmapped_name: source_file_unmapped_path, + } = source_file.name.clone(); StableSourceFileId::new_from_pieces( &source_file_name, source_file_was_remapped, - source_file.unmapped_path.as_ref(), + source_file_unmapped_path.as_ref(), ) } @@ -380,8 +384,7 @@ impl SourceMap { } let source_file = Lrc::new(SourceFile { - name: Name::new(filename, name_was_remapped), - unmapped_path: None, + name: SourceFileName::new(filename, name_was_remapped, None), src: None, src_hash, external_src: Lock::new(ExternalSource::Foreign { @@ -540,15 +543,11 @@ impl SourceMap { } pub fn span_to_filename(&self, sp: Span) -> FileName { - self.lookup_char_pos(sp.lo()).file.name.name_and_remapped().0 + self.lookup_char_pos(sp.lo()).file.name.name().clone() } pub fn span_to_unmapped_path(&self, sp: Span) -> FileName { - self.lookup_char_pos(sp.lo()) - .file - .unmapped_path - .clone() - .expect("`SourceMap::span_to_unmapped_path` called for imported `SourceFile`?") + self.lookup_char_pos(sp.lo()).file.name.unmapped_path().clone() } pub fn is_multiline(&self, sp: Span) -> bool { @@ -906,7 +905,7 @@ impl SourceMap { } pub fn get_source_file(&self, filename: &FileName) -> Option> { - self.files.borrow().source_files.iter().find(|sf| sf.name == *filename).cloned() + self.files.borrow().source_files.iter().find(|sf| sf.name.name() == filename).cloned() } /// For a global `BytePos`, computes the local offset within the containing `SourceFile`. @@ -1047,8 +1046,8 @@ impl SourceMap { None } pub fn ensure_source_file_source_present(&self, source_file: Lrc) -> bool { - source_file.add_external_src(|| match source_file.name.name_and_remapped() { - (FileName::Real(ref name), _) => self.file_loader.read_file(name.local_path()).ok(), + source_file.add_external_src(|| match source_file.name.name() { + FileName::Real(ref name) => self.file_loader.read_file(name.local_path()).ok(), _ => None, }) } From 6ffac62b8863c0e244cdfc57d22adc657b5f08ac Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Mon, 17 Aug 2020 12:45:00 -0700 Subject: [PATCH 04/11] rustc_errors compiles `name.name()` seems a bit awkward, but it follows the semantics from the old code. --- src/librustc_errors/emitter.rs | 9 ++++++--- src/librustc_span/lib.rs | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 5a654e83aed8e..78eef64d3c490 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -1299,7 +1299,7 @@ impl EmitterWriter { &format!( "{}:{}:{}", loc.file.name, - sm.doctest_offset_line(&loc.file.name, loc.line), + sm.doctest_offset_line(&loc.file.name.name(), loc.line), loc.col.0 + 1, ), Style::LineAndColumn, @@ -1313,7 +1313,7 @@ impl EmitterWriter { &format!( "{}:{}:{}: ", loc.file.name, - sm.doctest_offset_line(&loc.file.name, loc.line), + sm.doctest_offset_line(&loc.file.name.name(), loc.line), loc.col.0 + 1, ), Style::LineAndColumn, @@ -1337,7 +1337,10 @@ impl EmitterWriter { format!( "{}:{}{}", annotated_file.file.name, - sm.doctest_offset_line(&annotated_file.file.name, first_line.line_index), + sm.doctest_offset_line( + &annotated_file.file.name.name(), + first_line.line_index + ), col ) } else { diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index e5f41e91a1386..6a6e1aa9ac01b 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1074,7 +1074,7 @@ impl SourceFileHash { } } -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct SourceFileName { /// The name of the file that the source came from. Source that doesn't /// originate from files has names between angle brackets by convention From ffdc0c4cb24eb75f3e5d6711198d0892da84c5cc Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Mon, 17 Aug 2020 14:39:15 -0700 Subject: [PATCH 05/11] Got rustc_expand and rustc_middle compiling --- src/librustc_expand/proc_macro_server.rs | 2 +- src/librustc_middle/ich/impls_syntax.rs | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/librustc_expand/proc_macro_server.rs b/src/librustc_expand/proc_macro_server.rs index 33e35cd0404fb..53168917cf7dd 100644 --- a/src/librustc_expand/proc_macro_server.rs +++ b/src/librustc_expand/proc_macro_server.rs @@ -610,7 +610,7 @@ impl server::SourceFile for Rustc<'_> { Lrc::ptr_eq(file1, file2) } fn path(&mut self, file: &Self::SourceFile) -> String { - match file.name { + match file.name.name() { FileName::Real(ref name) => name .local_path() .to_str() diff --git a/src/librustc_middle/ich/impls_syntax.rs b/src/librustc_middle/ich/impls_syntax.rs index e3d4655831b32..75fd9db78da63 100644 --- a/src/librustc_middle/ich/impls_syntax.rs +++ b/src/librustc_middle/ich/impls_syntax.rs @@ -56,8 +56,6 @@ impl<'a> HashStable> for SourceFile { let SourceFile { name: _, // We hash the smaller name_hash instead of this name_hash, - name_was_remapped, - unmapped_path: _, cnum, // Do not hash the source as it is not encoded src: _, @@ -72,7 +70,6 @@ impl<'a> HashStable> for SourceFile { } = *self; (name_hash as u64).hash_stable(hcx, hasher); - name_was_remapped.hash_stable(hcx, hasher); src_hash.hash_stable(hcx, hasher); From 876dc715f6479209a9b0846aeeea4dda33f97042 Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Mon, 17 Aug 2020 14:47:17 -0700 Subject: [PATCH 06/11] Made rustc_save_analysis compile. --- src/librustc_save_analysis/span_utils.rs | 4 ++-- src/librustc_span/lib.rs | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/librustc_save_analysis/span_utils.rs b/src/librustc_save_analysis/span_utils.rs index d5f992b0de05d..3a6b2cc8be14a 100644 --- a/src/librustc_save_analysis/span_utils.rs +++ b/src/librustc_save_analysis/span_utils.rs @@ -15,8 +15,8 @@ impl<'a> SpanUtils<'a> { } pub fn make_filename_string(&self, file: &SourceFile) -> String { - match &file.name { - FileName::Real(name) if !file.name_was_remapped => { + match file.name.name() { + FileName::Real(name) if !file.name.was_remapped() => { let path = name.local_path(); if path.is_absolute() { self.sess diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index 6a6e1aa9ac01b..3102d652e7369 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1103,6 +1103,10 @@ impl SourceFileName { fn is_real(&self) -> bool { self.name.is_real() } + + pub fn was_remapped(&self) -> bool { + self.was_remapped + } } impl Encodable for SourceFileName { From 07e12db3fa10ec625f0f93342a823b779293ea0b Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Mon, 17 Aug 2020 15:39:46 -0700 Subject: [PATCH 07/11] Got rustc_metadata compiling. --- src/librustc_metadata/rmeta/decoder.rs | 7 +++---- src/librustc_metadata/rmeta/encoder.rs | 6 +++--- src/librustc_span/lib.rs | 12 ++++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/librustc_metadata/rmeta/decoder.rs b/src/librustc_metadata/rmeta/decoder.rs index 10f5b671748da..b5dfc08ed1d60 100644 --- a/src/librustc_metadata/rmeta/decoder.rs +++ b/src/librustc_metadata/rmeta/decoder.rs @@ -1635,7 +1635,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { // containing the information we need. let rustc_span::SourceFile { mut name, - name_was_remapped, src_hash, start_pos, end_pos, @@ -1652,7 +1651,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { // on `try_to_translate_virtual_to_real`). // FIXME(eddyb) we could check `name_was_remapped` here, // but in practice it seems to be always `false`. - try_to_translate_virtual_to_real(&mut name); + try_to_translate_virtual_to_real(name.name_mut()); let source_length = (end_pos - start_pos).to_usize(); @@ -1675,8 +1674,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } let local_version = sess.source_map().new_imported_source_file( - name, - name_was_remapped, + name.name().clone(), + name.was_remapped(), src_hash, name_hash, source_length, diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index 78abf341e33a9..81453522f9ea9 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -445,11 +445,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { (!source_file.is_imported() || self.is_proc_macro) }) .map(|(_, source_file)| { - let mut adapted = match source_file.name { + let mut adapted = match source_file.name.name() { // This path of this SourceFile has been modified by // path-remapping, so we use it verbatim (and avoid // cloning the whole map in the process). - _ if source_file.name_was_remapped => source_file.clone(), + _ if source_file.name.was_remapped() => source_file.clone(), // Otherwise expand all paths to absolute paths because // any relative paths are potentially relative to a @@ -460,7 +460,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { adapted.name = Path::new(&working_dir).join(name).into(); adapted.name_hash = { let mut hasher: StableHasher = StableHasher::new(); - adapted.name.hash(&mut hasher); + adapted.name.name().hash(&mut hasher); hasher.finish::() }; Lrc::new(adapted) diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index 3102d652e7369..d56bdfd2f9539 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1096,6 +1096,10 @@ impl SourceFileName { &self.name } + pub fn name_mut(&mut self) -> &mut FileName { + &mut self.name + } + pub fn unmapped_path(&self) -> &FileName { self.unmapped_name.as_ref().unwrap_or(self.name()) } @@ -1109,6 +1113,14 @@ impl SourceFileName { } } +/// This conversion assumes that the path was not remapped (ie name == unmapped_name) +impl From for SourceFileName { + fn from(path: PathBuf) -> Self { + let name: FileName = path.into(); + Self::new(name.clone(), false, Some(name)) + } +} + impl Encodable for SourceFileName { fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_struct("SourceFileName", 3, |s| { From c1a59911c95b428af840cd4cc0283357fb6917cb Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Mon, 17 Aug 2020 17:26:26 -0700 Subject: [PATCH 08/11] Stage0 compiles without errors --- src/librustc_interface/passes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 403aea8b304eb..7a40b8eb71c7b 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -547,7 +547,7 @@ fn write_out_deps( .iter() .filter(|fmap| fmap.is_real_file()) .filter(|fmap| !fmap.is_imported()) - .map(|fmap| escape_dep_filename(&fmap.unmapped_path.as_ref().unwrap_or(&fmap.name))) + .map(|fmap| escape_dep_filename(&fmap.name.unmapped_path())) .collect(); if sess.binary_dep_depinfo() { From b41248ca44324defcdc9b3445c060a9322e77ecf Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Mon, 17 Aug 2020 18:01:12 -0700 Subject: [PATCH 09/11] Rebasing branch onto nightly. --- src/librustc_span/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index d56bdfd2f9539..810b73bb957e7 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1121,8 +1121,8 @@ impl From for SourceFileName { } } -impl Encodable for SourceFileName { - fn encode(&self, s: &mut S) -> Result<(), S::Error> { +impl Encodable for SourceFileName { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_struct("SourceFileName", 3, |s| { s.emit_struct_field("name", 0, |s| self.name.encode(s))?; s.emit_struct_field("was_remapped", 1, |s| self.was_remapped.encode(s))?; @@ -1132,8 +1132,8 @@ impl Encodable for SourceFileName { } } -impl Decodable for SourceFileName { - fn decode(d: &mut D) -> Result { +impl Decodable for SourceFileName { + fn decode(d: &mut D) -> Result { d.read_struct("SourceFileName", 3, |d| { let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?; let was_remapped: bool = From 2809eccf12ba036c66658cd61097415c1f7607ba Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Mon, 17 Aug 2020 18:44:35 -0700 Subject: [PATCH 10/11] Implemented Display for SourceFileName. --- src/librustc_span/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index 810b73bb957e7..d231170d54cd9 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1146,8 +1146,12 @@ impl Decodable for SourceFileName { } impl std::fmt::Display for SourceFileName { - fn fmt(&self, _fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - todo!() + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.was_remapped() { + write!(fmt, "SourceFileName({}, remapped from {})", self.name, self.unmapped_path()) + } else { + write!(fmt, "SourceFileName({})", self.name) + } } } From 8117c3261d7a79bbbeb85704b3fb3011c0cf8f0c Mon Sep 17 00:00:00 2001 From: Nicholas-Baron Date: Wed, 19 Aug 2020 20:10:48 -0700 Subject: [PATCH 11/11] Readded change lost during rebase --- src/librustc_mir/transform/instrument_coverage.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_mir/transform/instrument_coverage.rs b/src/librustc_mir/transform/instrument_coverage.rs index f60e6da714a65..a8096110f3786 100644 --- a/src/librustc_mir/transform/instrument_coverage.rs +++ b/src/librustc_mir/transform/instrument_coverage.rs @@ -214,7 +214,7 @@ fn make_code_region<'tcx>(tcx: TyCtxt<'tcx>, span: &Span) -> CodeRegion { ); end }; - match &start.file.name { + match &start.file.name.name() { FileName::Real(RealFileName::Named(path)) => CodeRegion { file_name: Symbol::intern(&path.to_string_lossy()), start_line: start.line as u32,