mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-05-02 10:22:15 +00:00
Add a more helpful error message when you haven't defined an allocator (#108)
Also adds a system for writing tests that assert that certain error codes are triggered so that I could test this.
This commit is contained in:
parent
edf4aaa966
commit
558ed78cc9
@ -2721,6 +2721,10 @@ export function compileAllocate(
|
||||
DiagnosticCode.Cannot_find_name_0,
|
||||
reportNode.range, allocateInternalName
|
||||
);
|
||||
program.info(
|
||||
DiagnosticCode.An_allocator_must_be_declared_to_allocate_memory_Try_importing_allocator_arena_or_allocator_tlsf,
|
||||
reportNode.range
|
||||
);
|
||||
return module.createUnreachable();
|
||||
}
|
||||
if (allocatePrototype.kind != ElementKind.FUNCTION_PROTOTYPE) {
|
||||
|
@ -23,6 +23,7 @@ export enum DiagnosticCode {
|
||||
Class_0_is_sealed_and_cannot_be_extended = 211,
|
||||
Decorator_0_is_not_valid_here = 212,
|
||||
Duplicate_decorator = 213,
|
||||
An_allocator_must_be_declared_to_allocate_memory_Try_importing_allocator_arena_or_allocator_tlsf = 214,
|
||||
Unterminated_string_literal = 1002,
|
||||
Identifier_expected = 1003,
|
||||
_0_expected = 1005,
|
||||
@ -131,6 +132,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
|
||||
case 211: return "Class '{0}' is sealed and cannot be extended.";
|
||||
case 212: return "Decorator '{0}' is not valid here.";
|
||||
case 213: return "Duplicate decorator.";
|
||||
case 214: return "An allocator must be declared to allocate memory. Try importing allocator/arena or allocator/tlsf.";
|
||||
case 1002: return "Unterminated string literal.";
|
||||
case 1003: return "Identifier expected.";
|
||||
case 1005: return "'{0}' expected.";
|
||||
|
@ -15,6 +15,7 @@
|
||||
"Class '{0}' is sealed and cannot be extended.": 211,
|
||||
"Decorator '{0}' is not valid here.": 212,
|
||||
"Duplicate decorator.": 213,
|
||||
"An allocator must be declared to allocate memory. Try importing allocator/arena or allocator/tlsf.": 214,
|
||||
|
||||
"Unterminated string literal.": 1002,
|
||||
"Identifier expected.": 1003,
|
||||
|
@ -40,10 +40,23 @@ if (args._.length) {
|
||||
}
|
||||
}
|
||||
|
||||
const EXPECT_ERROR_PREFIX = '// Expect error:';
|
||||
|
||||
// Returns an array of error strings to expect, or null if compilation should succeed.
|
||||
function getExpectedErrors(filePath) {
|
||||
const lines = fs.readFileSync(filePath).toString().split('\n');
|
||||
const expectErrorLines = lines.filter(line => line.startsWith(EXPECT_ERROR_PREFIX));
|
||||
if (expectErrorLines.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return expectErrorLines.map(line => line.slice(EXPECT_ERROR_PREFIX.length).trim());
|
||||
}
|
||||
|
||||
// TODO: asc's callback is synchronous here. This might change.
|
||||
tests.forEach(filename => {
|
||||
console.log(chalk.whiteBright("Testing compiler/" + filename) + "\n");
|
||||
|
||||
const expectedErrors = getExpectedErrors(path.join(basedir, filename));
|
||||
const basename = filename.replace(/\.ts$/, "");
|
||||
|
||||
const stdout = asc.createMemoryStream();
|
||||
@ -66,6 +79,24 @@ tests.forEach(filename => {
|
||||
stderr: stderr
|
||||
}, err => {
|
||||
console.log();
|
||||
|
||||
if (expectedErrors) {
|
||||
const stderrString = stderr.toString();
|
||||
for (const expectedError of expectedErrors) {
|
||||
if (!stderrString.includes(expectedError)) {
|
||||
console.log(`Expected error "${expectedError}" was not in the error output.`);
|
||||
console.log("- " + chalk.red("error check ERROR"));
|
||||
failedTests.push(basename);
|
||||
console.log();
|
||||
return;
|
||||
}
|
||||
}
|
||||
console.log("- " + chalk.green("error check OK"));
|
||||
++successes;
|
||||
console.log();
|
||||
return;
|
||||
}
|
||||
|
||||
if (err)
|
||||
stderr.write(err + os.EOL);
|
||||
var actual = stdout.toString().replace(/\r\n/g, "\n");
|
||||
|
8
tests/compiler/new-without-allocator.ts
Normal file
8
tests/compiler/new-without-allocator.ts
Normal file
@ -0,0 +1,8 @@
|
||||
// Expect error: TS2304: Cannot find name 'allocate_memory'.
|
||||
// Expect error: AS214: An allocator must be declared to allocate memory. Try importing allocator/arena or allocator/tlsf.
|
||||
class A {}
|
||||
|
||||
export function test(): i32 {
|
||||
var a = new A();
|
||||
return 3;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user