Skip to content

Commit 953901e

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 a86e758 + d5f0f44 commit 953901e

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_lexer::{tokenize, TokenKind};
1212
use rustc_lint::{LateContext, LateLintPass, LintContext};
1313
use rustc_middle::lint::in_external_macro;
1414
use rustc_session::{declare_tool_lint, impl_lint_pass};
15-
use rustc_span::{BytePos, Pos, Span, SyntaxContext};
15+
use rustc_span::{BytePos, Pos, RelativeBytePos, Span, SyntaxContext};
1616

1717
declare_clippy_lint! {
1818
/// ### What it does
@@ -514,7 +514,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
514514
match text_has_safety_comment(
515515
src,
516516
&lines[comment_start_line.line + 1..=unsafe_line.line],
517-
unsafe_line.sf.start_pos.to_usize(),
517+
unsafe_line.sf.start_pos,
518518
) {
519519
Some(b) => HasSafetyComment::Yes(b),
520520
None => HasSafetyComment::No,
@@ -558,7 +558,7 @@ fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> H
558558
match text_has_safety_comment(
559559
src,
560560
&lines[comment_start_line.line + 1..=unsafe_line.line],
561-
unsafe_line.sf.start_pos.to_usize(),
561+
unsafe_line.sf.start_pos,
562562
) {
563563
Some(b) => HasSafetyComment::Yes(b),
564564
None => HasSafetyComment::No,
@@ -619,7 +619,7 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
619619
match text_has_safety_comment(
620620
src,
621621
&lines[macro_line.line + 1..=unsafe_line.line],
622-
unsafe_line.sf.start_pos.to_usize(),
622+
unsafe_line.sf.start_pos,
623623
) {
624624
Some(b) => HasSafetyComment::Yes(b),
625625
None => HasSafetyComment::No,
@@ -675,7 +675,7 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
675675
body_line.line < unsafe_line.line && text_has_safety_comment(
676676
src,
677677
&lines[body_line.line + 1..=unsafe_line.line],
678-
unsafe_line.sf.start_pos.to_usize(),
678+
unsafe_line.sf.start_pos,
679679
).is_some()
680680
})
681681
} else {
@@ -688,13 +688,13 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
688688
}
689689

690690
/// Checks if the given text has a safety comment for the immediately proceeding line.
691-
fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> Option<BytePos> {
691+
fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos: BytePos) -> Option<BytePos> {
692692
let mut lines = line_starts
693693
.array_windows::<2>()
694694
.rev()
695695
.map_while(|[start, end]| {
696-
let start = start.to_usize() - offset;
697-
let end = end.to_usize() - offset;
696+
let start = start.to_usize();
697+
let end = end.to_usize();
698698
let text = src.get(start..end)?;
699699
let trimmed = text.trim_start();
700700
Some((start + (text.len() - trimmed.len()), trimmed))
@@ -709,9 +709,7 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) ->
709709
let (mut line, mut line_start) = (line, line_start);
710710
loop {
711711
if line.to_ascii_uppercase().contains("SAFETY:") {
712-
return Some(BytePos(
713-
u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(),
714-
));
712+
return Some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
715713
}
716714
match lines.next() {
717715
Some((s, x)) if x.starts_with("//") => (line, line_start) = (x, s),
@@ -724,15 +722,13 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) ->
724722
let (mut line_start, mut line) = (line_start, line);
725723
loop {
726724
if line.starts_with("/*") {
727-
let src = &src[line_start..line_starts.last().unwrap().to_usize() - offset];
725+
let src = &src[line_start..line_starts.last().unwrap().to_usize()];
728726
let mut tokens = tokenize(src);
729727
return (src[..tokens.next().unwrap().len as usize]
730728
.to_ascii_uppercase()
731729
.contains("SAFETY:")
732730
&& tokens.all(|t| t.kind == TokenKind::Whitespace))
733-
.then_some(BytePos(
734-
u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(),
735-
));
731+
.then_some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
736732
}
737733
match lines.next() {
738734
Some(x) => (line_start, line) = x,

clippy_utils/src/source.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::{BlockCheckMode, Expr, ExprKind, UnsafeSource};
88
use rustc_lint::{LateContext, LintContext};
99
use rustc_session::Session;
1010
use rustc_span::source_map::{original_sp, SourceMap};
11-
use rustc_span::{hygiene, BytePos, Pos, SourceFile, Span, SpanData, SyntaxContext, DUMMY_SP};
11+
use rustc_span::{hygiene, BytePos, SourceFileAndLine, Pos, SourceFile, Span, SpanData, SyntaxContext, DUMMY_SP};
1212
use std::borrow::Cow;
1313
use std::ops::Range;
1414

@@ -117,9 +117,9 @@ fn first_char_in_first_line<T: LintContext>(cx: &T, span: Span) -> Option<BytePo
117117
/// ```
118118
fn line_span<T: LintContext>(cx: &T, span: Span) -> Span {
119119
let span = original_sp(span, DUMMY_SP);
120-
let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap();
121-
let line_no = source_map_and_line.line;
122-
let line_start = source_map_and_line.sf.lines(|lines| lines[line_no]);
120+
let SourceFileAndLine { sf, line } = cx.sess().source_map().lookup_line(span.lo()).unwrap();
121+
let line_start = sf.lines(|lines| lines[line]);
122+
let line_start = sf.absolute_position(line_start);
123123
span.with_lo(line_start)
124124
}
125125

0 commit comments

Comments
 (0)