Skip to content

Commit 89f892e

Browse files
committed
newtype over 1-based line number instead of usize
1 parent 1603a70 commit 89f892e

File tree

24 files changed

+91
-71
lines changed

24 files changed

+91
-71
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn expand_line(
3030
let topmost = cx.expansion_cause().unwrap_or(sp);
3131
let loc = cx.source_map().lookup_char_pos(topmost.lo());
3232

33-
base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
33+
base::MacEager::expr(cx.expr_u32(topmost, loc.line.to_u32()))
3434
}
3535

3636
/* column!(): expands to the current column number */

compiler/rustc_codegen_cranelift/src/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
349349
rustc_span::symbol::Symbol::intern(
350350
&caller.file.name.prefer_remapped().to_string_lossy(),
351351
),
352-
caller.line as u32,
352+
caller.line.to_u32(),
353353
caller.col_display as u32 + 1,
354354
));
355355
crate::constant::codegen_const_value(fx, const_loc, fx.tcx.caller_location_ty())

compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'tcx> DebugContext<'tcx> {
109109
let entry = self.dwarf.unit.get_mut(entry_id);
110110

111111
entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
112-
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(loc.line as u64));
112+
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(loc.line.to_usize() as u64));
113113
entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(loc.col.to_usize() as u64));
114114
}
115115

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ mod value_and_place;
7171
mod vtable;
7272

7373
mod prelude {
74-
pub(crate) use rustc_span::{FileNameDisplayPreference, Span};
74+
pub(crate) use rustc_span::{FileNameDisplayPreference, Pos, Span};
7575

7676
pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
7777
pub(crate) use rustc_middle::bug;

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
1919
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
2020
use rustc_middle::ty::{self, Instance, Ty, TypeVisitable};
2121
use rustc_span::source_map::Span;
22-
use rustc_span::{sym, Symbol};
22+
use rustc_span::{sym, Pos, Symbol};
2323
use rustc_symbol_mangling::typeid::typeid_for_fnabi;
2424
use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
2525
use rustc_target::abi::{self, HasDataLayout, WrappingRange};
@@ -1290,7 +1290,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12901290
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
12911291
let const_loc = tcx.const_caller_location((
12921292
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
1293-
caller.line as u32,
1293+
caller.line.to_u32(),
12941294
caller.col_display as u32 + 1,
12951295
));
12961296
OperandRef::from_const(bx, const_loc, bx.tcx().caller_location_ty())

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
270270
f,
271271
" at {}:{}:{}",
272272
sm.filename_for_diagnostics(&lo.file.name),
273-
lo.line,
273+
lo.line.to_usize(),
274274
lo.col.to_usize() + 1
275275
)?;
276276
}

compiler/rustc_const_eval/src/interpret/intrinsics/caller_location.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::lang_items::LangItem;
55
use rustc_middle::mir::TerminatorKind;
66
use rustc_middle::ty::layout::LayoutOf;
77
use rustc_middle::ty::subst::Subst;
8-
use rustc_span::{Span, Symbol};
8+
use rustc_span::{Pos, Span, Symbol};
99

1010
use crate::interpret::{
1111
intrinsics::{InterpCx, Machine},
@@ -118,7 +118,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
118118
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
119119
(
120120
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
121-
u32::try_from(caller.line).unwrap(),
121+
caller.line.to_u32(),
122122
u32::try_from(caller.col_display).unwrap().checked_add(1).unwrap(),
123123
)
124124
}

compiler/rustc_errors/src/emitter.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Destination::*;
1111

1212
use rustc_span::source_map::SourceMap;
13-
use rustc_span::{FileLines, SourceFile, Span};
13+
use rustc_span::{FileLines, Pos, SourceFile, Span};
1414

1515
use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Style, StyledString};
1616
use crate::styled_buffer::StyledBuffer;
@@ -1293,14 +1293,14 @@ impl EmitterWriter {
12931293
for primary_span in msp.primary_spans() {
12941294
if will_be_emitted(*primary_span) {
12951295
let hi = sm.lookup_char_pos(primary_span.hi());
1296-
max = (hi.line).max(max);
1296+
max = (hi.line.to_usize()).max(max);
12971297
}
12981298
}
12991299
if !self.short_message {
13001300
for span_label in msp.span_labels() {
13011301
if will_be_emitted(span_label.span) {
13021302
let hi = sm.lookup_char_pos(span_label.span.hi());
1303-
max = (hi.line).max(max);
1303+
max = (hi.line.to_usize()).max(max);
13041304
}
13051305
}
13061306
}
@@ -1498,7 +1498,7 @@ impl EmitterWriter {
14981498
&format!(
14991499
"{}:{}:{}",
15001500
sm.filename_for_diagnostics(&loc.file.name),
1501-
sm.doctest_offset_line(&loc.file.name, loc.line),
1501+
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
15021502
loc.col.0 + 1,
15031503
),
15041504
Style::LineAndColumn,
@@ -1512,7 +1512,7 @@ impl EmitterWriter {
15121512
&format!(
15131513
"{}:{}:{}: ",
15141514
sm.filename_for_diagnostics(&loc.file.name),
1515-
sm.doctest_offset_line(&loc.file.name, loc.line),
1515+
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
15161516
loc.col.0 + 1,
15171517
),
15181518
Style::LineAndColumn,
@@ -1795,7 +1795,7 @@ impl EmitterWriter {
17951795
&format!(
17961796
"{}:{}:{}",
17971797
sm.filename_for_diagnostics(&loc.file.name),
1798-
sm.doctest_offset_line(&loc.file.name, loc.line),
1798+
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
17991799
loc.col.0 + 1,
18001800
),
18011801
Style::LineAndColumn,
@@ -1832,27 +1832,27 @@ impl EmitterWriter {
18321832
if lines.clone().next().is_none() {
18331833
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
18341834
let line_end = sm.lookup_char_pos(parts[0].span.hi()).line;
1835-
for line in line_start..=line_end {
1835+
for line in line_start.to_usize()..=line_end.to_usize() {
18361836
buffer.puts(
1837-
row_num - 1 + line - line_start,
1837+
row_num - 1 + line - line_start.to_usize(),
18381838
0,
18391839
&self.maybe_anonymized(line),
18401840
Style::LineNumber,
18411841
);
18421842
buffer.puts(
1843-
row_num - 1 + line - line_start,
1843+
row_num - 1 + line - line_start.to_usize(),
18441844
max_line_num_len + 1,
18451845
"- ",
18461846
Style::Removal,
18471847
);
18481848
buffer.puts(
1849-
row_num - 1 + line - line_start,
1849+
row_num - 1 + line - line_start.to_usize(),
18501850
max_line_num_len + 3,
18511851
&normalize_whitespace(&*file_lines.file.get_line(line - 1).unwrap()),
18521852
Style::Removal,
18531853
);
18541854
}
1855-
row_num += line_end - line_start;
1855+
row_num += line_end.to_usize() - line_start.to_usize();
18561856
}
18571857
let mut unhighlighted_lines = Vec::new();
18581858
for (line_pos, (line, highlight_parts)) in lines.by_ref().zip(highlights).enumerate() {
@@ -1877,7 +1877,7 @@ impl EmitterWriter {
18771877
&Vec::new(),
18781878
p,
18791879
l,
1880-
line_start,
1880+
line_start.to_usize(),
18811881
show_code_change,
18821882
max_line_num_len,
18831883
&file_lines,
@@ -1902,7 +1902,7 @@ impl EmitterWriter {
19021902
&Vec::new(),
19031903
p,
19041904
l,
1905-
line_start,
1905+
line_start.to_usize(),
19061906
show_code_change,
19071907
max_line_num_len,
19081908
&file_lines,
@@ -1920,7 +1920,7 @@ impl EmitterWriter {
19201920
&Vec::new(),
19211921
p,
19221922
l,
1923-
line_start,
1923+
line_start.to_usize(),
19241924
show_code_change,
19251925
max_line_num_len,
19261926
&file_lines,
@@ -1936,7 +1936,7 @@ impl EmitterWriter {
19361936
highlight_parts,
19371937
line_pos,
19381938
line,
1939-
line_start,
1939+
line_start.to_usize(),
19401940
show_code_change,
19411941
max_line_num_len,
19421942
&file_lines,
@@ -2275,8 +2275,8 @@ impl FileWithAnnotatedLines {
22752275
if lo.line != hi.line {
22762276
let ml = MultilineAnnotation {
22772277
depth: 1,
2278-
line_start: lo.line,
2279-
line_end: hi.line,
2278+
line_start: lo.line.to_usize(),
2279+
line_end: hi.line.to_usize(),
22802280
start_col: lo.col_display,
22812281
end_col: hi.col_display,
22822282
is_primary: span_label.is_primary,
@@ -2298,7 +2298,7 @@ impl FileWithAnnotatedLines {
22982298
.map(|m| emitter.translate_message(m, args).to_string()),
22992299
annotation_type: AnnotationType::Singleline,
23002300
};
2301-
add_annotation_to_file(&mut output, lo.file, lo.line, ann);
2301+
add_annotation_to_file(&mut output, lo.file, lo.line.to_usize(), ann);
23022302
};
23032303
}
23042304
}

compiler/rustc_errors/src/json.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_lint_defs::Applicability;
2222
use rustc_data_structures::sync::Lrc;
2323
use rustc_error_messages::FluentArgs;
2424
use rustc_span::hygiene::ExpnData;
25-
use rustc_span::Span;
25+
use rustc_span::{Pos, Span};
2626
use std::io::{self, Write};
2727
use std::path::Path;
2828
use std::sync::{Arc, Mutex};
@@ -453,8 +453,8 @@ impl DiagnosticSpan {
453453
file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
454454
byte_start: start.file.original_relative_byte_pos(span.lo()).0,
455455
byte_end: start.file.original_relative_byte_pos(span.hi()).0,
456-
line_start: start.line,
457-
line_end: end.line,
456+
line_start: start.line.to_usize(),
457+
line_end: end.line.to_usize(),
458458
column_start: start.col.0 + 1,
459459
column_end: end.col.0 + 1,
460460
is_primary,

compiler/rustc_errors/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,14 @@ impl CodeSuggestion {
284284
count -= 1;
285285
}
286286
// push lines between the previous and current span (if any)
287-
for idx in prev_hi.line..(cur_lo.line - 1) {
287+
for idx in prev_hi.line.to_usize()..(cur_lo.line.to_usize() - 1) {
288288
if let Some(line) = sf.get_line(idx) {
289289
buf.push_str(line.as_ref());
290290
buf.push('\n');
291291
highlights.push(std::mem::take(&mut line_highlight));
292292
}
293293
}
294-
if let Some(cur_line) = sf.get_line(cur_lo.line - 1) {
294+
if let Some(cur_line) = sf.get_line(cur_lo.line.to_usize() - 1) {
295295
let end = match cur_line.char_indices().nth(cur_lo.col.to_usize()) {
296296
Some((i, _)) => i,
297297
None => cur_line.len(),
@@ -324,7 +324,7 @@ impl CodeSuggestion {
324324
acc += len as isize - (cur_hi.col.0 - cur_lo.col.0) as isize;
325325
}
326326
prev_hi = cur_hi;
327-
prev_line = sf.get_line(prev_hi.line - 1);
327+
prev_line = sf.get_line(prev_hi.line.to_usize() - 1);
328328
for line in part.snippet.split('\n').skip(1) {
329329
acc = 0;
330330
highlights.push(std::mem::take(&mut line_highlight));

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,12 +632,14 @@ impl server::Span for Rustc<'_, '_> {
632632

633633
fn start(&mut self, span: Self::Span) -> LineColumn {
634634
let loc = self.sess().source_map().lookup_char_pos(span.lo());
635-
LineColumn { line: loc.line, column: loc.col.to_usize() }
635+
//FIXME: LineColumn.column is 1-based, but loc.col is 0-based
636+
LineColumn { line: loc.line.to_usize(), column: loc.col.to_usize() }
636637
}
637638

638639
fn end(&mut self, span: Self::Span) -> LineColumn {
639640
let loc = self.sess().source_map().lookup_char_pos(span.hi());
640-
LineColumn { line: loc.line, column: loc.col.to_usize() }
641+
//FIXME: and here
642+
LineColumn { line: loc.line.to_usize(), column: loc.col.to_usize() }
641643
}
642644

643645
fn before(&mut self, span: Self::Span) -> Self::Span {

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16581658
format!(
16591659
" (opaque type at <{}:{}:{}>)",
16601660
sm.filename_for_diagnostics(&pos.file.name),
1661-
pos.line,
1661+
pos.line.to_usize(),
16621662
pos.col.to_usize() + 1,
16631663
)
16641664
}

compiler/rustc_middle/src/mir/spanview.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ where
165165
write!(
166166
w,
167167
r#"<div class="code" style="counter-reset: line {}"><span class="line">{}"#,
168-
start.line - 1,
168+
start.line.to_usize() - 1,
169169
indent_to_initial_start_col,
170170
)?;
171171
span_viewables.sort_unstable_by(|a, b| {
@@ -234,7 +234,13 @@ pub fn source_range_no_file<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String {
234234
let source_map = tcx.sess.source_map();
235235
let start = source_map.lookup_char_pos(span.lo());
236236
let end = source_map.lookup_char_pos(span.hi());
237-
format!("{}:{}-{}:{}", start.line, start.col.to_usize() + 1, end.line, end.col.to_usize() + 1)
237+
format!(
238+
"{}:{}-{}:{}",
239+
start.line.to_usize(),
240+
start.col.to_usize() + 1,
241+
end.line.to_usize(),
242+
end.col.to_usize() + 1
243+
)
238244
}
239245

240246
pub fn statement_kind_name(statement: &Statement<'_>) -> &'static str {

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ fn make_code_region(
522522
} else {
523523
source_file.lookup_file_pos(span.hi())
524524
};
525-
let start_line = source_map.doctest_offset_line(&source_file.name, start_line);
526-
let end_line = source_map.doctest_offset_line(&source_file.name, end_line);
525+
let start_line = source_map.doctest_offset_line(&source_file.name, start_line.to_usize());
526+
let end_line = source_map.doctest_offset_line(&source_file.name, end_line.to_usize());
527527
CodeRegion {
528528
file_name,
529529
start_line: start_line as u32,

compiler/rustc_save_analysis/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ impl<'tcx> SaveContext<'tcx> {
8383
file_name: start.file.name.prefer_remapped().to_string().into(),
8484
byte_start: span.lo().0,
8585
byte_end: span.hi().0,
86-
line_start: Row::new_one_indexed(start.line as u32),
87-
line_end: Row::new_one_indexed(end.line as u32),
86+
line_start: Row::new_one_indexed(start.line.to_u32()),
87+
line_end: Row::new_one_indexed(end.line.to_u32()),
8888
column_start: Column::new_one_indexed(start.col.0 as u32 + 1),
8989
column_end: Column::new_one_indexed(end.col.0 as u32 + 1),
9090
}

0 commit comments

Comments
 (0)