File tree 2 files changed +15
-7
lines changed
2 files changed +15
-7
lines changed Original file line number Diff line number Diff line change @@ -46,12 +46,20 @@ impl<'a> StringReader<'a> {
46
46
source_file : Lrc < rustc_span:: SourceFile > ,
47
47
override_span : Option < Span > ,
48
48
) -> Self {
49
- if source_file. src . is_none ( ) {
49
+ // Make sure external source is loaded first, before accessing it.
50
+ // While this can't show up during normal parsing, `retokenize` may
51
+ // be called with a source file from an external crate.
52
+ sess. source_map ( ) . ensure_source_file_source_present ( source_file. clone ( ) ) ;
53
+
54
+ // FIXME(eddyb) use `Lrc<str>` or similar to avoid cloning the `String`.
55
+ let src = if let Some ( src) = & source_file. src {
56
+ src. clone ( )
57
+ } else if let Some ( src) = source_file. external_src . borrow ( ) . get_source ( ) {
58
+ src. clone ( )
59
+ } else {
50
60
sess. span_diagnostic
51
61
. bug ( & format ! ( "cannot lex `source_file` without source: {}" , source_file. name) ) ;
52
- }
53
-
54
- let src = ( * source_file. src . as_ref ( ) . unwrap ( ) ) . clone ( ) ;
62
+ } ;
55
63
56
64
StringReader {
57
65
sess,
Original file line number Diff line number Diff line change @@ -856,7 +856,7 @@ pub enum ExternalSource {
856
856
#[ derive( PartialEq , Eq , Clone , Debug ) ]
857
857
pub enum ExternalSourceKind {
858
858
/// The external source has been loaded already.
859
- Present ( String ) ,
859
+ Present ( Lrc < String > ) ,
860
860
/// No attempt has been made to load the external source.
861
861
AbsentOk ,
862
862
/// A failed attempt has been made to load the external source.
@@ -872,7 +872,7 @@ impl ExternalSource {
872
872
}
873
873
}
874
874
875
- pub fn get_source ( & self ) -> Option < & str > {
875
+ pub fn get_source ( & self ) -> Option < & Lrc < String > > {
876
876
match self {
877
877
ExternalSource :: Foreign { kind : ExternalSourceKind :: Present ( ref src) , .. } => Some ( src) ,
878
878
_ => None ,
@@ -1138,7 +1138,7 @@ impl SourceFile {
1138
1138
hasher. write ( src. as_bytes ( ) ) ;
1139
1139
1140
1140
if hasher. finish :: < u128 > ( ) == self . src_hash {
1141
- * src_kind = ExternalSourceKind :: Present ( src) ;
1141
+ * src_kind = ExternalSourceKind :: Present ( Lrc :: new ( src) ) ;
1142
1142
return true ;
1143
1143
}
1144
1144
} else {
You can’t perform that action at this time.
0 commit comments