Skip to content

feat: add out of order function calls#282

Open
stringhandler wants to merge 2 commits intoBlockstreamResearch:masterfrom
stringhandler:feat/add-out-of-order-funcs
Open

feat: add out of order function calls#282
stringhandler wants to merge 2 commits intoBlockstreamResearch:masterfrom
stringhandler:feat/add-out-of-order-funcs

Conversation

@stringhandler
Copy link
Copy Markdown
Contributor

@stringhandler stringhandler commented Apr 13, 2026

Adds a double parsing step for analysis in order to allow functions to appear later in the source code.

Motivation:

With the upcoming library references PRs I created a test standard library here. It quickly became clear that it would be difficult to maintain the ordering of function calls so that they were always declared before they were called. This PR removes that restriction by doing a few prechecks before analyzing the parsed tree.

Allowing functions to be declared after they are called now means that extra checks must be done to prevent recursion. Since main is treated a little differently, I have not allowed a function main to be called.

Update

As noted by @KyrylR, types are also allowed to be referenced out of order.

@stringhandler stringhandler requested a review from delta1 as a code owner April 13, 2026 11:38
@stringhandler stringhandler force-pushed the feat/add-out-of-order-funcs branch from fc68b48 to d00b714 Compare April 13, 2026 13:07
@KyrylR
Copy link
Copy Markdown
Collaborator

KyrylR commented Apr 15, 2026

It is not mentioned in the description, but with this PR types could be also declared out of order, reproduction test:

    #[test]
    fn type_alias_must_be_declared_before_use() {
        compile_err(
            r#"fn main() {
    let x: Later = 32;
    assert!(jet::eq_32(x, 32));
}

type Later = u32;"#,
            "Type alias `Later` is not defined",
        );
    }

Also this PR introduces regression over duplicated functions, here is the test:

    #[test]
    fn duplicate_helper_function_redefinition_rejected() {
        compile_err(
            r#"fn foo() -> bool {
    true
}

fn foo() -> bool {
    false
}

fn main() {
    assert!(foo());
}"#,
            "Function `foo` was defined multiple times",
        );
    }

Copy link
Copy Markdown
Collaborator

@KyrylR KyrylR left a comment

Choose a reason for hiding this comment

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

See comment above

@stringhandler
Copy link
Copy Markdown
Contributor Author

Thanks, will fix

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