Skip to content

Commit f6dfbbb

Browse files
committed
On recursive ADT, provide indirection structured suggestion
1 parent 74e8046 commit f6dfbbb

27 files changed

+315
-66
lines changed

src/librustc_errors/diagnostic.rs

+23
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,29 @@ impl Diagnostic {
296296
self
297297
}
298298

299+
pub fn multipart_suggestions(
300+
&mut self,
301+
msg: &str,
302+
suggestions: Vec<Vec<(Span, String)>>,
303+
applicability: Applicability,
304+
) -> &mut Self {
305+
self.suggestions.push(CodeSuggestion {
306+
substitutions: suggestions
307+
.into_iter()
308+
.map(|suggestion| Substitution {
309+
parts: suggestion
310+
.into_iter()
311+
.map(|(span, snippet)| SubstitutionPart { snippet, span })
312+
.collect(),
313+
})
314+
.collect(),
315+
msg: msg.to_owned(),
316+
style: SuggestionStyle::ShowCode,
317+
applicability,
318+
});
319+
self
320+
}
321+
299322
/// Prints out a message with for a multipart suggestion without showing the suggested code.
300323
///
301324
/// This is intended to be used for suggestions that are obvious in what the changes need to

src/librustc_errors/diagnostic_builder.rs

+13
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ impl<'a> DiagnosticBuilder<'a> {
260260
self
261261
}
262262

263+
pub fn multipart_suggestions(
264+
&mut self,
265+
msg: &str,
266+
suggestions: Vec<Vec<(Span, String)>>,
267+
applicability: Applicability,
268+
) -> &mut Self {
269+
if !self.0.allow_suggestions {
270+
return self;
271+
}
272+
self.0.diagnostic.multipart_suggestions(msg, suggestions, applicability);
273+
self
274+
}
275+
263276
pub fn tool_only_multipart_suggestion(
264277
&mut self,
265278
msg: &str,

src/librustc_middle/ty/util.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,15 @@ impl<'tcx> ty::TyS<'tcx> {
827827
// Find non representable fields with their spans
828828
fold_repr(def.all_fields().map(|field| {
829829
let ty = field.ty(tcx, substs);
830-
let span = tcx.hir().span_if_local(field.did).unwrap_or(sp);
830+
let span = match field
831+
.did
832+
.as_local()
833+
.map(|id| tcx.hir().as_local_hir_id(id))
834+
.and_then(|id| tcx.hir().find(id))
835+
{
836+
Some(hir::Node::Field(field)) => field.ty.span,
837+
_ => sp,
838+
};
831839
match is_type_structurally_recursive(
832840
tcx,
833841
span,

src/librustc_trait_selection/traits/error_reporting/mod.rs

+52-14
Original file line numberDiff line numberDiff line change
@@ -1725,24 +1725,62 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
17251725
pub fn recursive_type_with_infinite_size_error(
17261726
tcx: TyCtxt<'tcx>,
17271727
type_def_id: DefId,
1728-
) -> DiagnosticBuilder<'tcx> {
1728+
spans: Vec<Span>,
1729+
) {
17291730
assert!(type_def_id.is_local());
17301731
let span = tcx.hir().span_if_local(type_def_id).unwrap();
17311732
let span = tcx.sess.source_map().guess_head_span(span);
1732-
let mut err = struct_span_err!(
1733-
tcx.sess,
1734-
span,
1735-
E0072,
1736-
"recursive type `{}` has infinite size",
1737-
tcx.def_path_str(type_def_id)
1738-
);
1733+
let path = tcx.def_path_str(type_def_id);
1734+
let mut err =
1735+
struct_span_err!(tcx.sess, span, E0072, "recursive type `{}` has infinite size", path);
17391736
err.span_label(span, "recursive type has infinite size");
1740-
err.help(&format!(
1741-
"insert indirection (e.g., a `Box`, `Rc`, or `&`) \
1742-
at some point to make `{}` representable",
1743-
tcx.def_path_str(type_def_id)
1744-
));
1745-
err
1737+
for &span in &spans {
1738+
err.span_label(span, "recursive without indirection");
1739+
}
1740+
let short_msg = format!("insert some indirection to make `{}` representable", path);
1741+
let msg = format!(
1742+
"insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `{}` representable",
1743+
path,
1744+
);
1745+
match &spans[..] {
1746+
[span] => {
1747+
err.multipart_suggestions(
1748+
&short_msg,
1749+
vec![
1750+
vec![
1751+
(span.shrink_to_lo(), "Box<".to_string()),
1752+
(span.shrink_to_hi(), ">".to_string()),
1753+
],
1754+
vec![
1755+
(span.shrink_to_lo(), "Rc<".to_string()),
1756+
(span.shrink_to_hi(), ">".to_string()),
1757+
],
1758+
vec![(span.shrink_to_lo(), "&".to_string())],
1759+
],
1760+
Applicability::HasPlaceholders,
1761+
);
1762+
}
1763+
_ if spans.len() <= 4 => {
1764+
err.multipart_suggestion(
1765+
&msg,
1766+
spans
1767+
.iter()
1768+
.flat_map(|&span| {
1769+
vec![
1770+
(span.shrink_to_lo(), "Box<".to_string()),
1771+
(span.shrink_to_hi(), ">".to_string()),
1772+
]
1773+
.into_iter()
1774+
})
1775+
.collect(),
1776+
Applicability::HasPlaceholders,
1777+
);
1778+
}
1779+
_ => {
1780+
err.help(&msg);
1781+
}
1782+
}
1783+
err.emit();
17461784
}
17471785

17481786
/// Summarizes information

src/librustc_typeck/check/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2374,11 +2374,7 @@ fn check_representable(tcx: TyCtxt<'_>, sp: Span, item_def_id: LocalDefId) -> bo
23742374
// caught by case 1.
23752375
match rty.is_representable(tcx, sp) {
23762376
Representability::SelfRecursive(spans) => {
2377-
let mut err = recursive_type_with_infinite_size_error(tcx, item_def_id.to_def_id());
2378-
for span in spans {
2379-
err.span_label(span, "recursive without indirection");
2380-
}
2381-
err.emit();
2377+
recursive_type_with_infinite_size_error(tcx, item_def_id.to_def_id(), spans);
23822378
return false;
23832379
}
23842380
Representability::Representable | Representability::ContainsRecursive => (),

src/test/ui/infinite/infinite-tag-type-recursion.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ LL | enum MList { Cons(isize, MList), Nil }
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `MList` representable
9+
help: insert some indirection to make `MList` representable
10+
|
11+
LL | enum MList { Cons(isize, Box<MList>), Nil }
12+
| ^^^^ ^
13+
LL | enum MList { Cons(isize, Rc<MList>), Nil }
14+
| ^^^ ^
15+
LL | enum MList { Cons(isize, &MList), Nil }
16+
| ^
1017

1118
error[E0391]: cycle detected when processing `MList`
1219
--> $DIR/infinite-tag-type-recursion.rs:1:1

src/test/ui/issues/issue-17431-1.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ error[E0072]: recursive type `Foo` has infinite size
22
--> $DIR/issue-17431-1.rs:1:1
33
|
44
LL | struct Foo { foo: Option<Option<Foo>> }
5-
| ^^^^^^^^^^ ------------------------ recursive without indirection
5+
| ^^^^^^^^^^ ------------------- recursive without indirection
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Foo` representable
9+
help: insert some indirection to make `Foo` representable
10+
|
11+
LL | struct Foo { foo: Box<Option<Option<Foo>>> }
12+
| ^^^^ ^
13+
LL | struct Foo { foo: Rc<Option<Option<Foo>>> }
14+
| ^^^ ^
15+
LL | struct Foo { foo: &Option<Option<Foo>> }
16+
| ^
1017

1118
error: aborting due to previous error
1219

src/test/ui/issues/issue-17431-2.stderr

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,35 @@ error[E0072]: recursive type `Baz` has infinite size
22
--> $DIR/issue-17431-2.rs:1:1
33
|
44
LL | struct Baz { q: Option<Foo> }
5-
| ^^^^^^^^^^ -------------- recursive without indirection
5+
| ^^^^^^^^^^ ----------- recursive without indirection
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Baz` representable
9+
help: insert some indirection to make `Baz` representable
10+
|
11+
LL | struct Baz { q: Box<Option<Foo>> }
12+
| ^^^^ ^
13+
LL | struct Baz { q: Rc<Option<Foo>> }
14+
| ^^^ ^
15+
LL | struct Baz { q: &Option<Foo> }
16+
| ^
1017

1118
error[E0072]: recursive type `Foo` has infinite size
1219
--> $DIR/issue-17431-2.rs:4:1
1320
|
1421
LL | struct Foo { q: Option<Baz> }
15-
| ^^^^^^^^^^ -------------- recursive without indirection
22+
| ^^^^^^^^^^ ----------- recursive without indirection
1623
| |
1724
| recursive type has infinite size
1825
|
19-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Foo` representable
26+
help: insert some indirection to make `Foo` representable
27+
|
28+
LL | struct Foo { q: Box<Option<Baz>> }
29+
| ^^^^ ^
30+
LL | struct Foo { q: Rc<Option<Baz>> }
31+
| ^^^ ^
32+
LL | struct Foo { q: &Option<Baz> }
33+
| ^
2034

2135
error: aborting due to 2 previous errors
2236

src/test/ui/issues/issue-17431-3.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ error[E0072]: recursive type `Foo` has infinite size
22
--> $DIR/issue-17431-3.rs:3:1
33
|
44
LL | struct Foo { foo: Mutex<Option<Foo>> }
5-
| ^^^^^^^^^^ ----------------------- recursive without indirection
5+
| ^^^^^^^^^^ ------------------ recursive without indirection
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Foo` representable
9+
help: insert some indirection to make `Foo` representable
10+
|
11+
LL | struct Foo { foo: Box<Mutex<Option<Foo>>> }
12+
| ^^^^ ^
13+
LL | struct Foo { foo: Rc<Mutex<Option<Foo>>> }
14+
| ^^^ ^
15+
LL | struct Foo { foo: &Mutex<Option<Foo>> }
16+
| ^
1017

1118
error: aborting due to previous error
1219

src/test/ui/issues/issue-17431-4.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ error[E0072]: recursive type `Foo` has infinite size
22
--> $DIR/issue-17431-4.rs:3:1
33
|
44
LL | struct Foo<T> { foo: Option<Option<Foo<T>>>, marker: marker::PhantomData<T> }
5-
| ^^^^^^^^^^^^^ --------------------------- recursive without indirection
5+
| ^^^^^^^^^^^^^ ---------------------- recursive without indirection
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Foo` representable
9+
help: insert some indirection to make `Foo` representable
10+
|
11+
LL | struct Foo<T> { foo: Box<Option<Option<Foo<T>>>>, marker: marker::PhantomData<T> }
12+
| ^^^^ ^
13+
LL | struct Foo<T> { foo: Rc<Option<Option<Foo<T>>>>, marker: marker::PhantomData<T> }
14+
| ^^^ ^
15+
LL | struct Foo<T> { foo: &Option<Option<Foo<T>>>, marker: marker::PhantomData<T> }
16+
| ^
1017

1118
error: aborting due to previous error
1219

src/test/ui/issues/issue-17431-5.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ error[E0072]: recursive type `Bar` has infinite size
22
--> $DIR/issue-17431-5.rs:5:1
33
|
44
LL | struct Bar<T> { x: Bar<Foo> , marker: marker::PhantomData<T> }
5-
| ^^^^^^^^^^^^^ ----------- recursive without indirection
5+
| ^^^^^^^^^^^^^ -------- recursive without indirection
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Bar` representable
9+
help: insert some indirection to make `Bar` representable
10+
|
11+
LL | struct Bar<T> { x: Box<Bar<Foo>> , marker: marker::PhantomData<T> }
12+
| ^^^^ ^
13+
LL | struct Bar<T> { x: Rc<Bar<Foo>> , marker: marker::PhantomData<T> }
14+
| ^^^ ^
15+
LL | struct Bar<T> { x: &Bar<Foo> , marker: marker::PhantomData<T> }
16+
| ^
1017

1118
error: aborting due to previous error
1219

src/test/ui/issues/issue-17431-6.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ LL | enum Foo { X(Mutex<Option<Foo>>) }
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Foo` representable
9+
help: insert some indirection to make `Foo` representable
10+
|
11+
LL | enum Foo { X(Box<Mutex<Option<Foo>>>) }
12+
| ^^^^ ^
13+
LL | enum Foo { X(Rc<Mutex<Option<Foo>>>) }
14+
| ^^^ ^
15+
LL | enum Foo { X(&Mutex<Option<Foo>>) }
16+
| ^
1017

1118
error: aborting due to previous error
1219

src/test/ui/issues/issue-17431-7.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ LL | enum Foo { Voo(Option<Option<Foo>>) }
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Foo` representable
9+
help: insert some indirection to make `Foo` representable
10+
|
11+
LL | enum Foo { Voo(Box<Option<Option<Foo>>>) }
12+
| ^^^^ ^
13+
LL | enum Foo { Voo(Rc<Option<Option<Foo>>>) }
14+
| ^^^ ^
15+
LL | enum Foo { Voo(&Option<Option<Foo>>) }
16+
| ^
1017

1118
error: aborting due to previous error
1219

src/test/ui/issues/issue-2718-a.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ LL | pub struct Pong(SendPacket<Ping>);
77
| | recursive without indirection
88
| recursive type has infinite size
99
|
10-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `pingpong::Pong` representable
10+
help: insert some indirection to make `pingpong::Pong` representable
11+
|
12+
LL | pub struct Pong(Box<SendPacket<Ping>>);
13+
| ^^^^ ^
14+
LL | pub struct Pong(Rc<SendPacket<Ping>>);
15+
| ^^^ ^
16+
LL | pub struct Pong(&SendPacket<Ping>);
17+
| ^
1118

1219
error: aborting due to previous error
1320

src/test/ui/issues/issue-3008-1.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ LL | enum Bar {
77
LL | BarSome(Bar)
88
| --- recursive without indirection
99
|
10-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Bar` representable
10+
help: insert some indirection to make `Bar` representable
11+
|
12+
LL | BarSome(Box<Bar>)
13+
| ^^^^ ^
14+
LL | BarSome(Rc<Bar>)
15+
| ^^^ ^
16+
LL | BarSome(&Bar)
17+
| ^
1118

1219
error: aborting due to previous error
1320

src/test/ui/issues/issue-3008-2.stderr

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@ error[E0072]: recursive type `Bar` has infinite size
22
--> $DIR/issue-3008-2.rs:2:1
33
|
44
LL | struct Bar { x: Bar }
5-
| ^^^^^^^^^^ ------ recursive without indirection
5+
| ^^^^^^^^^^ --- recursive without indirection
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `Bar` representable
9+
help: insert some indirection to make `Bar` representable
10+
|
11+
LL | struct Bar { x: Box<Bar> }
12+
| ^^^^ ^
13+
LL | struct Bar { x: Rc<Bar> }
14+
| ^^^ ^
15+
LL | struct Bar { x: &Bar }
16+
| ^
1017

1118
error: aborting due to previous error
1219

src/test/ui/issues/issue-3008-3.stderr

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ LL | enum E2<T> { V2(E2<E1>, marker::PhantomData<T>), }
66
| |
77
| recursive type has infinite size
88
|
9-
= help: insert indirection (e.g., a `Box`, `Rc`, or `&`) at some point to make `E2` representable
9+
help: insert some indirection to make `E2` representable
10+
|
11+
LL | enum E2<T> { V2(Box<E2<E1>>, marker::PhantomData<T>), }
12+
| ^^^^ ^
13+
LL | enum E2<T> { V2(Rc<E2<E1>>, marker::PhantomData<T>), }
14+
| ^^^ ^
15+
LL | enum E2<T> { V2(&E2<E1>, marker::PhantomData<T>), }
16+
| ^
1017

1118
error: aborting due to previous error
1219

0 commit comments

Comments
 (0)