Skip to content

Commit

Permalink
fix: Fix a panic due to the recursion of normalize and assign (#1031
Browse files Browse the repository at this point in the history
)

**Description:**

```ts
// The following are errors because of circular references
var c: typeof c;
var c: any;
var d: typeof e;
var d: any;
var e: typeof d;
var e: any;

interface Foo<T> {}
var f: Array<typeof f>;
var f: any;
var f2: Foo<typeof f2>;
var f2: any;
var f3: Foo<typeof f3>[];
var f3: any;

// None of these declarations should have any errors!
// Truly recursive types
var g: { x: typeof g };
var g: typeof g.x;
var h: () => typeof h;
var h = h();
var i: (x: typeof i) => typeof x;
var i = i(i);
var j: <T extends typeof j>(x: T) => T;
var j = j(j);

// Same as h, i, j with construct signatures
var h2: new () => typeof h2;
var h2 = new h2();
var i2: new (x: typeof i2) => typeof x;
var i2 = new i2(i2);
var j2: new <T extends typeof j2>(x: T) => T;
var j2 = new j2(j2);

// Indexers
var k: { [n: number]: typeof k; [s: string]: typeof k };
var k = k[0];
var k = k[""];

// Hybrid - contains type literals as well as type arguments
// These two are recursive
var hy1: { x: typeof hy1 }[];
var hy1 = hy1[0].x;
var hy2: { x: Array<typeof hy2> };
var hy2 = hy2.x[0];

interface Foo2<T, U> {}

// This one should be an error because the first type argument is not contained inside a type literal
var hy3: Foo2<typeof hy3, { x: typeof hy3 }>;
var hy3: any;
```
  • Loading branch information
sunrabbit123 authored May 19, 2023
1 parent d932c27 commit 7c76ed2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
4 changes: 3 additions & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,9 @@ impl Analyzer<'_, '_> {
if to.type_eq(rhs) {
return Ok(());
}

if to.is_any() {
return Ok(());
}
// debug_assert!(!span.is_dummy(), "\n\t{:?}\n<-\n\t{:?}", to, rhs);
let mut to = self.normalize_for_assign(span, to, opts).context("tried to normalize lhs")?;
to.freeze();
Expand Down
3 changes: 3 additions & 0 deletions crates/stc_ts_file_analyzer/src/analyzer/scope/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,9 @@ impl Analyzer<'_, '_> {

true
});
if u.types.len() == 1 {
return Ok(u.types.first().unwrap().clone());
}
}

Ok(ty)
Expand Down
7 changes: 0 additions & 7 deletions crates/stc_ts_file_analyzer/src/analyzer/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,6 @@ impl Analyzer<'_, '_> {
)));
}

if expanded_ty.is_query() {
unreachable!(
"normalize: resolve_typeof returned a query type: {}",
dump_type_as_string(&expanded_ty)
)
}

return self
.normalize(span, Cow::Owned(expanded_ty), opts)
.context("tried to normalize the type returned from typeof");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"required_errors": {
"TS2502": 7
},
"required_error_lines": {
"TS2502": [
2,
4,
6,
10,
12,
14,
51
]
},
"extra_errors": {
"TS2304": 1
},
"extra_error_lines": {
"TS2304": [
4
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 7,
matched_error: 0,
extra_error: 0,
panic: 1,
extra_error: 1,
panic: 0,
}
4 changes: 2 additions & 2 deletions crates/stc_ts_type_checker/tests/tsc-stats.rust-debug
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 3602,
matched_error: 6433,
extra_error: 773,
panic: 75,
extra_error: 774,
panic: 74,
}

0 comments on commit 7c76ed2

Please sign in to comment.