← Back to Pull requests

GDScript: Use narrowed types from `is` type checks in `if` blocks #123472

Closed
unknown closed this pull request without merging on Sep 14, 2026 10:15 UTC
View on GitHub
unknown unknown commented on Sep 13, 2026 21:09 UTC
Author

When debug/gdscript/warnings/unsafe_property_access and debug/gdscript/warnings/unsafe_method_access are set to warning or error, accessing a member on a variable that was just checked with is still reports the warning, even though the check proves the access is safe:

func foo(reward: Reward) -> void:
	if reward is GoldReward:
		print(reward.value) # UNSAFE_PROPERTY_ACCESS, even though it was just checked.
		print(reward.calc()) # UNSAFE_METHOD_ACCESS.

Approach

The analyzer now tracks a narrowed type for variables that are tested with is type tests in the condition of an if statement (including the operands of an and in that condition):

  • While the true block of that statement is being resolved, occurrences of the checked variable use the tested type instead of its declared type. Since the narrowed type is a hard type, the member access resolves statically and the unsafe access warnings no longer fire.

  • Only local variables, static variables, and function parameters can be narrowed. Identifiers are matched against their source node, so a shadowing declaration in the block (e.g. a for loop variable with the same name) is not affected.

  • If the variable is assigned inside the block, the narrowed type is invalidated from that point on, since it may then hold a value of another type. Code before the assignment still benefits from the narrowing, matching the execution order.

  • This applies to Variant variables as well, which is the most common case where the unsafe warnings fire on is-checked values.
  • Completions already partially understand is narrowing (see #120492), but this PR makes the analyzer itself aware of it, which is what drives the warnings.

    Tests

  • modules/gdscript/tests/scripts/analyzer/features/is_type_narrowing.gd: reproduces the issue scenario and checks the code both analyzes and runs correctly.

  • modules/gdscript/tests/scripts/analyzer/warnings/type_narrowing.gd: checks that unsafe access warnings are no longer emitted after an is check, that they still fire outside the narrowed block, and that an assignment inside the block invalidates the narrowing.
  • All file-based GDScript tests (512) pass, including the binary tokens variant on Linux CI.

    Diff: 284 lines (+213 / -71) across 3 files Reviewer: Adarsh-Me Review duration: 786m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 284 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.338
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #123472"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.7% (MEDIUM) · Higher = larger blast radius
    39.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.7% · Safe to merge
    15.7%