diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index c477a0d383e2..3cd7c5e0af3e 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -66,7 +66,7 @@ use hir::map as hir_map; use hir::def_id::DefId; use middle::region; use traits::{ObligationCause, ObligationCauseCode}; -use ty::{self, Region, Ty, TyCtxt, TypeFoldable, TypeVariants}; +use ty::{self, Region, RegionKind, Ty, TyCtxt, TypeFoldable, TypeVariants}; use ty::error::TypeError; use syntax::ast::DUMMY_NODE_ID; use syntax_pos::{Pos, Span}; @@ -1067,6 +1067,40 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { sub_region: Region<'tcx>, sup_origin: SubregionOrigin<'tcx>, sup_region: Region<'tcx>) { + + // #45983: when trying to assign the contents of an argument to a binding outside of a + // closure, provide a specific message pointing this out. + if let (&SubregionOrigin::BindingTypeIsNotValidAtDecl(ref external_span), + &SubregionOrigin::Subtype(TypeTrace { ref cause, .. }), + &RegionKind::ReFree(ref free_region)) = (&sub_origin, &sup_origin, sup_region) { + let hir = &self.tcx.hir; + if let Some(node_id) = hir.as_local_node_id(free_region.scope) { + match hir.get(node_id) { + hir_map::NodeExpr(hir::Expr { + node: hir::ExprClosure(_, _, _, closure_span, false), + .. + }) => { + let sp = var_origin.span(); + let mut err = self.tcx.sess.struct_span_err( + sp, + "borrowed data cannot be moved outside of its closure"); + let label = match cause.code { + ObligationCauseCode::ExprAssignable => { + "cannot be assigned to binding outside of its closure" + } + _ => "cannot be moved outside of its closure", + }; + err.span_label(sp, label); + err.span_label(*closure_span, "closure you can't escape"); + err.span_label(*external_span, "binding declared outside of closure"); + err.emit(); + return; + } + _ => {} + } + } + } + let mut err = self.report_inference_failure(var_origin); self.tcx.note_and_explain_region(region_scope_tree, &mut err, diff --git a/src/test/compile-fail/regions-escape-bound-fn-2.rs b/src/test/compile-fail/regions-escape-bound-fn-2.rs index 1329d05a0f69..042c55eed866 100644 --- a/src/test/compile-fail/regions-escape-bound-fn-2.rs +++ b/src/test/compile-fail/regions-escape-bound-fn-2.rs @@ -16,5 +16,5 @@ fn with_int(f: F) where F: FnOnce(&isize) { fn main() { let mut x = None; with_int(|y| x = Some(y)); - //~^ ERROR cannot infer + //~^ ERROR borrowed data cannot be moved outside of its closure } diff --git a/src/test/ui/borrowck/issue-45983.rs b/src/test/ui/borrowck/issue-45983.rs new file mode 100644 index 000000000000..b2316a6b61c8 --- /dev/null +++ b/src/test/ui/borrowck/issue-45983.rs @@ -0,0 +1,19 @@ +// Copyright 2018 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. + +fn give_any FnOnce(&'r ())>(f: F) { + f(&()); +} + +fn main() { + let x = None; + give_any(|y| x = Some(y)); + //~^ ERROR borrowed data cannot be moved outside of its closure +} diff --git a/src/test/ui/borrowck/issue-45983.stderr b/src/test/ui/borrowck/issue-45983.stderr new file mode 100644 index 000000000000..689fe6053c9a --- /dev/null +++ b/src/test/ui/borrowck/issue-45983.stderr @@ -0,0 +1,12 @@ +error: borrowed data cannot be moved outside of its closure + --> $DIR/issue-45983.rs:17:27 + | +16 | let x = None; + | - binding declared outside of closure +17 | give_any(|y| x = Some(y)); + | --- ^ cannot be assigned to binding outside of its closure + | | + | closure you can't escape + +error: aborting due to previous error +