Skip to content

Strict field initialization #1155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ export enum LocalFlags {

/** Flags indicating the current state of an instance field. */
export enum FieldFlags {
NONE = 0,
INITIALIZED = 1 << 0,
NONE = 1,
INITIALIZED = 1 << 1,
}

/** Condition kinds. */
Expand Down Expand Up @@ -207,7 +207,7 @@ export class Flow {
/** Local flags. */
localFlags: LocalFlags[];
/** Field flags. */
fieldFlags: Map<string, FieldFlags>;
fieldFlags: Map<string, FieldFlags> | null = null;
/** Function being inlined, when inlining. */
inlineFunction: Function | null;
/** The label we break to when encountering a return statement, when inlining. */
Expand All @@ -224,7 +224,9 @@ export class Flow {
flow.returnType = parentFunction.signature.returnType;
flow.contextualTypeArguments = parentFunction.contextualTypeArguments;
flow.localFlags = [];
flow.fieldFlags = new Map();
if (parentFunction.is(CommonFlags.CONSTRUCTOR)) {
flow.fieldFlags = new Map();
}
flow.inlineFunction = null;
flow.inlineReturnLabel = null;
return flow;
Expand Down Expand Up @@ -285,7 +287,9 @@ export class Flow {
branch.localFlags = this.localFlags.slice();
branch.inlineFunction = this.inlineFunction;
branch.inlineReturnLabel = this.inlineReturnLabel;
branch.fieldFlags = new Map(this.fieldFlags);
if (branch.parentFunction.is(CommonFlags.CONSTRUCTOR)) {
branch.fieldFlags = new Map(this.fieldFlags!);
}
return branch;
}

Expand Down Expand Up @@ -531,13 +535,17 @@ export class Flow {

setFieldFlag(name: string, flag: FieldFlags): void {
let fieldFlags = this.fieldFlags;
const flags = fieldFlags.get(name) || 0;
fieldFlags.set(name, flags | flag);
if (fieldFlags) {
const flags = fieldFlags.has(name) ? assert(fieldFlags.get(name)) : FieldFlags.NONE;
fieldFlags.set(name, flags | flag);
}
}

isFieldFlag(name: string, flag: FieldFlags): bool {
const flags = this.fieldFlags.get(name);
if (flags) {
const fieldFlags = this.fieldFlags;

if (fieldFlags && fieldFlags.has(name)) {
const flags = assert(fieldFlags.get(name));
return (flags & flag) == flag;
}
return false;
Expand Down Expand Up @@ -831,17 +839,22 @@ export class Flow {
}
}

// Only the most right flow will have an effect
// on the resulting flow.
const rightFieldFlags = right.fieldFlags;
const rightKeys = Map_keys(rightFieldFlags);

for (let _values = Map_values(rightFieldFlags), i = 0, k = _values.length; i < k; ++i) {
const rightValue = unchecked(_values[i]);
const currentKey = unchecked(rightKeys[i]);
if (left.fieldFlags && right.fieldFlags && right.fieldFlags.size > 0) {
const rightFieldFlags = right.fieldFlags;
const rightKeys = Map_keys(rightFieldFlags);
const rightValues = Map_values(rightFieldFlags);

const leftFieldFlags = left.fieldFlags;

if (rightValue & FieldFlags.INITIALIZED) {
this.setFieldFlag(currentKey, FieldFlags.INITIALIZED);
for (let i = 0, k = rightValues.length; i < k; ++i) {
const rightValue = unchecked(rightValues[i]);
const rightKey = unchecked(rightKeys[i]);
const leftValue = leftFieldFlags.has(rightKey) ? assert(leftFieldFlags.get(rightKey)) : FieldFlags.NONE;

if ((rightValue & FieldFlags.INITIALIZED) && (leftValue & FieldFlags.INITIALIZED)) {
this.setFieldFlag(rightKey, FieldFlags.INITIALIZED);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/compiler/strict-init.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"stderr": [
"TS2564: Property inlinedProp has no initializer and is not definitely assigned in constructor",
"TS2564: Property b has no initializer and is not definitely assigned in constructor",
"TS2564: Property fieldLeft has no initializer and is not definitely assigned in constructor",
"TS2564: Property fieldRight has no initializer and is not definitely assigned in constructor",
"TS2564: Property p has no initializer and is not definitely assigned in constructor"
]
}
17 changes: 14 additions & 3 deletions tests/compiler/strict-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,24 @@ export class ExplicitConstructorInit {
}
}

export class EmptyIfBlock {
field: i32;
export class EmptyLeftBlock {
fieldLeft: i32;

constructor(x: i32) {
if ((x + 5) == 6) {
} else {
this.field = 7;
this.fieldLeft = 7;
}
}
}

export class EmptyRightBlock {
fieldRight: i32;

constructor(x: i32) {
if ((x + 5) == 6) {
this.fieldRight = 7;
} else {
}
}
}
Expand Down