mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 20:34:44 +00:00
Fix a python module resolving bug
This commit is contained in:
parent
3c17518963
commit
954c6f752d
4 changed files with 41 additions and 36 deletions
|
@ -1135,12 +1135,14 @@ impl Context {
|
||||||
generic_module.register_trait(g_module_t.clone(), generic_module_eq);
|
generic_module.register_trait(g_module_t.clone(), generic_module_eq);
|
||||||
let Path = mono_q_tp("Path", instanceof(Str));
|
let Path = mono_q_tp("Path", instanceof(Str));
|
||||||
let module_t = module(Path.clone());
|
let module_t = module(Path.clone());
|
||||||
let py_module_t = py_module(Path);
|
let py_module_t = py_module(Path, self.cfg.python_compatible_mode);
|
||||||
let mut module = Self::builtin_poly_class("Module", vec![PS::named_nd("Path", Str)], 2);
|
let mut module = Self::builtin_poly_class("Module", vec![PS::named_nd("Path", Str)], 2);
|
||||||
module.register_superclass(g_module_t.clone(), &generic_module);
|
module.register_superclass(g_module_t.clone(), &generic_module);
|
||||||
let mut py_module =
|
let mut py_module =
|
||||||
Self::builtin_poly_class("PyModule", vec![PS::named_nd("Path", Str)], 2);
|
Self::builtin_poly_class("PyModule", vec![PS::named_nd("Path", Str)], 2);
|
||||||
|
if !self.cfg.python_compatible_mode {
|
||||||
py_module.register_superclass(g_module_t.clone(), &generic_module);
|
py_module.register_superclass(g_module_t.clone(), &generic_module);
|
||||||
|
}
|
||||||
/* Array */
|
/* Array */
|
||||||
let mut array_ =
|
let mut array_ =
|
||||||
Self::builtin_poly_class("Array", vec![PS::t_nd("T"), PS::named_nd("N", Nat)], 10);
|
Self::builtin_poly_class("Array", vec![PS::t_nd("T"), PS::named_nd("N", Nat)], 10);
|
||||||
|
@ -1672,7 +1674,6 @@ impl Context {
|
||||||
Const,
|
Const,
|
||||||
Some("ModuleType"),
|
Some("ModuleType"),
|
||||||
);
|
);
|
||||||
self.register_builtin_type(module_t, module, vis, Const, Some("ModuleType"));
|
|
||||||
self.register_builtin_type(py_module_t, py_module, vis, Const, Some("ModuleType"));
|
self.register_builtin_type(py_module_t, py_module, vis, Const, Some("ModuleType"));
|
||||||
self.register_builtin_type(arr_t, array_, vis, Const, Some("list"));
|
self.register_builtin_type(arr_t, array_, vis, Const, Some("list"));
|
||||||
self.register_builtin_type(set_t, set_, vis, Const, Some("set"));
|
self.register_builtin_type(set_t, set_, vis, Const, Some("set"));
|
||||||
|
@ -1714,6 +1715,7 @@ impl Context {
|
||||||
self.register_builtin_type(array_mut_t, array_mut_, vis, Const, Some("list"));
|
self.register_builtin_type(array_mut_t, array_mut_, vis, Const, Some("list"));
|
||||||
self.register_builtin_type(set_mut_t, set_mut_, vis, Const, Some("set"));
|
self.register_builtin_type(set_mut_t, set_mut_, vis, Const, Some("set"));
|
||||||
if !self.cfg.python_compatible_mode {
|
if !self.cfg.python_compatible_mode {
|
||||||
|
self.register_builtin_type(module_t, module, vis, Const, Some("ModuleType"));
|
||||||
self.register_builtin_type(mono("Obj!"), obj_mut, vis, Const, Some("object"));
|
self.register_builtin_type(mono("Obj!"), obj_mut, vis, Const, Some("object"));
|
||||||
self.register_builtin_type(mono("Int!"), int_mut, vis, Const, Some("int"));
|
self.register_builtin_type(mono("Int!"), int_mut, vis, Const, Some("int"));
|
||||||
self.register_builtin_type(mono("Nat!"), nat_mut, vis, Const, Some("Nat"));
|
self.register_builtin_type(mono("Nat!"), nat_mut, vis, Const, Some("Nat"));
|
||||||
|
@ -1863,7 +1865,7 @@ impl Context {
|
||||||
let t_pyimport = nd_func(
|
let t_pyimport = nd_func(
|
||||||
vec![anon(tp_enum(Str, set! {Path.clone()}))],
|
vec![anon(tp_enum(Str, set! {Path.clone()}))],
|
||||||
None,
|
None,
|
||||||
py_module(Path),
|
py_module(Path, self.cfg.python_compatible_mode),
|
||||||
)
|
)
|
||||||
.quantify();
|
.quantify();
|
||||||
let t_pycompile = nd_func(
|
let t_pycompile = nd_func(
|
||||||
|
@ -1885,21 +1887,20 @@ impl Context {
|
||||||
self.register_builtin_py_impl("chr", t_chr, Immutable, vis, Some("chr"));
|
self.register_builtin_py_impl("chr", t_chr, Immutable, vis, Some("chr"));
|
||||||
self.register_builtin_py_impl("classof", t_classof, Immutable, vis, Some("type"));
|
self.register_builtin_py_impl("classof", t_classof, Immutable, vis, Some("type"));
|
||||||
self.register_builtin_py_impl("compile", t_compile, Immutable, vis, Some("compile"));
|
self.register_builtin_py_impl("compile", t_compile, Immutable, vis, Some("compile"));
|
||||||
self.register_builtin_impl("cond", t_cond, Immutable, Private);
|
self.register_builtin_impl("cond", t_cond, Immutable, vis);
|
||||||
self.register_builtin_py_impl("exit", t_exit, Immutable, vis, Some("exit"));
|
self.register_builtin_py_impl("exit", t_exit, Immutable, vis, Some("exit"));
|
||||||
self.register_builtin_py_impl("import", t_import, Immutable, vis, Some("__import__"));
|
|
||||||
self.register_builtin_py_impl(
|
self.register_builtin_py_impl(
|
||||||
"isinstance",
|
"isinstance",
|
||||||
t_isinstance,
|
t_isinstance,
|
||||||
Immutable,
|
Immutable,
|
||||||
Private,
|
vis,
|
||||||
Some("isinstance"),
|
Some("isinstance"),
|
||||||
);
|
);
|
||||||
self.register_builtin_py_impl(
|
self.register_builtin_py_impl(
|
||||||
"issubclass",
|
"issubclass",
|
||||||
t_issubclass,
|
t_issubclass,
|
||||||
Immutable,
|
Immutable,
|
||||||
Private,
|
vis,
|
||||||
Some("issubclass"),
|
Some("issubclass"),
|
||||||
);
|
);
|
||||||
self.register_builtin_py_impl("len", t_len, Immutable, vis, Some("len"));
|
self.register_builtin_py_impl("len", t_len, Immutable, vis, Some("len"));
|
||||||
|
@ -1907,6 +1908,13 @@ impl Context {
|
||||||
self.register_builtin_py_impl("oct", t_oct, Immutable, vis, Some("oct"));
|
self.register_builtin_py_impl("oct", t_oct, Immutable, vis, Some("oct"));
|
||||||
self.register_builtin_py_impl("ord", t_ord, Immutable, vis, Some("ord"));
|
self.register_builtin_py_impl("ord", t_ord, Immutable, vis, Some("ord"));
|
||||||
self.register_builtin_py_impl("pow", t_pow, Immutable, vis, Some("pow"));
|
self.register_builtin_py_impl("pow", t_pow, Immutable, vis, Some("pow"));
|
||||||
|
self.register_builtin_py_impl(
|
||||||
|
"pyimport",
|
||||||
|
t_pyimport.clone(),
|
||||||
|
Immutable,
|
||||||
|
vis,
|
||||||
|
Some("__import__"),
|
||||||
|
);
|
||||||
self.register_builtin_py_impl("quit", t_quit, Immutable, vis, Some("quit"));
|
self.register_builtin_py_impl("quit", t_quit, Immutable, vis, Some("quit"));
|
||||||
self.register_builtin_py_impl("repr", t_repr, Immutable, vis, Some("repr"));
|
self.register_builtin_py_impl("repr", t_repr, Immutable, vis, Some("repr"));
|
||||||
self.register_builtin_py_impl("round", t_round, Immutable, vis, Some("round"));
|
self.register_builtin_py_impl("round", t_round, Immutable, vis, Some("round"));
|
||||||
|
@ -1919,37 +1927,19 @@ impl Context {
|
||||||
self.register_builtin_py_impl("int", t_int, Immutable, vis, Some(name));
|
self.register_builtin_py_impl("int", t_int, Immutable, vis, Some(name));
|
||||||
if !self.cfg.python_compatible_mode {
|
if !self.cfg.python_compatible_mode {
|
||||||
self.register_builtin_py_impl("if", t_if, Immutable, vis, Some("if__"));
|
self.register_builtin_py_impl("if", t_if, Immutable, vis, Some("if__"));
|
||||||
self.register_builtin_py_impl(
|
self.register_builtin_py_impl("discard", t_discard, Immutable, vis, Some("discard__"));
|
||||||
"discard",
|
self.register_builtin_py_impl("import", t_import, Immutable, vis, Some("__import__"));
|
||||||
t_discard,
|
|
||||||
Immutable,
|
|
||||||
Private,
|
|
||||||
Some("discard__"),
|
|
||||||
);
|
|
||||||
self.register_builtin_py_impl("log", t_log, Immutable, vis, Some("print"));
|
self.register_builtin_py_impl("log", t_log, Immutable, vis, Some("print"));
|
||||||
self.register_builtin_py_impl("nat", t_nat, Immutable, vis, Some("nat__"));
|
self.register_builtin_py_impl("nat", t_nat, Immutable, vis, Some("nat__"));
|
||||||
self.register_builtin_py_impl("panic", t_panic, Immutable, vis, Some("quit"));
|
self.register_builtin_py_impl("panic", t_panic, Immutable, vis, Some("quit"));
|
||||||
if cfg!(feature = "debug") {
|
if cfg!(feature = "debug") {
|
||||||
self.register_builtin_py_impl(
|
self.register_builtin_py_impl("py", t_pyimport, Immutable, vis, Some("__import__"));
|
||||||
"py",
|
|
||||||
t_pyimport.clone(),
|
|
||||||
Immutable,
|
|
||||||
Private,
|
|
||||||
Some("__import__"),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
self.register_builtin_py_impl(
|
|
||||||
"pyimport",
|
|
||||||
t_pyimport,
|
|
||||||
Immutable,
|
|
||||||
Private,
|
|
||||||
Some("__import__"),
|
|
||||||
);
|
|
||||||
self.register_builtin_py_impl(
|
self.register_builtin_py_impl(
|
||||||
"pycompile",
|
"pycompile",
|
||||||
t_pycompile,
|
t_pycompile,
|
||||||
Immutable,
|
Immutable,
|
||||||
Private,
|
vis,
|
||||||
Some("compile"),
|
Some("compile"),
|
||||||
);
|
);
|
||||||
// TODO: original implementation
|
// TODO: original implementation
|
||||||
|
@ -1957,7 +1947,7 @@ impl Context {
|
||||||
"unreachable",
|
"unreachable",
|
||||||
t_unreachable,
|
t_unreachable,
|
||||||
Immutable,
|
Immutable,
|
||||||
Private,
|
vis,
|
||||||
Some("exit"),
|
Some("exit"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -424,7 +424,15 @@ impl Context {
|
||||||
}
|
}
|
||||||
Type::Poly { name, mut params } => {
|
Type::Poly { name, mut params } => {
|
||||||
let typ = poly(&name, params.clone());
|
let typ = poly(&name, params.clone());
|
||||||
let (_, ctx) = self.get_nominal_type_ctx(&typ).unwrap();
|
let (_, ctx) = self.get_nominal_type_ctx(&typ).ok_or_else(|| {
|
||||||
|
TyCheckError::type_not_found(
|
||||||
|
self.cfg.input.clone(),
|
||||||
|
line!() as usize,
|
||||||
|
loc,
|
||||||
|
self.caused_by(),
|
||||||
|
&typ,
|
||||||
|
)
|
||||||
|
})?;
|
||||||
let variances = ctx.type_params_variance();
|
let variances = ctx.type_params_variance();
|
||||||
for (param, variance) in params.iter_mut().zip(variances.into_iter()) {
|
for (param, variance) in params.iter_mut().zip(variances.into_iter()) {
|
||||||
*param = self.deref_tp(mem::take(param), variance, loc)?;
|
*param = self.deref_tp(mem::take(param), variance, loc)?;
|
||||||
|
|
|
@ -65,8 +65,13 @@ pub fn module(path: TyParam) -> Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn py_module(path: TyParam) -> Type {
|
pub fn py_module(path: TyParam, py_comp_mode: bool) -> Type {
|
||||||
poly("PyModule", vec![path])
|
let name = if py_comp_mode {
|
||||||
|
"ModuleType"
|
||||||
|
} else {
|
||||||
|
"PyModule"
|
||||||
|
};
|
||||||
|
poly(name, vec![path])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn module_from_path<P: Into<PathBuf>>(path: P) -> Type {
|
pub fn module_from_path<P: Into<PathBuf>>(path: P) -> Type {
|
||||||
|
|
|
@ -1646,7 +1646,9 @@ impl Type {
|
||||||
match self {
|
match self {
|
||||||
Self::FreeVar(fv) if fv.is_linked() => fv.crack().is_module(),
|
Self::FreeVar(fv) if fv.is_linked() => fv.crack().is_module(),
|
||||||
Self::Refinement(refine) => refine.t.is_module(),
|
Self::Refinement(refine) => refine.t.is_module(),
|
||||||
Self::Poly { name, .. } => &name[..] == "PyModule" || &name[..] == "Module",
|
Self::Poly { name, .. } => {
|
||||||
|
&name[..] == "PyModule" || &name[..] == "Module" || &name[..] == "ModuleType"
|
||||||
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1655,7 +1657,7 @@ impl Type {
|
||||||
match self {
|
match self {
|
||||||
Self::FreeVar(fv) if fv.is_linked() => fv.crack().is_py_module(),
|
Self::FreeVar(fv) if fv.is_linked() => fv.crack().is_py_module(),
|
||||||
Self::Refinement(refine) => refine.t.is_py_module(),
|
Self::Refinement(refine) => refine.t.is_py_module(),
|
||||||
Self::Poly { name, .. } => &name[..] == "PyModule",
|
Self::Poly { name, .. } => &name[..] == "PyModule" || &name[..] == "ModuleType",
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue