Skip to content

Type hinting via comments, relaxed math compiler setting/flag, no casting flag - #355

Open
derpygamer2142 wants to merge 6 commits into
TurboWarp:developfrom
derpygamer2142:develop
Open

derpygamer2142 wants to merge 6 commits into
TurboWarp:developfrom
derpygamer2142:develop

Conversation

@derpygamer2142

@derpygamer2142 derpygamer2142 commented Oct 1, 2026 •

Copy link
Copy Markdown

Proposed Changes

Adds a system for hinting variable, list, and procedure argument types to the compiler. Also adds two settings, relaxedmath and nocast, which make the compiler less likely to determine that an operation will be NaN/Infinity, and severely limit casting, respectively. The compiler settings have a corresponding gui pr which lets you enable relaxedmath across the entire project: TurboWarp/scratch-gui#1196
Also has a corresponding docs pr because the format for hinting is weird: TurboWarp/docs#135

Reason for Changes

We can't really get around the conservative guesses that the compiler makes for perfect Scratch compatibility, relaxed math tries to account for some of that. Type hinting is to keep people from driving themselves to madness by trying to coerce the compiler into not generating toNotNaN() calls, and because there's no type inference for lists. nocast is just for the crazy people and breaks most projects I tested when enabled globally, but someone might find it interesting.

Test Coverage

Updated the usages of IROptimizer in the test suites to account for the change to the constructor

Adds type hinting for variables, lists and custom block arguments via comments, and settings to disable casting and to make the compiler less likely to determine a math operation is NaN.
@GarboMuffin

Copy link
Copy Markdown
Member

this should be 3 separate PRs and needs to be discussed a lot more

we know there are still ways to improve performance for everyone for free in the compiler. we can add types to procedure arguments if we tried, for example, so letting people put weird magic comments that can break behavior wouldn't really make sense until we've actually exhausted that

@derpygamer2142

Copy link
Copy Markdown
Author

i can split this into different prs if you want, and i wouldn't really be disappointed about removing the nocast flag because it's pretty esoteric.
my reasoning for relaxed math is that it stems from scratch compatibility behavior that rarely gets used in the first place unless someone is doing math with text input by a user or something like that. i tested a couple of different projects and saw a measurable speed increase without impacting functionality. i see it as sort of akin to the clone limit, where scratch added it to keep people from breaking their projects, but most of the time it doesn't do a whole lot.
i just thought that type hinting would be a neat way to work around compiler limitations, though i was also planning to try and add some other stuff like #85 and possibly list types. i could try to focus on those instead of the type hinting stuff, and procedure argument types do seem like they might be a reasonable thing to try and add.
the way i was planning on implementing them was by having an iroptimizer pass that looks at the types of each input when a procedure is called and stores that in a similar way to the variable type state, then uses that information when determining the type for an argument.

This branch has not been deployed

No deployments
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