Skip to content

Fix validation exception when calling roFunction#1588

Merged
TwitchBronBron merged 6 commits intov1from
fix_return_validation_on_roFunction
Nov 4, 2025
Merged

Fix validation exception when calling roFunction#1588
TwitchBronBron merged 6 commits intov1from
fix_return_validation_on_roFunction

Conversation

@markwpearce
Copy link
Collaborator

Fixes #1587

The return type of a roFunction was undefined. This PR changes it so if we're calling a variable typed as roFunction we get dynamic (just like as if we're calling something typed as function)

@markwpearce markwpearce added this to the v1.0.0 milestone Nov 2, 2025
Copy link
Collaborator

@luis-j-soares luis-j-soares left a comment

Choose a reason for hiding this comment

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

This one solves the original problem!

However, it reveals a new crash when you have a pattern like this:

function calc(a as dynamic, b as dynamic, op as string) as dynamic
    op = getOperation(op)
    return op(1, 2)
end function

function getOperation(name as string) as object
    return {
        "sum": function(a as dynamic, b as dynamic) as dynamic
            return a + b
        end function
        "sub": function(a as dynamic, b as dynamic) as dynamic
            return a - b
        end function
        "mul": function(a as dynamic, b as dynamic) as dynamic
            return a * b
        end function
        "div": function(a as dynamic, b as dynamic) as dynamic
            return a / b
        end function
    }
end function
[11:56:29:165 AM] Validating project
[11:56:29:212 AM] Error when calling plugin BscPlugin.onScopeValidate: TypeError: this.outerType.getTarget is not a function
    at Object.get (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/types/ReferenceType.ts:397:101)
    at isVoidType (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/reflection.ts:410:19)
    at ScopeValidator.validateReturnStatement (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/bscPlugin/validation/ScopeValidator.ts:673:47)
    at /Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/bscPlugin/validation/ScopeValidator.ts:205:34
    at ScopeValidator.addValidationKindMetric (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/bscPlugin/validation/ScopeValidator.ts:332:9)
    at Object.ReturnStatement (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/bscPlugin/validation/ScopeValidator.ts:204:30)
    at /Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:222:51
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:54:23)
    at walkArray (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:119:29)
    at Block.walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/parser/Statement.ts:467:22)
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:93:13)
    at FunctionExpression.walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/parser/Expression.ts:414:6)
    at walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/astUtils/visitors.ts:93:13)
    at FunctionStatement.walk (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/parser/Statement.ts:654:17)
    at /Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/bscPlugin/validation/ScopeValidator.ts:312:29
    at Scope.enumerateOwnFiles (/Users/lse16/dev/brighterscript-test-bench/node_modules/brighterscript/src/Scope.ts:706:17)
[11:56:29:214 AM] Validating project finished. (48.761ms)

actualReturnType is a TypePropertyReferenceType which lacks getTarget().

@luis-j-soares
Copy link
Collaborator

luis-j-soares commented Nov 3, 2025

Oh interesting! I don't get the crash if calc is the first function in the file. But as soon as I add a function above calc, then I get the crash. Does the same happen to you?

@markwpearce
Copy link
Collaborator Author

@luis-j-soares Yes, I do! weird!
I'll look at this.

@markwpearce
Copy link
Collaborator Author

@luis-j-soares I was able to reproduce, and I think this will fix that issue now


export function isCallableType(target): target is BaseFunctionType {
return isFunctionTypeLike(target) || isTypedFunctionType(target) || (isDynamicType(target) && !isAnyReferenceType(target));
return isFunctionTypeLike(target) || isTypedFunctionType(target) || isObjectType(target) || (isDynamicType(target) && !isAnyReferenceType(target));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

object is callable , because roFunction is an object. sheesh

@luis-j-soares luis-j-soares added the create-package create a temporary npm package on every commit label Nov 3, 2025
@rokucommunity-bot
Copy link
Contributor

Hey there! I just built a new temporary npm package based on 3f32e85. You can download it here or install it by running the following command:

npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-1.0.0-alpha.48-fix-return-validation-on-roFunction.20251103153805.tgz

Copy link
Collaborator

@luis-j-soares luis-j-soares left a comment

Choose a reason for hiding this comment

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

Tentatively approving since it's fixing all of the compiler errors I've seen, although the proxy code is still quite unknown to me. 😅

@rokucommunity-bot
Copy link
Contributor

Hey there! I just built a new temporary npm package based on 6700b81. You can download it here or install it by running the following command:

npm install https://github.com/rokucommunity/brighterscript/releases/download/v0.0.0-packages/brighterscript-1.0.0-alpha.48-fix-return-validation-on-roFunction.20251104175005.tgz

@TwitchBronBron TwitchBronBron merged commit 1f58090 into v1 Nov 4, 2025
13 of 16 checks passed
@TwitchBronBron TwitchBronBron deleted the fix_return_validation_on_roFunction branch November 4, 2025 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

create-package create a temporary npm package on every commit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants