@@ -267,10 +267,13 @@ impl SourceMap {
267267 self . files . borrow ( ) . stable_id_to_source_file . get ( & stable_id) . cloned ( )
268268 }
269269
270- fn allocate_address_space ( & self , size : usize ) -> Result < usize , OffsetOverflowError > {
271- let size = u32:: try_from ( size) . map_err ( |_| OffsetOverflowError ) ?;
270+ fn register_source_file (
271+ & self ,
272+ mut file : SourceFile ,
273+ ) -> Result < Lrc < SourceFile > , OffsetOverflowError > {
274+ let size = file. source_len . to_u32 ( ) ;
272275
273- loop {
276+ let start_pos = loop {
274277 let current = self . used_address_space . load ( Ordering :: Relaxed ) ;
275278 let next = current
276279 . checked_add ( size)
@@ -284,9 +287,20 @@ impl SourceMap {
284287 . compare_exchange ( current, next, Ordering :: Relaxed , Ordering :: Relaxed )
285288 . is_ok ( )
286289 {
287- return Ok ( usize:: try_from ( current) . unwrap ( ) ) ;
290+ break usize:: try_from ( current) . unwrap ( ) ;
288291 }
289- }
292+ } ;
293+
294+ file. start_pos = BytePos :: from_usize ( start_pos) ;
295+ let file_id = StableSourceFileId :: new ( & file) ;
296+
297+ let mut files = self . files . borrow_mut ( ) ;
298+
299+ let file = Lrc :: new ( file) ;
300+ files. source_files . push ( file. clone ( ) ) ;
301+ files. stable_id_to_source_file . insert ( file_id, file. clone ( ) ) ;
302+
303+ Ok ( file)
290304 }
291305
292306 /// Creates a new `SourceFile`.
@@ -310,29 +324,18 @@ impl SourceMap {
310324 let ( filename, _) = self . path_mapping . map_filename_prefix ( & filename) ;
311325
312326 let file_id = StableSourceFileId :: new_from_name ( & filename, LOCAL_CRATE ) ;
313-
314- let lrc_sf = match self . source_file_by_stable_id ( file_id) {
315- Some ( lrc_sf) => lrc_sf,
327+ match self . source_file_by_stable_id ( file_id) {
328+ Some ( lrc_sf) => Ok ( lrc_sf) ,
316329 None => {
317- let mut source_file = SourceFile :: new ( filename, src, self . hash_kind ) ;
330+ let source_file = SourceFile :: new ( filename, src, self . hash_kind ) ? ;
318331
319332 // Let's make sure the file_id we generated above actually matches
320333 // the ID we generate for the SourceFile we just created.
321334 debug_assert_eq ! ( StableSourceFileId :: new( & source_file) , file_id) ;
322335
323- let start_pos = self . allocate_address_space ( source_file. source_len . to_usize ( ) ) ?;
324- source_file. start_pos = BytePos :: from_usize ( start_pos) ;
325-
326- let mut files = self . files . borrow_mut ( ) ;
327-
328- let source_file = Lrc :: new ( source_file) ;
329- files. source_files . push ( source_file. clone ( ) ) ;
330- files. stable_id_to_source_file . insert ( file_id, source_file. clone ( ) ) ;
331-
332- source_file
336+ self . register_source_file ( source_file)
333337 }
334- } ;
335- Ok ( lrc_sf)
338+ }
336339 }
337340
338341 /// Allocates a new `SourceFile` representing a source file from an external
@@ -344,46 +347,36 @@ impl SourceMap {
344347 filename : FileName ,
345348 src_hash : SourceFileHash ,
346349 name_hash : Hash128 ,
347- source_len : usize ,
350+ source_len : u32 ,
348351 cnum : CrateNum ,
349352 file_local_lines : Lock < SourceFileLines > ,
350353 multibyte_chars : Vec < MultiByteChar > ,
351354 non_narrow_chars : Vec < NonNarrowChar > ,
352355 normalized_pos : Vec < NormalizedPos > ,
353356 metadata_index : u32 ,
354357 ) -> Lrc < SourceFile > {
355- let start_pos = self
356- . allocate_address_space ( source_len)
357- . expect ( "not enough address space for imported source file" ) ;
358+ let source_len = RelativeBytePos :: from_u32 ( source_len) ;
358359
359- let source_len = RelativeBytePos :: from_usize ( source_len) ;
360-
361- let source_file = Lrc :: new ( SourceFile {
360+ let source_file = SourceFile {
362361 name : filename,
363362 src : None ,
364363 src_hash,
365364 external_src : Lock :: new ( ExternalSource :: Foreign {
366365 kind : ExternalSourceKind :: AbsentOk ,
367366 metadata_index,
368367 } ) ,
369- start_pos : BytePos :: from_usize ( start_pos ) ,
368+ start_pos : BytePos ( 0 ) ,
370369 source_len,
371370 lines : file_local_lines,
372371 multibyte_chars,
373372 non_narrow_chars,
374373 normalized_pos,
375374 name_hash,
376375 cnum,
377- } ) ;
378-
379- let mut files = self . files . borrow_mut ( ) ;
380-
381- files. source_files . push ( source_file. clone ( ) ) ;
382- files
383- . stable_id_to_source_file
384- . insert ( StableSourceFileId :: new ( & source_file) , source_file. clone ( ) ) ;
376+ } ;
385377
386- source_file
378+ self . register_source_file ( source_file)
379+ . expect ( "not enough address space for imported source file" )
387380 }
388381
389382 /// If there is a doctest offset, applies it to the line.
0 commit comments