Skip to content

Commit 7497862

Browse files
committed
Exclude inherited fields for error reporting
1 parent ba39da9 commit 7497862

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/compiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,7 @@ export class Compiler extends DiagnosticEmitter {
15441544
if (instanceMembers) {
15451545
for (let _values = Map_values(instanceMembers), i = 0, k = _values.length; i < k; ++i) {
15461546
const element = unchecked(_values[i]);
1547-
if (element.kind == ElementKind.FIELD) {
1547+
if (element.kind == ElementKind.FIELD && element.parent == instance) {
15481548
const field = <Field>element;
15491549
if (!flow.isFieldFlag(field.internalName, FieldFlags.INITIALIZED)) {
15501550
this.error(
@@ -10038,7 +10038,7 @@ export class Compiler extends DiagnosticEmitter {
1003810038
let fieldPrototype = field.prototype;
1003910039
let initializerNode = fieldPrototype.initializerNode;
1004010040
let parameterIndex = fieldPrototype.parameterIndex;
10041-
let initExpr: ExpressionRef | null = null;
10041+
let initExpr: ExpressionRef = -1;
1004210042
const isDefiniteAssigment = field.is(CommonFlags.DEFINITE_ASSIGNMENT);
1004310043

1004410044
// if declared as a constructor parameter, use its value
@@ -10064,7 +10064,7 @@ export class Compiler extends DiagnosticEmitter {
1006410064
initExpr = this.makeZero(fieldType);
1006510065
}
1006610066

10067-
if (initExpr) {
10067+
if (initExpr >= 0) {
1006810068
stmts.push(
1006910069
module.store(fieldType.byteSize,
1007010070
module.local_get(thisLocalIndex, nativeSizeType),

tests/compiler/strict-init.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// OK - Field with explicit initializer
22
export class WithInitializer {
33
public a: i32 = 1;
4+
some: string | null = null;
45
}
56

67
// ERR - Field b not initialized
@@ -23,6 +24,7 @@ export class ExplicitConstructorInit {
2324
}
2425
}
2526

27+
// ERR - Left block empty
2628
export class EmptyLeftBlock {
2729
fieldLeft: i32;
2830

@@ -34,6 +36,7 @@ export class EmptyLeftBlock {
3436
}
3537
}
3638

39+
// ERR - Right block empty
3740
export class EmptyRightBlock {
3841
fieldRight: i32;
3942

@@ -45,6 +48,7 @@ export class EmptyRightBlock {
4548
}
4649
}
4750

51+
// ERR - Indefinite
4852
export class NonDefiniteIf {
4953
p: f64;
5054
constructor(a: i32) {
@@ -56,6 +60,7 @@ export class NonDefiniteIf {
5660
}
5761
}
5862

63+
// OK - All branches covered
5964
export class DefiniteIf {
6065
definite: i32;
6166

@@ -73,9 +78,29 @@ export class DefiniteIf {
7378
}
7479

7580

76-
class Inlined {
77-
inlinedProp: i32;
78-
constructor() {}
81+
// ERR
82+
export class Inlined {
83+
inlinedProp: string | null;
7984
}
8085

8186
new Inlined();
87+
88+
// OK - inherited fields
89+
class ElementKind {}
90+
91+
export abstract class Y {
92+
inherited: string | null = "the inherited string";
93+
}
94+
95+
export abstract class X extends Y {
96+
ax: string[] | null;
97+
protected constructor(public kind: ElementKind, other: ElementKind | null) {
98+
super();
99+
100+
if (other) {
101+
this.ax = ["string"];
102+
} else {
103+
this.ax = ["string", "string"];
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)