Skip to content

Commit 11d2eaa

Browse files
committed
Address code review: preserve CO_FAST_HIDDEN, fix varint overflow
- Use original localspluskinds from marshal data instead of rebuilding, preserving CO_FAST_HIDDEN and other flags - Fix write_varint_be to handle values >= 2^30 (add 6th chunk) - Remove unused build_localspluskinds_from_split
1 parent 14d6f73 commit 11d2eaa

File tree

2 files changed

+5
-38
lines changed

2 files changed

+5
-38
lines changed

crates/compiler-core/src/marshal.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,8 @@ pub fn deserialize_code<R: Read, Bag: ConstantBag>(
249249
let instructions = CodeUnits::try_from(code_bytes.as_slice())?;
250250
let locations = linetable_to_locations(&linetable, first_line_raw, instructions.len());
251251

252-
// Build localspluskinds from split vars (matching CodeObject's merged layout)
253-
let localspluskinds =
254-
build_localspluskinds_from_split(&lp.varnames, &lp.cellvars, &lp.freevars);
252+
// Use original localspluskinds from marshal data (preserves CO_FAST_HIDDEN etc.)
253+
let localspluskinds = localspluskinds.into_boxed_slice();
255254

256255
Ok(CodeObject {
257256
instructions,
@@ -1035,41 +1034,6 @@ pub struct LocalsPlusResult<S> {
10351034
pub deref_map: Vec<u32>,
10361035
}
10371036

1038-
/// Build localspluskinds from split varnames/cellvars/freevars.
1039-
/// This produces the merged layout matching CodeObject's localspluskinds field.
1040-
fn build_localspluskinds_from_split<S: AsRef<str>>(
1041-
varnames: &[S],
1042-
cellvars: &[S],
1043-
freevars: &[S],
1044-
) -> Box<[u8]> {
1045-
let nlocals = varnames.len();
1046-
let ncells = cellvars.len();
1047-
let nfrees = freevars.len();
1048-
let numdropped = cellvars
1049-
.iter()
1050-
.filter(|cv| varnames.iter().any(|v| v.as_ref() == cv.as_ref()))
1051-
.count();
1052-
let nlocalsplus = nlocals + ncells - numdropped + nfrees;
1053-
let mut kinds = alloc::vec![0u8; nlocalsplus];
1054-
for kind in kinds.iter_mut().take(nlocals) {
1055-
*kind = CO_FAST_LOCAL;
1056-
}
1057-
let mut cell_numdropped = 0usize;
1058-
for (i, cv) in cellvars.iter().enumerate() {
1059-
if let Some(local_idx) = varnames.iter().position(|v| v.as_ref() == cv.as_ref()) {
1060-
kinds[local_idx] |= CO_FAST_CELL;
1061-
cell_numdropped += 1;
1062-
} else {
1063-
kinds[nlocals + i - cell_numdropped] = CO_FAST_CELL;
1064-
}
1065-
}
1066-
let free_start = nlocals + ncells - numdropped;
1067-
for i in 0..nfrees {
1068-
kinds[free_start + i] = CO_FAST_FREE;
1069-
}
1070-
kinds.into_boxed_slice()
1071-
}
1072-
10731037
pub fn split_localplus<S: Clone>(
10741038
names: &[S],
10751039
kinds: &[u8],

crates/compiler-core/src/varint.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub fn write_signed_varint(buf: &mut Vec<u8>, val: i32) -> usize {
3434
/// Write a big-endian varint (used by exception tables).
3535
pub fn write_varint_be(buf: &mut Vec<u8>, val: u32) -> usize {
3636
let start_len = buf.len();
37+
if val >= 1 << 30 {
38+
buf.push(0x40 | ((val >> 30) & 0x3f) as u8);
39+
}
3740
if val >= 1 << 24 {
3841
buf.push(0x40 | ((val >> 24) & 0x3f) as u8);
3942
}

0 commit comments

Comments
 (0)