refine is/2 compilation so errors are thrown when expected without needlessly allocating (#1974, #1984)

This commit is contained in:
Mark
2023-08-29 16:42:23 -06:00
parent 53a1be78cc
commit 66f6399b8a
3 changed files with 69 additions and 12 deletions

View File

@@ -814,19 +814,34 @@ impl<'b> CodeGenerator<'b> {
);
self.marker.mark_safe_var_unconditionally(var_num);
compile_expr!(self, &terms[1], term_loc, code)
} else {
if self.marker.in_tail_position {
if self.marker.var_data.allocates {
code.push_back(instr!("deallocate"));
}
if let Term::Var(ref vr, ref var) = &terms[1] {
let var_num = var.to_var_num().unwrap();
code.push_back(instr!("proceed"));
// if var is an anonymous variable, insert
// is/2 call so that an instantiation error is
// thrown when the predicate is run.
if self.marker.var_data.records[var_num].num_occurrences > 1 {
self.marker.mark_var::<QueryInstruction>(
var_num,
Level::Shallow,
vr,
term_loc,
code,
);
self.marker.mark_safe_var_unconditionally(var_num);
let at = ArithmeticTerm::Reg(vr.get().norm());
self.add_call(code, instr!("$get_number", at), call_policy);
return Ok(());
}
}
return Ok(());
compile_expr!(self, &terms[1], term_loc, code)
}
compile_expr!(self, &terms[1], term_loc, code)
}
&Term::Literal(_, c @ Literal::Integer(_) |
c @ Literal::Float(_) |