Skip to content

Commit 6fea2f7

Browse files
committed
fix a crash in compiling external references in modules
1 parent 2490aad commit 6fea2f7

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

src/assembly.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,20 @@ impl From<&Assembly> for Assembly {
4646

4747
impl Assembly {
4848
/// Get the instructions of a function slice
49+
#[track_caller]
4950
pub fn instrs(&self, slice: FuncSlice) -> &[Instr] {
50-
&self.instrs[slice.start..slice.end()]
51+
let end = slice.end();
52+
assert!(
53+
slice.start <= self.instrs.len(),
54+
"Func slice start {} out of bounds",
55+
slice.start
56+
);
57+
assert!(
58+
slice.end() <= self.instrs.len(),
59+
"Func slice end {} out of bounds",
60+
end
61+
);
62+
&self.instrs[slice.start..end]
5163
}
5264
/// Get the mutable instructions of a function slice
5365
pub fn instrs_mut(&mut self, slice: FuncSlice) -> &mut [Instr] {

src/compile/binding.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ impl Compiler {
268268
);
269269
if let [Instr::PushFunc(f)] = instrs.as_slice() {
270270
// Binding is a single inline function
271-
sig = f.signature();
272-
let func = make_fn(f.instrs(&self.asm).into(), f.signature(), self);
271+
let mut func = f.clone();
272+
func.id = FunctionId::Named(name.clone());
273273
self.compile_bind_function(&name, local, func, spandex, comment.as_deref())?;
274274
} else if sig == (0, 1) && !is_setinv && !is_setund {
275275
if let &[Instr::Prim(Primitive::Tag, span)] = instrs.as_slice() {

src/compile/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ code:
642642
if len > 1 {
643643
(self.asm.instrs).push(Instr::Comment(format!("({id}").into()));
644644
}
645-
let start = self.asm.instrs.len();
645+
let start = if len == 0 { 0 } else { self.asm.instrs.len() };
646646
let mut hasher = DefaultHasher::new();
647647
instrs.hash(&mut hasher);
648648
let hash = hasher.finish();

src/function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ impl Function {
817817
self.slice
818818
}
819819
/// Get the function's instructions
820+
#[track_caller]
820821
pub fn instrs<'a>(&self, asm: &'a Assembly) -> &'a [Instr] {
821822
asm.instrs(self.slice)
822823
}

0 commit comments

Comments
 (0)