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

Unboxed closures #14539

Closed
wants to merge 1 commit into from
Closed

Conversation

pcwalton
Copy link
Contributor

let force_host_ptr = &mut force_host;
let check_stdout_ptr = &mut check_stdout;
let no_prefer_dynamic_ptr = &mut no_prefer_dynamic;
let no_pretty_expanded_ptr = &mut no_pretty_expanded;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we still considering the syntax sugar from @thestinger's RFC to potentially make this less horrifying?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or perhaps I'm misunderstanding, is this only a temporary affair until |&mut: foo| is implemented?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's not temporary, it's just how you need to capture by-reference and by-mutable-reference when there's not already a reference. This is the case the sugar would improve, but it doesn't appear to be very common.

@pcwalton
Copy link
Contributor Author

This is getting pretty close. Needs a rebase and some tests.

@pcwalton
Copy link
Contributor Author

OK, this is ready for initial review. There are several known problems, but I figured as this patch is over 1KLOC already it's best to fix them in followups. Known issues:

  • Some stuff may be broken cross-crate.
  • Trans fails with an assertion if you don't specify the return type on the closure. Probably a simple writeback issue.
  • Only FnMut is implemented, not Fn or FnOnce (again, probably not hard of a fix).
  • You have to explicitly call the closure with .call_mut() instead of using the () operator (probably the largest remaining work item).
  • Complex destructors on unboxed closures are untested and may be hilariously broken.

This patch includes simple unit tests for direct calls, calls through a generic type parameter, and boxed calls.

r? @nikomatsakis (or whoever)

@alexcrichton
Copy link
Member

(drive by comment)

Would it be easy to put this all behind a feature gate until the feature is ready for prime-time?

@pcwalton
Copy link
Contributor Author

This is now behind a gate. Also fixed the formatting issues.

@@ -1 +1 @@
Subproject commit 0a894645cf120539876e9eb4eb0d7b572dfa9d14
Subproject commit 4b4d0533b4f76cc3fbba31bd9e7ac02e0c738b1d
Copy link
Member

Choose a reason for hiding this comment

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

Accidental?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.

@nikomatsakis
Copy link
Contributor

Started reading this today. Looks pretty nice! Not finished yet.

pub fn main() {
let f = |&mut: x: int, y: int| -> int { x + y };
let z = call_it(3, f);
println!("{}", z);
Copy link
Contributor

Choose a reason for hiding this comment

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

change this to assert that z has the correct value

Copy link
Member

Choose a reason for hiding this comment

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

this comment seems like it was not addressed.

@nikomatsakis
Copy link
Contributor

OK, left a few comments on things that need addressing.

pcwalton added a commit to pcwalton/rust that referenced this pull request Jun 13, 2014
the leading quote part of the identifier for the purposes of hygiene.

This adopts @jbclements' solution to rust-lang#14539.

I'm not sure if this is a breaking change or not.

Closes rust-lang#12512.

[breaking-change]
alexcrichton pushed a commit to alexcrichton/rust that referenced this pull request Jun 13, 2014
the leading quote part of the identifier for the purposes of hygiene.

This adopts @jbclements' solution to rust-lang#14539.

I'm not sure if this is a breaking change or not.

Closes rust-lang#12512.

[breaking-change]
@alexcrichton
Copy link
Member

ping @pcwalton, what's the status of this?

@pcwalton
Copy link
Contributor Author

It's in my queue of P-backcompat-lang things to do.

@pcwalton
Copy link
Contributor Author

pcwalton commented Jul 9, 2014

Current status: I have rebased the patch and addressed about half of @nikomatsakis' comments. However, this collided badly with the other unboxed closures work, necessitating some extra functionality. Specifically, I need to add tupling and untupling machinery in several places in trans in order to keep the tests passing, as they call the traits by name and implement them manually.

I think that, semantically, @eddyb had the right idea all along: exposing the tupling/untupling is pretty messy, as is the rust-call ABI that I had to add. The elegant solution is to say that Fn/FnMut/FnOnce are actually an unbounded family of traits, only nameable via the |int| -> int syntax. I think that it may well be the case that the lang items should be marked experimental and behind a feature gate forever (unless we get variadic generics, I guess). The guts of the implementation are pretty gross in places, and I want to confine this ugliness to the implementation and not expose it as part of the real semantics of the language.

@eddyb, you are perfectly within your rights to say "I told you so". :)

@nikomatsakis
Copy link
Contributor

Hmm. It's possible @eddyb's solution of having variadic generics seems preferable to a family of unnamable traits. That is, it'd be a generic and reusable mechanism. Certainly worth talking through in more detail.

@pcwalton
Copy link
Contributor Author

Getting closer here. One test failing. I had to rework most of the way unboxed closures work in the previous PRs. Still need to address one major issue in method resolution.

@pcwalton
Copy link
Contributor Author

Tests oughta' be passing now.

@pcwalton
Copy link
Contributor Author

This should be ready now; all of @nikomatsakis' comments addressed. r? @pnkfelix

@sfackler
Copy link
Member

@pcwalton
Copy link
Contributor Author

fixed tidy failures.

#[rust_call_abi_hack]
fn call_once(self, args: Args) -> Result;
}
*/
Copy link
Member

Choose a reason for hiding this comment

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

why did you add these bits of code in comments? e.g. was it to make it easier to convert the test into something that uses #[no_std] (so that you could feed it into a stage1 rustc) ? I would still be inclined to remove them...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about that, I forgot to remove it.

@pnkfelix
Copy link
Member

@pcwalton LGTM, though my knowledge of trans is still pretty weak. I will try to at least give it a whirl locally, but I suspect that if you address the minor comments above, then it will be good to go.

@pcwalton
Copy link
Contributor Author

re-r? @pnkfelix

@pcwalton
Copy link
Contributor Author

Comments addressed.

@pcwalton
Copy link
Contributor Author

Rebased.

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.

9 participants