Skip to content
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

feat: add NonNullable builtin type to remove null from type union #1875

Merged
merged 19 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 30 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export namespace CommonNames {
export const indexof = "indexof";
export const valueof = "valueof";
export const returnof = "returnof";
export const notnullable = "NonNullable";
willemneal marked this conversation as resolved.
Show resolved Hide resolved
// aliases
export const null_ = "null";
export const true_ = "true";
Expand Down
6 changes: 6 additions & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,12 @@ export class Program extends DiagnosticEmitter {
this.makeNativeTypeDeclaration(CommonNames.returnof, CommonFlags.EXPORT | CommonFlags.GENERIC),
DecoratorFlags.BUILTIN
));
this.nativeFile.add(CommonNames.notnullable, new TypeDefinition(
CommonNames.notnullable,
this.nativeFile,
this.makeNativeTypeDeclaration(CommonNames.notnullable, CommonFlags.EXPORT | CommonFlags.GENERIC),
DecoratorFlags.BUILTIN
));

// The following types might not be enabled by compiler options, so the
// compiler needs to check this condition whenever such a value is created
Expand Down
111 changes: 55 additions & 56 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export class Resolver extends DiagnosticEmitter {
if (text == CommonNames.indexof) return this.resolveBuiltinIndexofType(node, ctxElement, ctxTypes, reportMode);
if (text == CommonNames.valueof) return this.resolveBuiltinValueofType(node, ctxElement, ctxTypes, reportMode);
if (text == CommonNames.returnof) return this.resolveBuiltinReturnTypeType(node, ctxElement, ctxTypes, reportMode);
if (text == CommonNames.notnullable) return this.resolveBuiltinNotNullableType(node, ctxElement, ctxTypes, reportMode);
}

// Resolve normally
Expand Down Expand Up @@ -438,19 +439,9 @@ export class Resolver extends DiagnosticEmitter {
/** How to proceed with eventual diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
var typeArgumentNodes = node.typeArguments;
if (!typeArgumentNodes || typeArgumentNodes.length != 1) {
if (reportMode == ReportMode.REPORT) {
let numTypeArguments = 0;
if (typeArgumentNodes) numTypeArguments = typeArgumentNodes.length;
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
node.range, "1", numTypeArguments.toString()
);
}
return null;
}
var typeArgument = this.resolveType(typeArgumentNodes[0], ctxElement, ctxTypes, reportMode);
const typeArgumentNode = this.ensureOneTypeArgument(node, reportMode);
if (!typeArgumentNode) return null;
var typeArgument = this.resolveType(typeArgumentNode, ctxElement, ctxTypes, reportMode);
if (!typeArgument) return null;
switch (typeArgument.kind) {
case TypeKind.I8:
Expand Down Expand Up @@ -483,26 +474,16 @@ export class Resolver extends DiagnosticEmitter {
/** How to proceed with eventual diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
var typeArgumentNodes = node.typeArguments;
if (!typeArgumentNodes || typeArgumentNodes.length != 1) {
if (reportMode == ReportMode.REPORT) {
let numTypeArguments = 0;
if (typeArgumentNodes) numTypeArguments = typeArgumentNodes.length;
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
node.range, "1", numTypeArguments.toString()
);
}
return null;
}
var typeArgument = this.resolveType(typeArgumentNodes[0], ctxElement, ctxTypes, reportMode);
const typeArgumentNode = this.ensureOneTypeArgument(node, reportMode);
if (!typeArgumentNode) return null;
var typeArgument = this.resolveType(typeArgumentNode, ctxElement, ctxTypes, reportMode);
if (!typeArgument) return null;
var classReference = typeArgument.classReference;
if (!classReference) {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
typeArgumentNodes[0].range, typeArgument.toString()
typeArgumentNode.range, typeArgument.toString()
);
}
return null;
Expand All @@ -520,7 +501,7 @@ export class Resolver extends DiagnosticEmitter {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
typeArgumentNodes[0].range, typeArgument.toString()
typeArgumentNode.range, typeArgument.toString()
);
}
return null;
Expand All @@ -536,19 +517,9 @@ export class Resolver extends DiagnosticEmitter {
/** How to proceed with eventual diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
var typeArgumentNodes = node.typeArguments;
if (!typeArgumentNodes || typeArgumentNodes.length != 1) {
let numTypeArguments = 0;
if (typeArgumentNodes) numTypeArguments = typeArgumentNodes.length;
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
node.range, "1", numTypeArguments.toString()
);
}
return null;
}
var typeArgument = this.resolveType(typeArgumentNodes[0], ctxElement, ctxTypes, reportMode);
const typeArgumentNode = this.ensureOneTypeArgument(node, reportMode);
if (!typeArgumentNode) return null;
var typeArgument = this.resolveType(typeArgumentNode, ctxElement, ctxTypes, reportMode);
if (!typeArgument) return null;
var classReference = typeArgument.getClassOrWrapper(this.program);
if (classReference) {
Expand All @@ -558,7 +529,7 @@ export class Resolver extends DiagnosticEmitter {
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0,
typeArgumentNodes[0].range, typeArgument.toString()
typeArgumentNode.range, typeArgument.toString()
);
}
return null;
Expand All @@ -574,31 +545,39 @@ export class Resolver extends DiagnosticEmitter {
/** How to proceed with eventualy diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
var typeArgumentNodes = node.typeArguments;
if (!typeArgumentNodes || typeArgumentNodes.length != 1) {
if (reportMode == ReportMode.REPORT) {
let numTypeArguments = 0;
if (typeArgumentNodes) numTypeArguments = typeArgumentNodes.length;
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
node.range, "1", numTypeArguments.toString()
);
}
return null;
}
var typeArgument = this.resolveType(typeArgumentNodes[0], ctxElement, ctxTypes, reportMode);
const typeArgumentNode = this.ensureOneTypeArgument(node, reportMode);
if (!typeArgumentNode) return null;
var typeArgument = this.resolveType(typeArgumentNode, ctxElement, ctxTypes, reportMode);
if (!typeArgument) return null;
var signatureReference = typeArgument.getSignature();
if (signatureReference) return signatureReference.returnType;
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Type_0_has_no_call_signatures,
typeArgumentNodes[0].range, typeArgument.toString()
typeArgumentNode.range, typeArgument.toString()
);
}
return null;
}

private resolveBuiltinNotNullableType(
/** The type to resolve. */
node: NamedTypeNode,
/** Contextual element. */
ctxElement: Element,
/** Contextual types, i.e. `T`. */
ctxTypes: Map<string,Type> | null = null,
/** How to proceed with eventual diagnostics. */
reportMode: ReportMode = ReportMode.REPORT
): Type | null {
const typeArgumentNode = this.ensureOneTypeArgument(node, reportMode);
if (!typeArgumentNode) return null;
var typeArgument = this.resolveType(typeArgumentNode, ctxElement, ctxTypes, reportMode);
if (!typeArgument) return null;
if (!typeArgument.isNullableReference) return typeArgument;
return typeArgument.nonNullableType;
}

/** Resolves a type name to the program element it refers to. */
resolveTypeName(
/** The type name to resolve. */
Expand Down Expand Up @@ -3362,4 +3341,24 @@ export class Resolver extends DiagnosticEmitter {
}
return instance;
}

private ensureOneTypeArgument(
/** The type to resolve. */
node: NamedTypeNode,
/** How to proceed with eventual diagnostics. */
reportMode: ReportMode = ReportMode.REPORT): TypeNode | null {
willemneal marked this conversation as resolved.
Show resolved Hide resolved
var typeArgumentNodes = node.typeArguments;
if (!typeArgumentNodes || typeArgumentNodes.length != 1) {
let numTypeArguments = 0;
if (typeArgumentNodes) numTypeArguments = typeArgumentNodes.length;
willemneal marked this conversation as resolved.
Show resolved Hide resolved
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Expected_0_type_arguments_but_got_1,
node.range, "1", numTypeArguments.toString()
);
}
return null;
}
return typeArgumentNodes[0];
}
}
2 changes: 2 additions & 0 deletions std/assembly/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,8 @@ declare type valueof<T extends unknown[]> = T[0];
declare type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
/** A special type evaluated to the return type of T if T is a callable function. */
declare type returnof<T extends (...args: any) => any> = ReturnType<T>;
/** A special type that excludes null and undefined from T. */
declare type NonNullable<T> = T extends null | undefined ? never : T;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, how does this work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, toyed around with it a little and it works, I just don't understand how

Copy link
Contributor Author

@willemneal willemneal Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a conditional type. NonNullable<null> becomes the never type which would cause it to fail to typecheck. We don't have this issue because it's not possible to type something as null, see here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that this somehow decomposes string | null (which extends null I guess?) to string and null, with the null turned into never, so that the outcome would be string | never with the never being dropped or something.


/** Pseudo-class representing the backing class of integer types. */
declare class _Integer {
Expand Down