about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Blum <bblum@andrew.cmu.edu>2013-07-01 15:38:57 -0400
committerBen Blum <bblum@andrew.cmu.edu>2013-07-01 15:48:49 -0400
commit313dd37acb78b9fba701e9aebb0a01cb70b98db1 (patch)
tree03aa8c0a4fb2ba99fd90362df80839df5550e194
parentb44953b8a3dcf97120e0cc4ed12ce115ce90e83e (diff)
downloadrust-313dd37acb78b9fba701e9aebb0a01cb70b98db1.tar.gz
rust-313dd37acb78b9fba701e9aebb0a01cb70b98db1.zip
Better error messages in report_use_of_moved_value; close #7286
-rw-r--r--src/librustc/middle/borrowck/mod.rs31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/librustc/middle/borrowck/mod.rs b/src/librustc/middle/borrowck/mod.rs
index 7d667c2043c..2e3813f57e0 100644
--- a/src/librustc/middle/borrowck/mod.rs
+++ b/src/librustc/middle/borrowck/mod.rs
@@ -538,12 +538,13 @@ impl BorrowckCtxt {
 
             move_data::MoveExpr(expr) => {
                 let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
+                let suggestion = move_suggestion(self.tcx, expr_ty,
+                        "moved by default (use `copy` to override)");
                 self.tcx.sess.span_note(
                     expr.span,
-                    fmt!("`%s` moved here because it has type `%s`, \
-                          which is moved by default (use `copy` to override)",
+                    fmt!("`%s` moved here because it has type `%s`, which is %s",
                          self.loan_path_to_str(moved_lp),
-                         expr_ty.user_string(self.tcx)));
+                         expr_ty.user_string(self.tcx), suggestion));
             }
 
             move_data::MovePat(pat) => {
@@ -557,12 +558,28 @@ impl BorrowckCtxt {
             }
 
             move_data::Captured(expr) => {
+                let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
+                let suggestion = move_suggestion(self.tcx, expr_ty,
+                        "moved by default (make a copy and \
+                         capture that instead to override)");
                 self.tcx.sess.span_note(
                     expr.span,
-                    fmt!("`%s` moved into closure environment here \
-                          because its type is moved by default \
-                          (make a copy and capture that instead to override)",
-                         self.loan_path_to_str(moved_lp)));
+                    fmt!("`%s` moved into closure environment here because it \
+                          has type `%s`, which is %s",
+                         self.loan_path_to_str(moved_lp),
+                         expr_ty.user_string(self.tcx), suggestion));
+            }
+        }
+
+        fn move_suggestion(tcx: ty::ctxt, ty: ty::t, default_msg: &'static str)
+                          -> &'static str {
+            match ty::get(ty).sty {
+                ty::ty_closure(ref cty) if cty.sigil == ast::BorrowedSigil =>
+                    "a non-copyable stack closure (capture it in a new closure, \
+                     e.g. `|x| f(x)`, to override)",
+                _ if !ty::type_is_copyable(tcx, ty) =>
+                    "non-copyable (perhaps you meant to use clone()?)",
+                _ => default_msg,
             }
         }
     }