Prove read-only effects through current source call graphs
This commit is contained in:
@@ -588,3 +588,289 @@ fn shared_local_assignment_and_dynamic_calls_are_not_inferred_pure() {
|
||||
.unwrap();
|
||||
assert!(!call.is_statically_read_only());
|
||||
}
|
||||
|
||||
fn global_caller(vm: &mut Vm, globals: ObjectRef, name: &str) -> ObjectRef {
|
||||
let code = EndorCode::from_instructions(
|
||||
&[
|
||||
Instruction::new(Opcode::LoadGlobal, 0),
|
||||
Instruction::simple(Opcode::PushNull),
|
||||
Instruction::new(Opcode::Call, 0),
|
||||
Instruction::simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
names: vec![name.into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let code = vm.new_code(code).unwrap();
|
||||
let function = vm
|
||||
.new_function("caller", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
vm.release(code).unwrap();
|
||||
function
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transitive_effect_queries_follow_rebinding_closures_and_recursive_graphs() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let builtins = vm.new_dict().unwrap();
|
||||
vm.dict_set(
|
||||
globals,
|
||||
Value::Text("__builtins__".into()),
|
||||
Value::Object(builtins),
|
||||
)
|
||||
.unwrap();
|
||||
let pure = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"pure",
|
||||
Native(CallSignature::exact(
|
||||
vec![],
|
||||
T::Number,
|
||||
CallEffect::ReadOnly,
|
||||
)),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
let impure = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"impure",
|
||||
Native(CallSignature::exact(
|
||||
vec![],
|
||||
T::Number,
|
||||
CallEffect::Mutating,
|
||||
)),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
vm.dict_set(builtins, Value::Text("target".into()), Value::Object(pure))
|
||||
.unwrap();
|
||||
let wrapper = global_caller(&mut vm, globals, "target");
|
||||
let outer = global_caller(&mut vm, globals, "wrapper");
|
||||
vm.dict_set(
|
||||
globals,
|
||||
Value::Text("wrapper".into()),
|
||||
Value::Object(wrapper),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(vm.call_effect(outer).unwrap(), Some(CallEffect::ReadOnly));
|
||||
vm.dict_set(globals, Value::Text("target".into()), Value::Object(impure))
|
||||
.unwrap();
|
||||
assert_eq!(vm.call_effect(outer).unwrap(), None);
|
||||
vm.dict_delete(globals, &Value::Text("target".into()))
|
||||
.unwrap();
|
||||
assert_eq!(vm.call_effect(outer).unwrap(), Some(CallEffect::ReadOnly));
|
||||
let cell = vm.new_cell(Some(Value::Object(pure))).unwrap();
|
||||
let code = EndorCode::from_instructions(
|
||||
&[
|
||||
Instruction::new(Opcode::LoadDeref, 0),
|
||||
Instruction::simple(Opcode::PushNull),
|
||||
Instruction::new(Opcode::Call, 0),
|
||||
Instruction::simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
free_variables: vec!["target".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let code = vm.new_code(code).unwrap();
|
||||
let closure = vm
|
||||
.new_function("closure", code, globals, vec![], vec![cell])
|
||||
.unwrap();
|
||||
assert_eq!(vm.call_effect(closure).unwrap(), Some(CallEffect::ReadOnly));
|
||||
vm.cell_set(cell, Value::Object(impure)).unwrap();
|
||||
assert_eq!(vm.call_effect(closure).unwrap(), None);
|
||||
vm.cell_set(cell, Value::Object(pure)).unwrap();
|
||||
assert_eq!(vm.call_effect(closure).unwrap(), Some(CallEffect::ReadOnly));
|
||||
let recursive = global_caller(&mut vm, globals, "recursive");
|
||||
vm.dict_set(
|
||||
globals,
|
||||
Value::Text("recursive".into()),
|
||||
Value::Object(recursive),
|
||||
)
|
||||
.unwrap();
|
||||
// No function body is executed, including this non-terminating call graph.
|
||||
assert_eq!(
|
||||
vm.call_effect(recursive).unwrap(),
|
||||
Some(CallEffect::ReadOnly)
|
||||
);
|
||||
vm.dict_set(
|
||||
globals,
|
||||
Value::Text("recursive".into()),
|
||||
Value::Object(impure),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(vm.call_effect(recursive).unwrap(), None);
|
||||
for object in [
|
||||
recursive, closure, code, cell, outer, wrapper, pure, impure, builtins, globals,
|
||||
] {
|
||||
vm.release(object).unwrap();
|
||||
}
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effect_control_flow_joins_do_not_keep_a_stale_known_call_target() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let pure = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"pure",
|
||||
Native(CallSignature::exact(
|
||||
vec![],
|
||||
T::Number,
|
||||
CallEffect::ReadOnly,
|
||||
)),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
let impure = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"impure",
|
||||
Native(CallSignature::exact(
|
||||
vec![],
|
||||
T::Number,
|
||||
CallEffect::External,
|
||||
)),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
for name in ["left", "right"] {
|
||||
vm.dict_set(globals, Value::Text(name.into()), Value::Object(pure))
|
||||
.unwrap();
|
||||
}
|
||||
let code = EndorCode::from_instructions(
|
||||
&[
|
||||
Instruction::new(Opcode::LoadFast, 0),
|
||||
Instruction::new(Opcode::PopJumpIfFalse, 2),
|
||||
Instruction::new(Opcode::LoadGlobal, 0),
|
||||
Instruction::new(Opcode::JumpForward, 1),
|
||||
Instruction::new(Opcode::LoadGlobal, 1),
|
||||
Instruction::simple(Opcode::PushNull),
|
||||
Instruction::new(Opcode::Call, 0),
|
||||
Instruction::simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
parameters: vec!["condition".into()],
|
||||
locals: vec!["condition".into()],
|
||||
names: vec!["left".into(), "right".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let code = vm.new_code(code).unwrap();
|
||||
let function = vm
|
||||
.new_function("choose", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
vm.call_effect(function).unwrap(),
|
||||
Some(CallEffect::ReadOnly)
|
||||
);
|
||||
vm.dict_set(globals, Value::Text("right".into()), Value::Object(impure))
|
||||
.unwrap();
|
||||
assert_eq!(vm.call_effect(function).unwrap(), None);
|
||||
vm.dict_set(globals, Value::Text("right".into()), Value::Object(pure))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
vm.call_effect(function).unwrap(),
|
||||
Some(CallEffect::ReadOnly)
|
||||
);
|
||||
for object in [function, code, pure, impure, globals] {
|
||||
vm.release(object).unwrap();
|
||||
}
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bound_source_effects_follow_the_actual_receiver_method_and_instance_field() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let roots = vm.bootstrap_types().unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let definition = vm
|
||||
.new_type("Receiver", roots.object, TypeConstructor::Record)
|
||||
.unwrap();
|
||||
let pure = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"helper",
|
||||
Native(CallSignature::exact(
|
||||
vec![T::Any],
|
||||
T::Number,
|
||||
CallEffect::ReadOnly,
|
||||
)),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
let impure = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"helper",
|
||||
Native(CallSignature::exact(
|
||||
vec![T::Any],
|
||||
T::Number,
|
||||
CallEffect::Mutating,
|
||||
)),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
let code = EndorCode::from_instructions(
|
||||
&[
|
||||
Instruction::new(Opcode::LoadFast, 0),
|
||||
Instruction::new(Opcode::LoadAttr, 0),
|
||||
Instruction::simple(Opcode::PushNull),
|
||||
Instruction::new(Opcode::Call, 0),
|
||||
Instruction::simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
parameters: vec!["self".into()],
|
||||
locals: vec!["self".into()],
|
||||
names: vec!["helper".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let code = vm.new_code(code).unwrap();
|
||||
let function = vm
|
||||
.new_function("check", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
vm.define_method(definition, "helper", pure).unwrap();
|
||||
vm.define_method(definition, "check", function).unwrap();
|
||||
let receiver = vm.new_object().unwrap();
|
||||
vm.attach_type(receiver, definition).unwrap();
|
||||
let Value::Object(bound) = vm.bind_attr(receiver, "check").unwrap() else {
|
||||
panic!("bound")
|
||||
};
|
||||
assert_eq!(vm.call_effect(function).unwrap(), None);
|
||||
assert_eq!(vm.call_effect(bound).unwrap(), Some(CallEffect::ReadOnly));
|
||||
vm.define_method(definition, "helper", impure).unwrap();
|
||||
assert_eq!(vm.call_effect(bound).unwrap(), None);
|
||||
vm.define_method(definition, "helper", pure).unwrap();
|
||||
assert_eq!(vm.call_effect(bound).unwrap(), Some(CallEffect::ReadOnly));
|
||||
vm.set_attr(receiver, "helper", Value::Object(impure))
|
||||
.unwrap();
|
||||
assert_eq!(vm.call_effect(bound).unwrap(), None);
|
||||
vm.del_attr(receiver, "helper").unwrap();
|
||||
assert_eq!(vm.call_effect(bound).unwrap(), Some(CallEffect::ReadOnly));
|
||||
for object in [
|
||||
bound,
|
||||
receiver,
|
||||
function,
|
||||
code,
|
||||
pure,
|
||||
impure,
|
||||
definition,
|
||||
globals,
|
||||
roots.object,
|
||||
roots.type_type,
|
||||
] {
|
||||
vm.release(object).unwrap();
|
||||
}
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user