[red-knot] Fix assertion for invalid match pattern (#14306)

## Summary

Fixes a failing debug assertion that triggers for the following code:
```py
match some_int:
    case x:=2:
        pass
```

closes #14305

## Test Plan

Added problematic code example to corpus.
This commit is contained in:
David Peter 2024-11-13 10:07:29 +01:00 committed by GitHub
parent 5c548dcc04
commit 3e36a7ab81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 9 deletions

View file

@ -4084,16 +4084,19 @@ impl<'db> TypeInferenceBuilder<'db> {
// https://typing.readthedocs.io/en/latest/spec/annotations.html#grammar-token-expression-grammar-type_expression
match expression {
ast::Expr::Name(name) => {
debug_assert_eq!(name.ctx, ast::ExprContext::Load);
self.infer_name_expression(name).to_instance(self.db)
}
ast::Expr::Name(name) => match name.ctx {
ast::ExprContext::Load => self.infer_name_expression(name).to_instance(self.db),
ast::ExprContext::Invalid => Type::Unknown,
ast::ExprContext::Store | ast::ExprContext::Del => Type::Todo,
},
ast::Expr::Attribute(attribute_expression) => {
debug_assert_eq!(attribute_expression.ctx, ast::ExprContext::Load);
self.infer_attribute_expression(attribute_expression)
.to_instance(self.db)
}
ast::Expr::Attribute(attribute_expression) => match attribute_expression.ctx {
ast::ExprContext::Load => self
.infer_attribute_expression(attribute_expression)
.to_instance(self.db),
ast::ExprContext::Invalid => Type::Unknown,
ast::ExprContext::Store | ast::ExprContext::Del => Type::Todo,
},
ast::Expr::NoneLiteral(_literal) => Type::none(self.db),

View file

@ -0,0 +1,3 @@
match some_int:
case x:=2:
pass