From ec4d668189b984f354b98b25ade4d4b1256d42c2 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Mon, 19 Dec 2022 21:02:01 +0900 Subject: [PATCH] Fix #296 --- .cargo/config.toml | 4 ++-- compiler/erg_compiler/codegen.rs | 14 +++++--------- compiler/erg_compiler/ty/codeobj.rs | 2 +- src/dummy.rs | 21 ++++++++++++++------- tests/common.rs | 4 +++- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 73f2b503..f5896695 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -58,5 +58,5 @@ bd_zh_cn_re = "build --features debug --features simplified_chinese --release" bd_zh_tw_re = "build --features debug --features traditional_chinese --release" # static linking to libc -[target.'cfg(unix)'] -rustflags = ["-C", "target-feature=+crt-static"] +# [target.'cfg(unix)'] +# rustflags = ["-C", "target-feature=+crt-static"] diff --git a/compiler/erg_compiler/codegen.rs b/compiler/erg_compiler/codegen.rs index 7bd658d6..b198a791 100644 --- a/compiler/erg_compiler/codegen.rs +++ b/compiler/erg_compiler/codegen.rs @@ -328,6 +328,7 @@ impl PyCodeGenerator { #[inline] fn edit_code(&mut self, idx: usize, arg: usize) { + log!(err "editing: {idx} {arg}"); match u8::try_from(arg) { Ok(u8code) => { *self.mut_cur_block_codeobj().code.get_mut(idx).unwrap() = u8code; @@ -660,14 +661,6 @@ impl PyCodeGenerator { .local_search(&escaped, Name) .unwrap_or_else(|| self.register_name(escaped)); let instr = self.select_load_instr(name.kind, Name); - /*let null_idx = self.cur_block_codeobj().code.len() - 2; - if instr == LOAD_GLOBAL - && self.cur_block_codeobj().code.get(null_idx) == Some(&(Opcode311::PUSH_NULL as u8)) - { - self.mut_cur_block_codeobj().code.pop(); - self.mut_cur_block_codeobj().code.pop(); - self.mut_cur_block().lasti -= 2; - }*/ self.write_instr(instr); self.write_arg(name.idx); self.stack_inc(); @@ -2879,8 +2872,11 @@ impl PyCodeGenerator { let freevars_len = self.cur_block_codeobj().freevars.len(); if freevars_len > 0 { self.mut_cur_block_codeobj().flags += CodeObjFlags::Nested as u32; - self.edit_code(idx_copy_free_vars + 1, freevars_len); + if self.py_version.minor >= Some(11) { + self.edit_code(idx_copy_free_vars + 1, freevars_len); + } } else if self.py_version.minor >= Some(11) { + // cancel copying let code = self.cur_block_codeobj().code.get(idx_copy_free_vars); debug_assert_eq!(code, Some(&(Opcode311::COPY_FREE_VARS as u8))); self.edit_code(idx_copy_free_vars, CommonOpcode::NOP as usize); diff --git a/compiler/erg_compiler/ty/codeobj.rs b/compiler/erg_compiler/ty/codeobj.rs index 15646efa..1262bd58 100644 --- a/compiler/erg_compiler/ty/codeobj.rs +++ b/compiler/erg_compiler/ty/codeobj.rs @@ -313,7 +313,7 @@ impl CodeObj { pub fn into_bytes(self, python_ver: PythonVersion) -> Vec { let mut bytes = vec![DataTypePrefix::Code as u8]; bytes.append(&mut self.argcount.to_le_bytes().to_vec()); - if python_ver.minor >= Some(10) { + if python_ver.minor >= Some(8) { bytes.append(&mut self.posonlyargcount.to_le_bytes().to_vec()); } bytes.append(&mut self.kwonlyargcount.to_le_bytes().to_vec()); diff --git a/src/dummy.rs b/src/dummy.rs index 3ec485f9..75be6e60 100644 --- a/src/dummy.rs +++ b/src/dummy.rs @@ -1,6 +1,7 @@ use std::fs::remove_file; use std::io::{Read, Write}; use std::net::{Ipv4Addr, SocketAddrV4, TcpListener, TcpStream}; +use std::process; use std::thread::sleep; use std::time::Duration; @@ -94,7 +95,10 @@ impl Runnable for DummyVM { fn finish(&mut self) { if let Some(stream) = &mut self.stream { - stream.write_all("exit".as_bytes()).unwrap(); + if let Err(err) = stream.write_all("exit".as_bytes()) { + eprintln!("Write error: {err}"); + process::exit(1); + } let mut buf = [0; 1024]; match stream.read(&mut buf) { Result::Ok(n) => { @@ -103,8 +107,9 @@ impl Runnable for DummyVM { println!("The REPL server is closed."); } } - Result::Err(e) => { - panic!("{}", format!("Read error: {e}")); + Result::Err(err) => { + eprintln!("Read error: {err}"); + process::exit(1); } } remove_file("o.pyc").unwrap_or(()); @@ -156,15 +161,17 @@ impl Runnable for DummyVM { } s.to_string() } - Result::Err(e) => { + Result::Err(err) => { self.finish(); - panic!("{}", format!("Read error: {e}")); + eprintln!("Read error: {err}"); + process::exit(1); } } } - Result::Err(e) => { + Result::Err(err) => { self.finish(); - panic!("{}", format!("Sending error: {e}")) + eprintln!("Sending error: {err}"); + process::exit(1); } }; if res.ends_with("None") { diff --git a/tests/common.rs b/tests/common.rs index 649383fb..ecd3948f 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -70,7 +70,9 @@ fn _exec_vm(file_path: &'static str) -> Result { } else { Some("python3") }; - // cfg.target_version = Some(PythonVersion::new(3, Some(10), Some(6))); // your Python's version + // cfg.target_version = Some(PythonVersion::new(3, Some(8), Some(10))); // your Python's version + // cfg.py_magic_num = Some(3413); // in (most) 3.8.x + // cfg.target_version = Some(PythonVersion::new(3, Some(10), Some(6))); // cfg.py_magic_num = Some(3439); // in (most) 3.10.x cfg.target_version = Some(PythonVersion::new(3, Some(11), Some(0))); cfg.py_magic_num = Some(3495); // in 3.11.0