about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-04-29 15:18:52 +0000
committerbors <bors@rust-lang.org>2017-04-29 15:18:52 +0000
commitb4d3ed64ec7f6d7a9fa0530377a29520a90a451f (patch)
treef3f3f4d58dbf98cabe13c3907b55e01fdceb369f /src
parente326e86b47edcded3313944a81f126a633032d86 (diff)
parent0d7e6cf900b05cc2c97f1d49eb245bd27d1b00c9 (diff)
downloadrust-b4d3ed64ec7f6d7a9fa0530377a29520a90a451f.tar.gz
rust-b4d3ed64ec7f6d7a9fa0530377a29520a90a451f.zip
Auto merge of #39291 - Freyskeyd:check_context_E0423, r=petrochenkov
Checker:: Execute levenshtein before other context checking

As explain [here]() i think it's better to check for a miss typing before checking context dependent help.

```rust
struct Handle {}

struct Something {
     handle: Handle
}

fn main() {
     let handle: Handle = Handle {};

     let s: Something = Something {
         // Checker detect an error and propose a solution with `Handle { /* ... */ }`
         // but it's a miss typing of `handle`
         handle: Handle
    };
}
```

Ping: @nagisa for #39226

Signed-off-by: Freyskeyd <simon.paitrault@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/librustc_resolve/lib.rs18
-rw-r--r--src/test/ui/resolve/issue-39226.rs24
-rw-r--r--src/test/ui/resolve/issue-39226.stderr11
-rw-r--r--src/test/ui/resolve/issue-5035.stderr5
-rw-r--r--src/test/ui/resolve/privacy-struct-ctor.stderr1
-rw-r--r--src/test/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr10
-rw-r--r--src/test/ui/resolve/tuple-struct-alias.stderr10
7 files changed, 65 insertions, 14 deletions
diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs
index 6ba214f20f9..a4e9a8be49f 100644
--- a/src/librustc_resolve/lib.rs
+++ b/src/librustc_resolve/lib.rs
@@ -2312,6 +2312,14 @@ impl<'a> Resolver<'a> {
                 }
             }
 
+            let mut levenshtein_worked = false;
+
+            // Try Levenshtein.
+            if let Some(candidate) = this.lookup_typo_candidate(path, ns, is_expected) {
+                err.span_label(ident_span, &format!("did you mean `{}`?", candidate));
+                levenshtein_worked = true;
+            }
+
             // Try context dependent help if relaxed lookup didn't work.
             if let Some(def) = def {
                 match (def, source) {
@@ -2354,14 +2362,10 @@ impl<'a> Resolver<'a> {
                 }
             }
 
-            // Try Levenshtein if nothing else worked.
-            if let Some(candidate) = this.lookup_typo_candidate(path, ns, is_expected) {
-                err.span_label(ident_span, &format!("did you mean `{}`?", candidate));
-                return err;
-            }
-
             // Fallback label.
-            err.span_label(base_span, &fallback_label);
+            if !levenshtein_worked {
+                err.span_label(base_span, &fallback_label);
+            }
             err
         };
         let report_errors = |this: &mut Self, def: Option<Def>| {
diff --git a/src/test/ui/resolve/issue-39226.rs b/src/test/ui/resolve/issue-39226.rs
new file mode 100644
index 00000000000..f290a74861d
--- /dev/null
+++ b/src/test/ui/resolve/issue-39226.rs
@@ -0,0 +1,24 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+struct Handle {}
+
+struct Something {
+    handle: Handle
+}
+
+fn main() {
+    let handle: Handle = Handle {};
+
+    let s: Something = Something {
+        handle: Handle
+        //~^ ERROR cannot find value `Handle` in this scope
+        //~| NOTE did you mean `handle`?
+    };
+}
diff --git a/src/test/ui/resolve/issue-39226.stderr b/src/test/ui/resolve/issue-39226.stderr
new file mode 100644
index 00000000000..f6ee0b025bb
--- /dev/null
+++ b/src/test/ui/resolve/issue-39226.stderr
@@ -0,0 +1,11 @@
+error[E0423]: expected value, found struct `Handle`
+  --> $DIR/issue-39226.rs:20:17
+   |
+20 |         handle: Handle
+   |                 ^^^^^^
+   |                 |
+   |                 did you mean `handle`?
+   |                 did you mean `Handle { /* fields */ }`?
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/resolve/issue-5035.stderr b/src/test/ui/resolve/issue-5035.stderr
index 6cb9a289379..19adecc7b4b 100644
--- a/src/test/ui/resolve/issue-5035.stderr
+++ b/src/test/ui/resolve/issue-5035.stderr
@@ -8,7 +8,10 @@ error[E0404]: expected trait, found type alias `K`
   --> $DIR/issue-5035.rs:13:6
    |
 13 | impl K for isize {} //~ ERROR expected trait, found type alias `K`
-   |      ^ type aliases cannot be used for traits
+   |      ^
+   |      |
+   |      type aliases cannot be used for traits
+   |      did you mean `I`?
 
 error: cannot continue compilation due to previous error
 
diff --git a/src/test/ui/resolve/privacy-struct-ctor.stderr b/src/test/ui/resolve/privacy-struct-ctor.stderr
index 25afb6147e4..940e4acabb2 100644
--- a/src/test/ui/resolve/privacy-struct-ctor.stderr
+++ b/src/test/ui/resolve/privacy-struct-ctor.stderr
@@ -5,6 +5,7 @@ error[E0423]: expected value, found struct `Z`
    |         ^
    |         |
    |         did you mean `Z { /* fields */ }`?
+   |         did you mean `S`?
    |         constructor is not visible here due to private fields
    |
    = help: possible better candidate is found in another module, you can import it into scope:
diff --git a/src/test/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr b/src/test/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr
index 85fb1777dea..f254ad3d87d 100644
--- a/src/test/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr
+++ b/src/test/ui/resolve/suggest-path-instead-of-mod-dot-item.stderr
@@ -26,8 +26,9 @@ error[E0423]: expected value, found module `a::b`
   --> $DIR/suggest-path-instead-of-mod-dot-item.rs:45:5
    |
 45 |     a::b.J
-   |     ^^^^--
-   |     |
+   |     ^^^---
+   |     |  |
+   |     |  did you mean `I`?
    |     did you mean `a::b::J`?
 
 error[E0423]: expected value, found module `a`
@@ -50,8 +51,9 @@ error[E0423]: expected value, found module `a::b`
   --> $DIR/suggest-path-instead-of-mod-dot-item.rs:61:5
    |
 61 |     a::b.f()
-   |     ^^^^----
-   |     |
+   |     ^^^-----
+   |     |  |
+   |     |  did you mean `I`?
    |     did you mean `a::b::f(...)`?
 
 error[E0423]: expected value, found module `a::b`
diff --git a/src/test/ui/resolve/tuple-struct-alias.stderr b/src/test/ui/resolve/tuple-struct-alias.stderr
index 485c8ebbaeb..e2ef8f0e568 100644
--- a/src/test/ui/resolve/tuple-struct-alias.stderr
+++ b/src/test/ui/resolve/tuple-struct-alias.stderr
@@ -14,13 +14,19 @@ error[E0423]: expected function, found type alias `A`
   --> $DIR/tuple-struct-alias.rs:24:13
    |
 24 |     let s = A(0, 1);
-   |             ^ did you mean `A { /* fields */ }`?
+   |             ^
+   |             |
+   |             did you mean `S`?
+   |             did you mean `A { /* fields */ }`?
 
 error[E0532]: expected tuple struct/variant, found type alias `A`
   --> $DIR/tuple-struct-alias.rs:26:9
    |
 26 |         A(..) => {}
-   |         ^ did you mean `A { /* fields */ }`?
+   |         ^
+   |         |
+   |         did you mean `S`?
+   |         did you mean `A { /* fields */ }`?
 
 error: aborting due to 4 previous errors