]> git.lizzy.rs Git - rust.git/commitdiff
review comments
authorEsteban Küber <esteban@kuber.com.ar>
Tue, 30 Apr 2019 02:56:50 +0000 (19:56 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Tue, 30 Apr 2019 02:56:50 +0000 (19:56 -0700)
src/librustc_typeck/check/_match.rs
src/test/ui/destructure-trait-ref.stderr
src/test/ui/mismatched_types/issue-38371.stderr
src/test/ui/suggestions/match-ergonomics.stderr

index 53d1ac7b02496528274f8c208034b41d54239ad7..124afc5491e89dba3510d2b308d0a7b94ba81400 100644 (file)
@@ -1,8 +1,8 @@
 use crate::check::{FnCtxt, Expectation, Diverges, Needs};
 use crate::check::coercion::CoerceMany;
 use crate::util::nodemap::FxHashMap;
-use errors::Applicability;
-use rustc::hir::{self, PatKind};
+use errors::{Applicability, DiagnosticBuilder};
+use rustc::hir::{self, PatKind, Pat};
 use rustc::hir::def::{Def, CtorKind};
 use rustc::hir::pat_util::EnumerateAndAdjustIterator;
 use rustc::infer;
@@ -377,64 +377,7 @@ pub fn check_pat_walk(
                             // Look for a case like `fn foo(&foo: u32)` and suggest
                             // `fn foo(foo: &u32)`
                             if let Some(mut err) = err {
-                                if let PatKind::Binding(..) = inner.node {
-                                    let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
-                                    let parent = tcx.hir().get_by_hir_id(parent_id);
-                                    debug!("inner {:?} pat {:?} parent {:?}", inner, pat, parent);
-                                    match parent {
-                                        hir::Node::Item(hir::Item {
-                                            node: hir::ItemKind::Fn(..), ..
-                                        }) |
-                                        hir::Node::ForeignItem(hir::ForeignItem {
-                                            node: hir::ForeignItemKind::Fn(..), ..
-                                        }) |
-                                        hir::Node::TraitItem(hir::TraitItem {
-                                            node: hir::TraitItemKind::Method(..), ..
-                                        }) |
-                                        hir::Node::ImplItem(hir::ImplItem {
-                                            node: hir::ImplItemKind::Method(..), ..
-                                        }) => { // this pat is likely an argument
-                                            if let Ok(snippet) = tcx.sess.source_map()
-                                                .span_to_snippet(inner.span)
-                                            { // FIXME: turn into structured suggestion, will need
-                                              // a span that also includes the the arg's type.
-                                                err.help(&format!(
-                                                    "did you mean `{}: &{}`?",
-                                                    snippet,
-                                                    expected,
-                                                ));
-                                            }
-                                        }
-                                        hir::Node::Expr(hir::Expr {
-                                            node: hir::ExprKind::Match(..), ..
-                                        }) => { // rely on match ergonomics
-                                            if let Ok(snippet) = tcx.sess.source_map()
-                                                .span_to_snippet(inner.span)
-                                            {
-                                                err.span_suggestion(
-                                                    pat.span,
-                                                    "you can rely on match ergonomics and remove \
-                                                     the explicit borrow",
-                                                    snippet,
-                                                    Applicability::MaybeIncorrect,
-                                                );
-                                            }
-                                        }
-                                        hir::Node::Pat(_) => {  // nested `&&pat`
-                                            if let Ok(snippet) = tcx.sess.source_map()
-                                                .span_to_snippet(inner.span)
-                                            {
-                                                err.span_suggestion(
-                                                    pat.span,
-                                                    "you can probaly remove the explicit borrow",
-                                                    snippet,
-                                                    Applicability::MaybeIncorrect,
-                                                );
-                                            }
-                                        }
-                                        _ => {} // don't provide suggestions in other cases #55175
-                                    }
-                                }
+                                self.borrow_pat_suggestion(&mut err, &pat, &inner, &expected);
                                 err.emit();
                             }
                             (rptr_ty, inner_ty)
@@ -566,6 +509,59 @@ pub fn check_pat_walk(
         // subtyping.
     }
 
+    fn borrow_pat_suggestion(
+        &self,
+        err: &mut DiagnosticBuilder<'_>,
+        pat: &Pat,
+        inner: &Pat,
+        expected: &Ty<'tcx>,
+    ) {
+        let tcx = self.tcx;
+        if let PatKind::Binding(..) = inner.node {
+            let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
+            let parent = tcx.hir().get_by_hir_id(parent_id);
+            debug!("inner {:?} pat {:?} parent {:?}", inner, pat, parent);
+            match parent {
+                hir::Node::Item(hir::Item { node: hir::ItemKind::Fn(..), .. }) |
+                hir::Node::ForeignItem(hir::ForeignItem {
+                    node: hir::ForeignItemKind::Fn(..), ..
+                }) |
+                hir::Node::TraitItem(hir::TraitItem { node: hir::TraitItemKind::Method(..), .. }) |
+                hir::Node::ImplItem(hir::ImplItem { node: hir::ImplItemKind::Method(..), .. }) => {
+                    // this pat is likely an argument
+                    if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
+                        // FIXME: turn into structured suggestion, will need a span that also
+                        // includes the the arg's type.
+                        err.help(&format!("did you mean `{}: &{}`?", snippet, expected));
+                    }
+                }
+                hir::Node::Expr(hir::Expr { node: hir::ExprKind::Match(..), .. }) => {
+                    // rely on match ergonomics
+                    if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
+                        err.span_suggestion(
+                            pat.span,
+                            "you can rely on match ergonomics and remove the explicit borrow",
+                            snippet,
+                            Applicability::MaybeIncorrect,
+                        );
+                    }
+                }
+                hir::Node::Pat(_) => {
+                    // nested `&&pat`
+                    if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(inner.span) {
+                        err.span_suggestion(
+                            pat.span,
+                            "you can probably remove the explicit borrow",
+                            snippet,
+                            Applicability::MaybeIncorrect,
+                        );
+                    }
+                }
+                _ => {} // don't provide suggestions in other cases #55175
+            }
+        }
+    }
+
     pub fn check_dereferencable(&self, span: Span, expected: Ty<'tcx>, inner: &hir::Pat) -> bool {
         if let PatKind::Binding(..) = inner.node {
             if let Some(mt) = self.shallow_resolve(expected).builtin_deref(true) {
index 47ecadfd01afb1508776a487e192b57c2307aafc..bc3013b78b38c8a0886f4df0834fdeb89c49bad9 100644 (file)
@@ -23,7 +23,7 @@ LL |     let &&x = &1isize as &T;
    |          ^^
    |          |
    |          expected trait T, found reference
-   |          help: you can probaly remove the explicit borrow: `x`
+   |          help: you can probably remove the explicit borrow: `x`
    |
    = note: expected type `dyn T`
               found type `&_`
@@ -35,7 +35,7 @@ LL |     let &&&x = &(&1isize as &T);
    |           ^^
    |           |
    |           expected trait T, found reference
-   |           help: you can probaly remove the explicit borrow: `x`
+   |           help: you can probably remove the explicit borrow: `x`
    |
    = note: expected type `dyn T`
               found type `&_`
index 833b8998c338a35a2ce93225c114863cd76cd76f..a9347926bda0a554ba69bb081e38d75d2b178540 100644 (file)
@@ -15,7 +15,7 @@ LL | fn agh(&&bar: &u32) {
    |         ^^^^
    |         |
    |         expected u32, found reference
-   |         help: you can probaly remove the explicit borrow: `bar`
+   |         help: you can probably remove the explicit borrow: `bar`
    |
    = note: expected type `u32`
               found type `&_`
index 4239089b7cec765364a7e3eaf1038c7ccfb47fcb..a064e2485ffe806a7b3e0a6722165e2a5bd1086c 100644 (file)
@@ -5,7 +5,7 @@ LL |         [&v] => {},
    |          ^^
    |          |
    |          expected i32, found reference
-   |          help: you can probaly remove the explicit borrow: `v`
+   |          help: you can probably remove the explicit borrow: `v`
    |
    = note: expected type `i32`
               found type `&_`