From d443f98f22a886a1c93908e842d999e28b07ec9f Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 23 Feb 2015 17:21:54 -0500 Subject: [PATCH] Apply borrowck to fns that appear in const declarations. Fixes #22382. --- src/librustc_borrowck/borrowck/mod.rs | 6 ++-- .../compile-fail/borrowck-fn-in-const-a.rs | 22 +++++++++++++ .../compile-fail/borrowck-fn-in-const-b.rs | 24 ++++++++++++++ .../compile-fail/borrowck-fn-in-const-c.rs | 33 +++++++++++++++++++ 4 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 src/test/compile-fail/borrowck-fn-in-const-a.rs create mode 100644 src/test/compile-fail/borrowck-fn-in-const-b.rs create mode 100644 src/test/compile-fail/borrowck-fn-in-const-c.rs diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index dfd98881ace86..ff92ee5b64e26 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -105,10 +105,10 @@ fn borrowck_item(this: &mut BorrowckCtxt, item: &ast::Item) { ast::ItemConst(_, ref ex) => { gather_loans::gather_loans_in_static_initializer(this, &**ex); } - _ => { - visit::walk_item(this, item); - } + _ => { } } + + visit::walk_item(this, item); } /// Collection of conclusions determined via borrow checker analyses. diff --git a/src/test/compile-fail/borrowck-fn-in-const-a.rs b/src/test/compile-fail/borrowck-fn-in-const-a.rs new file mode 100644 index 0000000000000..3098807f272f3 --- /dev/null +++ b/src/test/compile-fail/borrowck-fn-in-const-a.rs @@ -0,0 +1,22 @@ +// Copyright 2014 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. + +// Check that we check fns appearing in constant declarations. +// Issue #22382. + +const MOVE: fn(&String) -> String = { + fn broken(x: &String) -> String { + return *x //~ ERROR cannot move + } + broken +}; + +fn main() { +} diff --git a/src/test/compile-fail/borrowck-fn-in-const-b.rs b/src/test/compile-fail/borrowck-fn-in-const-b.rs new file mode 100644 index 0000000000000..7e29b2ee0fd4e --- /dev/null +++ b/src/test/compile-fail/borrowck-fn-in-const-b.rs @@ -0,0 +1,24 @@ +// Copyright 2014 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. + +// Check that we check fns appearing in constant declarations. +// Issue #22382. + +// How about mutating an immutable vector? +const MUTATE: fn(&Vec) = { + fn broken(x: &Vec) { + x.push(format!("this is broken")); + //~^ ERROR cannot borrow + } + broken +}; + +fn main() { +} diff --git a/src/test/compile-fail/borrowck-fn-in-const-c.rs b/src/test/compile-fail/borrowck-fn-in-const-c.rs new file mode 100644 index 0000000000000..e607397e920e2 --- /dev/null +++ b/src/test/compile-fail/borrowck-fn-in-const-c.rs @@ -0,0 +1,33 @@ +// Copyright 2014 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. + +// Check that we check fns appearing in constant declarations. +// Issue #22382. + +// Returning local references? +struct DropString { + inner: String +} +impl Drop for DropString { + fn drop(&mut self) { + self.inner.clear(); + self.inner.push_str("dropped"); + } +} +const LOCAL_REF: fn() -> &'static str = { + fn broken() -> &'static str { + let local = DropString { inner: format!("Some local string") }; + return &local.inner; //~ ERROR does not live long enough + } + broken +}; + +fn main() { +}