Skip to content

Add FnDef #713

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Add FnDef #713

wants to merge 6 commits into from

Conversation

N1ark
Copy link
Contributor

@N1ark N1ark commented Jun 3, 2025

Add FnDef as a type -- this represents a constant function, and is a ZST, unlike Arrow which is a function pointer.

Companion PR to cryspen/hax#1487.

Comment on lines 127 to 129
TyKind::FnDef(id, gargs) => {
self.found_use_fn(id, gargs);
}
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 remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it would monomorphize for<'a> foo<'a>, which is not the actual use of the fn :)

Copy link
Member

@Nadrieril Nadrieril Jun 4, 2025

Choose a reason for hiding this comment

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

If you don't want to deal with the binder, it's fine to erase regions during mono (there's RegionBinder::erase for you)

Copy link
Member

@Nadrieril Nadrieril Jun 4, 2025

Choose a reason for hiding this comment

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

it would monomorphize for<'a> foo<'a>, which is not the actual use of the fn :)

why not?

fn foo<'a, T>(_: &'a T) {}
fn takes_closure(_: impl for<'a> Fn(&'a u32)) {}
fn main() {
    takes_closure(foo);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it was monomorphising the fn twice, e.g. for this code:

fn foo<'a, T>(x: &'a T) {}

fn bar() {
    let fooint1: fn(&u8) = foo;
    fooint1(&11);
}

it was generating this:

fn test_crate::foo::<'_0_0, u8>(@1: &missing('_0_0) (u8))
{
    let @0: (); // return
    let x@1: &'_ (u8); // arg #1

    @0 := ()
    @0 := ()
    return
}

fn test_crate::foo::<'_, u8>(@1: &'_ (u8))
{
    let @0: (); // return
    let x@1: &'_ (u8); // arg #1

    @0 := ()
    @0 := ()
    return
}

Copy link
Member

Choose a reason for hiding this comment

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

That does look good, I guess I'm surprised why this works at all. Where does it find a reference to the function item if not through FnDef? Also, if you erase() the binder I think that would get rid of the duplication you mentioned.

Copy link
Contributor Author

@N1ark N1ark Jun 4, 2025

Choose a reason for hiding this comment

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

through the parameter ! it has a ConstantExpr, which itself has a FnPtr with the right parameters!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

basically a fn is used either via a ConstantExpr, which contains a FnPtr, or a call, that also has one, so it works out nicely

Copy link
Member

@Nadrieril Nadrieril Jun 5, 2025

Choose a reason for hiding this comment

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

Ok interesting. We still need to monomorphize all types though. I'm ok with RegionBinder::new_empty(fix_args(binder.erase()) for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, let me know if that's what you had in mind, and if there's anything else to do for this PR :p

N1ark and others added 4 commits June 4, 2025 17:35
@N1ark N1ark force-pushed the fndef branch 4 times, most recently from 92f2f62 to c2f8860 Compare June 4, 2025 18:45
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.

3 participants