-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Feature-gate explicit unboxed closure method calls & manual impls, #19587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious, any particular reason why we want these codes commented here? This file isn't really meant for any sort of read consumption, it's more of a manifest.