about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDaiki Ihara <sasurau4@gmail.com>2020-12-14 21:13:40 +0900
committerDaiki Ihara <sasurau4@gmail.com>2020-12-17 20:22:22 +0900
commit7b9ee11a4c1d4fde0ecb0e45e0ebf674e5c775a8 (patch)
tree7946118cc41837018d8a96afc4c6cfc0b395a4d4
parente99a89c7c0b6865a680a2d6169847ec8acc001d3 (diff)
downloadrust-7b9ee11a4c1d4fde0ecb0e45e0ebf674e5c775a8.tar.gz
rust-7b9ee11a4c1d4fde0ecb0e45e0ebf674e5c775a8.zip
Enhance error message when misspelled label to value in break expression
Apply suggestions from code review

Co-authored-by: lcnr <bastian_kauschke@hotmail.de>
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index 6ce299a9417..68f59baffce 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -542,6 +542,26 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
                 err.span_label(base_span, fallback_label);
             }
         }
+        if let Some(err_code) = &err.code {
+            if err_code == &rustc_errors::error_code!(E0425) {
+                for label_rib in &self.label_ribs {
+                    for (label_ident, _) in &label_rib.bindings {
+                        if format!("'{}", ident) == label_ident.to_string() {
+                            let msg = "a label with a similar name exists";
+                            // FIXME: consider only emitting this suggestion if a label would be valid here
+                            // which is pretty much only the case for `break` expressions.
+                            err.span_suggestion(
+                                span,
+                                &msg,
+                                label_ident.name.to_string(),
+                                Applicability::MaybeIncorrect,
+                            );
+                        }
+                    }
+                }
+            }
+        }
+
         (err, candidates)
     }