mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
fix: eliminate unwrap
s
This commit is contained in:
parent
0bedeb51ab
commit
c74513f507
9 changed files with 67 additions and 52 deletions
|
@ -537,7 +537,7 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
|
|||
.input
|
||||
.resolve_decl_path(Path::new(&__name__[..]), &self.cfg)
|
||||
{
|
||||
let size = metadata(&path).unwrap().len();
|
||||
let size = metadata(&path).or(Err(()))?.len();
|
||||
// if pylyzer crashed
|
||||
if !status.success() && size == 0 {
|
||||
// The presence of the decl file indicates that the analysis is in progress or completed,
|
||||
|
@ -602,7 +602,7 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
|
|||
let import_path = NormalizedPathBuf::from(import_path.clone());
|
||||
self.shared.graph.add_node_if_none(&import_path);
|
||||
// If we import `foo/bar`, we also need to import `foo`
|
||||
let first = __name__.split('/').next().unwrap();
|
||||
let first = __name__.split('/').next().unwrap_or_default();
|
||||
let root_path = if !first.is_empty() && first != "." && first != &__name__[..] {
|
||||
Some(Path::new(first))
|
||||
} else {
|
||||
|
|
|
@ -1220,12 +1220,13 @@ impl Context {
|
|||
let l_inf = self.inf(<);
|
||||
let l_sup = self.sup(<);
|
||||
if let (Some(inf), Some(sup)) = (l_inf, l_sup) {
|
||||
let (Some(l), Some(r)) = (self.try_cmp(&inf, p), self.try_cmp(&sup, p)) else {
|
||||
log!(err "{inf}, {sup}, {p}");
|
||||
return None;
|
||||
};
|
||||
// (n: Int, 1) -> (-inf..inf, 1) -> (cmp(-inf, 1), cmp(inf, 1)) -> (Less, Greater) -> Any
|
||||
// (n: 5..10, 2) -> (cmp(5..10, 2), cmp(5..10, 2)) -> (Greater, Greater) -> Greater
|
||||
match (
|
||||
self.try_cmp(&inf, p).unwrap(),
|
||||
self.try_cmp(&sup, p).unwrap()
|
||||
) {
|
||||
match (l, r) {
|
||||
(Less, Less) => Some(Less),
|
||||
(Less, Equal) => Some(LessEqual),
|
||||
(Less, LessEqual) => Some(LessEqual),
|
||||
|
@ -1254,8 +1255,12 @@ impl Context {
|
|||
(Any, Less) => Some(Less),
|
||||
(Any, Equal | LessEqual) => Some(LessEqual),
|
||||
(Any, Greater | NotEqual | GreaterEqual | Any) => Some(Any),
|
||||
(l, r) =>
|
||||
todo!("cmp({inf}, {sup}) = {l:?}, cmp({inf}, {sup}) = {r:?}"),
|
||||
(l, r) => {
|
||||
if DEBUG_MODE {
|
||||
todo!("cmp({inf}, {sup}) = {l:?}, cmp({inf}, {sup}) = {r:?}");
|
||||
}
|
||||
None
|
||||
},
|
||||
}
|
||||
} else {
|
||||
match (self.supertype_of(<, &pt), self.subtype_of(<, &pt)) {
|
||||
|
|
|
@ -309,7 +309,10 @@ impl<ASTBuilder: ASTBuildable> GenericASTLowerer<ASTBuilder> {
|
|||
}
|
||||
Expr::Def(def) => {
|
||||
if let Signature::Subr(subr) = &def.sig {
|
||||
let return_t = subr.ref_t().return_t().unwrap();
|
||||
let Some(return_t) = subr.ref_t().return_t() else {
|
||||
log!(err "{subr}");
|
||||
return;
|
||||
};
|
||||
if return_t.union_pair().is_some() && subr.return_t_spec.is_none() {
|
||||
let typ = if cfg!(feature = "debug") {
|
||||
return_t.clone()
|
||||
|
|
|
@ -737,14 +737,15 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
|||
})
|
||||
.and_then(|dict| (dict.len() == 1).then_some(dict));
|
||||
for kv in dict.kvs.into_iter() {
|
||||
let expect_key = expect.as_ref().map(|dict| dict.keys().next().unwrap());
|
||||
let expect_value = expect.as_ref().map(|dict| dict.values().next().unwrap());
|
||||
let expect_key = expect.as_ref().and_then(|dict| dict.keys().next());
|
||||
let expect_value = expect.as_ref().and_then(|dict| dict.values().next());
|
||||
let key = self.lower_expr(kv.key, expect_key)?;
|
||||
let value = self.lower_expr(kv.value, expect_value)?;
|
||||
if let Some(popped_val_t) = union.insert(key.t(), value.t()) {
|
||||
if PYTHON_MODE {
|
||||
let val_t = union.get_mut(key.ref_t()).unwrap();
|
||||
*val_t = self.module.context.union(&mem::take(val_t), &popped_val_t);
|
||||
if let Some(val_t) = union.get_mut(key.ref_t()) {
|
||||
*val_t = self.module.context.union(&mem::take(val_t), &popped_val_t);
|
||||
}
|
||||
} else {
|
||||
return Err(LowerErrors::from(LowerError::syntax_error(
|
||||
self.cfg.input.clone(),
|
||||
|
@ -942,7 +943,7 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
|||
.context
|
||||
.get_singular_ctxs_by_ident(&ident, &self.module.context)
|
||||
.ok()
|
||||
.map(|ctx| ctx.first().unwrap().name.clone()),
|
||||
.and_then(|ctxs| ctxs.first().map(|ctx| ctx.name.clone())),
|
||||
)
|
||||
};
|
||||
self.inc_ref(ident.inspect(), &vi, &ident.name);
|
||||
|
@ -1359,7 +1360,7 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
|||
.context
|
||||
.subtype_of(vi.t.return_t().unwrap(), &Type::Bool),
|
||||
"{} is not a subtype of Bool",
|
||||
vi.t.return_t().unwrap()
|
||||
vi.t.return_t().unwrap_or(&Type::Obj)
|
||||
);
|
||||
if let Some(ret_t) = vi.t.mut_return_t() {
|
||||
*ret_t = guard;
|
||||
|
@ -2416,7 +2417,9 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
|||
) {
|
||||
Err(err) => self.errs.push(err),
|
||||
Ok(_) => {
|
||||
*attr.ref_mut_t().unwrap() = derefined.clone();
|
||||
if let Some(attr_t) = attr.ref_mut_t() {
|
||||
*attr_t = derefined.clone();
|
||||
}
|
||||
if let hir::Accessor::Ident(ident) = &attr {
|
||||
if let Some(vi) = self
|
||||
.module
|
||||
|
|
|
@ -151,7 +151,9 @@ impl OwnershipChecker {
|
|||
Expr::Accessor(acc) => self.check_acc(acc, ownership, chunk),
|
||||
// TODO: referenced
|
||||
Expr::Call(call) => {
|
||||
let sig_t = call.signature_t().unwrap();
|
||||
let Some(sig_t) = call.signature_t() else {
|
||||
return;
|
||||
};
|
||||
if !sig_t.is_subr() {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
concat|T: Type, M, N: Nat|(l: [T; M], r: [T; N]): [T; M + N] = l + r
|
||||
concat|T, M: Nat, N: Nat|(l: [T; M], r: [T; N]): [T; M + N] = l + r
|
||||
|
||||
l: [Nat; 6] = concat [1, 2, 3], [4, 5, 6]
|
||||
assert l == [1, 2, 3, 4, 5, 6]
|
||||
|
|
|
@ -683,6 +683,36 @@ impl SubrType {
|
|||
self.return_t.derefine(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn args_ownership(&self) -> ArgsOwnership {
|
||||
let mut nd_args = vec![];
|
||||
for nd_param in self.non_default_params.iter() {
|
||||
let ownership = match nd_param.typ() {
|
||||
Type::Ref(_) => Ownership::Ref,
|
||||
Type::RefMut { .. } => Ownership::RefMut,
|
||||
_ => Ownership::Owned,
|
||||
};
|
||||
nd_args.push((nd_param.name().cloned(), ownership));
|
||||
}
|
||||
let var_args = self
|
||||
.var_params
|
||||
.as_ref()
|
||||
.map(|t| (t.name().cloned(), t.typ().ownership()));
|
||||
let mut d_args = vec![];
|
||||
for d_param in self.default_params.iter() {
|
||||
let ownership = match d_param.typ() {
|
||||
Type::Ref(_) => Ownership::Ref,
|
||||
Type::RefMut { .. } => Ownership::RefMut,
|
||||
_ => Ownership::Owned,
|
||||
};
|
||||
d_args.push((d_param.name().unwrap().clone(), ownership));
|
||||
}
|
||||
let kw_var_args = self
|
||||
.kw_var_params
|
||||
.as_ref()
|
||||
.map(|t| (t.name().cloned(), t.typ().ownership()));
|
||||
ArgsOwnership::new(nd_args, var_args, d_args, kw_var_args)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
|
@ -2500,35 +2530,7 @@ impl Type {
|
|||
match self {
|
||||
Self::FreeVar(fv) if fv.is_linked() => fv.crack().args_ownership(),
|
||||
Self::Refinement(refine) => refine.t.args_ownership(),
|
||||
Self::Subr(subr) => {
|
||||
let mut nd_args = vec![];
|
||||
for nd_param in subr.non_default_params.iter() {
|
||||
let ownership = match nd_param.typ() {
|
||||
Self::Ref(_) => Ownership::Ref,
|
||||
Self::RefMut { .. } => Ownership::RefMut,
|
||||
_ => Ownership::Owned,
|
||||
};
|
||||
nd_args.push((nd_param.name().cloned(), ownership));
|
||||
}
|
||||
let var_args = subr
|
||||
.var_params
|
||||
.as_ref()
|
||||
.map(|t| (t.name().cloned(), t.typ().ownership()));
|
||||
let mut d_args = vec![];
|
||||
for d_param in subr.default_params.iter() {
|
||||
let ownership = match d_param.typ() {
|
||||
Self::Ref(_) => Ownership::Ref,
|
||||
Self::RefMut { .. } => Ownership::RefMut,
|
||||
_ => Ownership::Owned,
|
||||
};
|
||||
d_args.push((d_param.name().unwrap().clone(), ownership));
|
||||
}
|
||||
let kw_var_args = subr
|
||||
.kw_var_params
|
||||
.as_ref()
|
||||
.map(|t| (t.name().cloned(), t.typ().ownership()));
|
||||
ArgsOwnership::new(nd_args, var_args, d_args, kw_var_args)
|
||||
}
|
||||
Self::Subr(subr) => subr.args_ownership(),
|
||||
Self::Quantified(quant) => quant.args_ownership(),
|
||||
other => todo!("{other}"),
|
||||
}
|
||||
|
|
|
@ -1249,15 +1249,15 @@ impl ValueObj {
|
|||
return Some(Ordering::Equal);
|
||||
}
|
||||
match (self, other) {
|
||||
(Self::NegInf, Self::Inf) => Some(Ordering::Less),
|
||||
(Self::Inf, Self::NegInf) => Some(Ordering::Greater),
|
||||
// REVIEW: 等しいとみなしてよいのか?
|
||||
(Self::Inf, Self::Inf) | (Self::NegInf, Self::NegInf) => Some(Ordering::Equal),
|
||||
(l, r) if l.is_num() && r.is_num() => {
|
||||
f64::try_from(l).ok()?.partial_cmp(&f64::try_from(r).ok()?)
|
||||
}
|
||||
(Self::Inf, n) | (n, Self::NegInf) if n.is_num() => Some(Ordering::Greater),
|
||||
(n, Self::Inf) | (Self::NegInf, n) if n.is_num() => Some(Ordering::Less),
|
||||
(Self::NegInf, Self::Inf) => Some(Ordering::Less),
|
||||
(Self::Inf, Self::NegInf) => Some(Ordering::Greater),
|
||||
// REVIEW: 等しいとみなしてよいのか?
|
||||
(Self::Inf, Self::Inf) | (Self::NegInf, Self::NegInf) => Some(Ordering::Equal),
|
||||
/* (Self::PlusEpsilon(l), r) => l.try_cmp(r)
|
||||
.map(|o| if matches!(o, Ordering::Equal) { Ordering::Less } else { o }),
|
||||
(l, Self::PlusEpsilon(r)) => l.try_cmp(r)
|
||||
|
|
|
@ -20,7 +20,7 @@ pub enum Mutability {
|
|||
|
||||
impl From<&str> for Mutability {
|
||||
fn from(item: &str) -> Self {
|
||||
if item.chars().next().unwrap().is_uppercase() {
|
||||
if item.chars().next().is_some_and(|c| c.is_uppercase()) {
|
||||
Self::Const
|
||||
} else {
|
||||
Self::Immutable
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue