Error for trying to modify external mappings

This commit is contained in:
evan-schott 2024-03-28 11:03:57 -07:00
parent 75f3647879
commit 2a152c2517
2 changed files with 21 additions and 0 deletions

View File

@ -974,6 +974,13 @@ impl<'a> TypeChecker<'a> {
}
// Check that the first argument is a mapping.
if let Some(mapping_type) = self.assert_mapping_type(&arguments[0].0, arguments[0].1) {
// Cannot modify external mappings.
if mapping_type.program != self.program_name.unwrap() {
self.handler.emit_err(TypeCheckerError::cannot_modify_external_mapping(
"set",
function_span,
));
}
// Check that the second argument matches the key type of the mapping.
self.assert_type(&arguments[1].0, &mapping_type.key, arguments[1].1);
// Check that the third argument matches the value type of the mapping.
@ -994,6 +1001,13 @@ impl<'a> TypeChecker<'a> {
}
// Check that the first argument is a mapping.
if let Some(mapping_type) = self.assert_mapping_type(&arguments[0].0, arguments[0].1) {
// Cannot modify external mappings.
if mapping_type.program != self.program_name.unwrap() {
self.handler.emit_err(TypeCheckerError::cannot_modify_external_mapping(
"remove",
function_span,
));
}
// Check that the second argument matches the key type of the mapping.
self.assert_type(&arguments[1].0, &mapping_type.key, arguments[1].1);
// Return nothing.

View File

@ -768,4 +768,11 @@ create_messages!(
msg: format!("The definition for `{struct_}` in program `{program_1}.aleo` does not match the definition in program `{program_2}.aleo`"),
help: Some("Check that the struct definition in the current program matches the definition in the imported program.".to_string()),
}
@formatted
cannot_modify_external_mapping {
args: (operation: impl Display),
msg: format!("Cannot use operation `{operation}` to modify external mapping."),
help: Some("The only valid operations on external mappings are get, get_or_use, and contains.".to_string()),
}
);