From 2a152c2517bdf04b60a100849f90d31105bc1027 Mon Sep 17 00:00:00 2001 From: evan-schott <53463459+evan-schott@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:03:57 -0700 Subject: [PATCH] Error for trying to modify external mappings --- compiler/passes/src/type_checking/checker.rs | 14 ++++++++++++++ .../src/errors/type_checker/type_checker_error.rs | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/compiler/passes/src/type_checking/checker.rs b/compiler/passes/src/type_checking/checker.rs index 2638d8bf82..906037e26d 100644 --- a/compiler/passes/src/type_checking/checker.rs +++ b/compiler/passes/src/type_checking/checker.rs @@ -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. diff --git a/errors/src/errors/type_checker/type_checker_error.rs b/errors/src/errors/type_checker/type_checker_error.rs index e228e0feb0..c784baf75c 100644 --- a/errors/src/errors/type_checker/type_checker_error.rs +++ b/errors/src/errors/type_checker/type_checker_error.rs @@ -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()), + } );