Skip to content

Commit 0449346

Browse files
committed
Make hash keys type-strict to match the lookup type.
This is actually a largely simple change - most places only use one type of hash key anyways. The one change is in function creation/execution, where we handle Integer and String literals one level higher in the call stack.
1 parent e2e887d commit 0449346

14 files changed

Lines changed: 701 additions & 562 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/engine_bibtex/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ edition = "2021"
2121

2222
[dependencies]
2323
libc = "^0.2"
24-
slotmap = "1.0"
24+
indexmap = "2.0"
2525
tectonic_io_base = { path = "../io_base", version = '0.0.0-dev.0' }
2626
tectonic_bridge_core = { path = "../bridge_core", version = "0.0.0-dev.0" }
2727
tectonic_errors = { path = "../errors", version = "0.0.0-dev.0" }

crates/engine_bibtex/src/auxi.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::{
44
char_info::LexClass,
55
cite::CiteInfo,
66
exec::print_bst_name,
7-
hash::{HashData, HashExtra},
7+
hash,
8+
hash::{HashData, HashPointer},
89
log::{
910
aux_end1_err_print, aux_end2_err_print, aux_err_illegal_another_print,
1011
aux_err_no_right_brace_print, aux_err_print, aux_err_stuff_after_right_brace_print,
@@ -15,11 +16,10 @@ use crate::{
1516
peekable::PeekableInput,
1617
pool::{StrNumber, StringPool},
1718
scan::Scan,
18-
Bibtex, BibtexError, File, GlobalItems, StrIlk,
19+
Bibtex, BibtexError, File, GlobalItems,
1920
};
2021
use std::ffi::CString;
2122
use tectonic_bridge_core::FileFormat;
22-
use crate::hash::HashPointer;
2323

2424
const AUX_STACK_SIZE: usize = 20;
2525

@@ -106,7 +106,7 @@ fn aux_bib_data_command(
106106

107107
let file = &buffers.buffer(BufTy::Base)
108108
[buffers.offset(BufTy::Base, 1)..buffers.offset(BufTy::Base, 2)];
109-
let res = hash.lookup_str_insert(pool, file, HashExtra::BibFile);
109+
let res = hash.lookup_str_insert::<hash::BibFile>(pool, file, ());
110110
if res.exists {
111111
ctx.write_logs("This database file appears more than once: ");
112112
print_bib_name(ctx, pool, hash.get(res.loc).text())?;
@@ -177,7 +177,7 @@ fn aux_bib_style_command(
177177

178178
let file = &buffers.buffer(BufTy::Base)
179179
[buffers.offset(BufTy::Base, 1)..buffers.offset(BufTy::Base, 2)];
180-
let res = hash.lookup_str_insert(pool, file, HashExtra::BstFile);
180+
let res = hash.lookup_str_insert::<hash::BstFile>(pool, file, ());
181181
if res.exists {
182182
ctx.write_logs("Already encountered style file");
183183
print_confusion(ctx);
@@ -277,20 +277,15 @@ fn aux_citation_command(
277277
let lc_cite = &mut buffers.buffer_mut(BufTy::Ex)[range];
278278
lc_cite.make_ascii_lowercase();
279279

280-
let lc_res = hash.lookup_str_insert(pool, lc_cite, HashExtra::LcCite(HashPointer::default()));
280+
let lc_res = hash.lookup_str_insert::<hash::LcCite>(pool, lc_cite, HashPointer::default());
281281
if lc_res.exists {
282-
let &HashExtra::LcCite(cite_loc) = hash.get(lc_res.loc).extra() else {
283-
panic!("LcCite lookup didn't have LcCite extra");
284-
};
282+
let &cite_loc = hash.get(lc_res.loc).extra();
285283

286284
let cite = &buffers.buffer(BufTy::Base)
287285
[buffers.offset(BufTy::Base, 1)..buffers.offset(BufTy::Base, 2)];
288-
let uc_res = hash.lookup_str(pool, cite, StrIlk::Cite);
286+
let uc_res = hash.lookup_str::<hash::Cite>(pool, cite);
289287
if uc_res.is_none() {
290-
let &HashExtra::Cite(cite) = hash.get(cite_loc).extra() else {
291-
panic!("LcCite location didn't have a Cite extra");
292-
};
293-
288+
let &cite = hash.get(cite_loc).extra();
294289
ctx.write_logs("Case mismatch error between cite keys ");
295290
print_a_token(ctx, buffers);
296291
ctx.write_logs(" and ");
@@ -302,7 +297,7 @@ fn aux_citation_command(
302297
} else {
303298
let cite = &buffers.buffer(BufTy::Base)
304299
[buffers.offset(BufTy::Base, 1)..buffers.offset(BufTy::Base, 2)];
305-
let uc_res = hash.lookup_str_insert(pool, cite, HashExtra::Cite(0));
300+
let uc_res = hash.lookup_str_insert::<hash::Cite>(pool, cite, 0);
306301
if uc_res.exists {
307302
hash_cite_confusion(ctx);
308303
return Err(BibtexError::Fatal);
@@ -313,8 +308,8 @@ fn aux_citation_command(
313308
}
314309

315310
cites.set_cite(cites.ptr(), hash.get(uc_res.loc).text());
316-
*hash.get_mut(uc_res.loc).extra_mut() = HashExtra::Cite(cites.ptr());
317-
*hash.get_mut(lc_res.loc).extra_mut() = HashExtra::LcCite(uc_res.loc);
311+
hash.set_extra(uc_res.loc, cites.ptr());
312+
hash.set_extra(lc_res.loc, uc_res.loc);
318313
cites.set_ptr(cites.ptr() + 1);
319314
}
320315
}
@@ -376,7 +371,7 @@ fn aux_input_command(
376371

377372
let file = &buffers.buffer(BufTy::Base)
378373
[buffers.offset(BufTy::Base, 1)..buffers.offset(BufTy::Base, 2)];
379-
let res = hash.lookup_str_insert(pool, file, HashExtra::AuxFile);
374+
let res = hash.lookup_str_insert::<hash::AuxFile>(pool, file, ());
380375
if res.exists {
381376
ctx.write_logs("Already encountered file ");
382377
print_aux_name(ctx, pool, hash.get(res.loc).text())?;
@@ -421,14 +416,10 @@ pub(crate) fn get_aux_command_and_process(
421416

422417
let line = &globals.buffers.buffer(BufTy::Base)
423418
[globals.buffers.offset(BufTy::Base, 1)..globals.buffers.offset(BufTy::Base, 2)];
424-
let res = globals
425-
.hash
426-
.lookup_str(globals.pool, line, StrIlk::AuxCommand);
419+
let res = globals.hash.lookup_str::<AuxCommand>(globals.pool, line);
427420

428421
if let Some(loc) = res {
429-
let &HashExtra::AuxCommand(cmd) = globals.hash.get(loc).extra() else {
430-
panic!("AuxCommand lookup didn't have AuxCommand extra");
431-
};
422+
let &cmd = globals.hash.get(loc).extra();
432423

433424
match cmd {
434425
AuxCommand::Data => aux_bib_data_command(

crates/engine_bibtex/src/bibs.rs

Lines changed: 38 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use slotmap::Key;
21
use crate::{
32
buffer::{BufTy, GlobalBuffer},
43
char_info::LexClass,
54
cite::add_database_cite,
6-
hash::{BstFn, HashData, HashExtra},
5+
hash,
6+
hash::{BstFn, HashData},
77
log::{
88
bib_equals_sign_print, bib_err_print, bib_id_print, bib_one_of_two_print, bib_warn_print,
99
cite_key_disappeared_confusion, eat_bib_print, hash_cite_confusion, print_a_token,
@@ -12,7 +12,7 @@ use crate::{
1212
peekable::input_ln,
1313
pool::{StrNumber, StringPool},
1414
scan::{scan_and_store_the_field_value_and_eat_white, scan_identifier, Scan, ScanRes},
15-
Bibtex, BibtexError, File, GlobalItems, HashPointer, LookupRes, StrIlk,
15+
Bibtex, BibtexError, File, GlobalItems, HashPointer, LookupRes,
1616
};
1717

1818
#[derive(Copy, Clone, Debug, PartialEq)]
@@ -129,8 +129,8 @@ pub(crate) fn compress_bib_white(
129129
pub(crate) fn get_bib_command_or_entry_and_process(
130130
ctx: &mut Bibtex<'_, '_>,
131131
globals: &mut GlobalItems<'_>,
132-
cur_macro_loc: &mut HashPointer,
133-
field_name_loc: &mut HashPointer,
132+
cur_macro_loc: &mut HashPointer<hash::Macro>,
133+
field_name_loc: &mut HashPointer<BstFn>,
134134
) -> Result<(), BibtexError> {
135135
let mut bib_command = None;
136136

@@ -191,17 +191,12 @@ pub(crate) fn get_bib_command_or_entry_and_process(
191191
let bib_cmd = &mut globals.buffers.buffer_mut(BufTy::Base)[range];
192192
bib_cmd.make_ascii_lowercase();
193193

194-
let res = globals
195-
.hash
196-
.lookup_str(globals.pool, bib_cmd, StrIlk::BibCommand);
194+
let res = globals.hash.lookup_str::<BibCommand>(globals.pool, bib_cmd);
197195

198196
let mut lc_cite_loc = HashPointer::default();
199197

200198
if let Some(loc) = res {
201-
let &HashExtra::BibCommand(cmd) = globals.hash.get(loc).extra() else {
202-
panic!("BibCommand lookup didn't have BibCommand extra");
203-
};
204-
199+
let &cmd = globals.hash.get(loc).extra();
205200
bib_command = Some(cmd);
206201
match cmd {
207202
BibCommand::Comment => (),
@@ -352,13 +347,14 @@ pub(crate) fn get_bib_command_or_entry_and_process(
352347
bib_macro.make_ascii_lowercase();
353348

354349
// let text = globals.hash.text(res.loc);
355-
let res = globals.hash.lookup_str_insert(
356-
globals.pool,
357-
bib_macro,
358-
HashExtra::Macro(StrNumber::default()),
359-
);
350+
let res =
351+
globals
352+
.hash
353+
.lookup_str_insert(globals.pool, bib_macro, StrNumber::default());
360354
// TODO: Insert overwriting?
361-
*globals.hash.get_mut(res.loc).extra_mut() = HashExtra::Macro(globals.hash.get(res.loc).text());
355+
globals
356+
.hash
357+
.set_extra(res.loc, globals.hash.get(res.loc).text());
362358
*cur_macro_loc = res.loc;
363359

364360
if !eat_bib_white_space(ctx, globals.buffers, globals.bibs) {
@@ -441,13 +437,8 @@ pub(crate) fn get_bib_command_or_entry_and_process(
441437
let bst_fn = &mut globals.buffers.buffer_mut(BufTy::Base)[range];
442438
let bst_res = globals
443439
.hash
444-
.lookup_str(globals.pool, bst_fn, StrIlk::BstFn)
445-
.filter(|&loc| {
446-
matches!(
447-
globals.hash.get(loc).extra(),
448-
HashExtra::BstFn(BstFn::Wizard(_))
449-
)
450-
});
440+
.lookup_str::<BstFn>(globals.pool, bst_fn)
441+
.filter(|&loc| matches!(globals.hash.get(loc).extra(), BstFn::Wizard(_),));
451442

452443
if !eat_bib_white_space(ctx, globals.buffers, globals.bibs) {
453444
eat_bib_print(
@@ -515,11 +506,11 @@ pub(crate) fn get_bib_command_or_entry_and_process(
515506
let lc_res = if ctx.all_entries {
516507
globals
517508
.hash
518-
.lookup_str_insert(globals.pool, lc_cite, HashExtra::LcCite(HashPointer::default()))
509+
.lookup_str_insert(globals.pool, lc_cite, HashPointer::default())
519510
} else {
520511
globals
521512
.hash
522-
.lookup_str(globals.pool, lc_cite, StrIlk::LcCite)
513+
.lookup_str::<hash::LcCite>(globals.pool, lc_cite)
523514
.map_or(
524515
LookupRes {
525516
loc: HashPointer::default(),
@@ -529,17 +520,13 @@ pub(crate) fn get_bib_command_or_entry_and_process(
529520
)
530521
};
531522

532-
let mut res = lc_res;
523+
let mut cite_exists = lc_res.exists;
533524

534525
// TODO: Improve this tangled control flow
535526
let mut inner = || {
536527
if lc_res.exists {
537-
let &HashExtra::LcCite(cite_loc) = globals.hash.get(lc_res.loc).extra() else {
538-
panic!("LcCite lookup didn't have LcCite extra");
539-
};
540-
let &HashExtra::Cite(cite) = globals.hash.get(cite_loc).extra() else {
541-
panic!("LcCite location didn't have Cite extra");
542-
};
528+
let &cite_loc = globals.hash.get(lc_res.loc).extra();
529+
let &cite = globals.hash.get(cite_loc).extra();
543530

544531
globals.cites.set_entry_ptr(cite);
545532
let entry_ptr = globals.cites.entry_ptr();
@@ -552,23 +539,23 @@ pub(crate) fn get_bib_command_or_entry_and_process(
552539
let range = globals.buffers.offset(BufTy::Base, 1)
553540
..globals.buffers.offset(BufTy::Base, 2);
554541
let cite = &globals.buffers.buffer(BufTy::Base)[range];
555-
let uc_res = globals.hash.lookup_str_insert(
556-
globals.pool,
557-
cite,
558-
HashExtra::Cite(0),
559-
);
542+
let uc_res =
543+
globals
544+
.hash
545+
.lookup_str_insert::<hash::Cite>(globals.pool, cite, 0);
560546

561-
res = uc_res;
547+
cite_exists = uc_res.exists;
562548

563549
if !uc_res.exists {
564-
*globals.hash.get_mut(lc_res.loc).extra_mut() = HashExtra::LcCite(uc_res.loc);
565-
*globals.hash.get_mut(uc_res.loc).extra_mut() = HashExtra::Cite(entry_ptr);
550+
globals.hash.set_extra(lc_res.loc, uc_res.loc);
551+
globals.hash.set_extra(uc_res.loc, entry_ptr);
566552
globals
567553
.cites
568554
.set_cite(entry_ptr, globals.hash.get(uc_res.loc).text());
569-
res.exists = true;
555+
cite_exists = true;
570556
}
571557
}
558+
// Break out of if
572559
return None;
573560
}
574561
} else if !globals.cites.exists(entry_ptr) {
@@ -579,7 +566,7 @@ pub(crate) fn get_bib_command_or_entry_and_process(
579566

580567
let lc_res2 = globals
581568
.hash
582-
.lookup_str(globals.pool, lc_cite, StrIlk::LcCite)
569+
.lookup_str::<hash::LcCite>(globals.pool, lc_cite)
583570
.map_or(
584571
LookupRes {
585572
loc: HashPointer::default(),
@@ -588,7 +575,7 @@ pub(crate) fn get_bib_command_or_entry_and_process(
588575
|loc| LookupRes { loc, exists: true },
589576
);
590577

591-
res = lc_res2;
578+
cite_exists = lc_res2.exists;
592579

593580
if !lc_res2.exists {
594581
cite_key_disappeared_confusion(ctx);
@@ -622,12 +609,10 @@ pub(crate) fn get_bib_command_or_entry_and_process(
622609
}
623610

624611
let store_entry = if ctx.all_entries {
625-
if res.exists {
612+
if cite_exists {
626613
if globals.cites.entry_ptr() >= globals.cites.all_marker() {
627614
globals.cites.set_exists(globals.cites.entry_ptr(), true);
628-
let &HashExtra::LcCite(cite_loc) = globals.hash.get(lc_res.loc).extra() else {
629-
panic!("LcCite lookup didn't have LcCite extra");
630-
};
615+
let &cite_loc = globals.hash.get(lc_res.loc).extra();
631616
globals.cites.set_entry_ptr(globals.cites.ptr());
632617
let num = add_database_cite(
633618
globals.cites,
@@ -642,10 +627,7 @@ pub(crate) fn get_bib_command_or_entry_and_process(
642627
} else {
643628
let cite = &globals.buffers.buffer(BufTy::Base)
644629
[globals.buffers.offset(BufTy::Base, 1)..globals.buffers.offset(BufTy::Base, 2)];
645-
let res =
646-
globals
647-
.hash
648-
.lookup_str_insert(globals.pool, cite, HashExtra::Cite(0));
630+
let res = globals.hash.lookup_str_insert(globals.pool, cite, 0);
649631
if res.exists {
650632
hash_cite_confusion(ctx);
651633
return Err(BibtexError::Fatal);
@@ -663,7 +645,7 @@ pub(crate) fn get_bib_command_or_entry_and_process(
663645
}
664646
true
665647
} else {
666-
res.exists
648+
cite_exists
667649
};
668650

669651
if store_entry {
@@ -749,15 +731,10 @@ pub(crate) fn get_bib_command_or_entry_and_process(
749731
let bst_fn = &mut globals.buffers.buffer_mut(BufTy::Base)[range];
750732
bst_fn.make_ascii_lowercase();
751733

752-
let res = globals.hash.lookup_str(globals.pool, bst_fn, StrIlk::BstFn);
734+
let res = globals.hash.lookup_str::<BstFn>(globals.pool, bst_fn);
753735

754736
match res {
755-
Some(loc)
756-
if matches!(
757-
globals.hash.get(loc).extra(),
758-
HashExtra::BstFn(BstFn::Field(_))
759-
) =>
760-
{
737+
Some(loc) if matches!(globals.hash.get(loc).extra(), BstFn::Field(_),) => {
761738
*field_name_loc = loc;
762739
store_field = true;
763740
}

0 commit comments

Comments
 (0)