Skip to content

Commit a9ee6ed

Browse files
committed
fix: use try_scoped_use_id
1 parent 83e4a99 commit a9ee6ed

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

crates/red_knot_python_semantic/src/semantic_index/ast_ids.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ impl AstIds {
4343
fn use_id(&self, key: impl Into<ExpressionNodeKey>) -> ScopedUseId {
4444
self.uses_map[&key.into()]
4545
}
46+
47+
fn try_use_id(&self, key: impl Into<ExpressionNodeKey>) -> Option<ScopedUseId> {
48+
self.uses_map.get(&key.into()).copied()
49+
}
4650
}
4751

4852
fn ast_ids<'db>(db: &'db dyn Db, scope: ScopeId) -> &'db AstIds {
@@ -56,27 +60,43 @@ pub struct ScopedUseId;
5660
pub trait HasScopedUseId {
5761
/// Returns the ID that uniquely identifies the use in `scope`.
5862
fn scoped_use_id(&self, db: &dyn Db, scope: ScopeId) -> ScopedUseId;
63+
fn try_scoped_use_id(&self, db: &dyn Db, scope: ScopeId) -> Option<ScopedUseId>;
5964
}
6065

6166
impl HasScopedUseId for ast::Identifier {
6267
fn scoped_use_id(&self, db: &dyn Db, scope: ScopeId) -> ScopedUseId {
6368
let ast_ids = ast_ids(db, scope);
6469
ast_ids.use_id(self)
6570
}
71+
72+
fn try_scoped_use_id(&self, db: &dyn Db, scope: ScopeId) -> Option<ScopedUseId> {
73+
let ast_ids = ast_ids(db, scope);
74+
ast_ids.try_use_id(self)
75+
}
6676
}
6777

6878
impl HasScopedUseId for ast::ExprName {
6979
fn scoped_use_id(&self, db: &dyn Db, scope: ScopeId) -> ScopedUseId {
7080
let expression_ref = ExprRef::from(self);
7181
expression_ref.scoped_use_id(db, scope)
7282
}
83+
84+
fn try_scoped_use_id(&self, db: &dyn Db, scope: ScopeId) -> Option<ScopedUseId> {
85+
let expression_ref = ExprRef::from(self);
86+
expression_ref.try_scoped_use_id(db, scope)
87+
}
7388
}
7489

7590
impl HasScopedUseId for ast::ExprRef<'_> {
7691
fn scoped_use_id(&self, db: &dyn Db, scope: ScopeId) -> ScopedUseId {
7792
let ast_ids = ast_ids(db, scope);
7893
ast_ids.use_id(*self)
7994
}
95+
96+
fn try_scoped_use_id(&self, db: &dyn Db, scope: ScopeId) -> Option<ScopedUseId> {
97+
let ast_ids = ast_ids(db, scope);
98+
ast_ids.try_use_id(*self)
99+
}
80100
}
81101

82102
/// Uniquely identifies an [`ast::Expr`] in a [`crate::semantic_index::symbol::FileScopeId`].

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4888,13 +4888,16 @@ impl<'db> TypeInferenceBuilder<'db> {
48884888
let db = self.db();
48894889
let scope = self.scope();
48904890

4891-
let use_id = name.scoped_use_id(db, scope);
4892-
let use_def = use_def_map(db, scope);
4893-
let mut bindings = use_def.bindings_at_use(use_id);
4894-
let binding = bindings.next().unwrap();
4895-
binding
4896-
.narrowing_constraint
4897-
.narrow(db, ty, DefinitionTarget::Expr(target))
4891+
if let Some(use_id) = name.try_scoped_use_id(db, scope) {
4892+
let use_def = use_def_map(db, scope);
4893+
let mut bindings = use_def.bindings_at_use(use_id);
4894+
let binding = bindings.next().unwrap();
4895+
binding
4896+
.narrowing_constraint
4897+
.narrow(db, ty, DefinitionTarget::Expr(target))
4898+
} else {
4899+
ty
4900+
}
48984901
} else {
48994902
ty
49004903
}

0 commit comments

Comments
 (0)