Skip to content

Commit 20e50bf

Browse files
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.
1 parent 5f6fcad commit 20e50bf

File tree

2 files changed

+99
-30
lines changed

2 files changed

+99
-30
lines changed

src/librustc_span/lib.rs

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,15 +1074,94 @@ impl SourceFileHash {
10741074
}
10751075
}
10761076

1077-
/// A single source in the `SourceMap`.
1078-
#[derive(Clone)]
1079-
pub struct SourceFile {
1077+
#[derive(Clone, Debug, PartialEq, Eq)]
1078+
enum Name {
10801079
/// The name of the file that the source came from. Source that doesn't
10811080
/// originate from files has names between angle brackets by convention
10821081
/// (e.g., `<anon>`).
1083-
pub name: FileName,
1084-
/// `true` if the `name` field above has been modified by `--remap-path-prefix`.
1085-
pub name_was_remapped: bool,
1082+
Normal(FileName),
1083+
/// FileName modified by `--remap-path-prefix`.
1084+
Remapped(FileName),
1085+
}
1086+
1087+
impl Name {
1088+
pub fn new(filename: FileName, was_remapped: bool) -> Self {
1089+
if was_remapped { Name::Remapped(filename) } else { Name::Normal(filename) }
1090+
}
1091+
1092+
fn is_real(&self) -> bool {
1093+
use Name::*;
1094+
match *self {
1095+
Normal(ref name) => name.is_real(),
1096+
Remapped(ref name) => name.is_real(),
1097+
}
1098+
}
1099+
1100+
fn is_remapped(&self) -> bool {
1101+
use Name::*;
1102+
match *self {
1103+
Normal(_) => false,
1104+
Remapped(_) => true,
1105+
}
1106+
}
1107+
1108+
fn name_and_remapped(&self) -> (FileName, bool) {
1109+
use Name::*;
1110+
let name = match *self {
1111+
Normal(ref name) => name,
1112+
Remapped(ref name) => name,
1113+
};
1114+
(name.clone(), self.is_remapped())
1115+
}
1116+
}
1117+
1118+
/// Does a comparison with the filename, ignoring any remapping.
1119+
impl PartialEq<FileName> for Name {
1120+
fn eq(&self, other: &FileName) -> bool {
1121+
use Name::*;
1122+
match *self {
1123+
Normal(ref name) => name == other,
1124+
Remapped(ref name) => name == other,
1125+
}
1126+
}
1127+
}
1128+
1129+
impl std::fmt::Display for Name {
1130+
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1131+
use Name::*;
1132+
match *self {
1133+
Normal(ref name) => write!(fmt, "{}", name),
1134+
Remapped(ref name) => write!(fmt, "remapped {}", name),
1135+
}
1136+
}
1137+
}
1138+
1139+
impl Encodable for Name {
1140+
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
1141+
s.emit_enum("Name", |s| match self {
1142+
Name::Normal(name) => s.emit_enum_variant("Normal", 0, 1, |s| name.encode(s)),
1143+
Name::Remapped(name) => s.emit_enum_variant("Remapped", 1, 1, |s| name.encode(s)),
1144+
})
1145+
}
1146+
}
1147+
1148+
impl Decodable for Name {
1149+
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
1150+
let names = ["Normal", "Remapped"];
1151+
d.read_enum("Name", |d| {
1152+
d.read_enum_variant(&names, |d, id| match id {
1153+
0 => Ok(Name::Normal(FileName::decode(d)?)),
1154+
1 => Ok(Name::Remapped(FileName::decode(d)?)),
1155+
_ => Err(d.error("Name enum variant could not be found")),
1156+
})
1157+
})
1158+
}
1159+
}
1160+
1161+
/// A single source in the `SourceMap`.
1162+
#[derive(Clone)]
1163+
pub struct SourceFile {
1164+
pub name: Name,
10861165
/// The unmapped path of the file that the source came from.
10871166
/// Set to `None` if the `SourceFile` was imported from an external crate.
10881167
pub unmapped_path: Option<FileName>,
@@ -1115,7 +1194,6 @@ impl<S: Encoder> Encodable<S> for SourceFile {
11151194
fn encode(&self, s: &mut S) -> Result<(), S::Error> {
11161195
s.emit_struct("SourceFile", 8, |s| {
11171196
s.emit_struct_field("name", 0, |s| self.name.encode(s))?;
1118-
s.emit_struct_field("name_was_remapped", 1, |s| self.name_was_remapped.encode(s))?;
11191197
s.emit_struct_field("src_hash", 2, |s| self.src_hash.encode(s))?;
11201198
s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?;
11211199
s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?;
@@ -1184,9 +1262,7 @@ impl<S: Encoder> Encodable<S> for SourceFile {
11841262
impl<D: Decoder> Decodable<D> for SourceFile {
11851263
fn decode(d: &mut D) -> Result<SourceFile, D::Error> {
11861264
d.read_struct("SourceFile", 8, |d| {
1187-
let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
1188-
let name_was_remapped: bool =
1189-
d.read_struct_field("name_was_remapped", 1, |d| Decodable::decode(d))?;
1265+
let name: Name = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
11901266
let src_hash: SourceFileHash =
11911267
d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?;
11921268
let start_pos: BytePos =
@@ -1230,7 +1306,6 @@ impl<D: Decoder> Decodable<D> for SourceFile {
12301306
let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?;
12311307
Ok(SourceFile {
12321308
name,
1233-
name_was_remapped,
12341309
unmapped_path: None,
12351310
start_pos,
12361311
end_pos,
@@ -1281,8 +1356,7 @@ impl SourceFile {
12811356
analyze_source_file::analyze_source_file(&src[..], start_pos);
12821357

12831358
SourceFile {
1284-
name,
1285-
name_was_remapped,
1359+
name: if name_was_remapped { Name::Remapped(name) } else { Name::Normal(name) },
12861360
unmapped_path: Some(unmapped_path),
12871361
src: Some(Lrc::new(src)),
12881362
src_hash,
@@ -1700,18 +1774,18 @@ pub enum SpanSnippetError {
17001774
IllFormedSpan(Span),
17011775
DistinctSources(DistinctSources),
17021776
MalformedForSourcemap(MalformedSourceMapPositions),
1703-
SourceNotAvailable { filename: FileName },
1777+
SourceNotAvailable { filename: Name },
17041778
}
17051779

17061780
#[derive(Clone, PartialEq, Eq, Debug)]
17071781
pub struct DistinctSources {
1708-
pub begin: (FileName, BytePos),
1709-
pub end: (FileName, BytePos),
1782+
pub begin: (Name, BytePos),
1783+
pub end: (Name, BytePos),
17101784
}
17111785

17121786
#[derive(Clone, PartialEq, Eq, Debug)]
17131787
pub struct MalformedSourceMapPositions {
1714-
pub name: FileName,
1788+
pub name: Name,
17151789
pub source_len: usize,
17161790
pub begin_pos: BytePos,
17171791
pub end_pos: BytePos,

src/librustc_span/source_map.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ pub struct StableSourceFileId(u128);
125125
// StableSourceFileId, perhaps built atop source_file.name_hash.
126126
impl StableSourceFileId {
127127
pub fn new(source_file: &SourceFile) -> StableSourceFileId {
128+
let (source_file_name, source_file_was_remapped) = source_file.name.name_and_remapped();
128129
StableSourceFileId::new_from_pieces(
129-
&source_file.name,
130-
source_file.name_was_remapped,
130+
&source_file_name,
131+
source_file_was_remapped,
131132
source_file.unmapped_path.as_ref(),
132133
)
133134
}
@@ -379,8 +380,7 @@ impl SourceMap {
379380
}
380381

381382
let source_file = Lrc::new(SourceFile {
382-
name: filename,
383-
name_was_remapped,
383+
name: Name::new(filename, name_was_remapped),
384384
unmapped_path: None,
385385
src: None,
386386
src_hash,
@@ -540,7 +540,7 @@ impl SourceMap {
540540
}
541541

542542
pub fn span_to_filename(&self, sp: Span) -> FileName {
543-
self.lookup_char_pos(sp.lo()).file.name.clone()
543+
self.lookup_char_pos(sp.lo()).file.name.name_and_remapped().0
544544
}
545545

546546
pub fn span_to_unmapped_path(&self, sp: Span) -> FileName {
@@ -906,12 +906,7 @@ impl SourceMap {
906906
}
907907

908908
pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
909-
for sf in self.files.borrow().source_files.iter() {
910-
if *filename == sf.name {
911-
return Some(sf.clone());
912-
}
913-
}
914-
None
909+
self.files.borrow().source_files.iter().find(|sf| sf.name == *filename).cloned()
915910
}
916911

917912
/// For a global `BytePos`, computes the local offset within the containing `SourceFile`.
@@ -1052,8 +1047,8 @@ impl SourceMap {
10521047
None
10531048
}
10541049
pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
1055-
source_file.add_external_src(|| match source_file.name {
1056-
FileName::Real(ref name) => self.file_loader.read_file(name.local_path()).ok(),
1050+
source_file.add_external_src(|| match source_file.name.name_and_remapped() {
1051+
(FileName::Real(ref name), _) => self.file_loader.read_file(name.local_path()).ok(),
10571052
_ => None,
10581053
})
10591054
}

0 commit comments

Comments
 (0)