-
-
Notifications
You must be signed in to change notification settings - Fork 400
Local Variable Type Hints #7892
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
Merged
APickledWalrus
merged 30 commits into
dev/feature
from
feature/local-variable-type-hints
Jul 1, 2025
Merged
Local Variable Type Hints #7892
APickledWalrus
merged 30 commits into
dev/feature
from
feature/local-variable-type-hints
Jul 1, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
These syntaxes were giving some false positive warnings
We do not want to copy hints from sections containing a statement that terminate the trigger
Merged
sovdeeth
requested changes
May 30, 2025
Variables will always use converters. so this allows us to represent a broader range of types (and likely reduce mistakes)
This was
linked to
issues
Jun 4, 2025
Type hints have become more advanced, and the goal is now for them to become errors. That is, the system should be opt-out rather than opt-in. Confidence in hint accuracy is high enough.
ContainerExpression behavior was wrong - variables should not be container expressions
Absolutionism
approved these changes
Jun 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
also I think EffZombify may be applicable for hint changing
sovdeeth
approved these changes
Jun 27, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking good to me
Efnilite
requested changes
Jun 28, 2025
src/main/java/ch/njol/skript/expressions/base/SectionExpression.java
Outdated
Show resolved
Hide resolved
Efnilite
approved these changes
Jul 1, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
An issue that needs to be fixed. Alternatively, a PR fixing an issue.
completed
The issue has been fully resolved and the change will be in the next Skript update.
feature
Pull request adding a new feature.
needs testing
Needs testing to determine current status or issue validity, or for WIP feature pulls.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Local variables are essentially accepted wherever they are used. This results in more potential mistakes and (some) strange syntax behavior that is hard to track. Detailed type information for variables helps in two cases:
1. Mismatched types
It's possible you use the wrong variable somewhere. Detailed type information can catch this and print a warning (or error). Consider:
We can catch that
{_a} in lowercase
is likely to be invalid and alert the user.2. Syntax accepting objects
Consider syntax like ExprArithmetic:
With detailed type information, we can now identify at parse time that this is an invalid operation, instead of failing at runtime and returning none.
Solution
With systems like ExecutionIntent and multiple return types, we are finally in a position to execute this system well. Type tracking for local variables has been implemented across syntax interacting with variables using a HintManager that is attached to the ParserInstance.
The testing script along with HintManager's documentation provide insight into how hints are managed.
As of now, I believe this system is accurate enough for type issues to be errors rather than warnings. However, there are still scenarios where type hints will fail:
That is, variables in loops whose types are changing across iterations may trigger false-positive errors (in rare cases hidden type changes could cause erroneous runtime failures). To work around this, it is possible to [un]suppress type hints (for a statement or block of code) using a new statement. We may also consider putting this behind an experiment as we continue to improve upon it (maybe whether it's an error?). Variables that cannot have hints and variables that do not have any hints act the same as they do now.
Variables have also been reworked here. When variables are passed to patterns request objects, the possible return types of the variable is simply an array of the known hints. Additionally, when variables are passed to patterns requesting some combination of types, the possible return types of the variable is the intersection of the hints and requested types.
I believe the system is accurate and stable, but further testing is needed. As a result, I have elected to put this behind an experiment. Additionally, it is possible to suppress type hints (in scripts where the experiment is enabled) for specific regions of code.
Change Explanations
experimentRegistry
(in Skript) was set to null too soon - it could be null even when more script code could run (consideron stop
triggers)getName
to arguments. This is used for providing type hints for named command arguments.StopTrigger
as theReturnableTrigger
is stopped in Functions whenreturn
is executed.EntryNode
as a possible return type for ExprNode. This is for ensuring type hints work properly for the syntax.beforeChange
runnable for loading sections into triggers. This was necessary for proper type hint implementation for these syntaxes. We need to be able to capture the hints and then merge them in once the trigger is loaded (since we copy variables at runtime).Variable#getConvertedExpression
has been updated to restrict what conversions are allowed on those variables.Classes#getSuperClassInfo
was not guaranteed to return the exact ClassInfo if one existed.SectionUtils
, which includes a static utility method for loading a section's code into a Trigger. More specifically, it handles the type of behavior that is becoming more common: Loading a section into a Trigger to use different context/events, preventing delays, and copying local variables at runtime. With type hints, the need to copy type hints arose, and the behavior became far more complicated. This method serves to handle that behavior and reduce the effort needed by the increasing number of implementing classes.API Support
Addon developers may need to support this feature if their syntax modifies variables (and results in type changes). First, you can use the HintManager to check if a Variable can use hints:
If so, you can obtain the active HintManager using the ParserInstance:
Then, depending on what type of operation your syntax is performing, you can manage hints as necessary:
Testing Completed
I have added a basic
type hints.sk
test (more to come). I plan to implement JUnit testing for HintManager.Supporting Information
Depends on:
Completes:
Related: