Skip to content

Commit

Permalink
fix: name shadowing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Oct 19, 2024
1 parent 9702000 commit bb6b731
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ edition = "2021"
repository = "https://github.com/mtshiba/pylyzer"

[workspace.dependencies]
erg_common = { version = "0.6.46", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.46", features = ["py_compat", "els"] }
els = { version = "0.1.58", features = ["py_compat"] }
erg_common = { version = "0.6.47-nightly.2", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.47-nightly.2", features = ["py_compat", "els"] }
els = { version = "0.1.59-nightly.2", features = ["py_compat"] }
# rustpython-parser = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
# rustpython-ast = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] }
rustpython-parser = { git = "https://github.com/RustPython/Parser", version = "0.4.0", features = ["all-nodes-with-ranges", "location"] }
Expand Down
14 changes: 13 additions & 1 deletion crates/py2erg/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ impl BlockKind {
pub const fn is_function(&self) -> bool {
matches!(self, Self::Function | Self::AsyncFunction)
}
pub const fn makes_scope(&self) -> bool {
matches!(self, Self::Function | Self::AsyncFunction | Self::Class)
}
}

/// Variables are automatically rewritten with `py_compat`,
Expand Down Expand Up @@ -554,6 +557,7 @@ pub struct ASTConverter {
comments: CommentStorage,
pyi_types: PyiTypeStorage,
block_id_counter: usize,
/// block != scope (if block doesn't make a scope, for example)
block_ids: Vec<usize>,
contexts: Vec<LocalContext>,
warns: CompileErrors,
Expand Down Expand Up @@ -628,6 +632,10 @@ impl ASTConverter {
self.contexts.pop();
}

fn cur_block_kind(&self) -> BlockKind {
self.contexts.last().unwrap().kind
}

fn cur_block_id(&self) -> usize {
*self.block_ids.last().unwrap()
}
Expand Down Expand Up @@ -657,6 +665,7 @@ impl ASTConverter {
fn register_name_info(&mut self, name: &str, kind: NameKind) -> CanShadow {
let cur_namespace = self.cur_namespace();
let cur_block_id = self.cur_block_id();
let cur_block_kind = self.cur_block_kind();
if let Some(name_info) = self.get_mut_name(name) {
if name_info.defined_in == cur_namespace && name_info.defined_block_id == cur_block_id {
name_info.defined_times += 1;
Expand All @@ -665,7 +674,10 @@ impl ASTConverter {
name_info.defined_in = DefinedPlace::Known(cur_namespace);
name_info.defined_times += 1; // 0 -> 1
}
if name_info.defined_block_id == cur_block_id || name_info.defined_times == 0 {
if cur_block_kind.makes_scope()
|| name_info.defined_block_id == cur_block_id
|| name_info.defined_times == 0
{
CanShadow::Yes
} else {
CanShadow::No
Expand Down
18 changes: 18 additions & 0 deletions tests/dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
dic = {"a": 1, "b": 2}

def f():
dic = {"a": 1}
_ = dic["b"] # ERR


class TaskManager:
def __init__(self):
self.tasks: list[dict[str, int]] = []

def add_task(self, title: str, id: int):
task = {title: id}
self.tasks.append(task)

def add_task2(self, title: str, id: int):
task = {id: title}
self.tasks.append(task) # ERR
5 changes: 5 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ fn exec_import() -> Result<(), String> {
expect("tests/import.py", 1, 2)
}

#[test]
fn exec_dict() -> Result<(), String> {
expect("tests/dict.py", 0, 2)
}

#[test]
fn exec_export() -> Result<(), String> {
expect("tests/export.py", 0, 0)
Expand Down

0 comments on commit bb6b731

Please sign in to comment.