Infer conservative read-only source effects for method contracts
This commit is contained in:
@@ -24,6 +24,7 @@ fn source(
|
||||
vec![Value::Number(42.into()), result],
|
||||
CodeLayout {
|
||||
parameters: (0..parameters.len()).map(|i| format!("p{i}")).collect(),
|
||||
locals: (0..parameters.len()).map(|i| format!("p{i}")).collect(),
|
||||
names: vec!["ran".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
@@ -339,7 +340,7 @@ fn native_overrides_preserve_union_inputs_and_do_not_expand_effects() {
|
||||
assert!(matches!(
|
||||
vm.define_method(child, "check", effect_fn),
|
||||
Err(VmError::OverrideViolation {
|
||||
reason: "override expands native effects",
|
||||
reason: "override expands known effects",
|
||||
..
|
||||
})
|
||||
));
|
||||
@@ -443,3 +444,147 @@ fn unknown_forward_names_defer_until_call_without_entering_the_body() {
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn verified_source_read_only_effects_constrain_native_overrides() {
|
||||
let mut vm = Vm::new().unwrap();
|
||||
let roots = vm.bootstrap_types().unwrap();
|
||||
vm.set_source_type_checks(true).unwrap();
|
||||
let globals = vm.new_dict().unwrap();
|
||||
let code = EndorCode::from_instructions(
|
||||
&[
|
||||
Instruction::new(Opcode::LoadFast, 1),
|
||||
Instruction::simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
parameters: vec!["self".into(), "value".into()],
|
||||
locals: vec!["self".into(), "value".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap()
|
||||
.with_source_signature(SourceSignature {
|
||||
parameters: vec!["PureBase".into(), "Number".into()],
|
||||
return_type: "Number".into(),
|
||||
method_owner: Some("PureBase".into()),
|
||||
})
|
||||
.unwrap();
|
||||
assert!(code.is_statically_read_only());
|
||||
let code = vm.new_code(code).unwrap();
|
||||
let source = vm
|
||||
.new_function("check", code, globals, vec![], vec![])
|
||||
.unwrap();
|
||||
let base = class(&mut vm, "PureBase", roots.object, source).unwrap();
|
||||
let child = vm.new_type("Child", base, TypeConstructor::Record).unwrap();
|
||||
assert_eq!(vm.call_effect(source).unwrap(), Some(CallEffect::ReadOnly));
|
||||
let receiver = vm.new_object().unwrap();
|
||||
vm.attach_type(receiver, child).unwrap();
|
||||
let Value::Object(bound) = vm.bind_attr(receiver, "check").unwrap() else {
|
||||
panic!("bound method")
|
||||
};
|
||||
assert_eq!(vm.call_effect(bound).unwrap(), Some(CallEffect::ReadOnly));
|
||||
let mut functions = vec![];
|
||||
for effect in [
|
||||
CallEffect::Mutating,
|
||||
CallEffect::External,
|
||||
CallEffect::ReadOnly,
|
||||
CallEffect::Delegated,
|
||||
] {
|
||||
let function = vm
|
||||
.allocate(ContextNativeFunction::new(
|
||||
"check",
|
||||
Native(CallSignature::exact(
|
||||
vec![T::Any, T::Number],
|
||||
T::Number,
|
||||
effect,
|
||||
)),
|
||||
vec![],
|
||||
))
|
||||
.unwrap();
|
||||
let version = vm.change_version(child).unwrap();
|
||||
let update = vm.define_method(child, "check", function);
|
||||
if matches!(effect, CallEffect::Mutating | CallEffect::External) {
|
||||
assert!(matches!(
|
||||
update,
|
||||
Err(VmError::OverrideViolation {
|
||||
reason: "override expands known effects",
|
||||
..
|
||||
})
|
||||
));
|
||||
assert_eq!(vm.change_version(child).unwrap(), version);
|
||||
} else {
|
||||
update.unwrap();
|
||||
}
|
||||
assert_eq!(
|
||||
vm.call_effect(function).unwrap(),
|
||||
(effect != CallEffect::Delegated).then_some(effect)
|
||||
);
|
||||
functions.push(function);
|
||||
}
|
||||
for object in functions.into_iter().chain([
|
||||
bound,
|
||||
receiver,
|
||||
child,
|
||||
base,
|
||||
source,
|
||||
code,
|
||||
globals,
|
||||
roots.object,
|
||||
roots.type_type,
|
||||
]) {
|
||||
vm.release(object).unwrap();
|
||||
}
|
||||
vm.collect_cycles();
|
||||
assert_eq!(vm.object_count(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_local_assignment_and_dynamic_calls_are_not_inferred_pure() {
|
||||
let assignment = EndorCode::from_instructions(
|
||||
&[
|
||||
Instruction::new(Opcode::LoadFast, 1),
|
||||
Instruction::new(Opcode::StoreFast, 0),
|
||||
Instruction::new(Opcode::LoadFast, 0),
|
||||
Instruction::simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![],
|
||||
CodeLayout {
|
||||
parameters: vec!["target".into(), "value".into()],
|
||||
locals: vec!["target".into(), "value".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!assignment.is_statically_read_only());
|
||||
let captured = EndorCode::from_instructions(
|
||||
&[
|
||||
Instruction::new(Opcode::LoadConst, 0),
|
||||
Instruction::new(Opcode::StoreDeref, 0),
|
||||
Instruction::new(Opcode::LoadConst, 0),
|
||||
Instruction::simple(Opcode::ReturnValue),
|
||||
],
|
||||
vec![Value::Nil],
|
||||
CodeLayout {
|
||||
free_variables: vec!["captured".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!captured.is_statically_read_only());
|
||||
let call = 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!["dynamic".into()],
|
||||
..CodeLayout::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!call.is_statically_read_only());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user