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

Changes expectedReturnStatement to returnTypeCoercionMismatch #1370

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
6 changes: 3 additions & 3 deletions src/DiagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,11 +1026,11 @@ export let DiagnosticMessages = {
severity: DiagnosticSeverity.Error,
code: 'inconsistent-conditional-compile-nesting'
}),
expectedReturnStatement: () => ({
message: `Expected return statement in function`,
returnTypeCoercionMismatch: (returnType = 'string') => ({
message: `Function has no return statement and will return 'invalid': '${returnType}' cannot be coerced into 'invalid'`,
legacyCode: 1151,
severity: DiagnosticSeverity.Error,
code: 'expected-return-statement'
code: 'return-type-coercion-mismatch'
})
};
export const defaultMaximumTruncationLength = 160;
Expand Down
66 changes: 46 additions & 20 deletions src/bscPlugin/validation/ScopeValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2476,7 +2476,7 @@ describe('ScopeValidator', () => {
});
});

describe('expectedReturnStatement', () => {
describe('returnTypeCoercionMismatch', () => {
it('allows functions, subs, and "function as void/dynamic" to not have return statements', () => {
program.setFile('source/util.bs', `
function noTypeSpecified()
Expand All @@ -2495,68 +2495,94 @@ describe('ScopeValidator', () => {
expectZeroDiagnostics(program);
});


it('detects when a function does not have a return statement', () => {
program.setFile('source/util.bs', `
function doSomething() as integer
function doSomething() as string
end function
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.expectedReturnStatement().message
DiagnosticMessages.returnTypeCoercionMismatch().message
]);
});

it('allows when a function does not have a return statement, but type coercsion is okay', () => {
program.setFile('source/util.bs', `
interface Whatever
name as string
end interface

function doSomething() as Whatever
end function

function doSomething2() as object
end function

function doSomething3() as integer
end function

function doSomething4() as float
end function

function doSomething5() as boolean
end function

`);
program.validate();
// all these are ok
expectZeroDiagnostics(program);
});

it('detects when a namespaced function does not have a return statement', () => {
program.setFile('source/util.bs', `
namespace alpha
function doSomething() as integer
function doSomething() as string
end function
end namespace
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.expectedReturnStatement().message
DiagnosticMessages.returnTypeCoercionMismatch().message
]);
});

it('detects when an inline function does not have a return statement', () => {
program.setFile('source/util.bs', `
function outer() as integer
inner = function() as integer
function outer() as string
inner = function () as string
print "no return!"
end function
return inner()
end function
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.expectedReturnStatement().message
DiagnosticMessages.returnTypeCoercionMismatch().message
]);
});

it('detects when an outer function does not have a return statement', () => {
program.setFile('source/util.bs', `
function outer() as integer
inner = function() as integer
return 1
function outer() as string
inner = function() as string
return "abc"
end function
print inner()
end function
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.expectedReturnStatement().message
DiagnosticMessages.returnTypeCoercionMismatch().message
]);
});

it('detects when a outer function has a return statement in a branch', () => {
program.setFile('source/util.bs', `
function hasBranch(x) as integer
function hasBranch(x) as string
if x = 1
return 1
return "1"
else
return 2
return "2"
end if
end function
`);
Expand All @@ -2566,20 +2592,20 @@ describe('ScopeValidator', () => {

it('works for sub with return types with missing return', () => {
program.setFile('source/util.bs', `
sub doSomething() as integer
sub doSomething() as string
end sub
`);
program.validate();
expectDiagnostics(program, [
DiagnosticMessages.expectedReturnStatement().message
DiagnosticMessages.returnTypeCoercionMismatch().message
]);
});


it('works for sub with return types', () => {
program.setFile('source/util.bs', `
sub doSomething() as integer
return 1
sub doSomething() as string
return "1"
end sub
`);
program.validate();
Expand Down
4 changes: 2 additions & 2 deletions src/bscPlugin/validation/ScopeValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,9 +897,9 @@ export class ScopeValidator {
return;
}
const returns = func.body?.findChild<ReturnStatement>(isReturnStatement, { walkMode: WalkMode.visitAll });
if (!returns) {
if (!returns && isStringType(returnType)) {
this.addMultiScopeDiagnostic({
...DiagnosticMessages.expectedReturnStatement(),
...DiagnosticMessages.returnTypeCoercionMismatch(returnType.toString()),
location: func.location
});
}
Expand Down
Loading