Skip to content

Commit a98e38f

Browse files
author
Ariel Ben-Yehuda
authored
Rollup merge of rust-lang#40287 - estebank:label-overlap, r=nrc
Fix incorrect span label formatting Fix rust-lang#40157.
2 parents 5f4a13e + 7b0dd7b commit a98e38f

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

src/librustc_errors/emitter.rs

+34-28
Original file line numberDiff line numberDiff line change
@@ -358,39 +358,45 @@ impl EmitterWriter {
358358
let mut annotations_position = vec![];
359359
let mut line_len = 0;
360360
let mut p = 0;
361-
let mut ann_iter = annotations.iter().peekable();
362-
while let Some(annotation) = ann_iter.next() {
363-
let peek = ann_iter.peek();
364-
if let Some(next) = peek {
365-
if overlaps(next, annotation) && !annotation.is_line() && !next.is_line()
361+
for (i, annotation) in annotations.iter().enumerate() {
362+
for (j, next) in annotations.iter().enumerate() {
363+
if overlaps(next, annotation, 0) // This label overlaps with another one and both
364+
&& !annotation.is_line() // take space (they have text and are not
365+
&& !next.is_line() // multiline lines).
366366
&& annotation.has_label()
367+
&& j > i
368+
&& p == 0 // We're currently on the first line, move the label one line down
367369
{
368370
// This annotation needs a new line in the output.
369371
p += 1;
372+
break;
370373
}
371374
}
372375
annotations_position.push((p, annotation));
373-
if let Some(next) = peek {
374-
let l = if let Some(ref label) = next.label {
375-
label.len() + 2
376-
} else {
377-
0
378-
};
379-
if (overlaps(next, annotation) // Do not allow two labels to be in the same line
380-
|| next.end_col + l > annotation.start_col) // if they overlap including
381-
// padding, to avoid situations like:
382-
//
383-
// fn foo(x: u32) {
384-
// -------^------
385-
// | |
386-
// fn_spanx_span
387-
//
388-
&& !annotation.is_line() // Do not add a new line if this annotation or the
389-
&& !next.is_line() // next are vertical line placeholders.
390-
&& annotation.has_label() // Both labels must have some text, otherwise
391-
&& next.has_label() // they are not overlapping.
392-
{
393-
p += 1;
376+
for (j, next) in annotations.iter().enumerate() {
377+
if j > i {
378+
let l = if let Some(ref label) = next.label {
379+
label.len() + 2
380+
} else {
381+
0
382+
};
383+
if overlaps(next, annotation, l) // Do not allow two labels to be in the same
384+
// line if they overlap including padding, to
385+
// avoid situations like:
386+
//
387+
// fn foo(x: u32) {
388+
// -------^------
389+
// | |
390+
// fn_spanx_span
391+
//
392+
&& !annotation.is_line() // Do not add a new line if this annotation
393+
&& !next.is_line() // or the next are vertical line placeholders.
394+
&& annotation.has_label() // Both labels must have some text, otherwise
395+
&& next.has_label() // they are not overlapping.
396+
{
397+
p += 1;
398+
break;
399+
}
394400
}
395401
}
396402
if line_len < p {
@@ -1088,8 +1094,8 @@ fn num_overlap(a_start: usize, a_end: usize, b_start: usize, b_end:usize, inclus
10881094
(b_start..b_end + extra).contains(a_start) ||
10891095
(a_start..a_end + extra).contains(b_start)
10901096
}
1091-
fn overlaps(a1: &Annotation, a2: &Annotation) -> bool {
1092-
num_overlap(a1.start_col, a1.end_col, a2.start_col, a2.end_col, false)
1097+
fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
1098+
num_overlap(a1.start_col, a1.end_col + padding, a2.start_col, a2.end_col, false)
10931099
}
10941100

10951101
fn emit_to_destination(rendered_buffer: &Vec<Vec<StyledString>>,

src/test/ui/span/issue-40157.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main () {
12+
{println!("{:?}", match { let foo = vec![1, 2]; foo.get(1) } { x => x });}
13+
}

src/test/ui/span/issue-40157.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: `foo` does not live long enough
2+
--> $DIR/issue-40157.rs:12:64
3+
|
4+
12 | {println!("{:?}", match { let foo = vec![1, 2]; foo.get(1) } { x => x });}
5+
| ----------------------------------------------------------^-------------
6+
| | | |
7+
| | | `foo` dropped here while still borrowed
8+
| | borrow occurs here
9+
| borrowed value needs to live until here
10+
|
11+
= note: this error originates in a macro outside of the current crate
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)