Skip to content

Fix circularity errors in intra-binding-pattern references #59183

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 9 commits into from
Jul 15, 2024
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
165 changes: 84 additions & 81 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2680,10 +2680,6 @@
"category": "Error",
"code": 2524
},
"Initializer provides no value for this binding element and the binding element has no default value.": {
"category": "Error",
"code": 2525
},
"A 'this' type is available only in a non-static member of a class or interface.": {
"category": "Error",
"code": 2526
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6693,7 +6693,7 @@ export const enum AccessFlags {
NoIndexSignatures = 1 << 1,
Writing = 1 << 2,
CacheSymbol = 1 << 3,
NoTupleBoundsCheck = 1 << 4,
AllowMissing = 1 << 4,
ExpressionPosition = 1 << 5,
ReportDeprecated = 1 << 6,
SuppressNoImplicitAnyError = 1 << 7,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ function Test({ b = '' } = {}) {}
> : ^^^^^^
>'' : ""
> : ^^
>{} : { b?: string; }
> : ^^^^^^^^^^^^^^^
>{} : {}
> : ^^

Test(({ b = '5' } = {}));
>Test(({ b = '5' } = {})) : void
> : ^^^^
>Test : ({ b }?: { b?: string; }) => void
> : ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>({ b = '5' } = {}) : { b?: any; }
> : ^^^^^^^^^^^^
>{ b = '5' } = {} : { b?: any; }
> : ^^^^^^^^^^^^
>({ b = '5' } = {}) : {}
> : ^^
>{ b = '5' } = {} : {}
> : ^^
>{ b = '5' } : { b?: any; }
> : ^^^^^^^^^^^^
>b : any
> : ^^^
>'5' : "5"
> : ^^^
>{} : { b?: any; }
> : ^^^^^^^^^^^^
>{} : {}
> : ^^

Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
checkDestructuringShorthandAssigment2.ts(4,7): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
checkDestructuringShorthandAssigment2.ts(4,27): error TS2353: Object literal may only specify known properties, and '[k]' does not exist in type '{ x: any; }'.


==== checkDestructuringShorthandAssigment2.ts (2 errors) ====
==== checkDestructuringShorthandAssigment2.ts (1 errors) ====
// GH #38175 -- should not crash while checking

let o: any, k: any;
let { x } = { x: 1, ...o, [k]: 1 };
~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
~~~
!!! error TS2353: Object literal may only specify known properties, and '[k]' does not exist in type '{ x: any; }'.

16 changes: 8 additions & 8 deletions tests/baselines/reference/circularReferenceInReturnType2.types
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ type Something = { foo: number };

// inference fails here, but ideally should not
const A = object<Something>()({
>A : ObjectType<Something>
> : ^^^^^^^^^^^^^^^^^^^^^
>A : any
Copy link
Contributor

Choose a reason for hiding this comment

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

The previous type was better here but i guess maybe that relied on broken assumptions from the PR that got reverted

Copy link
Member Author

Choose a reason for hiding this comment

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

Right, when we report circularity errors we also assign the error type, but the reverted PR sometimes didn't do that.

> : ^^^
>object<Something>()({ name: "A", fields: () => ({ a: field({ type: A, resolve() { return { foo: 100, }; }, }), }),}) : ObjectType<Something>
> : ^^^^^^^^^^^^^^^^^^^^^
>object<Something>() : <Fields extends { [Key in keyof Fields]: Field<Something, Key & string>; }>(config: { name: string; fields: Fields | (() => Fields); }) => ObjectType<Something>
Expand Down Expand Up @@ -138,14 +138,14 @@ const A = object<Something>()({
> : ^^^^^^^^^^^^^^^^^^^^^
>field : <Source, Type extends ObjectType<any>, Key extends string>(field: FieldFuncArgs<Source, Type>) => Field<Source, Key>
> : ^ ^^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^^^^
>{ type: A, resolve() { return { foo: 100, }; }, } : { type: ObjectType<Something>; resolve(): { foo: number; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ type: A, resolve() { return { foo: 100, }; }, } : { type: any; resolve(): { foo: number; }; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

type: A,
>type : ObjectType<Something>
> : ^^^^^^^^^^^^^^^^^^^^^
>A : ObjectType<Something>
> : ^^^^^^^^^^^^^^^^^^^^^
>type : any
> : ^^^
>A : any
> : ^^^

resolve() {
>resolve : () => { foo: number; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const fInferred = ({ a = 0 } = {}) => a;
> : ^^^^^^
>0 : 0
> : ^
>{} : { a?: number | undefined; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{} : {}
> : ^^
>a : number
> : ^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ try {
> : ^^^^^^
>1 : 1
> : ^
>{ } : { e?: number | undefined; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ } : {}
> : ^^

} catch {
console.error("error");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ declarationsAndAssignments.ts(63,13): error TS2493: Tuple type '[number]' of len
declarationsAndAssignments.ts(63,16): error TS2493: Tuple type '[number]' of length '1' has no element at index '2'.
declarationsAndAssignments.ts(67,9): error TS2461: Type '{}' is not an array type.
declarationsAndAssignments.ts(68,9): error TS2461: Type '{ 0: number; 1: number; }' is not an array type.
declarationsAndAssignments.ts(73,11): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
declarationsAndAssignments.ts(73,14): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
declarationsAndAssignments.ts(73,11): error TS2339: Property 'a' does not exist on type '{}'.
declarationsAndAssignments.ts(73,14): error TS2339: Property 'b' does not exist on type '{}'.
declarationsAndAssignments.ts(74,11): error TS2339: Property 'a' does not exist on type 'undefined[]'.
declarationsAndAssignments.ts(74,14): error TS2339: Property 'b' does not exist on type 'undefined[]'.
declarationsAndAssignments.ts(106,17): error TS2741: Property 'x' is missing in type '{ y: false; }' but required in type '{ x: any; y?: boolean; }'.
Expand Down Expand Up @@ -122,9 +122,9 @@ declarationsAndAssignments.ts(138,9): error TS2322: Type 'number' is not assigna
function f10() {
var { a, b } = {}; // Error
~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
!!! error TS2339: Property 'a' does not exist on type '{}'.
~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
!!! error TS2339: Property 'b' does not exist on type '{}'.
var { a, b } = []; // Error
~
!!! error TS2339: Property 'a' does not exist on type 'undefined[]'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/declarationsAndAssignments.types
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ function f10() {
> : ^^^
>b : any
> : ^^^
>{} : { a: any; b: any; }
> : ^^^^^^^^^^^^^^^^^^^
>{} : {}
> : ^^

var { a, b } = []; // Error
>a : any
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
destructuredLateBoundNameHasCorrectTypes.ts(11,21): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
destructuredLateBoundNameHasCorrectTypes.ts(11,8): error TS2339: Property 'prop2' does not exist on type '{ prop: string; }'.
destructuredLateBoundNameHasCorrectTypes.ts(11,37): error TS2353: Object literal may only specify known properties, and 'prop' does not exist in type '{ prop2: any; }'.


Expand All @@ -14,8 +14,8 @@ destructuredLateBoundNameHasCorrectTypes.ts(11,37): error TS2353: Object literal
const notPresent = "prop2";

let { [notPresent]: computed2 } = { prop: "b" };
~~~~~~~~~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
~~~~~~~~~~
!!! error TS2339: Property 'prop2' does not exist on type '{ prop: string; }'.
~~~~
!!! error TS2353: Object literal may only specify known properties, and 'prop' does not exist in type '{ prop2: any; }'.

Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ let { [notPresent]: computed2 } = { prop: "b" };
> : ^^^^^^^
>computed2 : any
> : ^^^
>{ prop: "b" } : { prop: string; prop2: any; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ prop: "b" } : { prop: string; }
> : ^^^^^^^^^^^^^^^^^
>prop : string
> : ^^^^^^
>"b" : "b"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ const [c, d = c, e = e] = [1]; // error for e = e
> : ^

const [f, g = f, h = i, i = f] = [1]; // error for h = i
>f : any
> : ^^^
>g : any
> : ^^^
>f : any
> : ^^^
>h : any
> : ^^^
>i : any
> : ^^^
>i : any
> : ^^^
>f : any
> : ^^^
>f : number
> : ^^^^^^
>g : number
> : ^^^^^^
>f : number
> : ^^^^^^
>h : number
> : ^^^^^^
>i : number
> : ^^^^^^
>i : number
> : ^^^^^^
>f : number
> : ^^^^^^
>[1] : [number]
> : ^^^^^^^^
>1 : 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
destructuringFromUnionSpread.ts(5,9): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
destructuringFromUnionSpread.ts(5,9): error TS2339: Property 'a' does not exist on type '{ a: string; } | { b: number; }'.


==== destructuringFromUnionSpread.ts (1 errors) ====
Expand All @@ -8,5 +8,5 @@ destructuringFromUnionSpread.ts(5,9): error TS2525: Initializer provides no valu
declare const x: A | B;
const { a } = { ...x } // error
~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
!!! error TS2339: Property 'a' does not exist on type '{ a: string; } | { b: number; }'.

4 changes: 2 additions & 2 deletions tests/baselines/reference/destructuringFromUnionSpread.types
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ declare const x: A | B;
const { a } = { ...x } // error
>a : any
> : ^^^
>{ ...x } : { a: any; } | { a: any; b: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^
>{ ...x } : { a: string; } | { b: number; }
> : ^^^^^ ^^^^^^^^^^^ ^^^
>x : A | B
> : ^^^^^

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ destructuringObjectBindingPatternAndAssignment3.ts(2,7): error TS1005: ',' expec
destructuringObjectBindingPatternAndAssignment3.ts(3,5): error TS2322: Type '{ i: number; }' is not assignable to type 'string | number'.
destructuringObjectBindingPatternAndAssignment3.ts(3,6): error TS2339: Property 'i' does not exist on type 'string | number'.
destructuringObjectBindingPatternAndAssignment3.ts(4,6): error TS2339: Property 'i1' does not exist on type 'string | number | {}'.
destructuringObjectBindingPatternAndAssignment3.ts(5,12): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
destructuringObjectBindingPatternAndAssignment3.ts(5,21): error TS2353: Object literal may only specify known properties, and 'f212' does not exist in type '{ f21: any; }'.
destructuringObjectBindingPatternAndAssignment3.ts(6,7): error TS1005: ':' expected.
destructuringObjectBindingPatternAndAssignment3.ts(6,15): error TS1005: ':' expected.
destructuringObjectBindingPatternAndAssignment3.ts(7,12): error TS1005: ':' expected.


==== destructuringObjectBindingPatternAndAssignment3.ts (9 errors) ====
==== destructuringObjectBindingPatternAndAssignment3.ts (8 errors) ====
// Error
var {h?} = { h?: 1 };
~
Expand All @@ -23,8 +22,6 @@ destructuringObjectBindingPatternAndAssignment3.ts(7,12): error TS1005: ':' expe
~~
!!! error TS2339: Property 'i1' does not exist on type 'string | number | {}'.
var { f2: {f21} = { f212: "string" } }: any = undefined;
~~~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
~~~~
!!! error TS2353: Object literal may only specify known properties, and 'f212' does not exist in type '{ f21: any; }'.
var {1} = { 1 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var { f2: {f21} = { f212: "string" } }: any = undefined;
> : ^^^
>f21 : any
> : ^^^
>{ f212: "string" } : { f212: string; f21: any; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ f212: "string" } : { f212: string; }
> : ^^^^^^^^^^^^^^^^^
>f212 : string
> : ^^^^^^
>"string" : "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function f1() {
> : ^^^^^^
>a1 : number
> : ^^^^^^
>{ a1: 1 } : { a1: number; b1?: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ a1: 1 } : { a1: number; }
> : ^^^^^^^^^^^^^^^
>a1 : number
> : ^^^^^^
>1 : 1
Expand All @@ -31,8 +31,8 @@ function f1() {
> : ^
>a2 : number
> : ^^^^^^
>{ a2: 1 } : { a2: number; b2?: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ a2: 1 } : { a2: number; }
> : ^^^^^^^^^^^^^^^
>a2 : number
> : ^^^^^^
>1 : 1
Expand All @@ -51,8 +51,8 @@ function f2() {
> : ^^^^^^
>a1 : string
> : ^^^^^^
>{ a1: 'hi' } : { a1: string; b1?: string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ a1: 'hi' } : { a1: string; }
> : ^^^^^^^^^^^^^^^
>a1 : string
> : ^^^^^^
>'hi' : "hi"
Expand All @@ -69,8 +69,8 @@ function f2() {
> : ^^^^^^
>'!' : "!"
> : ^^^
>{ a2: 'hi' } : { a2: string; b2?: string; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ a2: 'hi' } : { a2: string; }
> : ^^^^^^^^^^^^^^^
>a2 : string
> : ^^^^^^
>'hi' : "hi"
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/destructuringSpread.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
destructuringSpread.ts(16,21): error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
destructuringSpread.ts(16,21): error TS2339: Property 'g' does not exist on type '{ f: number; e: number; d: number; c: number; }'.


==== destructuringSpread.ts (1 errors) ====
Expand All @@ -19,7 +19,7 @@ destructuringSpread.ts(16,21): error TS2525: Initializer provides no value for t

const { c, d, e, f, g } = {
~
!!! error TS2525: Initializer provides no value for this binding element and the binding element has no default value.
!!! error TS2339: Property 'g' does not exist on type '{ f: number; e: number; d: number; c: number; }'.
...{
...{
...{
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/destructuringSpread.types
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ const { c, d, e, f, g } = {
> : ^^^^^^
>g : any
> : ^^^
>{ ...{ ...{ ...{ c: 0, }, d: 0 }, e: 0 }, f: 0} : { f: number; g: any; e: number; d: number; c: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ ...{ ...{ ...{ c: 0, }, d: 0 }, e: 0 }, f: 0} : { f: number; e: number; d: number; c: number; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

...{
>{ ...{ ...{ c: 0, }, d: 0 }, e: 0 } : { e: number; d: number; c: number; }
Expand Down
Loading