Skip to content

Commit c2dc7aa

Browse files
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.
1 parent c146be0 commit c2dc7aa

File tree

2 files changed

+56
-85
lines changed

2 files changed

+56
-85
lines changed

src/librustc_span/lib.rs

Lines changed: 44 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,97 +1074,71 @@ impl SourceFileHash {
10741074
}
10751075
}
10761076

1077-
#[derive(Clone, Debug, PartialEq, Eq)]
1078-
enum Name {
1077+
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd)]
1078+
pub struct SourceFileName {
10791079
/// The name of the file that the source came from. Source that doesn't
10801080
/// originate from files has names between angle brackets by convention
10811081
/// (e.g., `<anon>`).
1082-
Normal(FileName),
1083-
/// FileName modified by `--remap-path-prefix`.
1084-
Remapped(FileName),
1082+
name: FileName,
1083+
/// `true` if the name field was modified by `--remap-path-prefix`.
1084+
was_remapped: bool,
1085+
/// The unmapped path of the file that the source came from.
1086+
/// Set to `None` if the `SourceFile` was imported from an external crate.
1087+
unmapped_name: Option<FileName>,
10851088
}
10861089

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-
}
1090+
impl SourceFileName {
1091+
pub fn new(name: FileName, was_remapped: bool, unmapped_name: Option<FileName>) -> Self {
1092+
Self { name, was_remapped, unmapped_name }
11061093
}
11071094

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())
1095+
pub fn name(&self) -> &FileName {
1096+
&self.name
11151097
}
1116-
}
11171098

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-
}
1099+
pub fn unmapped_path(&self) -> &FileName {
1100+
self.unmapped_name.as_ref().unwrap_or(self.name())
11261101
}
1127-
}
11281102

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-
}
1103+
fn is_real(&self) -> bool {
1104+
self.name.is_real()
11361105
}
11371106
}
11381107

1139-
impl Encodable for Name {
1108+
impl Encodable for SourceFileName {
11401109
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)),
1110+
s.emit_struct("SourceFileName", 3, |s| {
1111+
s.emit_struct_field("name", 0, |s| self.name.encode(s))?;
1112+
s.emit_struct_field("was_remapped", 1, |s| self.was_remapped.encode(s))?;
1113+
s.emit_struct_field("unmapped_name", 2, |s| self.unmapped_name.encode(s))?;
1114+
Ok(())
11441115
})
11451116
}
11461117
}
11471118

1148-
impl Decodable for Name {
1119+
impl Decodable for SourceFileName {
11491120
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-
})
1121+
d.read_struct("SourceFileName", 3, |d| {
1122+
let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
1123+
let was_remapped: bool =
1124+
d.read_struct_field("was_remapped", 1, |d| Decodable::decode(d))?;
1125+
let unmapped_name: Option<FileName> =
1126+
d.read_struct_field("unmapped_name", 2, |d| Decodable::decode(d))?;
1127+
Ok(Self::new(name, was_remapped, unmapped_name))
11571128
})
11581129
}
11591130
}
11601131

1132+
impl std::fmt::Display for SourceFileName {
1133+
fn fmt(&self, _fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1134+
todo!()
1135+
}
1136+
}
1137+
11611138
/// A single source in the `SourceMap`.
11621139
#[derive(Clone)]
11631140
pub struct SourceFile {
1164-
pub name: Name,
1165-
/// The unmapped path of the file that the source came from.
1166-
/// Set to `None` if the `SourceFile` was imported from an external crate.
1167-
pub unmapped_path: Option<FileName>,
1141+
pub name: SourceFileName,
11681142
/// The complete source code.
11691143
pub src: Option<Lrc<String>>,
11701144
/// The source code's hash.
@@ -1262,7 +1236,7 @@ impl<S: Encoder> Encodable<S> for SourceFile {
12621236
impl<D: Decoder> Decodable<D> for SourceFile {
12631237
fn decode(d: &mut D) -> Result<SourceFile, D::Error> {
12641238
d.read_struct("SourceFile", 8, |d| {
1265-
let name: Name = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
1239+
let name: SourceFileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
12661240
let src_hash: SourceFileHash =
12671241
d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?;
12681242
let start_pos: BytePos =
@@ -1306,7 +1280,6 @@ impl<D: Decoder> Decodable<D> for SourceFile {
13061280
let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?;
13071281
Ok(SourceFile {
13081282
name,
1309-
unmapped_path: None,
13101283
start_pos,
13111284
end_pos,
13121285
src: None,
@@ -1356,8 +1329,7 @@ impl SourceFile {
13561329
analyze_source_file::analyze_source_file(&src[..], start_pos);
13571330

13581331
SourceFile {
1359-
name: Name::new(name, name_was_remapped),
1360-
unmapped_path: Some(unmapped_path),
1332+
name: SourceFileName::new(name, name_was_remapped, Some(unmapped_path)),
13611333
src: Some(Lrc::new(src)),
13621334
src_hash,
13631335
external_src: Lock::new(ExternalSource::Unneeded),
@@ -1774,18 +1746,18 @@ pub enum SpanSnippetError {
17741746
IllFormedSpan(Span),
17751747
DistinctSources(DistinctSources),
17761748
MalformedForSourcemap(MalformedSourceMapPositions),
1777-
SourceNotAvailable { filename: Name },
1749+
SourceNotAvailable { filename: SourceFileName },
17781750
}
17791751

17801752
#[derive(Clone, PartialEq, Eq, Debug)]
17811753
pub struct DistinctSources {
1782-
pub begin: (Name, BytePos),
1783-
pub end: (Name, BytePos),
1754+
pub begin: (SourceFileName, BytePos),
1755+
pub end: (SourceFileName, BytePos),
17841756
}
17851757

17861758
#[derive(Clone, PartialEq, Eq, Debug)]
17871759
pub struct MalformedSourceMapPositions {
1788-
pub name: Name,
1760+
pub name: SourceFileName,
17891761
pub source_len: usize,
17901762
pub begin_pos: BytePos,
17911763
pub end_pos: BytePos,

src/librustc_span/source_map.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,15 @@ 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();
128+
let SourceFileName {
129+
name: source_file_name,
130+
was_remapped: source_file_was_remapped,
131+
unmapped_name: source_file_unmapped_path,
132+
} = source_file.name.clone();
129133
StableSourceFileId::new_from_pieces(
130134
&source_file_name,
131135
source_file_was_remapped,
132-
source_file.unmapped_path.as_ref(),
136+
source_file_unmapped_path.as_ref(),
133137
)
134138
}
135139

@@ -380,8 +384,7 @@ impl SourceMap {
380384
}
381385

382386
let source_file = Lrc::new(SourceFile {
383-
name: Name::new(filename, name_was_remapped),
384-
unmapped_path: None,
387+
name: SourceFileName::new(filename, name_was_remapped, None),
385388
src: None,
386389
src_hash,
387390
external_src: Lock::new(ExternalSource::Foreign {
@@ -540,15 +543,11 @@ impl SourceMap {
540543
}
541544

542545
pub fn span_to_filename(&self, sp: Span) -> FileName {
543-
self.lookup_char_pos(sp.lo()).file.name.name_and_remapped().0
546+
self.lookup_char_pos(sp.lo()).file.name.name().clone()
544547
}
545548

546549
pub fn span_to_unmapped_path(&self, sp: Span) -> FileName {
547-
self.lookup_char_pos(sp.lo())
548-
.file
549-
.unmapped_path
550-
.clone()
551-
.expect("`SourceMap::span_to_unmapped_path` called for imported `SourceFile`?")
550+
self.lookup_char_pos(sp.lo()).file.name.unmapped_path().clone()
552551
}
553552

554553
pub fn is_multiline(&self, sp: Span) -> bool {
@@ -906,7 +905,7 @@ impl SourceMap {
906905
}
907906

908907
pub fn get_source_file(&self, filename: &FileName) -> Option<Lrc<SourceFile>> {
909-
self.files.borrow().source_files.iter().find(|sf| sf.name == *filename).cloned()
908+
self.files.borrow().source_files.iter().find(|sf| sf.name.name() == filename).cloned()
910909
}
911910

912911
/// For a global `BytePos`, computes the local offset within the containing `SourceFile`.
@@ -1047,8 +1046,8 @@ impl SourceMap {
10471046
None
10481047
}
10491048
pub fn ensure_source_file_source_present(&self, source_file: Lrc<SourceFile>) -> bool {
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(),
1049+
source_file.add_external_src(|| match source_file.name.name() {
1050+
FileName::Real(ref name) => self.file_loader.read_file(name.local_path()).ok(),
10521051
_ => None,
10531052
})
10541053
}

0 commit comments

Comments
 (0)