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

Assign invalid types as dynamic #1389

Merged
merged 2 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/bscPlugin/validation/BrsFileValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,58 @@ describe('BrsFileValidator', () => {
expectZeroDiagnostics(program);
});
});

describe('types', () => {
it('sets assignments of invalid as dynamic', () => {
const file = program.setFile<BrsFile>('source/main.bs', `
sub test()
channel = invalid
if true
channel = {
height: 123
}
end if

height = 0
if channel <> invalid then
height += channel.height
end if
end sub
`);
program.validate();
expectZeroDiagnostics(program);
const func = file.ast.statements[0].findChild<FunctionExpression>(isFunctionExpression, { walkMode: WalkMode.visitAllRecursive });
const table = func.body.getSymbolTable();
const data = {} as ExtraSymbolData;
const channelType = table.getSymbolType('channel', { flags: SymbolTypeFlag.runtime, data: data });
expectTypeToBe(channelType, DynamicType);
});

it('sets default arg of invalid as dynamic', () => {
const file = program.setFile<BrsFile>('source/main.bs', `
sub test(channel = invalid)
if true
channel = {
height: 123
}
end if

height = 0
if channel <> invalid then
height += channel.height
end if
end sub
`);
program.validate();
expectZeroDiagnostics(program);
const func = file.ast.statements[0].findChild<FunctionExpression>(isFunctionExpression, { walkMode: WalkMode.visitAllRecursive });
const table = func.body.getSymbolTable();
const data = {} as ExtraSymbolData;
const channelType = table.getSymbolType('channel', { flags: SymbolTypeFlag.runtime, data: data });
expectTypeToBe(channelType, DynamicType);
});
});

describe('instances of types', () => {
it('sets assigned variables as instances', () => {
const file = program.setFile<BrsFile>('source/main.bs', `
Expand Down
7 changes: 5 additions & 2 deletions src/bscPlugin/validation/BrsFileValidator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isAliasStatement, isArrayType, isBlock, isBody, isClassStatement, isConditionalCompileConstStatement, isConditionalCompileErrorStatement, isConditionalCompileStatement, isConstStatement, isDottedGetExpression, isDottedSetStatement, isEnumStatement, isForEachStatement, isForStatement, isFunctionExpression, isFunctionStatement, isImportStatement, isIndexedGetExpression, isIndexedSetStatement, isInterfaceStatement, isLibraryStatement, isLiteralExpression, isMethodStatement, isNamespaceStatement, isTypecastExpression, isTypecastStatement, isUnaryExpression, isVariableExpression, isWhileStatement } from '../../astUtils/reflection';
import { isAliasStatement, isArrayType, isBlock, isBody, isClassStatement, isConditionalCompileConstStatement, isConditionalCompileErrorStatement, isConditionalCompileStatement, isConstStatement, isDottedGetExpression, isDottedSetStatement, isEnumStatement, isForEachStatement, isForStatement, isFunctionExpression, isFunctionStatement, isImportStatement, isIndexedGetExpression, isIndexedSetStatement, isInterfaceStatement, isInvalidType, isLibraryStatement, isLiteralExpression, isMethodStatement, isNamespaceStatement, isTypecastExpression, isTypecastStatement, isUnaryExpression, isVariableExpression, isVoidType, isWhileStatement } from '../../astUtils/reflection';
import { createVisitor, WalkMode } from '../../astUtils/visitors';
import { DiagnosticMessages } from '../../DiagnosticMessages';
import type { BrsFile } from '../../files/BrsFile';
Expand Down Expand Up @@ -108,7 +108,10 @@ export class BrsFileValidator {
AssignmentStatement: (node) => {
const data: ExtraSymbolData = {};
//register this variable
const nodeType = node.getType({ flags: SymbolTypeFlag.runtime, data: data });
let nodeType = node.getType({ flags: SymbolTypeFlag.runtime, data: data });
if (isInvalidType(nodeType) || isVoidType(nodeType)) {
nodeType = DynamicType.instance;
}
node.parent.getSymbolTable()?.addSymbol(node.tokens.name.text, { definingNode: node, isInstance: true, isFromDocComment: data.isFromDocComment }, nodeType, SymbolTypeFlag.runtime);
},
DottedSetStatement: (node) => {
Expand Down
64 changes: 64 additions & 0 deletions src/bscPlugin/validation/ScopeValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,70 @@ describe('ScopeValidator', () => {
expectZeroDiagnostics(program);
});

it('allows access of properties of union with invalid', () => {
program.setFile<BrsFile>('source/main.bs', `
sub test()
channel = invalid
if true
channel = {
height: 123
}
end if

height = 0
if channel <> invalid then
height += channel.height
end if
end sub
`);
program.validate();
expectZeroDiagnostics(program);

});

it('sets default arg of invalid as dynamic', () => {
program.setFile<BrsFile>('source/main.bs', `
sub test(channel = invalid)
if true
channel = {
height: 123
}
end if

height = 0
if channel <> invalid then
height += channel.height
end if
end sub
`);
program.validate();
expectZeroDiagnostics(program);

});

it('sets assignment of function returning invalid as dynamic', () => {
program.setFile<BrsFile>('source/main.bs', `
sub test()
channel = noReturn()
if true
channel = {
height: 123
}
end if

height = 0
if channel <> invalid then
height += channel.height
end if
end sub

sub noReturn()
print "hello"
end sub
`);
program.validate();
expectZeroDiagnostics(program);
});
});

describe('itemCannotBeUsedAsVariable', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/types/VoidType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { BscType } from './BscType';
import { BscTypeKind } from './BscTypeKind';
import { isUnionTypeCompatible } from './helpers';
import { BuiltInInterfaceAdder } from './BuiltInInterfaceAdder';
import type { TypeCompatibilityData } from '../interfaces';
import type { GetTypeOptions, TypeCompatibilityData } from '../interfaces';
import { DynamicType } from './DynamicType';

export class VoidType extends BscType {
constructor(
Expand Down Expand Up @@ -36,6 +37,10 @@ export class VoidType extends BscType {
public isEqual(targetType: BscType) {
return isVoidType(targetType);
}

getMemberType(memberName: string, options: GetTypeOptions) {
return DynamicType.instance;
}
}

BuiltInInterfaceAdder.primitiveTypeInstanceCache.set('void', VoidType.instance);
4 changes: 2 additions & 2 deletions src/types/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TypeCompatibilityData } from '../interfaces';
import { isAnyReferenceType, isDynamicType, isEnumMemberType, isEnumType, isInheritableType, isInterfaceType, isReferenceType, isUnionType } from '../astUtils/reflection';
import { isAnyReferenceType, isDynamicType, isEnumMemberType, isEnumType, isInheritableType, isInterfaceType, isReferenceType, isUnionType, isVoidType } from '../astUtils/reflection';
import type { BscType } from './BscType';
import type { UnionType } from './UnionType';

Expand Down Expand Up @@ -126,7 +126,7 @@ export function getUniqueType(types: BscType[], unionTypeFactory: (types: BscTyp
if (!types || types.length === 0) {
return undefined;
}
const dynType = types.find((x) => !isAnyReferenceType(x) && isDynamicType(x));
const dynType = types.find((x) => !isAnyReferenceType(x) && (isDynamicType(x) || isVoidType(x)));
if (dynType) {
return dynType;
}
Expand Down
Loading