mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 20:10:09 +00:00
Remove separate ReferenceContext
enum (#4631)
This commit is contained in:
parent
86ced3516b
commit
fcdc7bdd33
7 changed files with 30 additions and 61 deletions
|
@ -24,13 +24,13 @@ use ruff_python_semantic::analyze::branch_detection;
|
||||||
use ruff_python_semantic::analyze::typing::{Callable, SubscriptKind};
|
use ruff_python_semantic::analyze::typing::{Callable, SubscriptKind};
|
||||||
use ruff_python_semantic::analyze::visibility::ModuleSource;
|
use ruff_python_semantic::analyze::visibility::ModuleSource;
|
||||||
use ruff_python_semantic::binding::{
|
use ruff_python_semantic::binding::{
|
||||||
Binding, BindingFlags, BindingId, BindingKind, Exceptions, ExecutionContext, Export,
|
Binding, BindingFlags, BindingId, BindingKind, Exceptions, Export, FromImportation,
|
||||||
FromImportation, Importation, StarImportation, SubmoduleImportation,
|
Importation, StarImportation, SubmoduleImportation,
|
||||||
};
|
};
|
||||||
|
use ruff_python_semantic::context::ExecutionContext;
|
||||||
use ruff_python_semantic::definition::{ContextualizedDefinition, Module, ModuleKind};
|
use ruff_python_semantic::definition::{ContextualizedDefinition, Module, ModuleKind};
|
||||||
use ruff_python_semantic::model::{ResolvedReference, SemanticModel, SemanticModelFlags};
|
use ruff_python_semantic::model::{ResolvedReference, SemanticModel, SemanticModelFlags};
|
||||||
use ruff_python_semantic::node::NodeId;
|
use ruff_python_semantic::node::NodeId;
|
||||||
use ruff_python_semantic::reference::ReferenceContext;
|
|
||||||
use ruff_python_semantic::scope::{ClassDef, FunctionDef, Lambda, Scope, ScopeId, ScopeKind};
|
use ruff_python_semantic::scope::{ClassDef, FunctionDef, Lambda, Scope, ScopeId, ScopeKind};
|
||||||
use ruff_python_stdlib::builtins::{BUILTINS, MAGIC_GLOBALS};
|
use ruff_python_stdlib::builtins::{BUILTINS, MAGIC_GLOBALS};
|
||||||
use ruff_python_stdlib::path::is_python_stub_file;
|
use ruff_python_stdlib::path::is_python_stub_file;
|
||||||
|
@ -300,7 +300,7 @@ where
|
||||||
self.semantic_model.add_local_reference(
|
self.semantic_model.add_local_reference(
|
||||||
*binding_id,
|
*binding_id,
|
||||||
stmt.range(),
|
stmt.range(),
|
||||||
ReferenceContext::Runtime,
|
ExecutionContext::Runtime,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Ensure that every nonlocal has an existing binding from a parent scope.
|
// Ensure that every nonlocal has an existing binding from a parent scope.
|
||||||
|
@ -4955,7 +4955,7 @@ impl<'a> Checker<'a> {
|
||||||
self.semantic_model.add_global_reference(
|
self.semantic_model.add_global_reference(
|
||||||
binding_id,
|
binding_id,
|
||||||
range,
|
range,
|
||||||
ReferenceContext::Runtime,
|
ExecutionContext::Runtime,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ use rustpython_parser::ast::{self, Constant, Expr};
|
||||||
|
|
||||||
use ruff_python_ast::call_path::from_qualified_name;
|
use ruff_python_ast::call_path::from_qualified_name;
|
||||||
use ruff_python_ast::helpers::map_callable;
|
use ruff_python_ast::helpers::map_callable;
|
||||||
use ruff_python_semantic::binding::{Binding, BindingKind, ExecutionContext};
|
use ruff_python_semantic::binding::{Binding, BindingKind};
|
||||||
use ruff_python_semantic::model::SemanticModel;
|
use ruff_python_semantic::model::SemanticModel;
|
||||||
use ruff_python_semantic::scope::ScopeKind;
|
use ruff_python_semantic::scope::ScopeKind;
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ pub(crate) fn is_valid_runtime_import(semantic_model: &SemanticModel, binding: &
|
||||||
| BindingKind::FromImportation(..)
|
| BindingKind::FromImportation(..)
|
||||||
| BindingKind::SubmoduleImportation(..)
|
| BindingKind::SubmoduleImportation(..)
|
||||||
) {
|
) {
|
||||||
matches!(binding.context, ExecutionContext::Runtime)
|
binding.context.is_runtime()
|
||||||
&& binding.references().any(|reference_id| {
|
&& binding.references().any(|reference_id| {
|
||||||
semantic_model
|
semantic_model
|
||||||
.references
|
.references
|
||||||
|
|
|
@ -7,6 +7,7 @@ use ruff_index::{newtype_index, IndexSlice, IndexVec};
|
||||||
use ruff_python_ast::helpers;
|
use ruff_python_ast::helpers;
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
|
|
||||||
|
use crate::context::ExecutionContext;
|
||||||
use crate::model::SemanticModel;
|
use crate::model::SemanticModel;
|
||||||
use crate::node::NodeId;
|
use crate::node::NodeId;
|
||||||
use crate::reference::ReferenceId;
|
use crate::reference::ReferenceId;
|
||||||
|
@ -272,9 +273,3 @@ bitflags! {
|
||||||
const IMPORT_ERROR = 0b0000_0100;
|
const IMPORT_ERROR = 0b0000_0100;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Debug, Clone, is_macro::Is)]
|
|
||||||
pub enum ExecutionContext {
|
|
||||||
Runtime,
|
|
||||||
Typing,
|
|
||||||
}
|
|
||||||
|
|
7
crates/ruff_python_semantic/src/context.rs
Normal file
7
crates/ruff_python_semantic/src/context.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#[derive(Debug, Copy, Clone, is_macro::Is)]
|
||||||
|
pub enum ExecutionContext {
|
||||||
|
/// The reference occurs in a runtime context.
|
||||||
|
Runtime,
|
||||||
|
/// The reference occurs in a typing-only context.
|
||||||
|
Typing,
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
pub mod analyze;
|
pub mod analyze;
|
||||||
pub mod binding;
|
pub mod binding;
|
||||||
|
pub mod context;
|
||||||
pub mod definition;
|
pub mod definition;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
pub mod node;
|
pub mod node;
|
||||||
|
|
|
@ -13,12 +13,13 @@ use ruff_python_stdlib::path::is_python_stub_file;
|
||||||
use ruff_python_stdlib::typing::TYPING_EXTENSIONS;
|
use ruff_python_stdlib::typing::TYPING_EXTENSIONS;
|
||||||
|
|
||||||
use crate::binding::{
|
use crate::binding::{
|
||||||
Binding, BindingId, BindingKind, Bindings, Exceptions, ExecutionContext, FromImportation,
|
Binding, BindingId, BindingKind, Bindings, Exceptions, FromImportation, Importation,
|
||||||
Importation, SubmoduleImportation,
|
SubmoduleImportation,
|
||||||
};
|
};
|
||||||
|
use crate::context::ExecutionContext;
|
||||||
use crate::definition::{Definition, DefinitionId, Definitions, Member, Module};
|
use crate::definition::{Definition, DefinitionId, Definitions, Member, Module};
|
||||||
use crate::node::{NodeId, Nodes};
|
use crate::node::{NodeId, Nodes};
|
||||||
use crate::reference::{ReferenceContext, References};
|
use crate::reference::References;
|
||||||
use crate::scope::{Scope, ScopeId, ScopeKind, Scopes};
|
use crate::scope::{Scope, ScopeId, ScopeKind, Scopes};
|
||||||
|
|
||||||
/// A semantic model for a Python module, to enable querying the module's semantic information.
|
/// A semantic model for a Python module, to enable querying the module's semantic information.
|
||||||
|
@ -126,26 +127,12 @@ impl<'a> SemanticModel<'a> {
|
||||||
if let Some(binding_id) = self.scopes.global().get(symbol).copied() {
|
if let Some(binding_id) = self.scopes.global().get(symbol).copied() {
|
||||||
// Mark the binding as used.
|
// Mark the binding as used.
|
||||||
let context = self.execution_context();
|
let context = self.execution_context();
|
||||||
let reference_id = self.references.push(
|
let reference_id = self.references.push(ScopeId::global(), range, context);
|
||||||
ScopeId::global(),
|
|
||||||
range,
|
|
||||||
match context {
|
|
||||||
ExecutionContext::Runtime => ReferenceContext::Runtime,
|
|
||||||
ExecutionContext::Typing => ReferenceContext::Typing,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
self.bindings[binding_id].references.push(reference_id);
|
self.bindings[binding_id].references.push(reference_id);
|
||||||
|
|
||||||
// Mark any submodule aliases as used.
|
// Mark any submodule aliases as used.
|
||||||
if let Some(binding_id) = self.resolve_submodule(ScopeId::global(), binding_id) {
|
if let Some(binding_id) = self.resolve_submodule(ScopeId::global(), binding_id) {
|
||||||
let reference_id = self.references.push(
|
let reference_id = self.references.push(ScopeId::global(), range, context);
|
||||||
ScopeId::global(),
|
|
||||||
range,
|
|
||||||
match context {
|
|
||||||
ExecutionContext::Runtime => ReferenceContext::Runtime,
|
|
||||||
ExecutionContext::Typing => ReferenceContext::Typing,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
self.bindings[binding_id].references.push(reference_id);
|
self.bindings[binding_id].references.push(reference_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,26 +163,12 @@ impl<'a> SemanticModel<'a> {
|
||||||
if let Some(binding_id) = scope.get(symbol).copied() {
|
if let Some(binding_id) = scope.get(symbol).copied() {
|
||||||
// Mark the binding as used.
|
// Mark the binding as used.
|
||||||
let context = self.execution_context();
|
let context = self.execution_context();
|
||||||
let reference_id = self.references.push(
|
let reference_id = self.references.push(self.scope_id, range, context);
|
||||||
self.scope_id,
|
|
||||||
range,
|
|
||||||
match context {
|
|
||||||
ExecutionContext::Runtime => ReferenceContext::Runtime,
|
|
||||||
ExecutionContext::Typing => ReferenceContext::Typing,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
self.bindings[binding_id].references.push(reference_id);
|
self.bindings[binding_id].references.push(reference_id);
|
||||||
|
|
||||||
// Mark any submodule aliases as used.
|
// Mark any submodule aliases as used.
|
||||||
if let Some(binding_id) = self.resolve_submodule(scope_id, binding_id) {
|
if let Some(binding_id) = self.resolve_submodule(scope_id, binding_id) {
|
||||||
let reference_id = self.references.push(
|
let reference_id = self.references.push(self.scope_id, range, context);
|
||||||
self.scope_id,
|
|
||||||
range,
|
|
||||||
match context {
|
|
||||||
ExecutionContext::Runtime => ReferenceContext::Runtime,
|
|
||||||
ExecutionContext::Typing => ReferenceContext::Typing,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
self.bindings[binding_id].references.push(reference_id);
|
self.bindings[binding_id].references.push(reference_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -585,7 +558,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
binding_id: BindingId,
|
binding_id: BindingId,
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
context: ReferenceContext,
|
context: ExecutionContext,
|
||||||
) {
|
) {
|
||||||
let reference_id = self.references.push(self.scope_id, range, context);
|
let reference_id = self.references.push(self.scope_id, range, context);
|
||||||
self.bindings[binding_id].references.push(reference_id);
|
self.bindings[binding_id].references.push(reference_id);
|
||||||
|
@ -596,7 +569,7 @@ impl<'a> SemanticModel<'a> {
|
||||||
&mut self,
|
&mut self,
|
||||||
binding_id: BindingId,
|
binding_id: BindingId,
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
context: ReferenceContext,
|
context: ExecutionContext,
|
||||||
) {
|
) {
|
||||||
let reference_id = self.references.push(ScopeId::global(), range, context);
|
let reference_id = self.references.push(ScopeId::global(), range, context);
|
||||||
self.bindings[binding_id].references.push(reference_id);
|
self.bindings[binding_id].references.push(reference_id);
|
||||||
|
|
|
@ -2,6 +2,7 @@ use ruff_text_size::TextRange;
|
||||||
|
|
||||||
use ruff_index::{newtype_index, IndexVec};
|
use ruff_index::{newtype_index, IndexVec};
|
||||||
|
|
||||||
|
use crate::context::ExecutionContext;
|
||||||
use crate::scope::ScopeId;
|
use crate::scope::ScopeId;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -11,7 +12,7 @@ pub struct Reference {
|
||||||
/// The range of the reference in the source code.
|
/// The range of the reference in the source code.
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
/// The context in which the reference occurs.
|
/// The context in which the reference occurs.
|
||||||
context: ReferenceContext,
|
context: ExecutionContext,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Reference {
|
impl Reference {
|
||||||
|
@ -23,19 +24,11 @@ impl Reference {
|
||||||
self.range
|
self.range
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn context(&self) -> &ReferenceContext {
|
pub const fn context(&self) -> &ExecutionContext {
|
||||||
&self.context
|
&self.context
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, is_macro::Is)]
|
|
||||||
pub enum ReferenceContext {
|
|
||||||
/// The reference occurs in a runtime context.
|
|
||||||
Runtime,
|
|
||||||
/// The reference occurs in a typing-only context.
|
|
||||||
Typing,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Id uniquely identifying a read reference in a program.
|
/// Id uniquely identifying a read reference in a program.
|
||||||
#[newtype_index]
|
#[newtype_index]
|
||||||
pub struct ReferenceId;
|
pub struct ReferenceId;
|
||||||
|
@ -50,7 +43,7 @@ impl References {
|
||||||
&mut self,
|
&mut self,
|
||||||
scope_id: ScopeId,
|
scope_id: ScopeId,
|
||||||
range: TextRange,
|
range: TextRange,
|
||||||
context: ReferenceContext,
|
context: ExecutionContext,
|
||||||
) -> ReferenceId {
|
) -> ReferenceId {
|
||||||
self.0.push(Reference {
|
self.0.push(Reference {
|
||||||
scope_id,
|
scope_id,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue