Skip to content

Implement non-capturing closure to fn coercion - #40025

Merged
bors merged 4 commits into
rust-lang:masterfrom
est31:master
Feb 25, 2017
Merged

Implement non-capturing closure to fn coercion#40025
bors merged 4 commits into
rust-lang:masterfrom
est31:master

Conversation

@est31

@est31est31 commented Feb 22, 2017

Copy link
Copy Markdown
Member

Implements non capturing closure coercion (RFC 1558).

cc tracking issue #39817

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @pnkfelix

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

@est31

Copy link
Copy Markdown
MemberAuthor

r? @eddyb

@rust-highfiverust-highfive assigned eddyb and unassigned pnkfelixFeb 22, 2017
@est31
est31force-pushed the master branch 3 times, most recently from f0d612f to 03b534cCompareFebruary 23, 2017 21:31
@est31

Copy link
Copy Markdown
MemberAuthor

@eddyb Its ready for review!

@est31est31 mentioned this pull request Feb 23, 2017
Comment threadsrc/librustc_trans/mir/constant.rs Outdated
.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
Copy Markdown
Contributor

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.

Comment threadsrc/librustc_trans/mir/rvalue.rs Outdated
.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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
Contributor

@bors r+

@bors

bors commented Feb 24, 2017

Copy link
Copy Markdown
Collaborator

📌 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
@eddybeddyb mentioned this pull request Feb 25, 2017
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
@eddybeddyb mentioned this pull request Feb 25, 2017
bors added a commit that referenced this pull request Feb 25, 2017
@bors
bors merged commit 77f131d into rust-lang:masterFeb 25, 2017
Sign up for freeto 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.

6 participants

@est31@rust-highfive@eddyb@bors@whitequark@pnkfelix