Skip to content

Commit 236978e

Browse files
authored
Rollup merge of rust-lang#41214 - estebank:less-multiline, r=petrochenkov
Add a way to get shorter spans until `char` for pointing at defs ```rust error[E0072]: recursive type `X` has infinite size --> file.rs:10:1 | 10 | struct X { | ^^^^^^^^ recursive type has infinite size | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `X` representable ``` vs ```rust error[E0072]: recursive type `X` has infinite size --> file.rs:10:1 | 10 | struct X { | _^ starting here... 11 | | x: X, 12 | | } | |_^ ...ending here: recursive type has infinite size | = help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `X` representable ``` Re: rust-lang#35965, rust-lang#38246. Follow up to rust-lang#38328. r? @jonathandturner
2 parents 1bb1530 + 439ff69 commit 236978e

File tree

7 files changed

+80
-18
lines changed

7 files changed

+80
-18
lines changed

src/librustc/traits/error_reporting.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -329,22 +329,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
329329
Some(val) => Some(val),
330330
None => {
331331
span_err!(self.tcx.sess, err_sp, E0272,
332-
"the #[rustc_on_unimplemented] \
333-
attribute on \
334-
trait definition for {} refers to \
335-
non-existent type parameter {}",
336-
trait_str, s);
332+
"the #[rustc_on_unimplemented] attribute on trait \
333+
definition for {} refers to non-existent type \
334+
parameter {}",
335+
trait_str, s);
337336
errored = true;
338337
None
339338
}
340339
},
341340
_ => {
342341
span_err!(self.tcx.sess, err_sp, E0273,
343-
"the #[rustc_on_unimplemented] attribute \
344-
on trait definition for {} must have \
345-
named format arguments, eg \
346-
`#[rustc_on_unimplemented = \
347-
\"foo {{T}}\"]`", trait_str);
342+
"the #[rustc_on_unimplemented] attribute on trait \
343+
definition for {} must have named format arguments, eg \
344+
`#[rustc_on_unimplemented = \"foo {{T}}\"]`",
345+
trait_str);
348346
errored = true;
349347
None
350348
}
@@ -485,8 +483,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
485483
"impl has stricter requirements than trait");
486484

487485
if let Some(trait_item_span) = self.tcx.hir.span_if_local(trait_item_def_id) {
488-
err.span_label(trait_item_span,
489-
&format!("definition of `{}` from trait", item_name));
486+
let span = self.tcx.sess.codemap().def_span(trait_item_span);
487+
err.span_label(span, &format!("definition of `{}` from trait", item_name));
490488
}
491489

492490
err.span_label(
@@ -692,6 +690,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
692690
{
693691
assert!(type_def_id.is_local());
694692
let span = self.hir.span_if_local(type_def_id).unwrap();
693+
let span = self.sess.codemap().def_span(span);
695694
let mut err = struct_span_err!(self.sess, span, E0072,
696695
"recursive type `{}` has infinite size",
697696
self.item_path_str(type_def_id));
@@ -709,13 +708,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
709708
-> DiagnosticBuilder<'tcx>
710709
{
711710
let trait_str = self.item_path_str(trait_def_id);
711+
let span = self.sess.codemap().def_span(span);
712712
let mut err = struct_span_err!(
713713
self.sess, span, E0038,
714714
"the trait `{}` cannot be made into an object",
715715
trait_str);
716-
err.span_label(span, &format!(
717-
"the trait `{}` cannot be made into an object", trait_str
718-
));
716+
err.span_label(span, &format!("the trait `{}` cannot be made into an object", trait_str));
719717

720718
let mut reported_violations = FxHashSet();
721719
for violation in violations {

src/libsyntax/codemap.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,25 @@ impl CodeMap {
441441
}
442442
}
443443

444+
/// Given a `Span`, try to get a shorter span ending before the first occurrence of `c` `char`
445+
pub fn span_until_char(&self, sp: Span, c: char) -> Span {
446+
match self.span_to_snippet(sp) {
447+
Ok(snippet) => {
448+
let snippet = snippet.split(c).nth(0).unwrap_or("").trim_right();
449+
if snippet.len() > 0 && !snippet.contains('\n') {
450+
Span { hi: BytePos(sp.lo.0 + snippet.len() as u32), ..sp }
451+
} else {
452+
sp
453+
}
454+
}
455+
_ => sp,
456+
}
457+
}
458+
459+
pub fn def_span(&self, sp: Span) -> Span {
460+
self.span_until_char(sp, '{')
461+
}
462+
444463
pub fn get_filemap(&self, filename: &str) -> Option<Rc<FileMap>> {
445464
for fm in self.files.borrow().iter() {
446465
if filename == fm.name {

src/test/ui/resolve/issue-3907-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0038]: the trait `issue_3907::Foo` cannot be made into an object
22
--> $DIR/issue-3907-2.rs:20:1
33
|
44
20 | fn bar(_x: Foo) {}
5-
| ^^^^^^^^^^^^^^^^^^ the trait `issue_3907::Foo` cannot be made into an object
5+
| ^^^^^^^^^^^^^^^ the trait `issue_3907::Foo` cannot be made into an object
66
|
77
= note: method `bar` has no receiver
88

src/test/compile-fail/E0072.rs renamed to src/test/ui/span/E0072.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
struct ListNode { //~ ERROR E0072
12-
//~| NOTE recursive type has infinite size
11+
struct ListNode {
1312
head: u8,
1413
tail: Option<ListNode>,
1514
}

src/test/ui/span/E0072.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0072]: recursive type `ListNode` has infinite size
2+
--> $DIR/E0072.rs:11:1
3+
|
4+
11 | struct ListNode {
5+
| ^^^^^^^^^^^^^^^ recursive type has infinite size
6+
|
7+
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `ListNode` representable
8+
9+
error: aborting due to previous error
10+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
// It should just use the entire body instead of pointing at the next two lines
12+
struct
13+
ListNode
14+
{
15+
head: u8,
16+
tail: Option<ListNode>,
17+
}
18+
19+
fn main() {
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0072]: recursive type `ListNode` has infinite size
2+
--> $DIR/multiline-span-E0072.rs:12:1
3+
|
4+
12 | struct
5+
| _^ starting here...
6+
13 | | ListNode
7+
14 | | {
8+
15 | | head: u8,
9+
16 | | tail: Option<ListNode>,
10+
17 | | }
11+
| |_^ ...ending here: recursive type has infinite size
12+
|
13+
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `ListNode` representable
14+
15+
error: aborting due to previous error
16+

0 commit comments

Comments
 (0)