Skip to content

Commit a991861

Browse files
committed
Auto merge of rust-lang#115507 - cjgillot:relative-source-file, r=oli-obk
Use relative positions inside a SourceFile. This allows to remove the normalization of start positions for hashing, and simplify allocation of global address space. cc `@Zoxc`
2 parents f91c53d + 3edeac0 commit a991861

File tree

20 files changed

+262
-371
lines changed

20 files changed

+262
-371
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,9 @@ impl DebugContext {
8282
match tcx.sess.source_map().lookup_line(span.lo()) {
8383
Ok(SourceFileAndLine { sf: file, line }) => {
8484
let line_pos = file.lines(|lines| lines[line]);
85+
let col = file.relative_position(span.lo()) - line_pos;
8586

86-
(
87-
file,
88-
u64::try_from(line).unwrap() + 1,
89-
u64::from((span.lo() - line_pos).to_u32()) + 1,
90-
)
87+
(file, u64::try_from(line).unwrap() + 1, u64::from(col.to_u32()) + 1)
9188
}
9289
Err(file) => (file, 0, 0),
9390
}

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn make_mir_scope<'ll, 'tcx>(
6868
let file = cx.sess().source_map().lookup_source_file(mir.span.lo());
6969
debug_context.scopes[scope] = DebugScope {
7070
file_start_pos: file.start_pos,
71-
file_end_pos: file.end_pos,
71+
file_end_pos: file.end_position(),
7272
..debug_context.scopes[scope]
7373
};
7474
instantiated.insert(scope);
@@ -120,7 +120,7 @@ fn make_mir_scope<'ll, 'tcx>(
120120
dbg_scope,
121121
inlined_at: inlined_at.or(parent_scope.inlined_at),
122122
file_start_pos: loc.file.start_pos,
123-
file_end_pos: loc.file.end_pos,
123+
file_end_pos: loc.file.end_position(),
124124
};
125125
instantiated.insert(scope);
126126
}

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl CodegenCx<'_, '_> {
267267

268268
// Use 1-based indexing.
269269
let line = (line + 1) as u32;
270-
let col = (pos - line_pos).to_u32() + 1;
270+
let col = (file.relative_position(pos) - line_pos).to_u32() + 1;
271271

272272
(file, line, col)
273273
}

Diff for: compiler/rustc_metadata/src/rmeta/decoder.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -1501,11 +1501,12 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15011501

15021502
// We can't reuse an existing SourceFile, so allocate a new one
15031503
// containing the information we need.
1504+
let original_end_pos = source_file_to_import.end_position();
15041505
let rustc_span::SourceFile {
15051506
mut name,
15061507
src_hash,
1507-
start_pos,
1508-
end_pos,
1508+
start_pos: original_start_pos,
1509+
source_len,
15091510
lines,
15101511
multibyte_chars,
15111512
non_narrow_chars,
@@ -1547,35 +1548,32 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
15471548
// on `try_to_translate_virtual_to_real`).
15481549
try_to_translate_virtual_to_real(&mut name);
15491550

1550-
let source_length = (end_pos - start_pos).to_usize();
1551-
15521551
let local_version = sess.source_map().new_imported_source_file(
15531552
name,
15541553
src_hash,
15551554
name_hash,
1556-
source_length,
1555+
source_len.to_u32(),
15571556
self.cnum,
15581557
lines,
15591558
multibyte_chars,
15601559
non_narrow_chars,
15611560
normalized_pos,
1562-
start_pos,
15631561
source_file_index,
15641562
);
15651563
debug!(
15661564
"CrateMetaData::imported_source_files alloc \
1567-
source_file {:?} original (start_pos {:?} end_pos {:?}) \
1568-
translated (start_pos {:?} end_pos {:?})",
1565+
source_file {:?} original (start_pos {:?} source_len {:?}) \
1566+
translated (start_pos {:?} source_len {:?})",
15691567
local_version.name,
1570-
start_pos,
1571-
end_pos,
1568+
original_start_pos,
1569+
source_len,
15721570
local_version.start_pos,
1573-
local_version.end_pos
1571+
local_version.source_len
15741572
);
15751573

15761574
ImportedSourceFile {
1577-
original_start_pos: start_pos,
1578-
original_end_pos: end_pos,
1575+
original_start_pos,
1576+
original_end_pos,
15791577
translated_source_file: local_version,
15801578
}
15811579
})

Diff for: compiler/rustc_middle/src/query/on_disk_cache.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_span::hygiene::{
2222
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextData,
2323
};
2424
use rustc_span::source_map::{SourceMap, StableSourceFileId};
25-
use rustc_span::{BytePos, ExpnData, ExpnHash, Pos, SourceFile, Span};
25+
use rustc_span::{BytePos, ExpnData, ExpnHash, Pos, RelativeBytePos, SourceFile, Span};
2626
use rustc_span::{CachingSourceMapView, Symbol};
2727
use std::collections::hash_map::Entry;
2828
use std::io;
@@ -688,11 +688,12 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
688688

689689
let file_lo_index = SourceFileIndex::decode(decoder);
690690
let line_lo = usize::decode(decoder);
691-
let col_lo = BytePos::decode(decoder);
691+
let col_lo = RelativeBytePos::decode(decoder);
692692
let len = BytePos::decode(decoder);
693693

694694
let file_lo = decoder.file_index_to_file(file_lo_index);
695695
let lo = file_lo.lines(|lines| lines[line_lo - 1] + col_lo);
696+
let lo = file_lo.absolute_position(lo);
696697
let hi = lo + len;
697698

698699
Span::new(lo, hi, ctxt, parent)

Diff for: compiler/rustc_mir_transform/src/coverage/mod.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_middle::mir::{
2828
use rustc_middle::ty::TyCtxt;
2929
use rustc_span::def_id::DefId;
3030
use rustc_span::source_map::SourceMap;
31-
use rustc_span::{CharPos, ExpnKind, Pos, SourceFile, Span, Symbol};
31+
use rustc_span::{ExpnKind, SourceFile, Span, Symbol};
3232

3333
/// A simple error message wrapper for `coverage::Error`s.
3434
#[derive(Debug)]
@@ -314,8 +314,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
314314
};
315315
graphviz_data.add_bcb_coverage_span_with_counter(bcb, &covspan, &counter_kind);
316316

317-
let code_region =
318-
make_code_region(source_map, file_name, &self.source_file, span, body_span);
317+
let code_region = make_code_region(source_map, file_name, span, body_span);
319318

320319
inject_statement(
321320
self.mir_body,
@@ -510,40 +509,36 @@ fn inject_intermediate_expression(mir_body: &mut mir::Body<'_>, expression: Cove
510509
fn make_code_region(
511510
source_map: &SourceMap,
512511
file_name: Symbol,
513-
source_file: &Lrc<SourceFile>,
514512
span: Span,
515513
body_span: Span,
516514
) -> CodeRegion {
517515
debug!(
518-
"Called make_code_region(file_name={}, source_file={:?}, span={}, body_span={})",
516+
"Called make_code_region(file_name={}, span={}, body_span={})",
519517
file_name,
520-
source_file,
521518
source_map.span_to_diagnostic_string(span),
522519
source_map.span_to_diagnostic_string(body_span)
523520
);
524521

525-
let (start_line, mut start_col) = source_file.lookup_file_pos(span.lo());
526-
let (end_line, end_col) = if span.hi() == span.lo() {
527-
let (end_line, mut end_col) = (start_line, start_col);
522+
let (file, mut start_line, mut start_col, mut end_line, mut end_col) =
523+
source_map.span_to_location_info(span);
524+
if span.hi() == span.lo() {
528525
// Extend an empty span by one character so the region will be counted.
529-
let CharPos(char_pos) = start_col;
530526
if span.hi() == body_span.hi() {
531-
start_col = CharPos(char_pos.saturating_sub(1));
527+
start_col = start_col.saturating_sub(1);
532528
} else {
533-
end_col = CharPos(char_pos + 1);
529+
end_col = start_col + 1;
534530
}
535-
(end_line, end_col)
536-
} else {
537-
source_file.lookup_file_pos(span.hi())
538531
};
539-
let start_line = source_map.doctest_offset_line(&source_file.name, start_line);
540-
let end_line = source_map.doctest_offset_line(&source_file.name, end_line);
532+
if let Some(file) = file {
533+
start_line = source_map.doctest_offset_line(&file.name, start_line);
534+
end_line = source_map.doctest_offset_line(&file.name, end_line);
535+
}
541536
CodeRegion {
542537
file_name,
543538
start_line: start_line as u32,
544-
start_col: start_col.to_u32() + 1,
539+
start_col: start_col as u32,
545540
end_line: end_line as u32,
546-
end_col: end_col.to_u32() + 1,
541+
end_col: end_col as u32,
547542
}
548543
}
549544

Diff for: compiler/rustc_parse/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ fn maybe_source_file_to_parser(
132132
sess: &ParseSess,
133133
source_file: Lrc<SourceFile>,
134134
) -> Result<Parser<'_>, Vec<Diagnostic>> {
135-
let end_pos = source_file.end_pos;
135+
let end_pos = source_file.end_position();
136136
let stream = maybe_file_to_stream(sess, source_file, None)?;
137137
let mut parser = stream_to_parser(sess, stream, None);
138138
if parser.token == token::Eof {

Diff for: compiler/rustc_query_system/src/ich/impls_syntax.rs

+7-33
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ich::StableHashingContext;
55

66
use rustc_ast as ast;
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8-
use rustc_span::{BytePos, NormalizedPos, SourceFile};
8+
use rustc_span::SourceFile;
99
use std::assert_matches::assert_matches;
1010

1111
use smallvec::SmallVec;
@@ -67,8 +67,8 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
6767
src: _,
6868
ref src_hash,
6969
external_src: _,
70-
start_pos,
71-
end_pos: _,
70+
start_pos: _,
71+
source_len: _,
7272
lines: _,
7373
ref multibyte_chars,
7474
ref non_narrow_chars,
@@ -85,56 +85,30 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
8585
// We only hash the relative position within this source_file
8686
lines.len().hash_stable(hcx, hasher);
8787
for &line in lines.iter() {
88-
stable_byte_pos(line, start_pos).hash_stable(hcx, hasher);
88+
line.hash_stable(hcx, hasher);
8989
}
9090
});
9191

9292
// We only hash the relative position within this source_file
9393
multibyte_chars.len().hash_stable(hcx, hasher);
9494
for &char_pos in multibyte_chars.iter() {
95-
stable_multibyte_char(char_pos, start_pos).hash_stable(hcx, hasher);
95+
char_pos.hash_stable(hcx, hasher);
9696
}
9797

9898
non_narrow_chars.len().hash_stable(hcx, hasher);
9999
for &char_pos in non_narrow_chars.iter() {
100-
stable_non_narrow_char(char_pos, start_pos).hash_stable(hcx, hasher);
100+
char_pos.hash_stable(hcx, hasher);
101101
}
102102

103103
normalized_pos.len().hash_stable(hcx, hasher);
104104
for &char_pos in normalized_pos.iter() {
105-
stable_normalized_pos(char_pos, start_pos).hash_stable(hcx, hasher);
105+
char_pos.hash_stable(hcx, hasher);
106106
}
107107

108108
cnum.hash_stable(hcx, hasher);
109109
}
110110
}
111111

112-
fn stable_byte_pos(pos: BytePos, source_file_start: BytePos) -> u32 {
113-
pos.0 - source_file_start.0
114-
}
115-
116-
fn stable_multibyte_char(mbc: rustc_span::MultiByteChar, source_file_start: BytePos) -> (u32, u32) {
117-
let rustc_span::MultiByteChar { pos, bytes } = mbc;
118-
119-
(pos.0 - source_file_start.0, bytes as u32)
120-
}
121-
122-
fn stable_non_narrow_char(
123-
swc: rustc_span::NonNarrowChar,
124-
source_file_start: BytePos,
125-
) -> (u32, u32) {
126-
let pos = swc.pos();
127-
let width = swc.width();
128-
129-
(pos.0 - source_file_start.0, width as u32)
130-
}
131-
132-
fn stable_normalized_pos(np: NormalizedPos, source_file_start: BytePos) -> (u32, u32) {
133-
let NormalizedPos { pos, diff } = np;
134-
135-
(pos.0 - source_file_start.0, diff)
136-
}
137-
138112
impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
139113
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
140114
// Unfortunately we cannot exhaustively list fields here, since the

0 commit comments

Comments
 (0)