@@ -1747,24 +1747,62 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
17471747pub fn recursive_type_with_infinite_size_error (
17481748 tcx : TyCtxt < ' tcx > ,
17491749 type_def_id : DefId ,
1750- ) -> DiagnosticBuilder < ' tcx > {
1750+ spans : Vec < Span > ,
1751+ ) {
17511752 assert ! ( type_def_id. is_local( ) ) ;
17521753 let span = tcx. hir ( ) . span_if_local ( type_def_id) . unwrap ( ) ;
17531754 let span = tcx. sess . source_map ( ) . guess_head_span ( span) ;
1754- let mut err = struct_span_err ! (
1755- tcx. sess,
1756- span,
1757- E0072 ,
1758- "recursive type `{}` has infinite size" ,
1759- tcx. def_path_str( type_def_id)
1760- ) ;
1755+ let path = tcx. def_path_str ( type_def_id) ;
1756+ let mut err =
1757+ struct_span_err ! ( tcx. sess, span, E0072 , "recursive type `{}` has infinite size" , path) ;
17611758 err. span_label ( span, "recursive type has infinite size" ) ;
1762- err. help ( & format ! (
1763- "insert indirection (e.g., a `Box`, `Rc`, or `&`) \
1764- at some point to make `{}` representable",
1765- tcx. def_path_str( type_def_id)
1766- ) ) ;
1767- err
1759+ for & span in & spans {
1760+ err. span_label ( span, "recursive without indirection" ) ;
1761+ }
1762+ let short_msg = format ! ( "insert some indirection to make `{}` representable" , path) ;
1763+ let msg = format ! (
1764+ "insert some indirection (e.g., a `Box`, `Rc`, or `&`) to make `{}` representable" ,
1765+ path,
1766+ ) ;
1767+ match & spans[ ..] {
1768+ [ span] => {
1769+ err. multipart_suggestions (
1770+ & short_msg,
1771+ vec ! [
1772+ vec![
1773+ ( span. shrink_to_lo( ) , "Box<" . to_string( ) ) ,
1774+ ( span. shrink_to_hi( ) , ">" . to_string( ) ) ,
1775+ ] ,
1776+ vec![
1777+ ( span. shrink_to_lo( ) , "Rc<" . to_string( ) ) ,
1778+ ( span. shrink_to_hi( ) , ">" . to_string( ) ) ,
1779+ ] ,
1780+ vec![ ( span. shrink_to_lo( ) , "&" . to_string( ) ) ] ,
1781+ ] ,
1782+ Applicability :: HasPlaceholders ,
1783+ ) ;
1784+ }
1785+ _ if spans. len ( ) <= 4 => {
1786+ err. multipart_suggestion (
1787+ & msg,
1788+ spans
1789+ . iter ( )
1790+ . flat_map ( |& span| {
1791+ vec ! [
1792+ ( span. shrink_to_lo( ) , "Box<" . to_string( ) ) ,
1793+ ( span. shrink_to_hi( ) , ">" . to_string( ) ) ,
1794+ ]
1795+ . into_iter ( )
1796+ } )
1797+ . collect ( ) ,
1798+ Applicability :: HasPlaceholders ,
1799+ ) ;
1800+ }
1801+ _ => {
1802+ err. help ( & msg) ;
1803+ }
1804+ }
1805+ err. emit ( ) ;
17681806}
17691807
17701808/// Summarizes information
0 commit comments