Merge pull request #444 from erg-lang/fix-443

Fix #443
This commit is contained in:
Shunsuke Shibayama 2023-07-26 20:45:23 +09:00 committed by GitHub
commit 17b44b1fa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 177 additions and 145 deletions

View file

@ -93,7 +93,7 @@ impl Runnable for HIRBuilder {
impl Buildable for HIRBuilder {
fn inherit(cfg: ErgConfig, shared: SharedCompilerResource) -> Self {
let mod_name = Str::rc(cfg.input.unescaped_file_stem());
let mod_name = Str::from(cfg.input.file_stem());
Self::new_with_cache(cfg, mod_name, shared)
}
fn build(&mut self, src: String, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> {

View file

@ -55,7 +55,7 @@ impl Context {
/// Get the context of the module. If it was in analysis, wait until analysis is complete and join the thread.
/// If you only want to know if the module is registered, use `mod_registered`.
pub(crate) fn get_mod_with_path(&self, path: &Path) -> Option<&Context> {
if self.module_path() == Some(path) {
if self.module_path() == path {
return self.get_module();
}
if self.shared.is_some()

View file

@ -976,12 +976,12 @@ impl Context {
)
}
pub(crate) fn module_path(&self) -> Option<&Path> {
pub(crate) fn module_path(&self) -> &Path {
self.cfg.input.path()
}
pub(crate) fn absolutize(&self, loc: Location) -> AbsLocation {
AbsLocation::new(self.module_path().map(PathBuf::from), loc)
AbsLocation::new(Some(PathBuf::from(self.module_path())), loc)
}
#[inline]
@ -1049,7 +1049,7 @@ impl Context {
pub(crate) fn _get_module_from_stack(&self, path: &Path) -> Option<&Context> {
self.get_outer().and_then(|outer| {
if outer.kind == ContextKind::Module && outer.module_path() == Some(path) {
if outer.kind == ContextKind::Module && outer.module_path() == path {
Some(outer)
} else {
outer._get_module_from_stack(path)

View file

@ -1854,10 +1854,9 @@ impl Context {
if ERG_MODE {
self.check_mod_vis(path.as_path(), __name__, loc)?;
}
if let Some(referrer) = self.cfg.input.path() {
if self.shared().graph.inc_ref(referrer, path.clone()).is_err() {
self.build_cyclic_mod(&path);
}
let referrer = self.cfg.input.path();
if self.shared().graph.inc_ref(referrer, path.clone()).is_err() {
self.build_cyclic_mod(&path);
}
self.build_erg_mod(path, __name__, loc)
}
@ -2123,7 +2122,7 @@ impl Context {
fn try_gen_py_decl_file(&self, __name__: &Str) -> Result<PathBuf, ()> {
if let Ok(path) = self.cfg.input.resolve_py(Path::new(&__name__[..])) {
if self.cfg.input.path() == Some(path.as_path()) {
if self.cfg.input.path() == path.as_path() {
return Ok(path);
}
let (out, err) = if self.cfg.mode == ErgMode::LanguageServer || self.cfg.quiet_repl {
@ -2163,13 +2162,12 @@ impl Context {
let py_mod_cache = self.py_mod_cache();
let path = self.get_decl_path(__name__, loc)?;
// module itself
if self.cfg.input.path() == Some(path.as_path()) {
if self.cfg.input.path() == path.as_path() {
return Ok(path);
}
if let Some(referrer) = self.cfg.input.path() {
if self.shared().graph.inc_ref(referrer, path.clone()).is_err() {
self.build_cyclic_mod(&path);
}
let referrer = self.cfg.input.path();
if self.shared().graph.inc_ref(referrer, path.clone()).is_err() {
self.build_cyclic_mod(&path);
}
if py_mod_cache.get(&path).is_some() {
return Ok(path);

View file

@ -383,7 +383,7 @@ impl<'a> HIRLinker<'a> {
// ↓
// # module.er
// self = __import__(__name__)
if matches!((path.canonicalize(), self.cfg.input.unescaped_path().canonicalize()), (Ok(l), Ok(r)) if l == r)
if matches!((path.canonicalize(), self.cfg.input.path().canonicalize()), (Ok(l), Ok(r)) if l == r)
{
*expr = Self::self_module();
return;

View file

@ -173,7 +173,7 @@ impl ASTLowerer {
}
let self_path = self.module.context.module_path();
for (referee, value) in self.module.context.index().members().iter() {
if referee.module.as_deref() != self_path {
if referee.module.as_deref() != Some(self_path) {
continue;
}
let name_is_auto = &value.name[..] == "_"

View file

@ -2490,10 +2490,9 @@ impl ASTLowerer {
pub fn lower(&mut self, ast: AST, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> {
log!(info "the AST lowering process has started.");
log!(info "the type-checking process has started.");
if let Some(path) = self.cfg.input.path() {
let graph = &self.module.context.shared().graph;
graph.add_node_if_none(path);
}
let path = self.cfg.input.path();
let graph = &self.module.context.shared().graph;
graph.add_node_if_none(path);
let ast = ASTLinker::new(self.cfg.clone())
.link(ast, mode)
.map_err(|errs| {

View file

@ -39,9 +39,7 @@ impl SharedCompilerResource {
trait_impls: SharedTraitImpls::new(),
promises: SharedPromises::new(
graph,
cfg.input
.path()
.map_or(PathBuf::default(), |p| p.canonicalize().unwrap_or_default()),
cfg.input.path().canonicalize().unwrap_or_default(),
),
errors: SharedCompileErrors::new(),
warns: SharedCompileWarnings::new(),

View file

@ -3,8 +3,10 @@ use std::fmt::Write as _;
use std::fs::File;
use std::io::{BufReader, Read, Write as _};
use std::path::Path;
use std::process::ExitStatus;
use erg_common::impl_display_from_debug;
use erg_common::io::Output;
#[allow(unused_imports)]
use erg_common::log;
use erg_common::opcode::CommonOpcode;
@ -12,7 +14,7 @@ use erg_common::opcode308::Opcode308;
use erg_common::opcode309::Opcode309;
use erg_common::opcode310::Opcode310;
use erg_common::opcode311::{BinOpCode, Opcode311};
use erg_common::python_util::{env_magic_number, PythonVersion};
use erg_common::python_util::{env_magic_number, exec_py_code, PythonVersion};
use erg_common::serialize::*;
use erg_common::Str;
@ -439,6 +441,22 @@ impl CodeObj {
Ok(())
}
pub fn executable_code(self, py_magic_num: Option<u32>) -> String {
let mut bytes = Vec::with_capacity(16);
let py_magic_num = py_magic_num.unwrap_or_else(env_magic_number);
let python_ver = get_ver_from_magic_num(py_magic_num);
bytes.append(&mut self.into_bytes(python_ver));
let mut bytecode = "".to_string();
for b in bytes {
write!(bytecode, "\\x{b:0>2x}").unwrap();
}
format!("import marshal; exec(marshal.loads(b'{bytecode}'))")
}
pub fn exec(self, py_magic_num: Option<u32>, output: Output) -> std::io::Result<ExitStatus> {
exec_py_code(&self.executable_code(py_magic_num), output)
}
fn tables_info(&self) -> String {
let mut tables = "".to_string();
if !self.consts.is_empty() {

View file

@ -111,10 +111,7 @@ impl Deserializer {
}
pub fn run(cfg: ErgConfig) -> ExitStatus {
let Some(filename) = cfg.input.path() else {
eprintln!("{:?} is not a filename", cfg.input);
return ExitStatus::ERR1;
};
let filename = cfg.input.path();
match CodeObj::from_pyc(filename) {
Ok(codeobj) => {
println!("{}", codeobj.code_info(None));