Skip to content

Commit

Permalink
fix: eliminate panics
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Aug 28, 2024
1 parent caadd3c commit fdca32f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
3 changes: 3 additions & 0 deletions crates/erg_compiler/context/initialize/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,9 @@ impl Context {
str_.register_py_builtin(OP_GE, fn1_met(Str, Str, Bool), Some(OP_GE), 0);
str_.register_py_builtin(OP_LT, fn1_met(Str, Str, Bool), Some(OP_LT), 0);
str_.register_py_builtin(OP_LE, fn1_met(Str, Str, Bool), Some(OP_LE), 0);
if PYTHON_MODE {
str_.register_py_builtin(OP_MOD, fn1_met(Str, Obj, Str), Some(OP_MOD), 0);
}
str_.register_trait(self, mono(ORD)).unwrap();
str_.register_trait(self, mono(PATH_LIKE)).unwrap();
let t_s_replace = fn_met(
Expand Down
9 changes: 5 additions & 4 deletions crates/erg_compiler/context/inquire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,9 +1442,9 @@ impl Context {
let coerced = self
.coerce(obj.t(), &())
.map_err(|mut errs| errs.remove(0))?;
if &coerced != obj.ref_t() {
if &coerced != obj.ref_t() && obj.ref_t().as_free().is_some() {
let hash = get_hash(obj.ref_t());
obj.ref_t().destructive_link(&coerced);
obj.ref_t().destructive_link(&coerced); // obj.ref_t().coerce(None);
if get_hash(obj.ref_t()) != hash {
return self
.search_method_info(obj, attr_name, pos_args, kw_args, input, namespace);
Expand Down Expand Up @@ -1587,9 +1587,10 @@ impl Context {
let coerced = self
.coerce(obj.t(), &())
.map_err(|mut errs| errs.remove(0))?;
if &coerced != obj.ref_t() {
// REVIEW: if obj.ref_t() is not a free-var but contains free-vars
if &coerced != obj.ref_t() && obj.ref_t().as_free().is_some() {
let hash = get_hash(obj.ref_t());
obj.ref_t().destructive_link(&coerced);
obj.ref_t().destructive_link(&coerced); // obj.ref_t().coerce(None);
if get_hash(obj.ref_t()) != hash {
return self.search_method_info_without_args(obj, attr_name, input, namespace);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/erg_compiler/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::artifact::{BuildRunnable, Buildable, CompleteArtifact, IncompleteArti
use crate::build_package::CheckStatus;
use crate::module::SharedCompilerResource;
use crate::ty::constructors::{
free_var, func, guard, list_t, mono, poly, proc, refinement, set_t, singleton, ty_tp,
free_var, from_str, func, guard, list_t, mono, poly, proc, refinement, set_t, singleton, ty_tp,
unsized_list_t, v_enum,
};
use crate::ty::free::Constraint;
Expand Down Expand Up @@ -3074,7 +3074,7 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
line!() as usize,
method_name.inspect(),
method_name.loc(),
&mono(&sup.name), // TODO: get super type
&from_str(&sup.name), // TODO: get super type
self.module.context.caused_by(),
));
}
Expand Down
28 changes: 28 additions & 0 deletions crates/erg_compiler/ty/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,34 @@ pub fn mono<S: Into<Str>>(name: S) -> Type {
Type::Mono(name)
}

pub fn from_str(name: impl Into<Str>) -> Type {
let name = name.into();
match &name[..] {
"Obj" => Type::Obj,
"Int" => Type::Int,
"Nat" => Type::Nat,
"Ratio" => Type::Ratio,
"Float" => Type::Float,
"Complex" => Type::Complex,
"Bool" => Type::Bool,
"Str" => Type::Str,
"NoneType" => Type::NoneType,
"Code" => Type::Code,
"Frame" => Type::Frame,
"Error" => Type::Error,
"Inf" => Type::Inf,
"NegInf" => Type::NegInf,
"Type" => Type::Type,
"ClassType" => Type::ClassType,
"TraitType" => Type::TraitType,
"Patch" => Type::Patch,
"NotImplementedType" => Type::NotImplementedType,
"Ellipsis" => Type::Ellipsis,
"Never" => Type::Never,
_ => Type::Mono(name),
}
}

#[inline]
pub fn poly<S: Into<Str>>(name: S, params: Vec<TyParam>) -> Type {
Type::Poly {
Expand Down
21 changes: 21 additions & 0 deletions crates/erg_compiler/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,25 @@ impl SubrType {
}
self
}

pub fn destructive_coerce(&self) {
for nd in self.non_default_params.iter() {
nd.typ().destructive_coerce();
}
if let Some(var) = self.var_params.as_ref() {
var.typ().destructive_coerce();
}
for d in self.default_params.iter() {
d.typ().destructive_coerce();
if let Some(default) = d.default_typ() {
default.destructive_coerce();
}
}
if let Some(kw_var) = self.kw_var_params.as_ref() {
kw_var.typ().destructive_coerce();
}
self.return_t.destructive_coerce();
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -3307,6 +3326,8 @@ impl Type {
}
}
}
Type::Subr(subr) => subr.destructive_coerce(),
// TODO:
_ => {}
}
}
Expand Down

0 comments on commit fdca32f

Please sign in to comment.