Skip to content

Commit 41ba864

Browse files
committed
CharPos: usize -> u32, as BytePos is u32
1 parent 89f892e commit 41ba864

File tree

10 files changed

+28
-25
lines changed

10 files changed

+28
-25
lines changed

compiler/rustc_ast/src/util/comments.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ pub fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comme
225225
let pos_in_file = start_bpos + BytePos(pos as u32);
226226
let line_begin_in_file = source_file.line_begin_pos(pos_in_file);
227227
let line_begin_pos = (line_begin_in_file - start_bpos).to_usize();
228-
let col = CharPos(text[line_begin_pos..pos].chars().count());
228+
// FIXME: maybe simpler way?
229+
let col = CharPos::from_usize(text[line_begin_pos..pos].chars().count());
229230

230231
let lines = split_block_comment_into_lines(token_text, col);
231232
comments.push(Comment { style, lines, pos: pos_in_file })

compiler/rustc_errors/src/emitter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ impl EmitterWriter {
14991499
"{}:{}:{}",
15001500
sm.filename_for_diagnostics(&loc.file.name),
15011501
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
1502-
loc.col.0 + 1,
1502+
loc.col.to_usize() + 1,
15031503
),
15041504
Style::LineAndColumn,
15051505
);
@@ -1513,7 +1513,7 @@ impl EmitterWriter {
15131513
"{}:{}:{}: ",
15141514
sm.filename_for_diagnostics(&loc.file.name),
15151515
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
1516-
loc.col.0 + 1,
1516+
loc.col.to_usize() + 1,
15171517
),
15181518
Style::LineAndColumn,
15191519
);
@@ -1796,7 +1796,7 @@ impl EmitterWriter {
17961796
"{}:{}:{}",
17971797
sm.filename_for_diagnostics(&loc.file.name),
17981798
sm.doctest_offset_line(&loc.file.name, loc.line.to_usize()),
1799-
loc.col.0 + 1,
1799+
loc.col.to_usize() + 1,
18001800
),
18011801
Style::LineAndColumn,
18021802
);

compiler/rustc_errors/src/json.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ impl DiagnosticSpan {
455455
byte_end: start.file.original_relative_byte_pos(span.hi()).0,
456456
line_start: start.line.to_usize(),
457457
line_end: end.line.to_usize(),
458-
column_start: start.col.0 + 1,
459-
column_end: end.col.0 + 1,
458+
column_start: start.col.to_usize() + 1,
459+
column_end: end.col.to_usize() + 1,
460460
is_primary,
461461
text: DiagnosticSpanLine::from_span(span, je),
462462
suggested_replacement: suggestion.map(|x| x.0.clone()),
@@ -535,8 +535,8 @@ impl DiagnosticSpanLine {
535535
DiagnosticSpanLine::line_from_source_file(
536536
sf,
537537
line.line_index,
538-
line.start_col.0 + 1,
539-
line.end_col.0 + 1,
538+
line.start_col.to_usize() + 1,
539+
line.end_col.to_usize() + 1,
540540
)
541541
})
542542
.collect()

compiler/rustc_errors/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,17 @@ impl CodeSuggestion {
312312
})
313313
.sum();
314314
line_highlight.push(SubstitutionHighlight {
315-
start: (cur_lo.col.0 as isize + acc) as usize,
316-
end: (cur_lo.col.0 as isize + acc + len) as usize,
315+
start: (cur_lo.col.to_usize() as isize + acc) as usize,
316+
end: (cur_lo.col.to_usize() as isize + acc + len) as usize,
317317
});
318318
buf.push_str(&part.snippet);
319319
let cur_hi = sm.lookup_char_pos(part.span.hi());
320320
if prev_hi.line == cur_lo.line && cur_hi.line == cur_lo.line {
321321
// Account for the difference between the width of the current code and the
322322
// snippet being suggested, so that the *later* suggestions are correctly
323323
// aligned on the screen.
324-
acc += len as isize - (cur_hi.col.0 - cur_lo.col.0) as isize;
324+
acc +=
325+
len as isize - (cur_hi.col.to_usize() - cur_lo.col.to_usize()) as isize;
325326
}
326327
prev_hi = cur_hi;
327328
prev_line = sf.get_line(prev_hi.line.to_usize() - 1);

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,10 @@ fn make_code_region(
512512
let (end_line, end_col) = if span.hi() == span.lo() {
513513
let (end_line, mut end_col) = (start_line, start_col);
514514
// Extend an empty span by one character so the region will be counted.
515-
let CharPos(char_pos) = start_col;
516515
if span.hi() == body_span.hi() {
517-
start_col = CharPos(char_pos - 1);
516+
start_col = start_col - CharPos::from_u32(1);
518517
} else {
519-
end_col = CharPos(char_pos + 1);
518+
end_col = start_col + CharPos::from_u32(1);
520519
}
521520
(end_line, end_col)
522521
} else {

compiler/rustc_save_analysis/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ impl<'tcx> SaveContext<'tcx> {
8585
byte_end: span.hi().0,
8686
line_start: Row::new_one_indexed(start.line.to_u32()),
8787
line_end: Row::new_one_indexed(end.line.to_u32()),
88-
column_start: Column::new_one_indexed(start.col.0 as u32 + 1),
89-
column_end: Column::new_one_indexed(end.col.0 as u32 + 1),
88+
column_start: Column::new_one_indexed(start.col.to_u32() + 1),
89+
column_end: Column::new_one_indexed(end.col.to_u32() + 1),
9090
}
9191
}
9292

compiler/rustc_span/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ impl SourceFile {
16871687
}
16881688

16891689
assert!(self.start_pos.to_u32() + total_extra_bytes <= bpos.to_u32());
1690-
CharPos(bpos.to_usize() - self.start_pos.to_usize() - total_extra_bytes as usize)
1690+
CharPos(bpos.to_u32() - self.start_pos.to_u32() - total_extra_bytes)
16911691
}
16921692

16931693
/// Looks up the file's (1-based) line number and (0-based `CharPos`) column offset, for a
@@ -1712,7 +1712,7 @@ impl SourceFile {
17121712

17131713
/// Looks up the file's (1-based) line number, (0-based `CharPos`) column offset, and (0-based)
17141714
/// column offset when displayed, for a given `BytePos`.
1715-
pub fn lookup_file_pos_with_col_display(&self, pos: BytePos) -> (LineNum, CharPos, usize) {
1715+
pub fn lookup_file_pos_with_col_display(&self, pos: BytePos) -> (LineNum, CharPos, u32) {
17161716
let (line, col_or_chpos) = self.lookup_file_pos(pos);
17171717
if line.to_usize() > 0 {
17181718
let col = col_or_chpos;
@@ -1731,7 +1731,8 @@ impl SourceFile {
17311731
.iter()
17321732
.map(|x| x.width())
17331733
.sum();
1734-
col.0 - special_chars + non_narrow
1734+
col.to_u32() - u32::try_from(special_chars).unwrap()
1735+
+ u32::try_from(non_narrow).unwrap()
17351736
};
17361737
(line, col, col_display)
17371738
} else {
@@ -1743,7 +1744,8 @@ impl SourceFile {
17431744
.unwrap_or_else(|x| x);
17441745
let non_narrow: usize =
17451746
self.non_narrow_chars[0..end_width_idx].iter().map(|x| x.width()).sum();
1746-
chpos.0 - end_width_idx + non_narrow
1747+
chpos.to_u32() - u32::try_from(end_width_idx).unwrap()
1748+
+ u32::try_from(non_narrow).unwrap()
17471749
};
17481750
(LineNum(0), chpos, col_display)
17491751
}
@@ -1911,7 +1913,7 @@ impl_pos! {
19111913
/// is not equivalent to a character offset. The [`SourceMap`] will convert [`BytePos`]
19121914
/// values to `CharPos` values as necessary.
19131915
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
1914-
pub struct CharPos(pub usize);
1916+
pub struct CharPos(u32);
19151917

19161918
/// A Line number
19171919
///

compiler/rustc_span/src/source_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl SourceMap {
428428
pub fn lookup_char_pos(&self, pos: BytePos) -> Loc {
429429
let sf = self.lookup_source_file(pos);
430430
let (line, col, col_display) = sf.lookup_file_pos_with_col_display(pos);
431-
Loc { file: sf, line, col, col_display }
431+
Loc { file: sf, line, col, col_display: col_display as usize }
432432
}
433433

434434
// If the corresponding `SourceFile` is empty, does not return a line number.

src/tools/clippy/clippy_lints/src/formatting.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
55
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
8-
use rustc_span::source_map::Span;
8+
use rustc_span::source_map::{Pos, Span};
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -264,7 +264,7 @@ fn has_unary_equivalent(bin_op: BinOpKind) -> bool {
264264
}
265265

266266
fn indentation(cx: &EarlyContext<'_>, span: Span) -> usize {
267-
cx.sess().source_map().lookup_char_pos(span.lo()).col.0
267+
cx.sess().source_map().lookup_char_pos(span.lo()).col.to_usize()
268268
}
269269

270270
/// Implementation of the `POSSIBLE_MISSING_COMMA` lint for array

src/tools/clippy/clippy_utils/src/sugg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ fn indentation<T: LintContext>(cx: &T, span: Span) -> Option<String> {
661661
.and_then(|line| {
662662
if let Some((pos, _)) = line.char_indices().find(|&(_, c)| c != ' ' && c != '\t') {
663663
// We can mix char and byte positions here because we only consider `[ \t]`.
664-
if lo.col == CharPos(pos) {
664+
if lo.col == CharPos::from_usize(pos) {
665665
Some(line[..pos].into())
666666
} else {
667667
None

0 commit comments

Comments
 (0)