[red-knot] Add definitions and limited type inference for exception handlers (#13267)

This commit is contained in:
Alex Waygood 2024-09-09 07:35:15 -04:00 committed by GitHub
parent 346dbf45b5
commit 1eb3e4057f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 190 additions and 3 deletions

View file

@ -880,6 +880,30 @@ where
self.current_match_case.as_mut().unwrap().index += 1;
}
fn visit_except_handler(&mut self, except_handler: &'ast ast::ExceptHandler) {
let ast::ExceptHandler::ExceptHandler(except_handler) = except_handler;
let ast::ExceptHandlerExceptHandler {
name: symbol_name,
type_: handled_exceptions,
body,
range: _,
} = except_handler;
if let Some(handled_exceptions) = handled_exceptions {
self.visit_expr(handled_exceptions);
}
// If `handled_exceptions` above was `None`, it's something like `except as e:`,
// which is invalid syntax. However, it's still pretty obvious here that the user
// *wanted* `e` to be bound, so we should still create a definition here nonetheless.
if let Some(symbol_name) = symbol_name {
let symbol = self.add_or_update_symbol(symbol_name.id.clone(), SymbolFlags::IS_DEFINED);
self.add_definition(symbol, except_handler);
}
self.visit_body(body);
}
}
#[derive(Copy, Clone, Debug)]