Handle import errors

This commit is contained in:
Shunsuke Shibayama 2022-09-22 22:02:59 +09:00
parent 9c967d32f6
commit a5a0324883
2 changed files with 21 additions and 11 deletions

View file

@ -1,7 +1,6 @@
use std::option::Option; use std::option::Option;
use std::path::PathBuf; // conflicting to Type::Option use std::path::PathBuf; // conflicting to Type::Option
use erg_common::color::{RED, RESET};
use erg_common::config::{ErgConfig, Input}; use erg_common::config::{ErgConfig, Input};
use erg_common::error::MultiErrorDisplay; use erg_common::error::MultiErrorDisplay;
use erg_common::traits::{Locational, Stream}; use erg_common::traits::{Locational, Stream};
@ -22,7 +21,7 @@ use crate::build_hir::HIRBuilder;
use crate::context::{ClassDefType, Context, DefaultInfo, RegistrationMode, TraitInstance}; use crate::context::{ClassDefType, Context, DefaultInfo, RegistrationMode, TraitInstance};
use crate::error::readable_name; use crate::error::readable_name;
use crate::error::{TyCheckError, TyCheckResult}; use crate::error::{TyCheckError, TyCheckResult};
use crate::hir; use crate::hir::{self, Literal};
use crate::mod_cache::SharedModuleCache; use crate::mod_cache::SharedModuleCache;
use crate::varinfo::{Mutability, ParamIdx, VarInfo, VarKind}; use crate::varinfo::{Mutability, ParamIdx, VarInfo, VarKind};
use Mutability::*; use Mutability::*;
@ -820,8 +819,9 @@ impl Context {
current_input, current_input,
var_name, var_name,
__name__, __name__,
lit,
mod_cache, mod_cache,
), )?,
} }
} else { } else {
// maybe unreachable // maybe unreachable
@ -857,8 +857,9 @@ impl Context {
current_input: Input, current_input: Input,
var_name: &VarName, var_name: &VarName,
__name__: Str, __name__: Str,
name_lit: &Literal,
mod_cache: &SharedModuleCache, mod_cache: &SharedModuleCache,
) { ) -> TyCheckResult<()> {
let mut dir = if let Input::File(mut path) = current_input { let mut dir = if let Input::File(mut path) = current_input {
path.pop(); path.pop();
path path
@ -867,13 +868,17 @@ impl Context {
}; };
dir.push(format!("{__name__}.er")); dir.push(format!("{__name__}.er"));
// TODO: returns an error // TODO: returns an error
let path = dir.canonicalize().unwrap_or_else(|err| { let path = match dir.canonicalize() {
eprintln!( Ok(path) => path,
"failed to open {RED}{}{RESET}: {err}", Err(err) => {
dir.to_string_lossy() return Err(TyCheckError::file_error(
); line!() as usize,
std::process::exit(err.raw_os_error().unwrap_or(1)); err.to_string(),
}); name_lit.loc(),
self.caused_by(),
));
}
};
let cfg = ErgConfig { let cfg = ErgConfig {
input: Input::File(path), input: Input::File(path),
..ErgConfig::default() ..ErgConfig::default()
@ -888,6 +893,7 @@ impl Context {
errs.fmt_all_stderr(); errs.fmt_all_stderr();
} }
} }
Ok(())
} }
pub(crate) fn _push_subtype_bound(&mut self, sub: Type, sup: Type) { pub(crate) fn _push_subtype_bound(&mut self, sub: Type, sup: Type) {

View file

@ -1292,6 +1292,10 @@ passed keyword args: {RED}{kw_args_len}{RESET}"
caused_by, caused_by,
) )
} }
pub fn file_error(errno: usize, desc: String, loc: Location, caused_by: AtomicStr) -> Self {
Self::new(ErrorCore::new(errno, IoError, loc, desc, None), caused_by)
}
} }
#[derive(Debug)] #[derive(Debug)]