Skip to content

Conversation

@chqrlie
Copy link
Contributor

@chqrlie chqrlie commented Nov 5, 2025

  • local functions are defined using the standard syntax in the body of a function.
  • they cannot be templates nor type functions
  • they can be named or anonymous
  • accept a capture list between square backets fater the parameter list (currently ignored)

@bvdberg
Copy link
Member

bvdberg commented Nov 5, 2025

I'm not really fond of the name mangling. Cant we do:
outer _ _ inner?

and then disallow double underscores in normal names?
It would be nicer during debugging

Another issue is the AST printing. The local function body is not printed.

You could add test cases, they dont depend on the bootstrap.

I tried this and it works...

module test;

import stdio local;

i32[10] numbers;
u32 count;

fn void add(i32 value) {
    numbers[count++] = value;
}

type VisitorFn fn void (void* arg, i32 value);

fn void visit(void* arg, VisitorFn handler) {
    for (u32 i = 0; i < count; i++) {
        handler(arg, numbers[i]);
    }
}


public fn i32 main() {
    add(1);
    add(2);
    add(3);
    add(4);


    i32 total = 0;

    fn void count_total(void* arg, i32 value) {
        i32* total = arg;
        *total += value;
    }

    visit(&total, count_total);

    printf("total %d\n", total);
    return 0;
}

If you generate the C code and check what GCC/Clang make of that, the call completely disappears :)

@chqrlie
Copy link
Contributor Author

chqrlie commented Nov 5, 2025

Disallowing double underscores and using them for mangled names, especially #defines, is a sane decision.

Yet the mangling for making local symbols global must handle the case of duplicates: it is quite common in the code to use the same local name in non intersecting scopes. The same could happen for local functions, and should be allowed, so outer__inner is not sufficient, we may need an extra suffix in case of duplicates. The case of anonymous functions must be covered too. I used SrcLoc as a quick and dirty hack, but I agree it makes debugging and testing more cumbersome as the C output is unpredictable

AST printing is on the way, I need to add a new AST node type for this.

I'm glad the optimizer does a good job on these things.

@chqrlie chqrlie force-pushed the local-functions branch 8 times, most recently from 6c6a97f to 516ed00 Compare November 10, 2025 11:09
@chqrlie chqrlie force-pushed the local-functions branch 5 times, most recently from d75a858 to b06527c Compare November 22, 2025 15:10
* local functions are defined using the standard syntax in the body
  of a function.
* they cannot be templates nor type functions
* they can be named or anonymous
* accept a capture list between square backets fater the parameter list
  (currently ignored)
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.

2 participants