Skip to content
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

Implement non-capturing closure to fn coercion #40025

Merged
merged 4 commits into from
Feb 25, 2017
Merged

Conversation

est31
Copy link
Member

@est31 est31 commented Feb 22, 2017

Implements non capturing closure coercion (RFC 1558).

cc tracking issue #39817

@rust-highfive
Copy link
Collaborator

r? @pnkfelix

(rust_highfive has picked a reviewer for you, use r? to override)

@est31
Copy link
Member Author

est31 commented Feb 22, 2017

r? @eddyb

@rust-highfive rust-highfive assigned eddyb and unassigned pnkfelix Feb 22, 2017
@est31 est31 force-pushed the master branch 3 times, most recently from f0d612f to 03b534c Compare February 23, 2017 21:31
@est31
Copy link
Member Author

est31 commented Feb 23, 2017

@eddyb Its ready for review!

.unwrap().def_id;
// Now create its substs [Closure, Tuple]
let input = tcx.closure_type(def_id, substs).sig.input(0);
let substs = Substs::for_item(tcx,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just make Substs by doing tcx.mk_substs([operand.ty, input.skip_binder()].iter().cloned()) IIRC.

.unwrap().def_id;
// Now create its substs [Closure, Tuple]
let input = bcx.tcx().closure_type(def_id, substs).sig.input(0);
let substs = Substs::for_item(bcx.tcx(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


let b = self.shallow_resolve(b);

let node_id_a :NodeId = self.tcx.hir.as_local_node_id(def_id_a).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type annotations shouldn't be needed.

let sig = self.closure_type(def_id_a, substs_a).sig;
let converted_sig = sig.input(0).map_bound(|v| {
let params_iter = match v.sty {
TypeVariants::TyTuple(params, _) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty::TyTuple

// to
// `fn(arg0,arg1,...) -> _`
let sig = self.closure_type(def_id_a, substs_a).sig;
let converted_sig = sig.input(0).map_bound(|v| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant sig.map_bound. Because that lets you avoid skip_binder on output too.


fn main() {
let mut a = 0u8;
let foo :fn(u8) -> u8 = |v: u8| { a += v; a };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let x :T is unidiomatic, should be let x: T.

assert_eq!({BAR[1](&mut a); a }, 1);
assert_eq!({BAR[2](&mut a); a }, 3);
assert_eq!({BAR[3](&mut a); a }, 6);
assert_eq!({BAR[4](&mut a); a }, 10);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ should be followed by a space just like } should be preceded by one.

	* use more convenient mk_substs function
	* remove type annotations
	* use map_bound one level farther outside
	* style improvements
@eddyb
Copy link
Member

eddyb commented Feb 24, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Feb 24, 2017

📌 Commit 77f131d has been approved by eddyb

eddyb added a commit to eddyb/rust that referenced this pull request Feb 25, 2017
Implement non-capturing closure to fn coercion

Implements non capturing closure coercion ([RFC 1558](https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md)).

cc tracking issue rust-lang#39817
eddyb added a commit to eddyb/rust that referenced this pull request Feb 25, 2017
Implement non-capturing closure to fn coercion

Implements non capturing closure coercion ([RFC 1558](https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md)).

cc tracking issue rust-lang#39817
bors added a commit that referenced this pull request Feb 25, 2017
@bors bors merged commit 77f131d into rust-lang:master Feb 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants