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 nested functions #523

Merged
merged 1 commit into from Jun 24, 2021
Merged

Add support for nested functions #523

merged 1 commit into from Jun 24, 2021

Conversation

philberty
Copy link
Member

This is a missed pieces of functionality for statements in rust.

@philberty philberty added this to the Data Structures 3 - Traits milestone Jun 24, 2021
@philberty philberty self-assigned this Jun 24, 2021
@philberty philberty added this to In progress in Data Structures 3 - Traits via automation Jun 24, 2021
@CohenArthur
Copy link
Member

It might be interesting to add a test checking for an error when using a nested function outside of its scope, just to make sure

fn test() {
   fn nested() {}
}

fn main() {
    nested(); // dg-error etc...
}

@philberty
Copy link
Member Author

Good point I will add that test case.

Another missing piece is: "error[E0434]: can't capture dynamic environment in a fn item" but it will need a little more work to add that in a general wayl.

@CohenArthur
Copy link
Member

Yeah I don't know how difficult that would be to implement, considering lambdas can use their dynamic environment etc etc

We missed that stmts in rust can be items like functions. This adds support
for resolution and compilation of nested functions. Rust allows nested
functions which are distinct to closures. Nested functions are not allowed
to encapsulate the enclosing scope so they can be extracted as normal functions.
@dkm
Copy link
Member

dkm commented Jun 24, 2021

Maybe similar to the GNU extension for nested function in C https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

@philberty
Copy link
Member Author

Yeah, I think I will likely need to wait to implement that error until closures are implemented.

@philberty
Copy link
Member Author

Maybe similar to the GNU extension for nested function in C https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

Good point let me add this link to the closures issue #195

I think a lot of this we get kind of for free. The abstraction we have in place allows us to say this function definition is enclosed within a parent scope to be able to reference outside variables. I think in theory closures are easy 'ish to implement in terms of GCC and this abstraction was used in gccgo so its full of them :D

@philberty philberty mentioned this pull request Jun 24, 2021
7 tasks
@philberty
Copy link
Member Author

bors r+

@bors
Copy link
Contributor

bors bot commented Jun 24, 2021

Build succeeded:

@bors bors bot merged commit c87f2c7 into master Jun 24, 2021
Data Structures 3 - Traits automation moved this from In progress to Done Jun 24, 2021
@philberty philberty deleted the phil/block-items branch June 24, 2021 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants