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

Improve error messages for potentially missing 'await' #32239

Merged
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
166 changes: 125 additions & 41 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2693,6 +2693,10 @@
"category": "Error",
"code": 2772
},
"Did you forget to use 'await'?": {
"category": "Error",
"code": 2773
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
104 changes: 104 additions & 0 deletions tests/baselines/reference/operationsAvailableOnPromisedType.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
tests/cases/compiler/operationsAvailableOnPromisedType.ts(11,9): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(12,5): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(13,5): error TS2365: Operator '+' cannot be applied to types 'number' and 'Promise<number>'.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(14,5): error TS2365: Operator '>' cannot be applied to types 'number' and 'Promise<number>'.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(15,5): error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(16,7): error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(17,5): error TS2367: This condition will always return 'false' since the types 'number' and 'Promise<number>' have no overlap.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(18,9): error TS2461: Type 'Promise<string[]>' is not an array type.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(19,21): error TS2495: Type 'Promise<string[]>' is not an array type or a string type.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(20,12): error TS2345: Argument of type 'Promise<number>' is not assignable to parameter of type 'number'.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(21,11): error TS2570: Property 'prop' does not exist on type 'Promise<{ prop: string; }>'. Did you forget to use 'await'?
tests/cases/compiler/operationsAvailableOnPromisedType.ts(23,27): error TS2495: Type 'Promise<string[]>' is not an array type or a string type.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(24,5): error TS2349: This expression is not callable.
Type 'Promise<() => void>' has no call signatures.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(25,5): error TS2349: This expression is not callable.
Not all constituents of type 'Promise<() => void> | (() => void)' are callable.
Type 'Promise<() => void>' has no call signatures.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(26,9): error TS2351: This expression is not constructable.
Type 'Promise<new () => any>' has no construct signatures.
tests/cases/compiler/operationsAvailableOnPromisedType.ts(27,5): error TS2349: This expression is not callable.
Type 'Promise<number>' has no call signatures.


==== tests/cases/compiler/operationsAvailableOnPromisedType.ts (16 errors) ====
async function fn(
a: number,
b: Promise<number>,
c: Promise<string[]>,
d: Promise<{ prop: string }>,
e: Promise<() => void>,
f: Promise<() => void> | (() => void),
g: Promise<{ new(): any }>
) {
// All errors
a | b;
~
!!! error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:11:9: Did you forget to use 'await'?
b | a;
~
!!! error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:12:5: Did you forget to use 'await'?
a + b;
~~~~~
!!! error TS2365: Operator '+' cannot be applied to types 'number' and 'Promise<number>'.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:13:5: Did you forget to use 'await'?
a > b;
~~~~~
!!! error TS2365: Operator '>' cannot be applied to types 'number' and 'Promise<number>'.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:14:5: Did you forget to use 'await'?
b++;
~
!!! error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:15:5: Did you forget to use 'await'?
--b;
~
!!! error TS2356: An arithmetic operand must be of type 'any', 'number', 'bigint' or an enum type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:16:7: Did you forget to use 'await'?
a === b;
~~~~~~~
!!! error TS2367: This condition will always return 'false' since the types 'number' and 'Promise<number>' have no overlap.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:17:5: Did you forget to use 'await'?
[...c];
~
!!! error TS2461: Type 'Promise<string[]>' is not an array type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:18:9: Did you forget to use 'await'?
for (const s of c) {
~
!!! error TS2495: Type 'Promise<string[]>' is not an array type or a string type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:19:21: Did you forget to use 'await'?
fn(b, b, c, d, e, f, g);
~
!!! error TS2345: Argument of type 'Promise<number>' is not assignable to parameter of type 'number'.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:20:12: Did you forget to use 'await'?
d.prop;
~~~~
!!! error TS2570: Property 'prop' does not exist on type 'Promise<{ prop: string; }>'. Did you forget to use 'await'?
}
for await (const s of c) {}
~
!!! error TS2495: Type 'Promise<string[]>' is not an array type or a string type.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:23:27: Did you forget to use 'await'?
e();
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Promise<() => void>' has no call signatures.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:24:5: Did you forget to use 'await'?
f();
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Not all constituents of type 'Promise<() => void> | (() => void)' are callable.
!!! error TS2349: Type 'Promise<() => void>' has no call signatures.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:25:5: Did you forget to use 'await'?
new g();
~
!!! error TS2351: This expression is not constructable.
!!! error TS2351: Type 'Promise<new () => any>' has no construct signatures.
!!! related TS2773 tests/cases/compiler/operationsAvailableOnPromisedType.ts:26:9: Did you forget to use 'await'?
b();
~
!!! error TS2349: This expression is not callable.
!!! error TS2349: Type 'Promise<number>' has no call signatures.
}

141 changes: 141 additions & 0 deletions tests/baselines/reference/operationsAvailableOnPromisedType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//// [operationsAvailableOnPromisedType.ts]
async function fn(
a: number,
b: Promise<number>,
c: Promise<string[]>,
d: Promise<{ prop: string }>,
e: Promise<() => void>,
f: Promise<() => void> | (() => void),
g: Promise<{ new(): any }>
) {
// All errors
a | b;
b | a;
a + b;
a > b;
b++;
--b;
a === b;
[...c];
for (const s of c) {
fn(b, b, c, d, e, f, g);
d.prop;
}
for await (const s of c) {}
e();
f();
new g();
b();
}


//// [operationsAvailableOnPromisedType.js]
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
function fn(a, b, c, d, e, f, g) {
var c_1, c_1_1;
var e_1, _a;
return __awaiter(this, void 0, void 0, function () {
var _i, c_2, s, s, e_1_1;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
// All errors
a | b;
b | a;
a + b;
a > b;
b++;
--b;
a === b;
__spreadArrays(c);
for (_i = 0, c_2 = c; _i < c_2.length; _i++) {
s = c_2[_i];
fn(b, b, c, d, e, f, g);
d.prop;
}
_b.label = 1;
case 1:
_b.trys.push([1, 6, 7, 12]);
c_1 = __asyncValues(c);
_b.label = 2;
case 2: return [4 /*yield*/, c_1.next()];
case 3:
if (!(c_1_1 = _b.sent(), !c_1_1.done)) return [3 /*break*/, 5];
s = c_1_1.value;
_b.label = 4;
case 4: return [3 /*break*/, 2];
case 5: return [3 /*break*/, 12];
case 6:
e_1_1 = _b.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 12];
case 7:
_b.trys.push([7, , 10, 11]);
if (!(c_1_1 && !c_1_1.done && (_a = c_1["return"]))) return [3 /*break*/, 9];
return [4 /*yield*/, _a.call(c_1)];
case 8:
_b.sent();
_b.label = 9;
case 9: return [3 /*break*/, 11];
case 10:
if (e_1) throw e_1.error;
return [7 /*endfinally*/];
case 11: return [7 /*endfinally*/];
case 12:
e();
f();
new g();
b();
return [2 /*return*/];
}
});
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
=== tests/cases/compiler/operationsAvailableOnPromisedType.ts ===
async function fn(
>fn : Symbol(fn, Decl(operationsAvailableOnPromisedType.ts, 0, 0))

a: number,
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))

b: Promise<number>,
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))

c: Promise<string[]>,
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))

d: Promise<{ prop: string }>,
>d : Symbol(d, Decl(operationsAvailableOnPromisedType.ts, 3, 25))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))
>prop : Symbol(prop, Decl(operationsAvailableOnPromisedType.ts, 4, 16))

e: Promise<() => void>,
>e : Symbol(e, Decl(operationsAvailableOnPromisedType.ts, 4, 33))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))

f: Promise<() => void> | (() => void),
>f : Symbol(f, Decl(operationsAvailableOnPromisedType.ts, 5, 27))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))

g: Promise<{ new(): any }>
>g : Symbol(g, Decl(operationsAvailableOnPromisedType.ts, 6, 42))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --))

) {
// All errors
a | b;
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))

b | a;
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))

a + b;
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))

a > b;
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))

b++;
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))

--b;
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))

a === b;
>a : Symbol(a, Decl(operationsAvailableOnPromisedType.ts, 0, 18))
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))

[...c];
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))

for (const s of c) {
>s : Symbol(s, Decl(operationsAvailableOnPromisedType.ts, 18, 14))
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))

fn(b, b, c, d, e, f, g);
>fn : Symbol(fn, Decl(operationsAvailableOnPromisedType.ts, 0, 0))
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))
>d : Symbol(d, Decl(operationsAvailableOnPromisedType.ts, 3, 25))
>e : Symbol(e, Decl(operationsAvailableOnPromisedType.ts, 4, 33))
>f : Symbol(f, Decl(operationsAvailableOnPromisedType.ts, 5, 27))
>g : Symbol(g, Decl(operationsAvailableOnPromisedType.ts, 6, 42))

d.prop;
>d : Symbol(d, Decl(operationsAvailableOnPromisedType.ts, 3, 25))
}
for await (const s of c) {}
>s : Symbol(s, Decl(operationsAvailableOnPromisedType.ts, 22, 20))
>c : Symbol(c, Decl(operationsAvailableOnPromisedType.ts, 2, 23))

e();
>e : Symbol(e, Decl(operationsAvailableOnPromisedType.ts, 4, 33))

f();
>f : Symbol(f, Decl(operationsAvailableOnPromisedType.ts, 5, 27))

new g();
>g : Symbol(g, Decl(operationsAvailableOnPromisedType.ts, 6, 42))

b();
>b : Symbol(b, Decl(operationsAvailableOnPromisedType.ts, 1, 14))
}

Loading