Skip to content

Commit

Permalink
fix: incorrect typing & method resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 26, 2024
1 parent 21caf6f commit a129141
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 79 deletions.
7 changes: 7 additions & 0 deletions crates/erg_compiler/context/initialize/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ impl Context {
Some(FUNDAMENTAL_FORMAT),
14,
);
obj.register_builtin_py_impl(
FUNDAMENTAL_CALL,
func0(Obj),
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNDAMENTAL_CALL),
);
// Obj does not implement Eq
let mut complex = Self::builtin_mono_class(COMPLEX, 2);
complex.register_superclass(Obj, &obj);
Expand Down
38 changes: 36 additions & 2 deletions crates/erg_compiler/context/initialize/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ impl Context {
)
.quantify();
let t_slice = no_var_func(
vec![kw(KW_START, Int)],
vec![kw(KW_STOP, Int), kw(KW_STEP, Int)],
vec![kw(KW_START, Int | NoneType)],
vec![kw(KW_STOP, Int | NoneType), kw(KW_STEP, Int | NoneType)],
mono(SLICE),
);
let t_sorted = nd_func(
Expand Down Expand Up @@ -975,6 +975,40 @@ impl Context {
Visibility::BUILTIN_PUBLIC,
None,
);
let t_exec = func(
vec![kw(KW_CODE, Str)],
None,
vec![
kw(KW_GLOBALS, mono(GENERIC_DICT)),
kw(KW_LOCALS, mono(GENERIC_DICT)),
],
None,
NoneType,
);
self.register_builtin_py_impl(
FUNC_EXEC,
t_exec,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_EXEC),
);
let t_eval = func(
vec![kw(KW_CODE, Str)],
None,
vec![
kw(KW_GLOBALS, mono(GENERIC_DICT)),
kw(KW_LOCALS, mono(GENERIC_DICT)),
],
None,
NoneType,
);
self.register_builtin_py_impl(
FUNC_EVAL,
t_eval,
Immutable,
Visibility::BUILTIN_PUBLIC,
Some(FUNC_EVAL),
);
}

pub(super) fn init_builtin_operators(&mut self) {
Expand Down
4 changes: 4 additions & 0 deletions crates/erg_compiler/context/initialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ const FUNC_HASATTR: &str = "hasattr";
const FUNC_GETATTR: &str = "getattr";
const FUNC_SETATTR: &str = "setattr";
const FUNC_DELATTR: &str = "delattr";
const FUNC_EXEC: &str = "exec";
const FUNC_EVAL: &str = "eval";
const FUNC_NEARLY_EQ: &str = "nearly_eq";
const FUNC_RESOLVE_PATH: &str = "ResolvePath";
const FUNC_RESOLVE_DECL_PATH: &str = "ResolveDeclPath";
Expand Down Expand Up @@ -732,6 +734,8 @@ const KW_NUMBER: &str = "number";
const KW_ITERABLE1: &str = "iterable1";
const KW_ITERABLE2: &str = "iterable2";
const KW_CODE: &str = "code";
const KW_GLOBALS: &str = "globals";
const KW_LOCALS: &str = "locals";
const KW_STOP: &str = "stop";
const KW_STEP: &str = "step";
const KW_REQUIREMENT: &str = "Requirement";
Expand Down
121 changes: 56 additions & 65 deletions crates/erg_compiler/context/inquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ impl Context {
})
}

/// ```erg
/// get_singular_ctxs_by_hir_expr(1) == Err
/// get_singular_ctxs_by_hir_expr(Int) == [<type Int>, <type Type>, ...]
/// get_singular_ctxs_by_hir_expr(math) == [<module math>]
/// ```
pub fn get_singular_ctxs_by_hir_expr(
&self,
obj: &hir::Expr,
Expand Down Expand Up @@ -1342,6 +1347,20 @@ impl Context {
}
_ => {}
}
if let Ok(singular_ctxs) = self.get_singular_ctxs_by_hir_expr(obj, namespace) {
for ctx in singular_ctxs {
if let Some(vi) = ctx.get_current_scope_non_param(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
for method_ctx in ctx.methods_list.iter() {
if let Some(vi) = method_ctx.get_current_scope_non_param(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
}
}
}
for ctx in self
.get_nominal_super_type_ctxs(obj.ref_t())
.ok_or_else(|| {
Expand Down Expand Up @@ -1376,30 +1395,6 @@ impl Context {
}
}
}
if let Ok(singular_ctxs) = self.get_singular_ctxs_by_hir_expr(obj, namespace) {
for ctx in singular_ctxs {
if let Some(vi) = ctx.get_current_scope_non_param(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
for method_ctx in ctx.methods_list.iter() {
if let Some(vi) = method_ctx.get_current_scope_non_param(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
}
}
return Err(TyCheckError::singular_no_attr_error(
self.cfg.input.clone(),
line!() as usize,
attr_name.loc(),
namespace.name.to_string(),
obj.qual_name().as_deref().unwrap_or("?"),
obj.ref_t(),
attr_name.inspect(),
self.get_similar_attr_from_singular(obj, attr_name.inspect()),
));
}
match self.get_attr_type_by_name(obj, attr_name, namespace) {
Triple::Ok(method) => {
let def_t = self
Expand Down Expand Up @@ -1436,17 +1431,18 @@ impl Context {
);
return Ok(vi);
}
}
for patch in self.find_patches_of(obj.ref_t()) {
if let Some(vi) = patch.get_current_scope_non_param(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
for methods_ctx in patch.methods_list.iter() {
if let Some(vi) = methods_ctx.get_current_scope_non_param(&attr_name.name) {
} else {
for patch in self.find_patches_of(obj.ref_t()) {
if let Some(vi) = patch.get_current_scope_non_param(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
for methods_ctx in patch.methods_list.iter() {
if let Some(vi) = methods_ctx.get_current_scope_non_param(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
}
}
}
let coerced = self
Expand Down Expand Up @@ -1487,6 +1483,20 @@ impl Context {
}
_ => {}
}
if let Ok(singular_ctxs) = self.get_singular_ctxs_by_hir_expr(obj, namespace) {
for ctx in singular_ctxs {
if let Some(vi) = ctx.get_current_scope_callable(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
for method_ctx in ctx.methods_list.iter() {
if let Some(vi) = method_ctx.get_current_scope_callable(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
}
}
}
for ctx in self
.get_nominal_super_type_ctxs(obj.ref_t())
.ok_or_else(|| {
Expand Down Expand Up @@ -1521,30 +1531,6 @@ impl Context {
}
}
}
if let Ok(singular_ctxs) = self.get_singular_ctxs_by_hir_expr(obj, namespace) {
for ctx in singular_ctxs {
if let Some(vi) = ctx.get_current_scope_callable(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
for method_ctx in ctx.methods_list.iter() {
if let Some(vi) = method_ctx.get_current_scope_callable(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
}
}
return Err(TyCheckError::singular_no_attr_error(
self.cfg.input.clone(),
line!() as usize,
attr_name.loc(),
namespace.name.to_string(),
obj.qual_name().as_deref().unwrap_or("?"),
obj.ref_t(),
attr_name.inspect(),
self.get_similar_attr_from_singular(obj, attr_name.inspect()),
));
}
match self.get_attr_type_by_name(obj, attr_name, namespace) {
Triple::Ok(method) => {
let def_t = self
Expand Down Expand Up @@ -1581,17 +1567,18 @@ impl Context {
);
return Ok(vi);
}
}
for patch in self.find_patches_of(obj.ref_t()) {
if let Some(vi) = patch.get_current_scope_callable(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
for methods_ctx in patch.methods_list.iter() {
if let Some(vi) = methods_ctx.get_current_scope_callable(&attr_name.name) {
} else {
for patch in self.find_patches_of(obj.ref_t()) {
if let Some(vi) = patch.get_current_scope_callable(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
for methods_ctx in patch.methods_list.iter() {
if let Some(vi) = methods_ctx.get_current_scope_callable(&attr_name.name) {
self.validate_visibility(attr_name, vi, input, namespace)?;
return Ok(vi.clone());
}
}
}
}
let coerced = self
Expand Down Expand Up @@ -2824,7 +2811,7 @@ impl Context {
)
}

pub(crate) fn get_similar_attr_from_singular<'a>(
pub(crate) fn _get_similar_attr_from_singular<'a>(
&'a self,
obj: &hir::Expr,
name: &str,
Expand Down Expand Up @@ -3023,6 +3010,10 @@ impl Context {
opt_max
}

/// ```erg
/// get_nominal_super_type_ctx(Nat) == [<Nat>, <Int>, <Float>, ..., <Obj>, <Eq>, ...]
/// get_nominal_super_type_ctx({Nat}) == [<Type>, <Obj>, <Eq>, ...]
/// ```
pub fn get_nominal_super_type_ctxs<'a>(&'a self, t: &Type) -> Option<Vec<&'a TypeContext>> {
match t {
Type::FreeVar(fv) if fv.is_linked() => self.get_nominal_super_type_ctxs(&fv.crack()),
Expand Down
Loading

0 comments on commit a129141

Please sign in to comment.