Skip to content

Commit fcdc7bd

Browse files
Remove separate ReferenceContext enum (#4631)
1 parent 86ced35 commit fcdc7bd

File tree

7 files changed

+30
-61
lines changed

7 files changed

+30
-61
lines changed

crates/ruff/src/checkers/ast/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ use ruff_python_semantic::analyze::branch_detection;
2424
use ruff_python_semantic::analyze::typing::{Callable, SubscriptKind};
2525
use ruff_python_semantic::analyze::visibility::ModuleSource;
2626
use ruff_python_semantic::binding::{
27-
Binding, BindingFlags, BindingId, BindingKind, Exceptions, ExecutionContext, Export,
28-
FromImportation, Importation, StarImportation, SubmoduleImportation,
27+
Binding, BindingFlags, BindingId, BindingKind, Exceptions, Export, FromImportation,
28+
Importation, StarImportation, SubmoduleImportation,
2929
};
30+
use ruff_python_semantic::context::ExecutionContext;
3031
use ruff_python_semantic::definition::{ContextualizedDefinition, Module, ModuleKind};
3132
use ruff_python_semantic::model::{ResolvedReference, SemanticModel, SemanticModelFlags};
3233
use ruff_python_semantic::node::NodeId;
33-
use ruff_python_semantic::reference::ReferenceContext;
3434
use ruff_python_semantic::scope::{ClassDef, FunctionDef, Lambda, Scope, ScopeId, ScopeKind};
3535
use ruff_python_stdlib::builtins::{BUILTINS, MAGIC_GLOBALS};
3636
use ruff_python_stdlib::path::is_python_stub_file;
@@ -300,7 +300,7 @@ where
300300
self.semantic_model.add_local_reference(
301301
*binding_id,
302302
stmt.range(),
303-
ReferenceContext::Runtime,
303+
ExecutionContext::Runtime,
304304
);
305305
} else {
306306
// Ensure that every nonlocal has an existing binding from a parent scope.
@@ -4955,7 +4955,7 @@ impl<'a> Checker<'a> {
49554955
self.semantic_model.add_global_reference(
49564956
binding_id,
49574957
range,
4958-
ReferenceContext::Runtime,
4958+
ExecutionContext::Runtime,
49594959
);
49604960
}
49614961
}

crates/ruff/src/rules/flake8_type_checking/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Constant, Expr};
33

44
use ruff_python_ast::call_path::from_qualified_name;
55
use ruff_python_ast::helpers::map_callable;
6-
use ruff_python_semantic::binding::{Binding, BindingKind, ExecutionContext};
6+
use ruff_python_semantic::binding::{Binding, BindingKind};
77
use ruff_python_semantic::model::SemanticModel;
88
use ruff_python_semantic::scope::ScopeKind;
99

@@ -51,7 +51,7 @@ pub(crate) fn is_valid_runtime_import(semantic_model: &SemanticModel, binding: &
5151
| BindingKind::FromImportation(..)
5252
| BindingKind::SubmoduleImportation(..)
5353
) {
54-
matches!(binding.context, ExecutionContext::Runtime)
54+
binding.context.is_runtime()
5555
&& binding.references().any(|reference_id| {
5656
semantic_model
5757
.references

crates/ruff_python_semantic/src/binding.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use ruff_index::{newtype_index, IndexSlice, IndexVec};
77
use ruff_python_ast::helpers;
88
use ruff_python_ast::source_code::Locator;
99

10+
use crate::context::ExecutionContext;
1011
use crate::model::SemanticModel;
1112
use crate::node::NodeId;
1213
use crate::reference::ReferenceId;
@@ -272,9 +273,3 @@ bitflags! {
272273
const IMPORT_ERROR = 0b0000_0100;
273274
}
274275
}
275-
276-
#[derive(Copy, Debug, Clone, is_macro::Is)]
277-
pub enum ExecutionContext {
278-
Runtime,
279-
Typing,
280-
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[derive(Debug, Copy, Clone, is_macro::Is)]
2+
pub enum ExecutionContext {
3+
/// The reference occurs in a runtime context.
4+
Runtime,
5+
/// The reference occurs in a typing-only context.
6+
Typing,
7+
}

crates/ruff_python_semantic/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod analyze;
22
pub mod binding;
3+
pub mod context;
34
pub mod definition;
45
pub mod model;
56
pub mod node;

crates/ruff_python_semantic/src/model.rs

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ use ruff_python_stdlib::path::is_python_stub_file;
1313
use ruff_python_stdlib::typing::TYPING_EXTENSIONS;
1414

1515
use crate::binding::{
16-
Binding, BindingId, BindingKind, Bindings, Exceptions, ExecutionContext, FromImportation,
17-
Importation, SubmoduleImportation,
16+
Binding, BindingId, BindingKind, Bindings, Exceptions, FromImportation, Importation,
17+
SubmoduleImportation,
1818
};
19+
use crate::context::ExecutionContext;
1920
use crate::definition::{Definition, DefinitionId, Definitions, Member, Module};
2021
use crate::node::{NodeId, Nodes};
21-
use crate::reference::{ReferenceContext, References};
22+
use crate::reference::References;
2223
use crate::scope::{Scope, ScopeId, ScopeKind, Scopes};
2324

2425
/// A semantic model for a Python module, to enable querying the module's semantic information.
@@ -126,26 +127,12 @@ impl<'a> SemanticModel<'a> {
126127
if let Some(binding_id) = self.scopes.global().get(symbol).copied() {
127128
// Mark the binding as used.
128129
let context = self.execution_context();
129-
let reference_id = self.references.push(
130-
ScopeId::global(),
131-
range,
132-
match context {
133-
ExecutionContext::Runtime => ReferenceContext::Runtime,
134-
ExecutionContext::Typing => ReferenceContext::Typing,
135-
},
136-
);
130+
let reference_id = self.references.push(ScopeId::global(), range, context);
137131
self.bindings[binding_id].references.push(reference_id);
138132

139133
// Mark any submodule aliases as used.
140134
if let Some(binding_id) = self.resolve_submodule(ScopeId::global(), binding_id) {
141-
let reference_id = self.references.push(
142-
ScopeId::global(),
143-
range,
144-
match context {
145-
ExecutionContext::Runtime => ReferenceContext::Runtime,
146-
ExecutionContext::Typing => ReferenceContext::Typing,
147-
},
148-
);
135+
let reference_id = self.references.push(ScopeId::global(), range, context);
149136
self.bindings[binding_id].references.push(reference_id);
150137
}
151138

@@ -176,26 +163,12 @@ impl<'a> SemanticModel<'a> {
176163
if let Some(binding_id) = scope.get(symbol).copied() {
177164
// Mark the binding as used.
178165
let context = self.execution_context();
179-
let reference_id = self.references.push(
180-
self.scope_id,
181-
range,
182-
match context {
183-
ExecutionContext::Runtime => ReferenceContext::Runtime,
184-
ExecutionContext::Typing => ReferenceContext::Typing,
185-
},
186-
);
166+
let reference_id = self.references.push(self.scope_id, range, context);
187167
self.bindings[binding_id].references.push(reference_id);
188168

189169
// Mark any submodule aliases as used.
190170
if let Some(binding_id) = self.resolve_submodule(scope_id, binding_id) {
191-
let reference_id = self.references.push(
192-
self.scope_id,
193-
range,
194-
match context {
195-
ExecutionContext::Runtime => ReferenceContext::Runtime,
196-
ExecutionContext::Typing => ReferenceContext::Typing,
197-
},
198-
);
171+
let reference_id = self.references.push(self.scope_id, range, context);
199172
self.bindings[binding_id].references.push(reference_id);
200173
}
201174

@@ -585,7 +558,7 @@ impl<'a> SemanticModel<'a> {
585558
&mut self,
586559
binding_id: BindingId,
587560
range: TextRange,
588-
context: ReferenceContext,
561+
context: ExecutionContext,
589562
) {
590563
let reference_id = self.references.push(self.scope_id, range, context);
591564
self.bindings[binding_id].references.push(reference_id);
@@ -596,7 +569,7 @@ impl<'a> SemanticModel<'a> {
596569
&mut self,
597570
binding_id: BindingId,
598571
range: TextRange,
599-
context: ReferenceContext,
572+
context: ExecutionContext,
600573
) {
601574
let reference_id = self.references.push(ScopeId::global(), range, context);
602575
self.bindings[binding_id].references.push(reference_id);

crates/ruff_python_semantic/src/reference.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ruff_text_size::TextRange;
22

33
use ruff_index::{newtype_index, IndexVec};
44

5+
use crate::context::ExecutionContext;
56
use crate::scope::ScopeId;
67

78
#[derive(Debug, Clone)]
@@ -11,7 +12,7 @@ pub struct Reference {
1112
/// The range of the reference in the source code.
1213
range: TextRange,
1314
/// The context in which the reference occurs.
14-
context: ReferenceContext,
15+
context: ExecutionContext,
1516
}
1617

1718
impl Reference {
@@ -23,19 +24,11 @@ impl Reference {
2324
self.range
2425
}
2526

26-
pub const fn context(&self) -> &ReferenceContext {
27+
pub const fn context(&self) -> &ExecutionContext {
2728
&self.context
2829
}
2930
}
3031

31-
#[derive(Debug, Clone, is_macro::Is)]
32-
pub enum ReferenceContext {
33-
/// The reference occurs in a runtime context.
34-
Runtime,
35-
/// The reference occurs in a typing-only context.
36-
Typing,
37-
}
38-
3932
/// Id uniquely identifying a read reference in a program.
4033
#[newtype_index]
4134
pub struct ReferenceId;
@@ -50,7 +43,7 @@ impl References {
5043
&mut self,
5144
scope_id: ScopeId,
5245
range: TextRange,
53-
context: ReferenceContext,
46+
context: ExecutionContext,
5447
) -> ReferenceId {
5548
self.0.push(Reference {
5649
scope_id,

0 commit comments

Comments
 (0)