about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-04-12 17:41:32 +0200
committerGitHub <noreply@github.com>2024-04-12 17:41:32 +0200
commitffea7e2a9b5c3e4f3b3edcef2aa859ff1b298da2 (patch)
treef830507a3d981670cb1a775c7c649ce2b7273f66 /compiler/rustc_errors/src
parentbd71213cf0a765705e7d72a099151bd4eb465ccb (diff)
parent1c41dd6320f1729e799171d98f5ff384246ddcd5 (diff)
downloadrust-ffea7e2a9b5c3e4f3b3edcef2aa859ff1b298da2.tar.gz
rust-ffea7e2a9b5c3e4f3b3edcef2aa859ff1b298da2.zip
Rollup merge of #123204 - notriddle:notriddle/include-str-span, r=pnkfelix
rustdoc: point at span in `include_str!`-ed md file

Fixes #118549
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/emitter.rs9
1 files changed, 7 insertions, 2 deletions
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index bd8e78bda26..6ce3fa3535d 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -1513,7 +1513,9 @@ impl HumanEmitter {
                 for line_idx in 0..annotated_file.lines.len() {
                     let file = annotated_file.file.clone();
                     let line = &annotated_file.lines[line_idx];
-                    if let Some(source_string) = file.get_line(line.line_index - 1) {
+                    if let Some(source_string) =
+                        line.line_index.checked_sub(1).and_then(|l| file.get_line(l))
+                    {
                         let leading_whitespace = source_string
                             .chars()
                             .take_while(|c| c.is_whitespace())
@@ -1553,7 +1555,10 @@ impl HumanEmitter {
                 for line in &annotated_file.lines {
                     max_line_len = max(
                         max_line_len,
-                        annotated_file.file.get_line(line.line_index - 1).map_or(0, |s| s.len()),
+                        line.line_index
+                            .checked_sub(1)
+                            .and_then(|l| annotated_file.file.get_line(l))
+                            .map_or(0, |s| s.len()),
                     );
                     for ann in &line.annotations {
                         span_right_margin = max(span_right_margin, ann.start_col.display);