File tree 2 files changed +31
-3
lines changed
rustc_codegen_ssa/src/back
2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change @@ -1821,9 +1821,15 @@ impl SharedEmitterMain {
1821
1821
let source = sess
1822
1822
. source_map ( )
1823
1823
. 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 ( ) ;
1827
1833
err. span_note ( spans, "instantiated into assembly here" ) ;
1828
1834
}
1829
1835
Original file line number Diff line number Diff line change @@ -1744,6 +1744,28 @@ impl SourceFile {
1744
1744
BytePos :: from_u32 ( pos. 0 - self . start_pos . 0 + diff)
1745
1745
}
1746
1746
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
+
1747
1769
/// Converts an absolute `BytePos` to a `CharPos` relative to the `SourceFile`.
1748
1770
pub fn bytepos_to_file_charpos ( & self , bpos : BytePos ) -> CharPos {
1749
1771
// The number of extra bytes due to multibyte chars in the `SourceFile`.
You can’t perform that action at this time.
0 commit comments