Skip to content

Commit 558ed78

Browse files
alangpiercedcodeIO
authored andcommitted
Add a more helpful error message when you haven't defined an allocator (AssemblyScript#108)
Also adds a system for writing tests that assert that certain error codes are triggered so that I could test this.
1 parent edf4aaa commit 558ed78

5 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/builtins.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2721,6 +2721,10 @@ export function compileAllocate(
27212721
DiagnosticCode.Cannot_find_name_0,
27222722
reportNode.range, allocateInternalName
27232723
);
2724+
program.info(
2725+
DiagnosticCode.An_allocator_must_be_declared_to_allocate_memory_Try_importing_allocator_arena_or_allocator_tlsf,
2726+
reportNode.range
2727+
);
27242728
return module.createUnreachable();
27252729
}
27262730
if (allocatePrototype.kind != ElementKind.FUNCTION_PROTOTYPE) {

src/diagnosticMessages.generated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export enum DiagnosticCode {
2323
Class_0_is_sealed_and_cannot_be_extended = 211,
2424
Decorator_0_is_not_valid_here = 212,
2525
Duplicate_decorator = 213,
26+
An_allocator_must_be_declared_to_allocate_memory_Try_importing_allocator_arena_or_allocator_tlsf = 214,
2627
Unterminated_string_literal = 1002,
2728
Identifier_expected = 1003,
2829
_0_expected = 1005,
@@ -131,6 +132,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
131132
case 211: return "Class '{0}' is sealed and cannot be extended.";
132133
case 212: return "Decorator '{0}' is not valid here.";
133134
case 213: return "Duplicate decorator.";
135+
case 214: return "An allocator must be declared to allocate memory. Try importing allocator/arena or allocator/tlsf.";
134136
case 1002: return "Unterminated string literal.";
135137
case 1003: return "Identifier expected.";
136138
case 1005: return "'{0}' expected.";

src/diagnosticMessages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"Class '{0}' is sealed and cannot be extended.": 211,
1616
"Decorator '{0}' is not valid here.": 212,
1717
"Duplicate decorator.": 213,
18+
"An allocator must be declared to allocate memory. Try importing allocator/arena or allocator/tlsf.": 214,
1819

1920
"Unterminated string literal.": 1002,
2021
"Identifier expected.": 1003,

tests/compiler.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,23 @@ if (args._.length) {
4040
}
4141
}
4242

43+
const EXPECT_ERROR_PREFIX = '// Expect error:';
44+
45+
// Returns an array of error strings to expect, or null if compilation should succeed.
46+
function getExpectedErrors(filePath) {
47+
const lines = fs.readFileSync(filePath).toString().split('\n');
48+
const expectErrorLines = lines.filter(line => line.startsWith(EXPECT_ERROR_PREFIX));
49+
if (expectErrorLines.length === 0) {
50+
return null;
51+
}
52+
return expectErrorLines.map(line => line.slice(EXPECT_ERROR_PREFIX.length).trim());
53+
}
54+
4355
// TODO: asc's callback is synchronous here. This might change.
4456
tests.forEach(filename => {
4557
console.log(chalk.whiteBright("Testing compiler/" + filename) + "\n");
4658

59+
const expectedErrors = getExpectedErrors(path.join(basedir, filename));
4760
const basename = filename.replace(/\.ts$/, "");
4861

4962
const stdout = asc.createMemoryStream();
@@ -66,6 +79,24 @@ tests.forEach(filename => {
6679
stderr: stderr
6780
}, err => {
6881
console.log();
82+
83+
if (expectedErrors) {
84+
const stderrString = stderr.toString();
85+
for (const expectedError of expectedErrors) {
86+
if (!stderrString.includes(expectedError)) {
87+
console.log(`Expected error "${expectedError}" was not in the error output.`);
88+
console.log("- " + chalk.red("error check ERROR"));
89+
failedTests.push(basename);
90+
console.log();
91+
return;
92+
}
93+
}
94+
console.log("- " + chalk.green("error check OK"));
95+
++successes;
96+
console.log();
97+
return;
98+
}
99+
69100
if (err)
70101
stderr.write(err + os.EOL);
71102
var actual = stdout.toString().replace(/\r\n/g, "\n");
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Expect error: TS2304: Cannot find name 'allocate_memory'.
2+
// Expect error: AS214: An allocator must be declared to allocate memory. Try importing allocator/arena or allocator/tlsf.
3+
class A {}
4+
5+
export function test(): i32 {
6+
var a = new A();
7+
return 3;
8+
}

0 commit comments

Comments
 (0)