Skip to content

Fall back to default type when inferring generic calls #1759

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

Merged
merged 5 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ export namespace CommonNames {
export const I31ref = "I31ref";
export const Dataref = "Dataref";
export const String = "String";
export const Object = "Object";
export const Array = "Array";
export const StaticArray = "StaticArray";
export const Set = "Set";
Expand Down
8 changes: 8 additions & 0 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,14 @@ export class Program extends DiagnosticEmitter {
}
private _stringInstance: Class | null = null;

/** Gets the standard `Object` instance. */
get objectInstance(): Class {
var cached = this._objectInstance;
if (!cached) this._objectInstance = cached = this.requireClass(CommonNames.Object);
return cached;
}
private _objectInstance: Class | null = null;

/** Gets the standard `TemplateStringsArray` instance. */
get templateStringsArrayInstance(): Class {
var cached = this._templateStringsArrayInstance;
Expand Down
30 changes: 25 additions & 5 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,13 +770,21 @@ export class Resolver extends DiagnosticEmitter {
// apply concrete types to the generic function signature
let resolvedTypeArguments = new Array<Type>(numTypeParameters);
for (let i = 0; i < numTypeParameters; ++i) {
let name = typeParameterNodes[i].name.text;
let typeParameterNode = typeParameterNodes[i];
let name = typeParameterNode.name.text;
if (contextualTypeArguments.has(name)) {
let inferredType = assert(contextualTypeArguments.get(name));
if (inferredType != Type.auto) {
resolvedTypeArguments[i] = inferredType;
continue;
}
let defaultType = typeParameterNode.defaultType;
if (defaultType) {
let resolvedDefaultType = this.resolveType(defaultType, ctxFlow.actualFunction, contextualTypeArguments, reportMode);
if (!resolvedDefaultType) return null;
resolvedTypeArguments[i] = resolvedDefaultType;
continue;
}
}
// unused template, e.g. `function test<T>(): void {...}` called as `test()`
// invalid because the type is effectively unknown inside the function body
Expand Down Expand Up @@ -2249,10 +2257,12 @@ export class Resolver extends DiagnosticEmitter {
if (numNullLiterals == length) { // all nulls infers as usize
elementType = this.program.options.usizeType;
} else {
this.error(
DiagnosticCode.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly,
node.range, "T"
);
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly,
node.range, "T"
);
}
return null;
}
}
Expand All @@ -2264,6 +2274,16 @@ export class Resolver extends DiagnosticEmitter {
}
return assert(this.resolveClass(this.program.arrayPrototype, [ elementType ]));
}
case LiteralKind.OBJECT: {
if (ctxType.isClass) return ctxType.classReference;
if (reportMode == ReportMode.REPORT) {
this.error(
DiagnosticCode.Expression_cannot_be_represented_by_a_type,
node.range
);
}
return null;
}
}
assert(false);
return null;
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy