Skip to content

Commit

Permalink
fix: Ensure call-less references to builtin functions in namespaces e…
Browse files Browse the repository at this point in the history
…rror (#2738)
  • Loading branch information
CountBleck authored Oct 1, 2023
1 parent 73193f0 commit 58ed2b2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9044,10 +9044,29 @@ export class Compiler extends DiagnosticEmitter {
}
case ElementKind.FunctionPrototype: {
let functionPrototype = <FunctionPrototype>target;
let typeParameterNodes = functionPrototype.typeParameterNodes;

if (typeParameterNodes && typeParameterNodes.length != 0) {
this.error(
DiagnosticCode.Type_argument_expected,
expression.range
);
break; // also diagnose 'not a value at runtime'
}

let functionInstance = this.resolver.resolveFunction(functionPrototype, null);
if (!functionInstance) return module.unreachable();
if (!this.compileFunction(functionInstance)) return module.unreachable();
this.currentType = functionInstance.type;

if (functionInstance.hasDecorator(DecoratorFlags.Builtin)) {
this.error(
DiagnosticCode.Not_implemented_0,
expression.range, "First-class built-ins"
);
return module.unreachable();
}

let offset = this.ensureRuntimeFunction(functionInstance);
return this.options.isWasm64
? module.i64(i64_low(offset), i64_high(offset))
Expand Down
15 changes: 15 additions & 0 deletions tests/compiler/issues/2737.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"stderr": [
"TS1140: Type argument expected.",
"foo.bar;",
"AS234: Expression does not compile to a value at runtime.",
"foo.bar;",
"TS1140: Type argument expected.",
"memory.data;",
"AS234: Expression does not compile to a value at runtime.",
"memory.data;",
"AS100: Not implemented: First-class built-ins",
"atomic.fence;",
"EOF"
]
}
12 changes: 12 additions & 0 deletions tests/compiler/issues/2737.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace foo {
export function bar<T>(): void {}
}

// Should error from missing type arguments:
foo.bar;
memory.data;

// Should error from lacking first-class builtins:
atomic.fence;

ERROR("EOF");

0 comments on commit 58ed2b2

Please sign in to comment.