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

Add support for binding a method off an impl #435

Closed
lkuper opened this issue Jun 1, 2011 · 8 comments
Closed

Add support for binding a method off an impl #435

lkuper opened this issue Jun 1, 2011 · 8 comments
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) A-typesystem Area: The type system
Milestone

Comments

@lkuper
Copy link
Contributor

lkuper commented Jun 1, 2011

Make it legal to do the following:

obj foo() {
    fn add5(n: int) -> int {
        ret n + 5;
    }
}

fn add5(n: int) -> int {
    ret n + 5;
}

fn main() {
    let fiveplusseven = bind add5(7);
    log_err fiveplusseven();

    // Can't do this currently
    let my_foo = foo();
    let fiveplusseven_too = bind my_foo.add5(7);
    log_err fiveplusseven_too();
}

This feature would make it possible to reify methods as standalone functions. (I haven't thought through all the implications of this; I suppose the resulting function would have to capture the object in its scope.)

@lkuper
Copy link
Contributor Author

lkuper commented Jun 1, 2011

Currently, this fails in trans with an "unimplemented re-binding existing function" error.

@msullivan
Copy link
Contributor

With my fix to #754, your program compiles but returns the wrong value.

@ghost ghost assigned lkuper Jul 28, 2011
lkuper added a commit that referenced this issue Jul 28, 2011
@msullivan
Copy link
Contributor

This is closely related to #758. One of them probably depends on the other, depending on how we tackle them.

@lkuper
Copy link
Contributor Author

lkuper commented Aug 9, 2011

This particular test is passing now, but it might be accidental. I'm not sure we've got this feature working in general yet; I haven't yet thought through how the latest incarnation of the object system behaves wrt bind.

lkuper added a commit that referenced this issue Aug 9, 2011
@msullivan
Copy link
Contributor

I think it working is basically accidental? I suspect that if the object had fields things would be a bit more problematic.

@lkuper
Copy link
Contributor Author

lkuper commented Aug 16, 2011

I'm unassigning myself from this one and marking it as unassigned, since, realistically speaking, I don't think I'll have time to tackle it in the next four days.

jdm pushed a commit to jdm/rust that referenced this issue Sep 21, 2011
LValues no longer carry information about generics and objs, instead
there's an extended form of lvalue, lval_maybe_callee, only used by
call and bind, which holds this info.

This makes it possible to take the value of a method and get a working
closure, and will (with some more work) allow us to call statically
known functions without loading from their pair.

Closes rust-lang#435
Closes rust-lang#758
@marijnh marijnh reopened this Sep 28, 2011
@marijnh
Copy link
Contributor

marijnh commented Sep 28, 2011

This was working only by accident (the resulting closure doesn't actually hold on to the object, so it segfaults when called after the object dies). I assumed the closed over thing was the object box, but in fact it's a {vtbl_, box_} pair. Extra machinery will be needed to wrap this in a closure.

@marijnh
Copy link
Contributor

marijnh commented Jan 13, 2012

Repurposing this bug to support the same for impls. The problem is basically the same: package up enough information into the closure to allow the resulting function value to find it's self value.

@ghost ghost assigned marijnh Feb 14, 2012
marijnh added a commit that referenced this issue Feb 14, 2012
This allows you to take the value of, for example, `[1].len`, or bind
it with `bind x.map(_)` syntax.

I'm holding off on implementing this for dynamic methods (those on
bounded type parameters or iface types) until it's clearer what we
will do with monomorphization.

Issue #435
@marijnh marijnh closed this as completed Mar 16, 2012
erickt pushed a commit to erickt/rust that referenced this issue Mar 16, 2012
keeperofdakeys pushed a commit to keeperofdakeys/rust that referenced this issue Dec 12, 2017
dlrobertson pushed a commit to dlrobertson/rust that referenced this issue Nov 29, 2018
bors pushed a commit to rust-lang-ci/rust that referenced this issue Oct 26, 2020
ZuseZ4 pushed a commit to EnzymeAD/rust that referenced this issue Mar 7, 2023
GuillaumeGomez pushed a commit to GuillaumeGomez/rust that referenced this issue Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-frontend Area: Compiler frontend (errors, parsing and HIR) A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

3 participants