File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed
rustc_codegen_ssa/src/back Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -1821,9 +1821,15 @@ impl SharedEmitterMain {
18211821 let source = sess
18221822 . source_map ( )
18231823 . new_source_file ( FileName :: inline_asm_source_code ( & buffer) , buffer) ;
1824- let source_span = Span :: with_root_ctxt ( source. start_pos , source. end_pos ) ;
1825- let spans: Vec < _ > =
1826- spans. iter ( ) . map ( |sp| source_span. from_inner ( * sp) ) . collect ( ) ;
1824+ let spans: Vec < _ > = spans
1825+ . iter ( )
1826+ . map ( |sp| {
1827+ Span :: with_root_ctxt (
1828+ source. normalized_byte_pos ( sp. start as u32 ) ,
1829+ source. normalized_byte_pos ( sp. end as u32 ) ,
1830+ )
1831+ } )
1832+ . collect ( ) ;
18271833 err. span_note ( spans, "instantiated into assembly here" ) ;
18281834 }
18291835
Original file line number Diff line number Diff line change @@ -1744,6 +1744,28 @@ impl SourceFile {
17441744 BytePos :: from_u32 ( pos. 0 - self . start_pos . 0 + diff)
17451745 }
17461746
1747+ /// Calculates a normalized byte position from a byte offset relative to the
1748+ /// start of the file.
1749+ ///
1750+ /// When we get an inline assembler error from LLVM during codegen, we
1751+ /// import the expanded assembly code as a new `SourceFile`, which can then
1752+ /// be used for error reporting with spans. However the byte offsets given
1753+ /// to us by LLVM are relative to the start of the original buffer, not the
1754+ /// normalized one. Hence we need to convert those offsets to the normalized
1755+ /// form when constructing spans.
1756+ pub fn normalized_byte_pos ( & self , offset : u32 ) -> BytePos {
1757+ let diff = match self
1758+ . normalized_pos
1759+ . binary_search_by ( |np| ( np. pos . 0 + np. diff ) . cmp ( & ( self . start_pos . 0 + offset) ) )
1760+ {
1761+ Ok ( i) => self . normalized_pos [ i] . diff ,
1762+ Err ( i) if i == 0 => 0 ,
1763+ Err ( i) => self . normalized_pos [ i - 1 ] . diff ,
1764+ } ;
1765+
1766+ BytePos :: from_u32 ( self . start_pos . 0 + offset - diff)
1767+ }
1768+
17471769 /// Converts an absolute `BytePos` to a `CharPos` relative to the `SourceFile`.
17481770 pub fn bytepos_to_file_charpos ( & self , bpos : BytePos ) -> CharPos {
17491771 // The number of extra bytes due to multibyte chars in the `SourceFile`.
You can’t perform that action at this time.
0 commit comments