diff --git a/src/builtins.ts b/src/builtins.ts index a7a23713..826cebfc 100644 --- a/src/builtins.ts +++ b/src/builtins.ts @@ -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) { diff --git a/src/diagnosticMessages.generated.ts b/src/diagnosticMessages.generated.ts index 07010fe9..60bd13c4 100644 --- a/src/diagnosticMessages.generated.ts +++ b/src/diagnosticMessages.generated.ts @@ -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."; diff --git a/src/diagnosticMessages.json b/src/diagnosticMessages.json index 97dbe98b..fd7a7267 100644 --- a/src/diagnosticMessages.json +++ b/src/diagnosticMessages.json @@ -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, diff --git a/tests/compiler.js b/tests/compiler.js index ff603eb3..fb9523c8 100644 --- a/tests/compiler.js +++ b/tests/compiler.js @@ -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"); diff --git a/tests/compiler/new-without-allocator.ts b/tests/compiler/new-without-allocator.ts new file mode 100644 index 00000000..6463ff33 --- /dev/null +++ b/tests/compiler/new-without-allocator.ts @@ -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; +}