From 313dd37acb78b9fba701e9aebb0a01cb70b98db1 Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Mon, 1 Jul 2013 15:38:57 -0400 Subject: [PATCH 1/3] Better error messages in report_use_of_moved_value; close #7286 --- src/librustc/middle/borrowck/mod.rs | 31 ++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/librustc/middle/borrowck/mod.rs b/src/librustc/middle/borrowck/mod.rs index 7d667c2043c1..2e3813f57e08 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, } } } From 919f5a7e687052b317028d1ba26f9d3c01ed94d4 Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Mon, 1 Jul 2013 15:54:54 -0400 Subject: [PATCH 2/3] Fix filenames of some compile-fail tests. --- ...osure-2.rs => closure-bounds-cant-mutably-borrow-with-copy.rs} | 0 ...osure.rs => moves-based-on-type-no-recursive-stack-closure.rs} | 0 .../{move-based-on-type-tuple.rs => moves-based-on-type-tuple.rs} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename src/test/compile-fail/{the-case-of-the-recurring-closure-2.rs => closure-bounds-cant-mutably-borrow-with-copy.rs} (100%) rename src/test/compile-fail/{the-case-of-the-recurring-closure.rs => moves-based-on-type-no-recursive-stack-closure.rs} (100%) rename src/test/compile-fail/{move-based-on-type-tuple.rs => moves-based-on-type-tuple.rs} (100%) diff --git a/src/test/compile-fail/the-case-of-the-recurring-closure-2.rs b/src/test/compile-fail/closure-bounds-cant-mutably-borrow-with-copy.rs similarity index 100% rename from src/test/compile-fail/the-case-of-the-recurring-closure-2.rs rename to src/test/compile-fail/closure-bounds-cant-mutably-borrow-with-copy.rs diff --git a/src/test/compile-fail/the-case-of-the-recurring-closure.rs b/src/test/compile-fail/moves-based-on-type-no-recursive-stack-closure.rs similarity index 100% rename from src/test/compile-fail/the-case-of-the-recurring-closure.rs rename to src/test/compile-fail/moves-based-on-type-no-recursive-stack-closure.rs diff --git a/src/test/compile-fail/move-based-on-type-tuple.rs b/src/test/compile-fail/moves-based-on-type-tuple.rs similarity index 100% rename from src/test/compile-fail/move-based-on-type-tuple.rs rename to src/test/compile-fail/moves-based-on-type-tuple.rs From 54e01eb7e0ea74c01a797f56e2bf602c298a9769 Mon Sep 17 00:00:00 2001 From: Ben Blum Date: Mon, 1 Jul 2013 18:05:46 -0400 Subject: [PATCH 3/3] Add a run-pass test for recursive copyable stack closures. --- .../closure-bounds-recursive-stack-closure.rs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/test/run-pass/closure-bounds-recursive-stack-closure.rs diff --git a/src/test/run-pass/closure-bounds-recursive-stack-closure.rs b/src/test/run-pass/closure-bounds-recursive-stack-closure.rs new file mode 100644 index 000000000000..8bb57ebaaf58 --- /dev/null +++ b/src/test/run-pass/closure-bounds-recursive-stack-closure.rs @@ -0,0 +1,31 @@ +// Copyright 2013 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Ensures that it's legal to create a recursive stack closure as long as +// its environment is copyable + +struct R<'self> { + // This struct is needed to create the + // otherwise infinite type of a fn that + // accepts itself as argument: + c: &'self fn:Copy(&R, uint) -> uint +} + +fn main() { + // Stupid version of fibonacci. + let fib: &fn:Copy(&R, uint) -> uint = |fib, x| { + if x == 0 || x == 1 { + x + } else { + (fib.c)(fib, x-1) + (fib.c)(fib, x-2) + } + }; + assert!(fib(&R { c: fib }, 7) == 13); +}