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

Enforce function returns at compile time #2788

Merged
merged 6 commits into from
Oct 13, 2023

Conversation

Vurv78
Copy link
Contributor

@Vurv78 Vurv78 commented Oct 13, 2023

Instead of doing a runtime check for functions returning values, (which would bring a minor amount of overhead to E2 functions), now they will be checked at compile time if they return.

This won't compile

function string test() {}

This function marked to return 'string' must return a value

But these two cases will (Featuring some cheeky logic to make if and switch statements mark the above scope as dead if all cases return and there is an else / default case)

function number increment() {
    if (Ran) {
        error("Ran twice")
    } else {
        Ran = 1
        return 1
    }
}

function number good(N:string) {
    switch (N) {
        case "E2",
            return 1
        default,
            return 0
    }
}

Changes to OPS

This also lowers base cost of userfunctions to 10 ops.

Forgot const optimization isn't currently being used. Needs an analyzer step.

Should really not have included that code in the rewrite but it's fine being there for now.
No need for `returned` field, can just check if scope is dead.
Adds logic for switch case to be the last statement in a function and be detected for return values.
@Vurv78 Vurv78 merged commit 59ca86e into wiremod:master Oct 13, 2023
1 check failed
@Vurv78 Vurv78 deleted the enforce-fn-return branch October 13, 2023 20:49
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.

1 participant