Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc_span: encode 1-based line number as it's own type, not usize #100234

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_ast/src/util/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme
let pos_in_file = start_bpos + BytePos(pos as u32);
let line_begin_in_file = source_file.line_begin_pos(pos_in_file);
let line_begin_pos = (line_begin_in_file - start_bpos).to_usize();
let col = CharPos(text[line_begin_pos..pos].chars().count());
// FIXME: maybe simpler way?
let col = CharPos::from_usize(text[line_begin_pos..pos].chars().count());

let lines = split_block_comment_into_lines(token_text, col);
comments.push(Comment { style, lines, pos: pos_in_file })
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/source_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn expand_line(
let topmost = cx.expansion_cause().unwrap_or(sp);
let loc = cx.source_map().lookup_char_pos(topmost.lo());

base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
base::MacEager::expr(cx.expr_u32(topmost, loc.line.to_u32()))
}

/* column!(): expands to the current column number */
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> {
rustc_span::symbol::Symbol::intern(
&caller.file.name.prefer_remapped().to_string_lossy(),
),
caller.line as u32,
caller.line.to_u32(),
caller.col_display as u32 + 1,
));
crate::constant::codegen_const_value(fx, const_loc, fx.tcx.caller_location_ty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'tcx> DebugContext<'tcx> {
let entry = self.dwarf.unit.get_mut(entry_id);

entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id)));
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(loc.line as u64));
entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(loc.line.to_usize() as u64));
entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(loc.col.to_usize() as u64));
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod value_and_place;
mod vtable;

mod prelude {
pub(crate) use rustc_span::{FileNameDisplayPreference, Span};
pub(crate) use rustc_span::{FileNameDisplayPreference, Pos, Span};

pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
pub(crate) use rustc_middle::bug;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
use rustc_middle::ty::{self, Instance, Ty, TypeVisitable};
use rustc_span::source_map::Span;
use rustc_span::{sym, Symbol};
use rustc_span::{sym, Pos, Symbol};
use rustc_symbol_mangling::typeid::typeid_for_fnabi;
use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode};
use rustc_target::abi::{self, HasDataLayout, WrappingRange};
Expand Down Expand Up @@ -1290,7 +1290,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let caller = tcx.sess.source_map().lookup_char_pos(topmost.lo());
let const_loc = tcx.const_caller_location((
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
caller.line as u32,
caller.line.to_u32(),
caller.col_display as u32 + 1,
));
OperandRef::from_const(bx, const_loc, bx.tcx().caller_location_ty())
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl<'tcx> fmt::Display for FrameInfo<'tcx> {
f,
" at {}:{}:{}",
sm.filename_for_diagnostics(&lo.file.name),
lo.line,
lo.line.to_usize(),
lo.col.to_usize() + 1
)?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hir::lang_items::LangItem;
use rustc_middle::mir::TerminatorKind;
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::subst::Subst;
use rustc_span::{Span, Symbol};
use rustc_span::{Pos, Span, Symbol};

use crate::interpret::{
intrinsics::{InterpCx, Machine},
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
(
Symbol::intern(&caller.file.name.prefer_remapped().to_string_lossy()),
u32::try_from(caller.line).unwrap(),
caller.line.to_u32(),
u32::try_from(caller.col_display).unwrap().checked_add(1).unwrap(),
)
}
Expand Down
42 changes: 21 additions & 21 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Destination::*;

use rustc_span::source_map::SourceMap;
use rustc_span::{FileLines, SourceFile, Span};
use rustc_span::{FileLines, Pos, SourceFile, Span};

use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Style, StyledString};
use crate::styled_buffer::StyledBuffer;
Expand Down Expand Up @@ -1293,14 +1293,14 @@ impl EmitterWriter {
for primary_span in msp.primary_spans() {
if will_be_emitted(*primary_span) {
let hi = sm.lookup_char_pos(primary_span.hi());
max = (hi.line).max(max);
max = (hi.line.to_usize()).max(max);
}
}
if !self.short_message {
for span_label in msp.span_labels() {
if will_be_emitted(span_label.span) {
let hi = sm.lookup_char_pos(span_label.span.hi());
max = (hi.line).max(max);
max = (hi.line.to_usize()).max(max);
}
}
}
Expand Down Expand Up @@ -1498,8 +1498,8 @@ impl EmitterWriter {
&format!(
"{}:{}:{}",
sm.filename_for_diagnostics(&loc.file.name),
sm.doctest_offset_line(&loc.file.name, loc.line),
loc.col.0 + 1,
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
loc.col.to_usize() + 1,
),
Style::LineAndColumn,
);
Expand All @@ -1512,8 +1512,8 @@ impl EmitterWriter {
&format!(
"{}:{}:{}: ",
sm.filename_for_diagnostics(&loc.file.name),
sm.doctest_offset_line(&loc.file.name, loc.line),
loc.col.0 + 1,
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
loc.col.to_usize() + 1,
),
Style::LineAndColumn,
);
Expand Down Expand Up @@ -1795,8 +1795,8 @@ impl EmitterWriter {
&format!(
"{}:{}:{}",
sm.filename_for_diagnostics(&loc.file.name),
sm.doctest_offset_line(&loc.file.name, loc.line),
loc.col.0 + 1,
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
loc.col.to_usize() + 1,
),
Style::LineAndColumn,
);
Expand Down Expand Up @@ -1832,27 +1832,27 @@ impl EmitterWriter {
if lines.clone().next().is_none() {
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
let line_end = sm.lookup_char_pos(parts[0].span.hi()).line;
for line in line_start..=line_end {
for line in line_start.to_usize()..=line_end.to_usize() {
buffer.puts(
row_num - 1 + line - line_start,
row_num - 1 + line - line_start.to_usize(),
0,
&self.maybe_anonymized(line),
Style::LineNumber,
);
buffer.puts(
row_num - 1 + line - line_start,
row_num - 1 + line - line_start.to_usize(),
max_line_num_len + 1,
"- ",
Style::Removal,
);
buffer.puts(
row_num - 1 + line - line_start,
row_num - 1 + line - line_start.to_usize(),
max_line_num_len + 3,
&normalize_whitespace(&*file_lines.file.get_line(line - 1).unwrap()),
Style::Removal,
);
}
row_num += line_end - line_start;
row_num += line_end.to_usize() - line_start.to_usize();
}
let mut unhighlighted_lines = Vec::new();
for (line_pos, (line, highlight_parts)) in lines.by_ref().zip(highlights).enumerate() {
Expand All @@ -1877,7 +1877,7 @@ impl EmitterWriter {
&Vec::new(),
p,
l,
line_start,
line_start.to_usize(),
show_code_change,
max_line_num_len,
&file_lines,
Expand All @@ -1902,7 +1902,7 @@ impl EmitterWriter {
&Vec::new(),
p,
l,
line_start,
line_start.to_usize(),
show_code_change,
max_line_num_len,
&file_lines,
Expand All @@ -1920,7 +1920,7 @@ impl EmitterWriter {
&Vec::new(),
p,
l,
line_start,
line_start.to_usize(),
show_code_change,
max_line_num_len,
&file_lines,
Expand All @@ -1936,7 +1936,7 @@ impl EmitterWriter {
highlight_parts,
line_pos,
line,
line_start,
line_start.to_usize(),
show_code_change,
max_line_num_len,
&file_lines,
Expand Down Expand Up @@ -2275,8 +2275,8 @@ impl FileWithAnnotatedLines {
if lo.line != hi.line {
let ml = MultilineAnnotation {
depth: 1,
line_start: lo.line,
line_end: hi.line,
line_start: lo.line.to_usize(),
line_end: hi.line.to_usize(),
start_col: lo.col_display,
end_col: hi.col_display,
is_primary: span_label.is_primary,
Expand All @@ -2298,7 +2298,7 @@ impl FileWithAnnotatedLines {
.map(|m| emitter.translate_message(m, args).to_string()),
annotation_type: AnnotationType::Singleline,
};
add_annotation_to_file(&mut output, lo.file, lo.line, ann);
add_annotation_to_file(&mut output, lo.file, lo.line.to_usize(), ann);
};
}
}
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_errors/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_lint_defs::Applicability;
use rustc_data_structures::sync::Lrc;
use rustc_error_messages::FluentArgs;
use rustc_span::hygiene::ExpnData;
use rustc_span::Span;
use rustc_span::{Pos, Span};
use std::io::{self, Write};
use std::path::Path;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -453,10 +453,10 @@ impl DiagnosticSpan {
file_name: je.sm.filename_for_diagnostics(&start.file.name).to_string(),
byte_start: start.file.original_relative_byte_pos(span.lo()).0,
byte_end: start.file.original_relative_byte_pos(span.hi()).0,
line_start: start.line,
line_end: end.line,
column_start: start.col.0 + 1,
column_end: end.col.0 + 1,
line_start: start.line.to_usize(),
line_end: end.line.to_usize(),
column_start: start.col.to_usize() + 1,
column_end: end.col.to_usize() + 1,
is_primary,
text: DiagnosticSpanLine::from_span(span, je),
suggested_replacement: suggestion.map(|x| x.0.clone()),
Expand Down Expand Up @@ -535,8 +535,8 @@ impl DiagnosticSpanLine {
DiagnosticSpanLine::line_from_source_file(
sf,
line.line_index,
line.start_col.0 + 1,
line.end_col.0 + 1,
line.start_col.to_usize() + 1,
line.end_col.to_usize() + 1,
)
})
.collect()
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,14 @@ impl CodeSuggestion {
count -= 1;
}
// push lines between the previous and current span (if any)
for idx in prev_hi.line..(cur_lo.line - 1) {
for idx in prev_hi.line.to_usize()..(cur_lo.line.to_usize() - 1) {
if let Some(line) = sf.get_line(idx) {
buf.push_str(line.as_ref());
buf.push('\n');
highlights.push(std::mem::take(&mut line_highlight));
}
}
if let Some(cur_line) = sf.get_line(cur_lo.line - 1) {
if let Some(cur_line) = sf.get_line(cur_lo.line.to_usize() - 1) {
let end = match cur_line.char_indices().nth(cur_lo.col.to_usize()) {
Some((i, _)) => i,
None => cur_line.len(),
Expand All @@ -312,19 +312,20 @@ impl CodeSuggestion {
})
.sum();
line_highlight.push(SubstitutionHighlight {
start: (cur_lo.col.0 as isize + acc) as usize,
end: (cur_lo.col.0 as isize + acc + len) as usize,
start: (cur_lo.col.to_usize() as isize + acc) as usize,
end: (cur_lo.col.to_usize() as isize + acc + len) as usize,
});
buf.push_str(&part.snippet);
let cur_hi = sm.lookup_char_pos(part.span.hi());
if prev_hi.line == cur_lo.line && cur_hi.line == cur_lo.line {
// Account for the difference between the width of the current code and the
// snippet being suggested, so that the *later* suggestions are correctly
// aligned on the screen.
acc += len as isize - (cur_hi.col.0 - cur_lo.col.0) as isize;
acc +=
len as isize - (cur_hi.col.to_usize() - cur_lo.col.to_usize()) as isize;
}
prev_hi = cur_hi;
prev_line = sf.get_line(prev_hi.line - 1);
prev_line = sf.get_line(prev_hi.line.to_usize() - 1);
for line in part.snippet.split('\n').skip(1) {
acc = 0;
highlights.push(std::mem::take(&mut line_highlight));
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,14 @@ impl server::Span for Rustc<'_, '_> {

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

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

fn before(&mut self, span: Self::Span) -> Self::Span {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1658,7 +1658,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
format!(
" (opaque type at <{}:{}:{}>)",
sm.filename_for_diagnostics(&pos.file.name),
pos.line,
pos.line.to_usize(),
pos.col.to_usize() + 1,
)
}
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_middle/src/mir/spanview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ where
write!(
w,
r#"<div class="code" style="counter-reset: line {}"><span class="line">{}"#,
start.line - 1,
start.line.to_usize() - 1,
indent_to_initial_start_col,
)?;
span_viewables.sort_unstable_by(|a, b| {
Expand Down Expand Up @@ -234,7 +234,13 @@ pub fn source_range_no_file<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String {
let source_map = tcx.sess.source_map();
let start = source_map.lookup_char_pos(span.lo());
let end = source_map.lookup_char_pos(span.hi());
format!("{}:{}-{}:{}", start.line, start.col.to_usize() + 1, end.line, end.col.to_usize() + 1)
format!(
"{}:{}-{}:{}",
start.line.to_usize(),
start.col.to_usize() + 1,
end.line.to_usize(),
end.col.to_usize() + 1
)
}

pub fn statement_kind_name(statement: &Statement<'_>) -> &'static str {
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_mir_transform/src/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,18 +512,17 @@ fn make_code_region(
let (end_line, end_col) = if span.hi() == span.lo() {
let (end_line, mut end_col) = (start_line, start_col);
// Extend an empty span by one character so the region will be counted.
let CharPos(char_pos) = start_col;
if span.hi() == body_span.hi() {
start_col = CharPos(char_pos - 1);
start_col = start_col - CharPos::from_u32(1);
} else {
end_col = CharPos(char_pos + 1);
end_col = start_col + CharPos::from_u32(1);
}
(end_line, end_col)
} else {
source_file.lookup_file_pos(span.hi())
};
let start_line = source_map.doctest_offset_line(&source_file.name, start_line);
let end_line = source_map.doctest_offset_line(&source_file.name, end_line);
let start_line = source_map.doctest_offset_line(&source_file.name, start_line.to_usize());
let end_line = source_map.doctest_offset_line(&source_file.name, end_line.to_usize());
CodeRegion {
file_name,
start_line: start_line as u32,
Expand Down
Loading