]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/traits/error_reporting.rs
Remove index type check (review comment)
[rust.git] / src / librustc / traits / error_reporting.rs
index 46ec2be4a1f9bf2765347cde135c60652d3fddb2..46e2c3af5e632c2220f2e4f8e9c960c8951fadc6 100644 (file)
@@ -581,6 +581,8 @@ pub fn report_selection_error(&self,
                                                      trait_ref.self_ty()));
                         }
 
+                        self.suggest_borrow_on_unsized_slice(&obligation.cause.code, &mut err);
+
                         // Try to report a help message
                         if !trait_ref.has_infer_types() &&
                             self.predicate_can_apply(obligation.param_env, trait_ref) {
@@ -821,6 +823,27 @@ pub fn report_selection_error(&self,
         err.emit();
     }
 
+    /// When encountering an assignment of an unsized trait, like `let x = ""[..];`, provide a
+    /// suggestion to borrow the initializer in order to use have a slice instead.
+    fn suggest_borrow_on_unsized_slice(&self,
+                                       code: &ObligationCauseCode<'tcx>,
+                                       err: &mut DiagnosticBuilder<'tcx>) {
+        if let &ObligationCauseCode::VariableType(node_id) = code {
+            let parent_node = self.tcx.hir.get_parent_node(node_id);
+            if let Some(hir::map::NodeLocal(ref local)) = self.tcx.hir.find(parent_node) {
+                if let Some(ref expr) = local.init {
+                    if let hir::ExprIndex(_, _) = expr.node {
+                        if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(expr.span) {
+                            err.span_suggestion(expr.span,
+                                                "consider a slice instead",
+                                                format!("&{}", snippet));
+                        }
+                    }
+                }
+            }
+        }
+    }
+
     fn report_arg_count_mismatch(
         &self,
         span: Span,