From 2883186180161185e72b8fa183b75c8854eb5db4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Esteban=20K=C3=BCber?= <esteban@kuber.com.ar>
Date: Wed, 18 Jan 2017 16:33:25 -0800
Subject: [PATCH] Use multiline Diagnostic for candidate in other module

---
 src/librustc_resolve/lib.rs                   | 33 ++++++++++---------
 .../ui/resolve/enums-are-namespaced-xc.stderr |  6 ++--
 src/test/ui/resolve/issue-16058.stderr        |  6 ++--
 src/test/ui/resolve/issue-17518.stderr        |  2 +-
 src/test/ui/resolve/issue-21221-1.stderr      | 18 +++++-----
 src/test/ui/resolve/issue-21221-2.stderr      |  2 +-
 src/test/ui/resolve/issue-21221-3.stderr      |  2 +-
 src/test/ui/resolve/issue-21221-4.stderr      |  2 +-
 src/test/ui/resolve/issue-3907.stderr         |  2 +-
 src/test/ui/span/issue-35987.stderr           |  2 +-
 10 files changed, 38 insertions(+), 37 deletions(-)

diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs
index 56e8c75b859a1..fabc8790c2a6d 100644
--- a/src/librustc_resolve/lib.rs
+++ b/src/librustc_resolve/lib.rs
@@ -67,6 +67,7 @@ use syntax_pos::{Span, DUMMY_SP, MultiSpan};
 use errors::DiagnosticBuilder;
 
 use std::cell::{Cell, RefCell};
+use std::cmp;
 use std::fmt;
 use std::mem::replace;
 use std::rc::Rc;
@@ -3224,7 +3225,7 @@ fn show_candidates(session: &mut DiagnosticBuilder,
                    better: bool) {
     // don't show more than MAX_CANDIDATES results, so
     // we're consistent with the trait suggestions
-    const MAX_CANDIDATES: usize = 5;
+    const MAX_CANDIDATES: usize = 4;
 
     // we want consistent results across executions, but candidates are produced
     // by iterating through a hash map, so make sure they are ordered:
@@ -3237,21 +3238,21 @@ fn show_candidates(session: &mut DiagnosticBuilder,
         1 => " is found in another module, you can import it",
         _ => "s are found in other modules, you can import them",
     };
-    session.help(&format!("possible {}candidate{} into scope:", better, msg_diff));
-
-    let count = path_strings.len() as isize - MAX_CANDIDATES as isize + 1;
-    for (idx, path_string) in path_strings.iter().enumerate() {
-        if idx == MAX_CANDIDATES - 1 && count > 1 {
-            session.help(
-                &format!("  and {} other candidates", count).to_string(),
-            );
-            break;
-        } else {
-            session.help(
-                &format!("  `use {};`", path_string).to_string(),
-            );
-        }
-    }
+
+    let end = cmp::min(MAX_CANDIDATES, path_strings.len());
+    session.help(&format!("possible {}candidate{} into scope:{}{}",
+                          better,
+                          msg_diff,
+                          &path_strings[0..end].iter().map(|candidate| {
+                              format!("\n  `use {};`", candidate)
+                          }).collect::<String>(),
+                          if path_strings.len() > MAX_CANDIDATES {
+                              format!("\nand {} other candidates",
+                                      path_strings.len() - MAX_CANDIDATES)
+                          } else {
+                              "".to_owned()
+                          }
+                          ));
 }
 
 /// A somewhat inefficient routine to obtain the name of a module.
diff --git a/src/test/ui/resolve/enums-are-namespaced-xc.stderr b/src/test/ui/resolve/enums-are-namespaced-xc.stderr
index 204c574a3a996..d541aa599a48b 100644
--- a/src/test/ui/resolve/enums-are-namespaced-xc.stderr
+++ b/src/test/ui/resolve/enums-are-namespaced-xc.stderr
@@ -5,7 +5,7 @@ error[E0425]: cannot find value `A` in module `namespaced_enums`
    |             ^^^^^^^^^^^^^^^^^^^ not found in `namespaced_enums`
    |
    = help: possible candidate is found in another module, you can import it into scope:
-   = help:   `use namespaced_enums::Foo::A;`
+             `use namespaced_enums::Foo::A;`
 
 error[E0425]: cannot find function `B` in module `namespaced_enums`
   --> $DIR/enums-are-namespaced-xc.rs:18:13
@@ -14,7 +14,7 @@ error[E0425]: cannot find function `B` in module `namespaced_enums`
    |             ^^^^^^^^^^^^^^^^^^^ not found in `namespaced_enums`
    |
    = help: possible candidate is found in another module, you can import it into scope:
-   = help:   `use namespaced_enums::Foo::B;`
+             `use namespaced_enums::Foo::B;`
 
 error[E0422]: cannot find struct, variant or union type `C` in module `namespaced_enums`
   --> $DIR/enums-are-namespaced-xc.rs:21:13
@@ -23,7 +23,7 @@ error[E0422]: cannot find struct, variant or union type `C` in module `namespace
    |             ^^^^^^^^^^^^^^^^^^^ not found in `namespaced_enums`
    |
    = help: possible candidate is found in another module, you can import it into scope:
-   = help:   `use namespaced_enums::Foo::C;`
+             `use namespaced_enums::Foo::C;`
 
 error: aborting due to 3 previous errors
 
diff --git a/src/test/ui/resolve/issue-16058.stderr b/src/test/ui/resolve/issue-16058.stderr
index 6b00cfb869316..69c48cc1f3241 100644
--- a/src/test/ui/resolve/issue-16058.stderr
+++ b/src/test/ui/resolve/issue-16058.stderr
@@ -5,9 +5,9 @@ error[E0574]: expected struct, variant or union type, found enum `Result`
    |         ^^^^^^ not a struct, variant or union type
    |
    = help: possible better candidates are found in other modules, you can import them into scope:
-   = help:   `use std::fmt::Result;`
-   = help:   `use std::io::Result;`
-   = help:   `use std::thread::Result;`
+             `use std::fmt::Result;`
+             `use std::io::Result;`
+             `use std::thread::Result;`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/resolve/issue-17518.stderr b/src/test/ui/resolve/issue-17518.stderr
index 335510ed7e9d5..ea6841e600972 100644
--- a/src/test/ui/resolve/issue-17518.stderr
+++ b/src/test/ui/resolve/issue-17518.stderr
@@ -5,7 +5,7 @@ error[E0422]: cannot find struct, variant or union type `E` in this scope
    |     ^ not found in this scope
    |
    = help: possible candidate is found in another module, you can import it into scope:
-   = help:   `use SomeEnum::E;`
+             `use SomeEnum::E;`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/resolve/issue-21221-1.stderr b/src/test/ui/resolve/issue-21221-1.stderr
index d9c2f9e681eee..f38491d536258 100644
--- a/src/test/ui/resolve/issue-21221-1.stderr
+++ b/src/test/ui/resolve/issue-21221-1.stderr
@@ -5,9 +5,9 @@ error[E0405]: cannot find trait `Mul` in this scope
    |      ^^^ not found in this scope
    |
    = help: possible candidates are found in other modules, you can import them into scope:
-   = help:   `use mul1::Mul;`
-   = help:   `use mul2::Mul;`
-   = help:   `use std::ops::Mul;`
+             `use mul1::Mul;`
+             `use mul2::Mul;`
+             `use std::ops::Mul;`
 
 error[E0412]: cannot find type `Mul` in this scope
   --> $DIR/issue-21221-1.rs:72:16
@@ -16,11 +16,11 @@ error[E0412]: cannot find type `Mul` in this scope
    |                ^^^ not found in this scope
    |
    = help: possible candidates are found in other modules, you can import them into scope:
-   = help:   `use mul1::Mul;`
-   = help:   `use mul2::Mul;`
-   = help:   `use mul3::Mul;`
-   = help:   `use mul4::Mul;`
-   = help:   and 2 other candidates
+             `use mul1::Mul;`
+             `use mul2::Mul;`
+             `use mul3::Mul;`
+             `use mul4::Mul;`
+           and 2 other candidates
 
 error[E0405]: cannot find trait `ThisTraitReallyDoesntExistInAnyModuleReally` in this scope
   --> $DIR/issue-21221-1.rs:83:6
@@ -35,7 +35,7 @@ error[E0405]: cannot find trait `Div` in this scope
    |      ^^^ not found in this scope
    |
    = help: possible candidate is found in another module, you can import it into scope:
-   = help:   `use std::ops::Div;`
+             `use std::ops::Div;`
 
 error: cannot continue compilation due to previous error
 
diff --git a/src/test/ui/resolve/issue-21221-2.stderr b/src/test/ui/resolve/issue-21221-2.stderr
index eb5b1ab6918a9..14dac7de4b2e1 100644
--- a/src/test/ui/resolve/issue-21221-2.stderr
+++ b/src/test/ui/resolve/issue-21221-2.stderr
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `T` in this scope
    |      ^ not found in this scope
    |
    = help: possible candidate is found in another module, you can import it into scope:
-   = help:   `use foo::bar::T;`
+             `use foo::bar::T;`
 
 error: main function not found
 
diff --git a/src/test/ui/resolve/issue-21221-3.stderr b/src/test/ui/resolve/issue-21221-3.stderr
index 3594e9105a276..e1e00571e5d4a 100644
--- a/src/test/ui/resolve/issue-21221-3.stderr
+++ b/src/test/ui/resolve/issue-21221-3.stderr
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `OuterTrait` in this scope
    |      ^^^^^^^^^^ not found in this scope
    |
    = help: possible candidate is found in another module, you can import it into scope:
-   = help:   `use issue_21221_3::outer::OuterTrait;`
+             `use issue_21221_3::outer::OuterTrait;`
 
 error: cannot continue compilation due to previous error
 
diff --git a/src/test/ui/resolve/issue-21221-4.stderr b/src/test/ui/resolve/issue-21221-4.stderr
index 98486e5743cc9..569315a59cf34 100644
--- a/src/test/ui/resolve/issue-21221-4.stderr
+++ b/src/test/ui/resolve/issue-21221-4.stderr
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `T` in this scope
    |      ^ not found in this scope
    |
    = help: possible candidate is found in another module, you can import it into scope:
-   = help:   `use issue_21221_4::T;`
+             `use issue_21221_4::T;`
 
 error: cannot continue compilation due to previous error
 
diff --git a/src/test/ui/resolve/issue-3907.stderr b/src/test/ui/resolve/issue-3907.stderr
index 0a402680aa8be..a7dd494d75b0d 100644
--- a/src/test/ui/resolve/issue-3907.stderr
+++ b/src/test/ui/resolve/issue-3907.stderr
@@ -5,7 +5,7 @@ error[E0404]: expected trait, found type alias `Foo`
    |      ^^^ type aliases cannot be used for traits
    |
    = help: possible better candidate is found in another module, you can import it into scope:
-   = help:   `use issue_3907::Foo;`
+             `use issue_3907::Foo;`
 
 error: cannot continue compilation due to previous error
 
diff --git a/src/test/ui/span/issue-35987.stderr b/src/test/ui/span/issue-35987.stderr
index 764f34cabde9a..9dab2f7789815 100644
--- a/src/test/ui/span/issue-35987.stderr
+++ b/src/test/ui/span/issue-35987.stderr
@@ -5,7 +5,7 @@ error[E0404]: expected trait, found type parameter `Add`
    |                     ^^^ not a trait
    |
    = help: possible better candidate is found in another module, you can import it into scope:
-   = help:   `use std::ops::Add;`
+             `use std::ops::Add;`
 
 error: main function not found