also looking at this
fix(types): free for-loop and capture scopes after checking them
#939
2 files
+22
-0
| @@ -2012,6 +2012,7 @@ pub const TypeChecker = struct { | ||
| 2012 | 2012 | for (if_let_stmt.then_block.statements) |then_stmt| { |
| 2013 | 2013 | self.checkStatement(then_stmt) catch |err| { |
| 2014 | 2014 | if (err != error.TypeMismatch and err != error.UndefinedVariable) { |
| 2015 | self.env.deinit(); | |
| 2015 | 2016 | self.env = saved_env_ptr.*; |
| 2016 | 2017 | self.allocator.destroy(saved_env_ptr); |
| 2017 | 2018 | return err; |
| @@ -2019,6 +2020,7 @@ pub const TypeChecker = struct { | ||
| 2019 | 2020 | }; |
| 2020 | 2021 | } |
| 2021 | 2022 | |
| 2023 | self.env.deinit(); | |
| 2022 | 2024 | self.env = saved_env_ptr.*; |
| 2023 | 2025 | self.allocator.destroy(saved_env_ptr); |
| 2024 | 2026 | |
| @@ -2087,6 +2089,7 @@ pub const TypeChecker = struct { | ||
| 2087 | 2089 | for (for_stmt.body.statements) |body_stmt| { |
| 2088 | 2090 | self.checkStatement(body_stmt) catch |err| { |
| 2089 | 2091 | if (err != error.TypeMismatch and err != error.UndefinedVariable) { |
| 2092 | self.env.deinit(); | |
| 2090 | 2093 | self.env = saved_env_ptr.*; |
| 2091 | 2094 | self.allocator.destroy(saved_env_ptr); |
| 2092 | 2095 | return err; |
| @@ -2094,6 +2097,7 @@ pub const TypeChecker = struct { | ||
| 2094 | 2097 | }; |
| 2095 | 2098 | } |
| 2096 | 2099 | |
| 2100 | self.env.deinit(); | |
| 2097 | 2101 | self.env = saved_env_ptr.*; |
| 2098 | 2102 | self.allocator.destroy(saved_env_ptr); |
| 2099 | 2103 | }, |
| @@ -141,6 +141,24 @@ test "checker recognizes never as the bottom type" { | ||
| 141 | 141 | )); |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | test "checker frees for-loop and capture scopes" { | |
| 145 | try std.testing.expect(try checkSource( | |
| 146 | \\fn run() { | |
| 147 | \\ for x in [10, 20] { | |
| 148 | \\ let next = x + 1 | |
| 149 | \\ } | |
| 150 | \\} | |
| 151 | )); | |
| 152 | try std.testing.expect(try checkSource( | |
| 153 | \\fn run() { | |
| 154 | \\ let value = 1 | |
| 155 | \\ if (value) |v| { | |
| 156 | \\ let next = v + 1 | |
| 157 | \\ } | |
| 158 | \\} | |
| 159 | )); | |
| 160 | } | |
| 161 | ||
| 144 | 162 | test "checker types panic as a string-taking bottom expression" { |
| 145 | 163 | try std.testing.expect(try checkSource( |
| 146 | 164 | \\fn fail(): string { |