@@ -1074,15 +1074,94 @@ impl SourceFileHash {
1074
1074
}
1075
1075
}
1076
1076
1077
- /// A single source in the `SourceMap`.
1078
- #[ derive( Clone ) ]
1079
- pub struct SourceFile {
1077
+ #[ derive( Clone , Debug , PartialEq , Eq ) ]
1078
+ enum Name {
1080
1079
/// The name of the file that the source came from. Source that doesn't
1081
1080
/// originate from files has names between angle brackets by convention
1082
1081
/// (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 ,
1086
1165
/// The unmapped path of the file that the source came from.
1087
1166
/// Set to `None` if the `SourceFile` was imported from an external crate.
1088
1167
pub unmapped_path : Option < FileName > ,
@@ -1115,7 +1194,6 @@ impl<S: Encoder> Encodable<S> for SourceFile {
1115
1194
fn encode ( & self , s : & mut S ) -> Result < ( ) , S :: Error > {
1116
1195
s. emit_struct ( "SourceFile" , 8 , |s| {
1117
1196
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) ) ?;
1119
1197
s. emit_struct_field ( "src_hash" , 2 , |s| self . src_hash . encode ( s) ) ?;
1120
1198
s. emit_struct_field ( "start_pos" , 3 , |s| self . start_pos . encode ( s) ) ?;
1121
1199
s. emit_struct_field ( "end_pos" , 4 , |s| self . end_pos . encode ( s) ) ?;
@@ -1184,9 +1262,7 @@ impl<S: Encoder> Encodable<S> for SourceFile {
1184
1262
impl < D : Decoder > Decodable < D > for SourceFile {
1185
1263
fn decode ( d : & mut D ) -> Result < SourceFile , D :: Error > {
1186
1264
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) ) ?;
1190
1266
let src_hash: SourceFileHash =
1191
1267
d. read_struct_field ( "src_hash" , 2 , |d| Decodable :: decode ( d) ) ?;
1192
1268
let start_pos: BytePos =
@@ -1230,7 +1306,6 @@ impl<D: Decoder> Decodable<D> for SourceFile {
1230
1306
let cnum: CrateNum = d. read_struct_field ( "cnum" , 10 , |d| Decodable :: decode ( d) ) ?;
1231
1307
Ok ( SourceFile {
1232
1308
name,
1233
- name_was_remapped,
1234
1309
unmapped_path : None ,
1235
1310
start_pos,
1236
1311
end_pos,
@@ -1281,8 +1356,7 @@ impl SourceFile {
1281
1356
analyze_source_file:: analyze_source_file ( & src[ ..] , start_pos) ;
1282
1357
1283
1358
SourceFile {
1284
- name,
1285
- name_was_remapped,
1359
+ name : if name_was_remapped { Name :: Remapped ( name) } else { Name :: Normal ( name) } ,
1286
1360
unmapped_path : Some ( unmapped_path) ,
1287
1361
src : Some ( Lrc :: new ( src) ) ,
1288
1362
src_hash,
@@ -1700,18 +1774,18 @@ pub enum SpanSnippetError {
1700
1774
IllFormedSpan ( Span ) ,
1701
1775
DistinctSources ( DistinctSources ) ,
1702
1776
MalformedForSourcemap ( MalformedSourceMapPositions ) ,
1703
- SourceNotAvailable { filename : FileName } ,
1777
+ SourceNotAvailable { filename : Name } ,
1704
1778
}
1705
1779
1706
1780
#[ derive( Clone , PartialEq , Eq , Debug ) ]
1707
1781
pub struct DistinctSources {
1708
- pub begin : ( FileName , BytePos ) ,
1709
- pub end : ( FileName , BytePos ) ,
1782
+ pub begin : ( Name , BytePos ) ,
1783
+ pub end : ( Name , BytePos ) ,
1710
1784
}
1711
1785
1712
1786
#[ derive( Clone , PartialEq , Eq , Debug ) ]
1713
1787
pub struct MalformedSourceMapPositions {
1714
- pub name : FileName ,
1788
+ pub name : Name ,
1715
1789
pub source_len : usize ,
1716
1790
pub begin_pos : BytePos ,
1717
1791
pub end_pos : BytePos ,
0 commit comments