about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-04-25 03:05:20 +0200
committerGitHub <noreply@github.com>2019-04-25 03:05:20 +0200
commita552bebaf60cfdce1a53241ce78462415ae3125c (patch)
treef6fae8ca1d4dac4d0db2c70b00817a94a8a69427 /src
parent5440cf1cb62a3c60a1295c8754582b334e252334 (diff)
parent3b686d5c251be3354d53c08c5a0c1f46894627a6 (diff)
downloadrust-a552bebaf60cfdce1a53241ce78462415ae3125c.tar.gz
rust-a552bebaf60cfdce1a53241ce78462415ae3125c.zip
Rollup merge of #59697 - euclio:label-fixes, r=zackmdavis
tweak unresolved label suggestion

Only suggest label names in the same hygiene context, and use a
structured suggestion.

Question for reviewer: Is this the right way to check for label hygiene?
Diffstat (limited to 'src')
-rw-r--r--src/librustc_resolve/lib.rs15
-rw-r--r--src/test/ui/hygiene/hygienic-label-1.stderr2
-rw-r--r--src/test/ui/hygiene/hygienic-label-2.stderr2
-rw-r--r--src/test/ui/hygiene/hygienic-label-3.stderr2
-rw-r--r--src/test/ui/hygiene/hygienic-label-4.stderr2
-rw-r--r--src/test/ui/suggestions/suggest-labels.stderr18
6 files changed, 32 insertions, 9 deletions
diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs
index 2ef05f7efeb..7754bb26f90 100644
--- a/src/librustc_resolve/lib.rs
+++ b/src/librustc_resolve/lib.rs
@@ -364,7 +364,12 @@ fn resolve_struct_error<'sess, 'a>(resolver: &'sess Resolver<'_>,
                                            "use of undeclared label `{}`",
                                            name);
             if let Some(lev_candidate) = lev_candidate {
-                err.span_label(span, format!("did you mean `{}`?", lev_candidate));
+                err.span_suggestion(
+                    span,
+                    "a label with a similar name exists in this scope",
+                    lev_candidate.to_string(),
+                    Applicability::MaybeIncorrect,
+                );
             } else {
                 err.span_label(span, format!("undeclared label `{}`", name));
             }
@@ -4280,7 +4285,13 @@ impl<'a> Resolver<'a> {
                         // Picks the first label that is "close enough", which is not necessarily
                         // the closest match
                         let close_match = self.search_label(label.ident, |rib, ident| {
-                            let names = rib.bindings.iter().map(|(id, _)| &id.name);
+                            let names = rib.bindings.iter().filter_map(|(id, _)| {
+                                if id.span.ctxt() == label.ident.span.ctxt() {
+                                    Some(&id.name)
+                                } else {
+                                    None
+                                }
+                            });
                             find_best_match_for_name(names, &*ident.as_str(), None)
                         });
                         self.record_def(expr.id, err_path_resolution());
diff --git a/src/test/ui/hygiene/hygienic-label-1.stderr b/src/test/ui/hygiene/hygienic-label-1.stderr
index 80cd1547b45..d61c0687c16 100644
--- a/src/test/ui/hygiene/hygienic-label-1.stderr
+++ b/src/test/ui/hygiene/hygienic-label-1.stderr
@@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
   --> $DIR/hygienic-label-1.rs:2:19
    |
 LL |     () => { break 'x; }
-   |                   ^^ did you mean `'x`?
+   |                   ^^ undeclared label `'x`
 ...
 LL |     'x: loop { foo!() }
    |                ------ in this macro invocation
diff --git a/src/test/ui/hygiene/hygienic-label-2.stderr b/src/test/ui/hygiene/hygienic-label-2.stderr
index c20cbd9f687..f23e741debe 100644
--- a/src/test/ui/hygiene/hygienic-label-2.stderr
+++ b/src/test/ui/hygiene/hygienic-label-2.stderr
@@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
   --> $DIR/hygienic-label-2.rs:6:16
    |
 LL |     foo!(break 'x);
-   |                ^^ did you mean `'x`?
+   |                ^^ undeclared label `'x`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/hygiene/hygienic-label-3.stderr b/src/test/ui/hygiene/hygienic-label-3.stderr
index b5839fe5c3d..0c4173a61aa 100644
--- a/src/test/ui/hygiene/hygienic-label-3.stderr
+++ b/src/test/ui/hygiene/hygienic-label-3.stderr
@@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
   --> $DIR/hygienic-label-3.rs:2:19
    |
 LL |     () => { break 'x; }
-   |                   ^^ did you mean `'x`?
+   |                   ^^ undeclared label `'x`
 ...
 LL |         foo!()
    |         ------ in this macro invocation
diff --git a/src/test/ui/hygiene/hygienic-label-4.stderr b/src/test/ui/hygiene/hygienic-label-4.stderr
index 1dd74895746..1c93da02f61 100644
--- a/src/test/ui/hygiene/hygienic-label-4.stderr
+++ b/src/test/ui/hygiene/hygienic-label-4.stderr
@@ -2,7 +2,7 @@ error[E0426]: use of undeclared label `'x`
   --> $DIR/hygienic-label-4.rs:6:16
    |
 LL |     foo!(break 'x);
-   |                ^^ did you mean `'x`?
+   |                ^^ undeclared label `'x`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/suggestions/suggest-labels.stderr b/src/test/ui/suggestions/suggest-labels.stderr
index ead8f94209b..02d46a3f596 100644
--- a/src/test/ui/suggestions/suggest-labels.stderr
+++ b/src/test/ui/suggestions/suggest-labels.stderr
@@ -2,19 +2,31 @@ error[E0426]: use of undeclared label `'fo`
   --> $DIR/suggest-labels.rs:4:15
    |
 LL |         break 'fo;
-   |               ^^^ did you mean `'foo`?
+   |               ^^^
+help: a label with a similar name exists in this scope
+   |
+LL |         break 'foo;
+   |               ^^^^
 
 error[E0426]: use of undeclared label `'bor`
   --> $DIR/suggest-labels.rs:8:18
    |
 LL |         continue 'bor;
-   |                  ^^^^ did you mean `'bar`?
+   |                  ^^^^
+help: a label with a similar name exists in this scope
+   |
+LL |         continue 'bar;
+   |                  ^^^^
 
 error[E0426]: use of undeclared label `'longlable`
   --> $DIR/suggest-labels.rs:13:19
    |
 LL |             break 'longlable;
-   |                   ^^^^^^^^^^ did you mean `'longlabel1`?
+   |                   ^^^^^^^^^^
+help: a label with a similar name exists in this scope
+   |
+LL |             break 'longlabel1;
+   |                   ^^^^^^^^^^^
 
 error: aborting due to 3 previous errors