feat: type spec of reassignment

This commit is contained in:
Shunsuke Shibayama 2024-08-16 15:30:47 +09:00
parent 11527b305f
commit bbe828db94
4 changed files with 32 additions and 27 deletions

20
Cargo.lock generated
View File

@ -136,9 +136,9 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]] [[package]]
name = "els" name = "els"
version = "0.1.54-nightly.0" version = "0.1.54-nightly.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb27dc0754e2490b8984d95f4fe6368258de544eedd37798a314acfeb5ccfc6" checksum = "19d774d511ed129b8438a9633b17f3303768fa7a3941372c1650e61a7bc7b00c"
dependencies = [ dependencies = [
"erg_common", "erg_common",
"erg_compiler", "erg_compiler",
@ -150,9 +150,9 @@ dependencies = [
[[package]] [[package]]
name = "erg_common" name = "erg_common"
version = "0.6.42-nightly.0" version = "0.6.42-nightly.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "415899164e7873f66eecad5ad37683c5fa31c038802033f35ad08f3fa38882dc" checksum = "b084d80afdb59d10d300595b2860868e52d1a0a72ad98ac995c9f5abfba9acd8"
dependencies = [ dependencies = [
"backtrace-on-stack-overflow", "backtrace-on-stack-overflow",
"erg_proc_macros", "erg_proc_macros",
@ -162,9 +162,9 @@ dependencies = [
[[package]] [[package]]
name = "erg_compiler" name = "erg_compiler"
version = "0.6.42-nightly.0" version = "0.6.42-nightly.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "048b3d74d5ca2c5279e18f15fd5997fe0cdfcad0a8d6ff2e4e238a8b76d86b36" checksum = "805b009c668055c8d72d11f8b26853c9a65a2744548909beda8b32bd4058a375"
dependencies = [ dependencies = [
"erg_common", "erg_common",
"erg_parser", "erg_parser",
@ -172,9 +172,9 @@ dependencies = [
[[package]] [[package]]
name = "erg_parser" name = "erg_parser"
version = "0.6.42-nightly.0" version = "0.6.42-nightly.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca774922a3555cb1973ead24f9ad27056057480bbcfff2ccab0b8b3637517054" checksum = "4ec4f2cc69904baae639ff154323d1cc0e100ca2785bfb6d8c392c88e0560771"
dependencies = [ dependencies = [
"erg_common", "erg_common",
"erg_proc_macros", "erg_proc_macros",
@ -183,9 +183,9 @@ dependencies = [
[[package]] [[package]]
name = "erg_proc_macros" name = "erg_proc_macros"
version = "0.6.42-nightly.0" version = "0.6.42-nightly.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7500b1a02978ccf36cef9eecb31f99911a0b56753589e375f0a51b6ff57b750" checksum = "6234abaef2fed929391add7520409890b2b7ee7029f2a5dcb9c2f4905bb7556b"
dependencies = [ dependencies = [
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",

View File

@ -22,9 +22,9 @@ edition = "2021"
repository = "https://github.com/mtshiba/pylyzer" repository = "https://github.com/mtshiba/pylyzer"
[workspace.dependencies] [workspace.dependencies]
erg_common = { version = "0.6.42-nightly.0", features = ["py_compat", "els"] } erg_common = { version = "0.6.42-nightly.2", features = ["py_compat", "els"] }
erg_compiler = { version = "0.6.42-nightly.0", features = ["py_compat", "els"] } erg_compiler = { version = "0.6.42-nightly.2", features = ["py_compat", "els"] }
els = { version = "0.1.54-nightly.0", features = ["py_compat"] } els = { version = "0.1.54-nightly.2", features = ["py_compat"] }
# rustpython-parser = { version = "0.3.0", features = ["all-nodes-with-ranges", "location"] } # 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-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"] } rustpython-parser = { git = "https://github.com/RustPython/Parser", version = "0.4.0", features = ["all-nodes-with-ranges", "location"] }

View File

@ -1356,7 +1356,7 @@ impl ASTConverter {
// if `self.foo == ...` // if `self.foo == ...`
if attr.obj.get_name().map(|s| &s[..]) == Some("self") { if attr.obj.get_name().map(|s| &s[..]) == Some("self") {
// get attribute types // get attribute types
let arg_typ_ident = if let Some(t_spec_op) = sig let typ = if let Some(t_spec_op) = sig
.params .params
.non_defaults .non_defaults
.iter() .iter()
@ -1370,19 +1370,20 @@ impl ASTConverter {
.and_then(|param| param.sig.t_spec.as_ref()) .and_then(|param| param.sig.t_spec.as_ref())
}) { }) {
let typ_name = t_spec_op.t_spec.to_string().replace('.', ""); let typ_name = t_spec_op.t_spec.to_string().replace('.', "");
Identifier::public_with_line( Expr::from(Accessor::Ident(Identifier::public_with_line(
DOT, DOT,
typ_name.into(), typ_name.into(),
attr.obj.ln_begin().unwrap_or(0), attr.obj.ln_begin().unwrap_or(0),
) )))
} else if let Some(typ) = redef.t_spec.map(|t_spec| t_spec.t_spec_as_expr) {
*typ
} else { } else {
Identifier::public_with_line( Expr::from(Accessor::Ident(Identifier::public_with_line(
DOT, DOT,
"Never".into(), "Never".into(),
attr.obj.ln_begin().unwrap_or(0), attr.obj.ln_begin().unwrap_or(0),
) )))
}; };
let typ = Expr::Accessor(Accessor::Ident(arg_typ_ident));
let sig = let sig =
Signature::Var(VarSignature::new(VarPattern::Ident(attr.ident), None)); Signature::Var(VarSignature::new(VarPattern::Ident(attr.ident), None));
let body = DefBody::new(EQUAL, Block::new(vec![typ]), DefId(0)); let body = DefBody::new(EQUAL, Block::new(vec![typ]), DefId(0));
@ -1746,7 +1747,7 @@ impl ASTConverter {
let attr = value.attr(ident); let attr = value.attr(ident);
if let Some(value) = ann_assign.value { if let Some(value) = ann_assign.value {
let expr = self.convert_expr(*value); let expr = self.convert_expr(*value);
let redef = ReDef::new(attr, expr); let redef = ReDef::new(attr, Some(t_spec), expr);
Expr::ReDef(redef) Expr::ReDef(redef)
} else { } else {
let tasc = TypeAscription::new(Expr::Accessor(attr), t_spec); let tasc = TypeAscription::new(Expr::Accessor(attr), t_spec);
@ -1774,7 +1775,7 @@ impl ASTConverter {
let def = Def::new(sig, body); let def = Def::new(sig, body);
Expr::Def(def) Expr::Def(def)
} else { } else {
let redef = ReDef::new(Accessor::Ident(ident), expr); let redef = ReDef::new(Accessor::Ident(ident), None, expr);
Expr::ReDef(redef) Expr::ReDef(redef)
} }
} }
@ -1784,7 +1785,7 @@ impl ASTConverter {
.convert_attr_ident(attr.attr.to_string(), attr_name_loc(&value)); .convert_attr_ident(attr.attr.to_string(), attr_name_loc(&value));
let attr = value.attr(ident); let attr = value.attr(ident);
let expr = self.convert_expr(*assign.value); let expr = self.convert_expr(*assign.value);
let adef = ReDef::new(attr, expr); let adef = ReDef::new(attr, None, expr);
Expr::ReDef(adef) Expr::ReDef(adef)
} }
py_ast::Expr::Tuple(tuple) => { py_ast::Expr::Tuple(tuple) => {
@ -1897,7 +1898,7 @@ impl ASTConverter {
let ident = self.convert_ident(name.id.to_string(), name.location()); let ident = self.convert_ident(name.id.to_string(), name.location());
let bin = let bin =
BinOp::new(op, Expr::Accessor(Accessor::Ident(prev_ident)), val); BinOp::new(op, Expr::Accessor(Accessor::Ident(prev_ident)), val);
let redef = ReDef::new(Accessor::Ident(ident), Expr::BinOp(bin)); let redef = ReDef::new(Accessor::Ident(ident), None, Expr::BinOp(bin));
Expr::ReDef(redef) Expr::ReDef(redef)
} }
} }
@ -1908,7 +1909,7 @@ impl ASTConverter {
.convert_attr_ident(attr.attr.to_string(), attr_name_loc(&attr_value)); .convert_attr_ident(attr.attr.to_string(), attr_name_loc(&attr_value));
let attr = attr_value.attr(ident); let attr = attr_value.attr(ident);
let bin = BinOp::new(op, Expr::Accessor(attr.clone()), assign_value); let bin = BinOp::new(op, Expr::Accessor(attr.clone()), assign_value);
let redef = ReDef::new(attr, Expr::BinOp(bin)); let redef = ReDef::new(attr, None, Expr::BinOp(bin));
Expr::ReDef(redef) Expr::ReDef(redef)
} }
other => { other => {

View File

@ -1,7 +1,8 @@
i_arr = [0] i_lis = [0]
i_arr.append(1) i_lis.append(1)
i_arr.append("a") # ERR i_lis.append("a") # ERR
_ = i_lis[0:0]
union_arr: list[int | str] = [] union_arr: list[int | str] = []
union_arr.append(1) union_arr.append(1)
@ -20,6 +21,7 @@ _ = dic2["c"] # OK
t: tuple[int, str] = (1, "a") t: tuple[int, str] = (1, "a")
_ = t[0] == 1 # OK _ = t[0] == 1 # OK
_ = t[1] == 1 # ERR _ = t[1] == 1 # ERR
_ = t[0:1]
def f(s: Str): return None def f(s: Str): return None
for i in getattr(1, "aaa", ()): for i in getattr(1, "aaa", ()):
@ -27,3 +29,5 @@ for i in getattr(1, "aaa", ()):
assert 1 in [1, 2] assert 1 in [1, 2]
assert 1 in {1, 2} assert 1 in {1, 2}
assert 1 in {1: "a"}
assert 1 in (1, 2)