forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rollup merge of rust-lang#19587: huonw/closure-feature-gate
detect UFCS drop and allow UFCS methods to have explicit type parameters. Work towards rust-lang#18875. Since code could previously call the methods & implement the traits manually, this is a [breaking-change] Closes rust-lang#19586. Closes rust-lang#19375.
- Loading branch information
Showing
13 changed files
with
199 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use syntax::ast; | ||
use syntax::codemap::Span; | ||
use CrateCtxt; | ||
|
||
/// Check that it is legal to call methods of the trait corresponding | ||
/// to `trait_id` (this only cares about the trait, not the specific | ||
/// method that is called) | ||
pub fn check_legal_trait_for_method_call(ccx: &CrateCtxt, span: Span, trait_id: ast::DefId) { | ||
let tcx = ccx.tcx; | ||
let did = Some(trait_id); | ||
let li = &tcx.lang_items; | ||
|
||
if did == li.drop_trait() { | ||
span_err!(tcx.sess, span, E0040, "explicit use of destructor method"); | ||
} else if !tcx.sess.features.borrow().unboxed_closures { | ||
// the #[feature(unboxed_closures)] feature isn't | ||
// activated so we need to enforce the closure | ||
// restrictions. | ||
|
||
let method = if did == li.fn_trait() { | ||
"call" | ||
} else if did == li.fn_mut_trait() { | ||
"call_mut" | ||
} else if did == li.fn_once_trait() { | ||
"call_once" | ||
} else { | ||
return // not a closure method, everything is OK. | ||
}; | ||
|
||
span_err!(tcx.sess, span, E0174, | ||
"explicit use of unboxed closure method `{}` is experimental", | ||
method); | ||
span_help!(tcx.sess, span, | ||
"add `#![feature(unboxed_closures)]` to the crate attributes to enable"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/test/compile-fail/feature-gate-unboxed-closures-manual-impls.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![allow(dead_code)] | ||
|
||
struct Foo; | ||
impl Fn<(), ()> for Foo { //~ ERROR manual implementations of `Fn` are experimental | ||
extern "rust-call" fn call(&self, args: ()) -> () {} | ||
} | ||
struct Bar; | ||
impl FnMut<(), ()> for Bar { //~ ERROR manual implementations of `FnMut` are experimental | ||
extern "rust-call" fn call_mut(&self, args: ()) -> () {} | ||
} | ||
struct Baz; | ||
impl FnOnce<(), ()> for Baz { //~ ERROR manual implementations of `FnOnce` are experimental | ||
extern "rust-call" fn call_once(&self, args: ()) -> () {} | ||
} | ||
|
||
fn main() {} |
19 changes: 19 additions & 0 deletions
19
src/test/compile-fail/feature-gate-unboxed-closures-method-calls.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![allow(dead_code)] | ||
|
||
fn foo<F: Fn<(), ()>>(mut f: F) { | ||
f.call(()); //~ ERROR explicit use of unboxed closure method `call` | ||
f.call_mut(()); //~ ERROR explicit use of unboxed closure method `call_mut` | ||
f.call_once(()); //~ ERROR explicit use of unboxed closure method `call_once` | ||
} | ||
|
||
fn main() {} |
19 changes: 19 additions & 0 deletions
19
src/test/compile-fail/feature-gate-unboxed-closures-ufcs-calls.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
#![allow(dead_code)] | ||
|
||
fn foo<F: Fn<(), ()>>(mut f: F, mut g: F) { | ||
Fn::call(&g, ()); //~ ERROR explicit use of unboxed closure method `call` | ||
FnMut::call_mut(&mut g, ()); //~ ERROR explicit use of unboxed closure method `call_mut` | ||
FnOnce::call_once(g, ()); //~ ERROR explicit use of unboxed closure method `call_once` | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
|
||
struct Foo; | ||
|
||
impl Drop for Foo { | ||
fn drop(&mut self) {} | ||
} | ||
|
||
fn main() { | ||
Drop::drop(&mut Foo) //~ ERROR explicit use of destructor method | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
trait Foo<T> { | ||
fn get(&self) -> T; | ||
} | ||
|
||
impl Foo<i32> for i32 { | ||
fn get(&self) -> i32 { *self } | ||
} | ||
|
||
fn main() { | ||
let x: i32 = 1; | ||
Foo::<i32>::get(&x) | ||
} |