Skip to content

Commit 439ff69

Browse files
committed
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 ```
1 parent d616f47 commit 439ff69

File tree

7 files changed

+80
-18
lines changed

7 files changed

+80
-18
lines changed

src/librustc/traits/error_reporting.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -293,22 +293,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
293293
Some(val) => Some(val),
294294
None => {
295295
span_err!(self.tcx.sess, err_sp, E0272,
296-
"the #[rustc_on_unimplemented] \
297-
attribute on \
298-
trait definition for {} refers to \
299-
non-existent type parameter {}",
300-
trait_str, s);
296+
"the #[rustc_on_unimplemented] attribute on trait \
297+
definition for {} refers to non-existent type \
298+
parameter {}",
299+
trait_str, s);
301300
errored = true;
302301
None
303302
}
304303
},
305304
_ => {
306305
span_err!(self.tcx.sess, err_sp, E0273,
307-
"the #[rustc_on_unimplemented] attribute \
308-
on trait definition for {} must have \
309-
named format arguments, eg \
310-
`#[rustc_on_unimplemented = \
311-
\"foo {{T}}\"]`", trait_str);
306+
"the #[rustc_on_unimplemented] attribute on trait \
307+
definition for {} must have named format arguments, eg \
308+
`#[rustc_on_unimplemented = \"foo {{T}}\"]`",
309+
trait_str);
312310
errored = true;
313311
None
314312
}
@@ -449,8 +447,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
449447
"impl has stricter requirements than trait");
450448

451449
if let Some(trait_item_span) = self.tcx.hir.span_if_local(trait_item_def_id) {
452-
err.span_label(trait_item_span,
453-
&format!("definition of `{}` from trait", item_name));
450+
let span = self.tcx.sess.codemap().def_span(trait_item_span);
451+
err.span_label(span, &format!("definition of `{}` from trait", item_name));
454452
}
455453

456454
err.span_label(
@@ -652,6 +650,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
652650
{
653651
assert!(type_def_id.is_local());
654652
let span = self.hir.span_if_local(type_def_id).unwrap();
653+
let span = self.sess.codemap().def_span(span);
655654
let mut err = struct_span_err!(self.sess, span, E0072,
656655
"recursive type `{}` has infinite size",
657656
self.item_path_str(type_def_id));
@@ -669,13 +668,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
669668
-> DiagnosticBuilder<'tcx>
670669
{
671670
let trait_str = self.item_path_str(trait_def_id);
671+
let span = self.sess.codemap().def_span(span);
672672
let mut err = struct_span_err!(
673673
self.sess, span, E0038,
674674
"the trait `{}` cannot be made into an object",
675675
trait_str);
676-
err.span_label(span, &format!(
677-
"the trait `{}` cannot be made into an object", trait_str
678-
));
676+
err.span_label(span, &format!("the trait `{}` cannot be made into an object", trait_str));
679677

680678
let mut reported_violations = FxHashSet();
681679
for violation in violations {

src/libsyntax/codemap.rs

+19
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

+1-1
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

+1-2
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

+10
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+
+20
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+
}
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)