]> git.lizzy.rs Git - rust.git/commitdiff
Don't suggest incorrect syntax
authorEsteban Küber <esteban@kuber.com.ar>
Wed, 20 Jun 2018 23:55:52 +0000 (16:55 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Wed, 20 Jun 2018 23:55:52 +0000 (16:55 -0700)
src/librustc_typeck/check/method/suggest.rs
src/test/ui/suggestions/method-on-ambiguous-numeric-type.rs
src/test/ui/suggestions/method-on-ambiguous-numeric-type.stderr

index 90680b4156e7a825693bc6399eb9ebd360e2de21..59439d80bac466073d5bf5b04abe70773ee4c27b 100644 (file)
@@ -187,7 +187,6 @@ pub fn report_method_error(&self,
                 out_of_scope_traits,
                 lev_candidate,
                 mode,
-                ..
             }) => {
                 let tcx = self.tcx;
 
@@ -264,13 +263,29 @@ pub fn report_method_error(&self,
                                         let span = tcx.hir.span(node_id);
                                         let snippet = tcx.sess.codemap().span_to_snippet(span)
                                             .unwrap();
-                                        err.span_suggestion(span,
-                                                            &format!("you must specify a type for \
-                                                                      this binding, like `{}`",
-                                                                     concrete_type),
-                                                            format!("{}: {}",
-                                                                    snippet,
-                                                                    concrete_type));
+
+                                        let parent_node = self.tcx.hir.get(
+                                            self.tcx.hir.get_parent_node(node_id),
+                                        );
+                                        let msg = format!(
+                                            "you must specify a type for this binding, like `{}`",
+                                            concrete_type,
+                                        );
+                                        match parent_node {
+                                            hir_map::NodeLocal(hir::Local {
+                                                source: hir::LocalSource::Normal,
+                                                ..
+                                            }) => {
+                                                err.span_suggestion(
+                                                    span,
+                                                    &msg,
+                                                    format!("{}: {}", snippet, concrete_type),
+                                                );
+                                            }
+                                            _ => {
+                                                err.span_label(span, msg);
+                                            }
+                                        }
                                     }
                                 }
                             }
index 9bf74c3875fed5f379d35372a1baea0db62d2ec2..42cfdb22c3724ed3401cfba7ed56011d274f5d24 100644 (file)
@@ -15,4 +15,8 @@ fn main() {
     let x = y.neg();
     //~^ ERROR can't call method `neg` on ambiguous numeric type `{float}`
     println!("{:?}", x);
+    for i in 0..100 {
+        println!("{}", i.pow(2));
+        //~^ ERROR can't call method `pow` on ambiguous numeric type `{integer}`
+    }
 }
index 68c8be7dff8c941de2164d31a72574735a55e3b6..417d7e8481d07fcd543fcf99b6efc130646e6476 100644 (file)
@@ -18,6 +18,14 @@ help: you must specify a type for this binding, like `f32`
 LL |     let y: f32 = 2.0;
    |         ^^^^^^
 
-error: aborting due to 2 previous errors
+error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
+  --> $DIR/method-on-ambiguous-numeric-type.rs:19:26
+   |
+LL |     for i in 0..100 {
+   |         - you must specify a type for this binding, like `i32`
+LL |         println!("{}", i.pow(2));
+   |                          ^^^
+
+error: aborting due to 3 previous errors
 
 For more information about this error, try `rustc --explain E0689`.