Skip to content

Commit 493492d

Browse files
authored
Merge pull request #371 from erg-lang/fix-370
Fix #370
2 parents 44781cb + e246fad commit 493492d

File tree

9 files changed

+110
-72
lines changed

9 files changed

+110
-72
lines changed

crates/erg_common/config.rs

Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::str::FromStr;
1212
use crate::help_messages::{command_message, mode_message};
1313
use crate::normalize_path;
1414
use crate::python_util::{detect_magic_number, get_python_version, PythonVersion};
15+
use crate::random::random;
1516
use crate::serialize::{get_magic_num_from_bytes, get_ver_from_magic_num};
1617
use crate::stdin::GLOBAL_STDIN;
1718
use crate::{power_assert, read_file};
@@ -111,18 +112,30 @@ impl DummyStdin {
111112
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
112113
pub enum Input {
113114
File(PathBuf),
114-
REPL,
115+
REPL(u64),
115116
DummyREPL(DummyStdin),
116117
/// same content as cfg.command
117-
Pipe(String),
118+
Pipe(u64, String),
118119
/// from command option | eval
119-
Str(String),
120+
Str(u64, String),
120121
Dummy,
121122
}
122123

123124
impl Input {
124-
pub fn is_repl(&self) -> bool {
125-
matches!(self, Input::REPL | Input::DummyREPL(_))
125+
pub const fn is_repl(&self) -> bool {
126+
matches!(self, Input::REPL(_) | Input::DummyREPL(_))
127+
}
128+
129+
pub fn pipe(src: String) -> Self {
130+
Self::Pipe(random(), src)
131+
}
132+
133+
pub fn str(src: String) -> Self {
134+
Self::Str(random(), src)
135+
}
136+
137+
pub fn repl() -> Self {
138+
Self::REPL(random())
126139
}
127140

128141
pub fn path(&self) -> Option<&Path> {
@@ -132,40 +145,57 @@ impl Input {
132145
}
133146
}
134147

148+
pub const fn id(&self) -> u64 {
149+
match self {
150+
Input::File(_) | Input::DummyREPL(_) | Input::Dummy => 0,
151+
Input::REPL(id) | Input::Pipe(id, _) | Input::Str(id, _) => *id,
152+
}
153+
}
154+
135155
pub fn enclosed_name(&self) -> &str {
136156
match self {
137157
Self::File(filename) => filename.to_str().unwrap_or("_"),
138-
Self::REPL | Self::DummyREPL(_) | Self::Pipe(_) => "<stdin>",
139-
Self::Str(_) => "<string>",
158+
Self::REPL(_) | Self::DummyREPL(_) | Self::Pipe(_, _) => "<stdin>",
159+
Self::Str(_, _) => "<string>",
140160
Self::Dummy => "<dummy>",
141161
}
142162
}
143163

144-
pub fn full_path(&self) -> &str {
164+
pub fn full_path(&self) -> PathBuf {
145165
match self {
146-
Self::File(filename) => filename.to_str().unwrap_or("_"),
147-
Self::REPL | Self::DummyREPL(_) | Self::Pipe(_) => "stdin",
148-
Self::Str(_) => "string",
149-
Self::Dummy => "dummy",
166+
Self::File(filename) => filename.clone(),
167+
Self::REPL(id) | Self::Pipe(id, _) => PathBuf::from(format!("stdin_{id}")),
168+
Self::DummyREPL(dummy) => PathBuf::from(format!("stdin_{}", dummy.name)),
169+
Self::Str(id, _) => PathBuf::from(format!("string_{id}")),
170+
Self::Dummy => PathBuf::from("dummy"),
150171
}
151172
}
152173

153-
pub fn file_stem(&self) -> &str {
174+
pub fn file_stem(&self) -> String {
154175
match self {
155-
Self::File(filename) => filename.file_stem().and_then(|f| f.to_str()).unwrap_or("_"),
156-
Self::REPL | Self::DummyREPL(_) | Self::Pipe(_) => "stdin",
157-
Self::Str(_) => "string",
158-
Self::Dummy => "dummy",
176+
Self::File(filename) => filename
177+
.file_stem()
178+
.and_then(|f| f.to_str())
179+
.unwrap_or("_")
180+
.to_string(),
181+
Self::REPL(id) | Self::Pipe(id, _) => format!("stdin_{id}"),
182+
Self::DummyREPL(stdin) => format!("stdin_{}", stdin.name),
183+
Self::Str(id, _) => format!("string_{id}"),
184+
Self::Dummy => "dummy".to_string(),
159185
}
160186
}
161187

162-
pub fn filename(&self) -> &str {
188+
pub fn filename(&self) -> String {
163189
match self {
164-
Self::File(filename) => filename.file_name().and_then(|f| f.to_str()).unwrap_or("_"),
165-
Self::REPL | Self::Pipe(_) => "stdin",
166-
Self::DummyREPL(stdin) => &stdin.name,
167-
Self::Str(_) => "string",
168-
Self::Dummy => "dummy",
190+
Self::File(filename) => filename
191+
.file_name()
192+
.and_then(|f| f.to_str())
193+
.unwrap_or("_")
194+
.to_string(),
195+
Self::REPL(id) | Self::Pipe(id, _) => format!("stdin_{id}"),
196+
Self::DummyREPL(stdin) => format!("stdin_{}", stdin.name),
197+
Self::Str(id, _) => format!("string_{id}"),
198+
Self::Dummy => "dummy".to_string(),
169199
}
170200
}
171201

@@ -193,8 +223,8 @@ impl Input {
193223
}
194224
}
195225
}
196-
Self::Pipe(s) | Self::Str(s) => s.clone(),
197-
Self::REPL => GLOBAL_STDIN.read(),
226+
Self::Pipe(_, s) | Self::Str(_, s) => s.clone(),
227+
Self::REPL(_) => GLOBAL_STDIN.read(),
198228
Self::DummyREPL(dummy) => dummy.read_line().unwrap_or_default(),
199229
Self::Dummy => panic!("cannot read from a dummy file"),
200230
}
@@ -224,8 +254,8 @@ impl Input {
224254
}
225255
}
226256
}
227-
Self::Pipe(s) | Self::Str(s) => s.clone(),
228-
Self::REPL => GLOBAL_STDIN.read(),
257+
Self::Pipe(_, s) | Self::Str(_, s) => s.clone(),
258+
Self::REPL(_) => GLOBAL_STDIN.read(),
229259
Self::Dummy | Self::DummyREPL(_) => panic!("cannot read from a dummy file"),
230260
}
231261
}
@@ -244,12 +274,12 @@ impl Input {
244274
}
245275
Err(_) => vec!["<file not found>".into()],
246276
},
247-
Self::Pipe(s) | Self::Str(s) => s.split('\n').collect::<Vec<_>>()
277+
Self::Pipe(_, s) | Self::Str(_, s) => s.split('\n').collect::<Vec<_>>()
248278
[ln_begin - 1..=ln_end - 1]
249279
.iter()
250280
.map(|s| s.to_string())
251281
.collect(),
252-
Self::REPL => GLOBAL_STDIN.reread_lines(ln_begin, ln_end),
282+
Self::REPL(_) => GLOBAL_STDIN.reread_lines(ln_begin, ln_end),
253283
Self::DummyREPL(dummy) => dummy.reread_lines(ln_begin, ln_end),
254284
Self::Dummy => panic!("cannot read lines from a dummy file"),
255285
}
@@ -258,8 +288,8 @@ impl Input {
258288
pub fn reread(&self) -> String {
259289
match self {
260290
Self::File(_filename) => todo!(),
261-
Self::Pipe(s) | Self::Str(s) => s.clone(),
262-
Self::REPL => GLOBAL_STDIN.reread().trim_end().to_owned(),
291+
Self::Pipe(_, s) | Self::Str(_, s) => s.clone(),
292+
Self::REPL(_) => GLOBAL_STDIN.reread().trim_end().to_owned(),
263293
Self::DummyREPL(dummy) => dummy.reread().unwrap_or_default(),
264294
Self::Dummy => panic!("cannot read from a dummy file"),
265295
}
@@ -366,7 +396,7 @@ impl Default for ErgConfig {
366396
py_server_timeout: 10,
367397
quiet_repl: false,
368398
show_type: false,
369-
input: Input::REPL,
399+
input: Input::repl(),
370400
output_dir: None,
371401
module: "<module>",
372402
verbose: 1,
@@ -393,29 +423,26 @@ impl ErgConfig {
393423
self.clone()
394424
}
395425

396-
pub fn dump_path(&self) -> String {
426+
pub fn dump_path(&self) -> PathBuf {
397427
if let Some(output) = &self.output_dir {
398-
format!("{output}/{}", self.input.filename())
428+
PathBuf::from(format!("{output}/{}", self.input.filename()))
399429
} else {
400-
self.input.full_path().to_string()
430+
self.input.full_path()
401431
}
402432
}
403433

404434
pub fn dump_filename(&self) -> String {
405435
if let Some(output) = &self.output_dir {
406436
format!("{output}/{}", self.input.filename())
407437
} else {
408-
self.input.filename().to_string()
438+
self.input.filename()
409439
}
410440
}
411441

412-
pub fn dump_pyc_path(&self) -> String {
413-
let dump_path = self.dump_path();
414-
if dump_path.ends_with(".er") {
415-
dump_path.replace(".er", ".pyc")
416-
} else {
417-
dump_path + ".pyc"
418-
}
442+
pub fn dump_pyc_path(&self) -> PathBuf {
443+
let mut dump_path = self.dump_path();
444+
dump_path.set_extension(".pyc");
445+
dump_path
419446
}
420447

421448
pub fn dump_pyc_filename(&self) -> String {
@@ -450,7 +477,7 @@ impl ErgConfig {
450477
break;
451478
}
452479
"-c" | "--code" => {
453-
cfg.input = Input::Str(args.next().expect("the value of `-c` is not passed"));
480+
cfg.input = Input::str(args.next().expect("the value of `-c` is not passed"));
454481
}
455482
"--check" => {
456483
cfg.mode = ErgMode::FullCheck;
@@ -630,15 +657,15 @@ USAGE:
630657
}
631658
}
632659
}
633-
if cfg.input == Input::REPL && cfg.mode != ErgMode::LanguageServer {
660+
if cfg.input.is_repl() && cfg.mode != ErgMode::LanguageServer {
634661
use crate::tty::IsTty;
635662
let is_stdin_piped = !stdin().is_tty();
636663
let input = if is_stdin_piped {
637664
let mut buffer = String::new();
638665
stdin().read_to_string(&mut buffer).unwrap();
639-
Input::Pipe(buffer)
666+
Input::pipe(buffer)
640667
} else {
641-
Input::REPL
668+
Input::repl()
642669
};
643670
cfg.input = input;
644671
}

crates/erg_common/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub mod opcode308;
2121
pub mod opcode310;
2222
pub mod opcode311;
2323
pub mod python_util;
24+
pub mod random;
2425
pub mod serialize;
2526
pub mod set;
2627
pub mod shared;

crates/erg_common/random.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use std::collections::hash_map::RandomState;
2+
use std::hash::{BuildHasher, Hasher};
3+
4+
pub fn random() -> u64 {
5+
let state = RandomState::new();
6+
state.build_hasher().finish()
7+
}

crates/erg_common/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,8 @@ pub trait Runnable: Sized + Default {
577577
let mut num_errors = 0;
578578
let mut instance = Self::new(cfg);
579579
let res = match instance.input() {
580-
Input::File(_) | Input::Pipe(_) | Input::Str(_) => instance.exec(),
581-
Input::REPL | Input::DummyREPL(_) => {
580+
Input::File(_) | Input::Pipe(_, _) | Input::Str(_, _) => instance.exec(),
581+
Input::REPL(_) | Input::DummyREPL(_) => {
582582
let output = stdout();
583583
let mut output = BufWriter::new(output.lock());
584584
if !quiet_repl {

crates/erg_compiler/build_hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl Runnable for HIRBuilder {
8383

8484
impl Buildable for HIRBuilder {
8585
fn inherit(cfg: ErgConfig, shared: SharedCompilerResource) -> Self {
86-
let mod_name = Str::rc(cfg.input.file_stem());
86+
let mod_name = Str::rc(&cfg.input.file_stem());
8787
Self::new_with_cache(cfg, mod_name, shared)
8888
}
8989
fn build(&mut self, src: String, mode: &str) -> Result<CompleteArtifact, IncompleteArtifact> {

0 commit comments

Comments
 (0)