Skip to content

Commit

Permalink
fix: eliminate unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 2, 2023
1 parent 7fd093a commit d8835fd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
12 changes: 9 additions & 3 deletions crates/els/call_hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
) -> ELSResult<Option<Vec<CallHierarchyIncomingCall>>> {
let mut res = vec![];
_log!("call hierarchy incoming calls requested: {params:?}");
let data = params.item.data.as_ref().unwrap().as_str().unwrap();
let loc = AbsLocation::from_str(data).unwrap();
let shared = self.get_shared().unwrap();
let Some(data) = params.item.data.as_ref().and_then(|d| d.as_str()) else {
return Ok(None);
};
let Ok(loc) = AbsLocation::from_str(data) else {
return Ok(None);
};
let Some(shared) = self.get_shared() else {
return Ok(None);
};
if let Some(refs) = shared.index.get_refs(&loc) {
for referrer_loc in refs.referrers.iter() {
let Some(uri) = referrer_loc
Expand Down
14 changes: 7 additions & 7 deletions crates/els/folding_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use crate::_log;
use crate::server::{ELSResult, Server};
use crate::util::NormalizedUrl;

fn imports_range(start: &Location, end: &Location) -> FoldingRange {
FoldingRange {
start_line: start.ln_begin().unwrap().saturating_sub(1),
fn imports_range(start: &Location, end: &Location) -> Option<FoldingRange> {
Some(FoldingRange {
start_line: start.ln_begin()?.saturating_sub(1),
start_character: start.col_begin(),
end_line: end.ln_end().unwrap().saturating_sub(1),
end_line: end.ln_end()?.saturating_sub(1),
end_character: end.col_end(),
kind: Some(FoldingRangeKind::Imports),
}
})
}

impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
if !ranges.is_empty() {
let start = ranges.first().unwrap();
let end = ranges.last().unwrap();
res.push(imports_range(start, end));
res.extend(imports_range(start, end));
ranges.clear();
}
}
Expand All @@ -54,7 +54,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
if !ranges.is_empty() {
let start = ranges.first().unwrap();
let end = ranges.last().unwrap();
res.push(imports_range(start, end));
res.extend(imports_range(start, end));
}
}
res
Expand Down
27 changes: 15 additions & 12 deletions crates/els/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,45 +81,48 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {

fn symbol(&self, chunk: &Expr) -> Option<DocumentSymbol> {
match chunk {
Expr::Def(def) =>
{
Expr::Def(def) => {
let range = loc_to_range(def.loc())?;
let selection_range = loc_to_range(def.sig.loc())?;
#[allow(deprecated)]
Some(DocumentSymbol {
name: def.sig.name().to_string(),
detail: Some(def.sig.ident().ref_t().to_string()),
kind: symbol_kind(&def.sig.ident().vi),
tags: None,
deprecated: None,
range: loc_to_range(def.loc()).unwrap(),
selection_range: loc_to_range(def.sig.loc()).unwrap(),
range,
selection_range,
children: Some(self.child_symbols(chunk)),
})
}
Expr::ClassDef(def) =>
{
Expr::ClassDef(def) => {
let range = loc_to_range(def.loc())?;
let selection_range = loc_to_range(def.sig.loc())?;
#[allow(deprecated)]
Some(DocumentSymbol {
name: def.sig.name().to_string(),
detail: Some(def.sig.ident().ref_t().to_string()),
kind: symbol_kind(&def.sig.ident().vi),
tags: None,
deprecated: None,
range: loc_to_range(def.loc()).unwrap(),
selection_range: loc_to_range(def.sig.loc()).unwrap(),
range,
selection_range,
children: Some(self.child_symbols(chunk)),
})
}
Expr::PatchDef(def) =>
{
Expr::PatchDef(def) => {
let range = loc_to_range(def.loc())?;
let selection_range = loc_to_range(def.sig.loc())?;
#[allow(deprecated)]
Some(DocumentSymbol {
name: def.sig.name().to_string(),
detail: Some(def.sig.ident().ref_t().to_string()),
kind: symbol_kind(&def.sig.ident().vi),
tags: None,
deprecated: None,
range: loc_to_range(def.loc()).unwrap(),
selection_range: loc_to_range(def.sig.loc()).unwrap(),
range,
selection_range,
children: Some(self.child_symbols(chunk)),
})
}
Expand Down
9 changes: 9 additions & 0 deletions crates/erg_compiler/context/initialize/funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,15 @@ impl Context {
bin_op(S, R.clone(), O).quantify()
};
self.register_builtin_erg_impl(OP_OR, op_t, Const, Visibility::BUILTIN_PRIVATE);
let T = mono_q(TY_T, instanceof(Type));
let C = mono_q(TY_C, subtypeof(poly(CONTAINER, vec![ty_tp(T.clone())])));
let op_t = bin_op(C, T, Bool).quantify();
self.register_builtin_erg_impl(
FUNDAMENTAL_CONTAINS,
op_t.clone(),
Const,
Visibility::BUILTIN_PRIVATE,
);
/* unary */
let M = mono_q(TY_M, subtypeof(mono(MUTIZABLE)));
let op_t = func1(M.clone(), proj(M, MUTABLE_MUT_TYPE)).quantify();
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/lib/std/_erg_contains_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import namedtuple

# (elem in y) == contains_operator(y, elem)
def contains_operator(y, elem):
def contains_operator(y, elem) -> bool:
if hasattr(elem, "type_check"):
return elem.type_check(y)
# 1 in Int
Expand Down

0 comments on commit d8835fd

Please sign in to comment.