From 321ed3d44c34f9a57d9513769f5ba8447d3293a9 Mon Sep 17 00:00:00 2001 From: CountBleck Date: Mon, 14 Aug 2023 00:09:28 +0800 Subject: [PATCH] fix: Error on usage of reference types in fields (#2733) --- src/resolver.ts | 8 ++++++++ src/types.ts | 2 +- tests/compiler/bindings/esm.debug.js | 2 +- tests/compiler/bindings/esm.release.js | 2 +- tests/compiler/bindings/raw.debug.js | 2 +- tests/compiler/bindings/raw.release.js | 2 +- tests/compiler/features/reference-types.debug.wat | 8 ++++---- tests/compiler/field-reference-types.json | 7 +++++++ tests/compiler/field-reference-types.ts | 12 ++++++++++++ 9 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 tests/compiler/field-reference-types.json create mode 100644 tests/compiler/field-reference-types.ts diff --git a/src/resolver.ts b/src/resolver.ts index 16bd967847..e3e389d331 100644 --- a/src/resolver.ts +++ b/src/resolver.ts @@ -3407,6 +3407,14 @@ export class Resolver extends DiagnosticEmitter { if (boundInstance) { let fieldType = boundInstance.type; if (fieldType == Type.void) break; // failed to resolve earlier + if (fieldType.isExternalReference) { + this.error( + DiagnosticCode.Not_implemented_0, + assert(boundPrototype.typeNode).range, + "Reference typed fields" + ); + break; + } let needsLayout = true; if (base) { let existingMember = base.getMember(boundPrototype.name); diff --git a/src/types.ts b/src/types.ts index 4aef00a5b2..17bf8cbdbb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -638,7 +638,7 @@ export class Type { : signatureReference.toString(validWat); } else { return this.isNullableReference - ? `${this.kindToString()}${nullablePostfix}}` + ? `${this.kindToString()}${nullablePostfix}` : this.kindToString(); } } diff --git a/tests/compiler/bindings/esm.debug.js b/tests/compiler/bindings/esm.debug.js index 15804abe90..d2d89456b8 100644 --- a/tests/compiler/bindings/esm.debug.js +++ b/tests/compiler/bindings/esm.debug.js @@ -23,7 +23,7 @@ async function instantiate(module, imports = {}) { return Math.log(x); }, "globalThis.globalThis": ( - // bindings/esm/immutableGlobalNested: ref_extern | null} + // bindings/esm/immutableGlobalNested: ref_extern | null globalThis.globalThis ), "Date.getTimezoneOffset"() { diff --git a/tests/compiler/bindings/esm.release.js b/tests/compiler/bindings/esm.release.js index 6df7b0c319..30c529402d 100644 --- a/tests/compiler/bindings/esm.release.js +++ b/tests/compiler/bindings/esm.release.js @@ -23,7 +23,7 @@ async function instantiate(module, imports = {}) { return Math.log(x); }, "globalThis.globalThis": ( - // bindings/esm/immutableGlobalNested: ref_extern | null} + // bindings/esm/immutableGlobalNested: ref_extern | null globalThis.globalThis ), "Date.getTimezoneOffset"() { diff --git a/tests/compiler/bindings/raw.debug.js b/tests/compiler/bindings/raw.debug.js index 80551c4d39..cc59b6a796 100644 --- a/tests/compiler/bindings/raw.debug.js +++ b/tests/compiler/bindings/raw.debug.js @@ -23,7 +23,7 @@ export async function instantiate(module, imports = {}) { return Math.log(x); }, "globalThis.globalThis": ( - // bindings/esm/immutableGlobalNested: ref_extern | null} + // bindings/esm/immutableGlobalNested: ref_extern | null globalThis.globalThis ), "Date.getTimezoneOffset"() { diff --git a/tests/compiler/bindings/raw.release.js b/tests/compiler/bindings/raw.release.js index 80551c4d39..cc59b6a796 100644 --- a/tests/compiler/bindings/raw.release.js +++ b/tests/compiler/bindings/raw.release.js @@ -23,7 +23,7 @@ export async function instantiate(module, imports = {}) { return Math.log(x); }, "globalThis.globalThis": ( - // bindings/esm/immutableGlobalNested: ref_extern | null} + // bindings/esm/immutableGlobalNested: ref_extern | null globalThis.globalThis ), "Date.getTimezoneOffset"() { diff --git a/tests/compiler/features/reference-types.debug.wat b/tests/compiler/features/reference-types.debug.wat index d7dfc388c9..2e036c937e 100644 --- a/tests/compiler/features/reference-types.debug.wat +++ b/tests/compiler/features/reference-types.debug.wat @@ -35,7 +35,7 @@ (export "nonNullReal" (global $features/reference-types/nonNullReal)) (export "memory" (memory $0)) (start $~start) - (func $features/reference-types/testLocal + (func $features/reference-types/testLocal (local $local funcref) ref.null nofunc local.set $local @@ -68,7 +68,7 @@ unreachable end ) - (func $features/reference-types/testLocal + (func $features/reference-types/testLocal (local $local externref) ref.null noextern local.set $local @@ -256,8 +256,8 @@ call $~lib/builtins/abort unreachable end - call $features/reference-types/testLocal - call $features/reference-types/testLocal + call $features/reference-types/testLocal + call $features/reference-types/testLocal ref.func $features/reference-types/someFunc global.set $features/reference-types/funcGlobal global.get $features/reference-types/funcGlobal diff --git a/tests/compiler/field-reference-types.json b/tests/compiler/field-reference-types.json new file mode 100644 index 0000000000..328a6864a7 --- /dev/null +++ b/tests/compiler/field-reference-types.json @@ -0,0 +1,7 @@ +{ + "stderr": [ + "Not implemented: Reference typed fields", + "Not implemented: Reference typed fields", + "EOF" + ] +} diff --git a/tests/compiler/field-reference-types.ts b/tests/compiler/field-reference-types.ts new file mode 100644 index 0000000000..181e62a0ef --- /dev/null +++ b/tests/compiler/field-reference-types.ts @@ -0,0 +1,12 @@ +class Foo { + bar: externref = null; +} + +class Baz { + qux: T; +} + +new Foo(); +new Baz(); + +ERROR("EOF");