about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-05 21:03:56 +0000
committerbors <bors@rust-lang.org>2023-09-05 21:03:56 +0000
commit953901e35c5b9dae76a092e940487b650774fbb4 (patch)
treeddbc0e02e664762b9ad3dd8a828af6eae9526968
parenta86e758228042a43be6a662126789f9b0fb873c5 (diff)
parentd5f0f443b98f1aa74eae4f26fcaa637cabaa1dab (diff)
downloadrust-953901e35c5b9dae76a092e940487b650774fbb4.tar.gz
rust-953901e35c5b9dae76a092e940487b650774fbb4.zip
Auto merge of #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`
-rw-r--r--clippy_lints/src/undocumented_unsafe_blocks.rs26
-rw-r--r--clippy_utils/src/source.rs8
2 files changed, 15 insertions, 19 deletions
diff --git a/clippy_lints/src/undocumented_unsafe_blocks.rs b/clippy_lints/src/undocumented_unsafe_blocks.rs
index f2ef602012f..a1ea3a495eb 100644
--- a/clippy_lints/src/undocumented_unsafe_blocks.rs
+++ b/clippy_lints/src/undocumented_unsafe_blocks.rs
@@ -12,7 +12,7 @@ use rustc_lexer::{tokenize, TokenKind};
 use rustc_lint::{LateContext, LateLintPass, LintContext};
 use rustc_middle::lint::in_external_macro;
 use rustc_session::{declare_tool_lint, impl_lint_pass};
-use rustc_span::{BytePos, Pos, Span, SyntaxContext};
+use rustc_span::{BytePos, Pos, RelativeBytePos, Span, SyntaxContext};
 
 declare_clippy_lint! {
     /// ### What it does
@@ -514,7 +514,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
                     match text_has_safety_comment(
                         src,
                         &lines[comment_start_line.line + 1..=unsafe_line.line],
-                        unsafe_line.sf.start_pos.to_usize(),
+                        unsafe_line.sf.start_pos,
                     ) {
                         Some(b) => HasSafetyComment::Yes(b),
                         None => HasSafetyComment::No,
@@ -558,7 +558,7 @@ fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> H
                     match text_has_safety_comment(
                         src,
                         &lines[comment_start_line.line + 1..=unsafe_line.line],
-                        unsafe_line.sf.start_pos.to_usize(),
+                        unsafe_line.sf.start_pos,
                     ) {
                         Some(b) => HasSafetyComment::Yes(b),
                         None => HasSafetyComment::No,
@@ -619,7 +619,7 @@ fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span
                     match text_has_safety_comment(
                         src,
                         &lines[macro_line.line + 1..=unsafe_line.line],
-                        unsafe_line.sf.start_pos.to_usize(),
+                        unsafe_line.sf.start_pos,
                     ) {
                         Some(b) => HasSafetyComment::Yes(b),
                         None => HasSafetyComment::No,
@@ -675,7 +675,7 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
                 body_line.line < unsafe_line.line && text_has_safety_comment(
                     src,
                     &lines[body_line.line + 1..=unsafe_line.line],
-                    unsafe_line.sf.start_pos.to_usize(),
+                    unsafe_line.sf.start_pos,
                 ).is_some()
             })
         } else {
@@ -688,13 +688,13 @@ fn span_in_body_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
 }
 
 /// Checks if the given text has a safety comment for the immediately proceeding line.
-fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) -> Option<BytePos> {
+fn text_has_safety_comment(src: &str, line_starts: &[RelativeBytePos], start_pos: BytePos) -> Option<BytePos> {
     let mut lines = line_starts
         .array_windows::<2>()
         .rev()
         .map_while(|[start, end]| {
-            let start = start.to_usize() - offset;
-            let end = end.to_usize() - offset;
+            let start = start.to_usize();
+            let end = end.to_usize();
             let text = src.get(start..end)?;
             let trimmed = text.trim_start();
             Some((start + (text.len() - trimmed.len()), trimmed))
@@ -709,9 +709,7 @@ fn text_has_safety_comment(src: &str, line_starts: &[BytePos], offset: usize) ->
         let (mut line, mut line_start) = (line, line_start);
         loop {
             if line.to_ascii_uppercase().contains("SAFETY:") {
-                return Some(BytePos(
-                    u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(),
-                ));
+                return Some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
             }
             match lines.next() {
                 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) ->
     let (mut line_start, mut line) = (line_start, line);
     loop {
         if line.starts_with("/*") {
-            let src = &src[line_start..line_starts.last().unwrap().to_usize() - offset];
+            let src = &src[line_start..line_starts.last().unwrap().to_usize()];
             let mut tokens = tokenize(src);
             return (src[..tokens.next().unwrap().len as usize]
                 .to_ascii_uppercase()
                 .contains("SAFETY:")
                 && tokens.all(|t| t.kind == TokenKind::Whitespace))
-            .then_some(BytePos(
-                u32::try_from(line_start).unwrap() + u32::try_from(offset).unwrap(),
-            ));
+            .then_some(start_pos + BytePos(u32::try_from(line_start).unwrap()));
         }
         match lines.next() {
             Some(x) => (line_start, line) = x,
diff --git a/clippy_utils/src/source.rs b/clippy_utils/src/source.rs
index dc4ee725681..03416d35ba4 100644
--- a/clippy_utils/src/source.rs
+++ b/clippy_utils/src/source.rs
@@ -8,7 +8,7 @@ use rustc_hir::{BlockCheckMode, Expr, ExprKind, UnsafeSource};
 use rustc_lint::{LateContext, LintContext};
 use rustc_session::Session;
 use rustc_span::source_map::{original_sp, SourceMap};
-use rustc_span::{hygiene, BytePos, Pos, SourceFile, Span, SpanData, SyntaxContext, DUMMY_SP};
+use rustc_span::{hygiene, BytePos, SourceFileAndLine, Pos, SourceFile, Span, SpanData, SyntaxContext, DUMMY_SP};
 use std::borrow::Cow;
 use std::ops::Range;
 
@@ -117,9 +117,9 @@ fn first_char_in_first_line<T: LintContext>(cx: &T, span: Span) -> Option<BytePo
 /// ```
 fn line_span<T: LintContext>(cx: &T, span: Span) -> Span {
     let span = original_sp(span, DUMMY_SP);
-    let source_map_and_line = cx.sess().source_map().lookup_line(span.lo()).unwrap();
-    let line_no = source_map_and_line.line;
-    let line_start = source_map_and_line.sf.lines(|lines| lines[line_no]);
+    let SourceFileAndLine { sf, line } = cx.sess().source_map().lookup_line(span.lo()).unwrap();
+    let line_start = sf.lines(|lines| lines[line]);
+    let line_start = sf.absolute_position(line_start);
     span.with_lo(line_start)
 }