Skip to content

Commit

Permalink
fix a crash in compiling external references in modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 28, 2024
1 parent 2490aad commit 6fea2f7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,20 @@ impl From<&Assembly> for Assembly {

impl Assembly {
/// Get the instructions of a function slice
#[track_caller]
pub fn instrs(&self, slice: FuncSlice) -> &[Instr] {
&self.instrs[slice.start..slice.end()]
let end = slice.end();
assert!(
slice.start <= self.instrs.len(),
"Func slice start {} out of bounds",
slice.start
);
assert!(
slice.end() <= self.instrs.len(),
"Func slice end {} out of bounds",
end
);
&self.instrs[slice.start..end]
}
/// Get the mutable instructions of a function slice
pub fn instrs_mut(&mut self, slice: FuncSlice) -> &mut [Instr] {
Expand Down
4 changes: 2 additions & 2 deletions src/compile/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ impl Compiler {
);
if let [Instr::PushFunc(f)] = instrs.as_slice() {
// Binding is a single inline function
sig = f.signature();
let func = make_fn(f.instrs(&self.asm).into(), f.signature(), self);
let mut func = f.clone();
func.id = FunctionId::Named(name.clone());
self.compile_bind_function(&name, local, func, spandex, comment.as_deref())?;
} else if sig == (0, 1) && !is_setinv && !is_setund {
if let &[Instr::Prim(Primitive::Tag, span)] = instrs.as_slice() {
Expand Down
2 changes: 1 addition & 1 deletion src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ code:
if len > 1 {
(self.asm.instrs).push(Instr::Comment(format!("({id}").into()));
}
let start = self.asm.instrs.len();
let start = if len == 0 { 0 } else { self.asm.instrs.len() };
let mut hasher = DefaultHasher::new();
instrs.hash(&mut hasher);
let hash = hasher.finish();
Expand Down
1 change: 1 addition & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ impl Function {
self.slice
}
/// Get the function's instructions
#[track_caller]
pub fn instrs<'a>(&self, asm: &'a Assembly) -> &'a [Instr] {
asm.instrs(self.slice)
}
Expand Down

0 comments on commit 6fea2f7

Please sign in to comment.