fix: import bugs

This commit is contained in:
Shunsuke Shibayama 2023-05-05 17:32:51 +09:00
parent 192b583678
commit ffb7e4bbba
4 changed files with 44 additions and 20 deletions

View file

@ -350,6 +350,19 @@ impl Input {
} }
} }
pub fn try_read(&mut self) -> std::io::Result<String> {
match &mut self.kind {
InputKind::File(filename) => {
let file = File::open(filename)?;
read_file(file)
}
InputKind::Pipe(s) | InputKind::Str(s) => Ok(s.clone()),
InputKind::REPL => Ok(GLOBAL_STDIN.read()),
InputKind::DummyREPL(dummy) => Ok(dummy.read_line()),
InputKind::Dummy => panic!("cannot read from a dummy file"),
}
}
pub fn read_non_dummy(&self) -> String { pub fn read_non_dummy(&self) -> String {
match &self.kind { match &self.kind {
InputKind::File(filename) => { InputKind::File(filename) => {

View file

@ -51,7 +51,7 @@ use super::instantiate::TyVarCache;
use super::instantiate_spec::ParamKind; use super::instantiate_spec::ParamKind;
pub fn valid_mod_name(name: &str) -> bool { pub fn valid_mod_name(name: &str) -> bool {
!name.is_empty() && name.trim() == name !name.is_empty() && !name.starts_with('/') && name.trim() == name
} }
/// format: /// format:
@ -1762,24 +1762,28 @@ impl Context {
} }
} }
fn import_erg_mod(&self, __name__: &Str, loc: &impl Locational) -> CompileResult<PathBuf> { fn import_err(&self, __name__: &Str, loc: &impl Locational) -> TyCheckErrors {
let mod_cache = self.mod_cache(); let mod_cache = self.mod_cache();
let py_mod_cache = self.py_mod_cache(); let py_mod_cache = self.py_mod_cache();
TyCheckErrors::from(TyCheckError::import_error(
self.cfg.input.clone(),
line!() as usize,
format!("module {__name__} not found"),
loc.loc(),
self.caused_by(),
self.similar_builtin_erg_mod_name(__name__)
.or_else(|| mod_cache.get_similar_name(__name__)),
self.similar_builtin_py_mod_name(__name__)
.or_else(|| py_mod_cache.get_similar_name(__name__)),
))
}
fn import_erg_mod(&self, __name__: &Str, loc: &impl Locational) -> CompileResult<PathBuf> {
let mod_cache = self.mod_cache();
let path = match Self::resolve_real_path(&self.cfg, Path::new(&__name__[..])) { let path = match Self::resolve_real_path(&self.cfg, Path::new(&__name__[..])) {
Some(path) => path, Some(path) => path,
None => { None => {
let err = TyCheckErrors::from(TyCheckError::import_error( return Err(self.import_err(__name__, loc));
self.cfg.input.clone(),
line!() as usize,
format!("module {__name__} not found"),
loc.loc(),
self.caused_by(),
self.similar_builtin_erg_mod_name(__name__)
.or_else(|| mod_cache.get_similar_name(__name__)),
self.similar_builtin_py_mod_name(__name__)
.or_else(|| py_mod_cache.get_similar_name(__name__)),
));
return Err(err);
} }
}; };
if let Some(referrer) = self.cfg.input.path() { if let Some(referrer) = self.cfg.input.path() {
@ -1790,7 +1794,10 @@ impl Context {
return Ok(path); return Ok(path);
} }
let mut cfg = self.cfg.inherit(path.clone()); let mut cfg = self.cfg.inherit(path.clone());
let src = cfg.input.read(); let src = cfg
.input
.try_read()
.map_err(|_| self.import_err(__name__, loc))?;
let mut builder = let mut builder =
HIRBuilder::new_with_cache(cfg, __name__, self.shared.as_ref().unwrap().clone()); HIRBuilder::new_with_cache(cfg, __name__, self.shared.as_ref().unwrap().clone());
match builder.build(src, "exec") { match builder.build(src, "exec") {
@ -1942,7 +1949,10 @@ impl Context {
return Ok(path); return Ok(path);
} }
let mut cfg = self.cfg.inherit(path.clone()); let mut cfg = self.cfg.inherit(path.clone());
let src = cfg.input.read(); let src = cfg
.input
.try_read()
.map_err(|_| self.import_err(__name__, loc))?;
let mut builder = HIRBuilder::new_with_cache( let mut builder = HIRBuilder::new_with_cache(
cfg, cfg,
self.mod_name(&path), self.mod_name(&path),

View file

@ -40,7 +40,7 @@ impl ASTLowerer {
} else { } else {
None None
}; };
let chunk = self.declare_chunk(body.block.remove(0))?; let chunk = self.declare_chunk(body.block.remove(0), true)?;
let py_name = if let hir::Expr::TypeAsc(tasc) = &chunk { let py_name = if let hir::Expr::TypeAsc(tasc) = &chunk {
enum_unwrap!(tasc.expr.as_ref(), hir::Expr::Accessor) enum_unwrap!(tasc.expr.as_ref(), hir::Expr::Accessor)
.local_name() .local_name()
@ -645,12 +645,13 @@ impl ASTLowerer {
} }
} }
fn declare_chunk(&mut self, expr: ast::Expr) -> LowerResult<hir::Expr> { fn declare_chunk(&mut self, expr: ast::Expr, allow_acc: bool) -> LowerResult<hir::Expr> {
log!(info "entered {}", fn_name!()); log!(info "entered {}", fn_name!());
match expr { match expr {
ast::Expr::Literal(lit) if lit.is_doc_comment() => { ast::Expr::Literal(lit) if lit.is_doc_comment() => {
Ok(hir::Expr::Lit(self.lower_literal(lit)?)) Ok(hir::Expr::Lit(self.lower_literal(lit)?))
} }
ast::Expr::Accessor(acc) if allow_acc => Ok(hir::Expr::Accessor(self.lower_acc(acc)?)),
ast::Expr::Def(def) => Ok(hir::Expr::Def(self.declare_def(def)?)), ast::Expr::Def(def) => Ok(hir::Expr::Def(self.declare_def(def)?)),
ast::Expr::TypeAscription(tasc) => Ok(hir::Expr::TypeAsc(self.declare_ident(tasc)?)), ast::Expr::TypeAscription(tasc) => Ok(hir::Expr::TypeAsc(self.declare_ident(tasc)?)),
ast::Expr::Call(call) ast::Expr::Call(call)
@ -674,7 +675,7 @@ impl ASTLowerer {
let mut module = hir::Module::with_capacity(ast.module.len()); let mut module = hir::Module::with_capacity(ast.module.len());
let _ = self.module.context.preregister(ast.module.block()); let _ = self.module.context.preregister(ast.module.block());
for chunk in ast.module.into_iter() { for chunk in ast.module.into_iter() {
match self.declare_chunk(chunk) { match self.declare_chunk(chunk, false) {
Ok(chunk) => { Ok(chunk) => {
module.push(chunk); module.push(chunk);
} }

View file

@ -591,7 +591,7 @@ impl ASTLowerer {
)) ))
} }
fn lower_acc(&mut self, acc: ast::Accessor) -> LowerResult<hir::Accessor> { pub(crate) fn lower_acc(&mut self, acc: ast::Accessor) -> LowerResult<hir::Accessor> {
log!(info "entered {}({acc})", fn_name!()); log!(info "entered {}({acc})", fn_name!());
match acc { match acc {
ast::Accessor::Ident(ident) => { ast::Accessor::Ident(ident) => {