Merge pull request #2340 from AleoHQ/fix/issue-2239

[Fix] Check type of struct init expression.
This commit is contained in:
d0cd 2023-04-05 17:28:59 -07:00 committed by GitHub
commit 298c2036af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 14 deletions

View File

@ -321,12 +321,11 @@ impl<'a> TypeChecker<'a> {
/// Returns the `struct` type and emits an error if the `expected` type does not match.
pub(crate) fn check_expected_struct(&mut self, struct_: Identifier, expected: &Option<Type>, span: Span) -> Type {
if let Some(Type::Identifier(expected)) = expected {
if !struct_.matches(expected) {
self.emit_err(TypeCheckerError::type_should_be(struct_.name, expected.name, span));
if let Some(expected) = expected {
if !Type::Identifier(struct_).eq_flat(expected) {
self.emit_err(TypeCheckerError::type_should_be(struct_.name, expected, span));
}
}
Type::Identifier(struct_)
}

View File

@ -1,5 +0,0 @@
---
namespace: Compile
expectation: Fail
outputs:
- "Error [ETYC0372035]: `increment` or `decrement` statements must be inside a finalize block.\n --> compiler-test:8:9\n |\n 8 | increment(values, 0u8, 1u8);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^\nError [ETYC0372035]: `increment` or `decrement` statements must be inside a finalize block.\n --> compiler-test:9:9\n |\n 9 | increment(account, self.caller, 1u64);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nError [ETYC0372035]: `increment` or `decrement` statements must be inside a finalize block.\n --> compiler-test:13:9\n |\n 13 | decrement(values, 0u8, 1u8);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^\nError [ETYC0372035]: `increment` or `decrement` statements must be inside a finalize block.\n --> compiler-test:14:9\n |\n 14 | decrement(account, self.caller, 1u64);\n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\nError [ETYC0372044]: Function must contain a `finalize` statement on all execution paths.\n --> compiler-test:12:5\n |\n 12 | inline bar() {\n 13 | decrement(values, 0u8, 1u8);\n 14 | decrement(account, self.caller, 1u64);\n 15 | }\n | ^\nError [ETYC0372031]: Only transition functions can have a `finalize` block.\n --> compiler-test:17:5\n |\n 17 | finalize finalize_no_params() {\n 18 | foo();\n 19 | bar();\n 20 | }\n | ^\n |\n = Remove the `finalize` block or use the keyword `transition` instead of `function`.\nError [ETYC0372045]: `finalize` name `bar` does not match function name `finalize_no_params`\n --> compiler-test:17:5\n |\n 17 | finalize finalize_no_params() {\n 18 | foo();\n 19 | bar();\n 20 | }\n | ^\nError [ETYC0372066]: Cyclic dependency between functions: `bar` --> `bar`\n"

View File

@ -0,0 +1,5 @@
---
namespace: Compile
expectation: Fail
outputs:
- "Error [ETYC0372003]: Expected type `()` but type `test_credits` was found\n --> compiler-test:11:16\n |\n 11 | return test_credits {\n | ^^^^^^^^^^^^\n"

View File

@ -1,5 +0,0 @@
---
namespace: Compile
expectation: Fail
outputs:
- "Failed to parse string. Parsing Error: VerboseError { errors: [(\"closure main:\\n input r0 as boolean;\\n input r1 as boolean;\\n\", Nom(Tag)), (\"\\n\\n\\nclosure main:\\n input r0 as boolean;\\n input r1 as boolean;\\n\", Nom(Alt)), (\"\\n\\n\\nclosure main:\\n input r0 as boolean;\\n input r1 as boolean;\\n\", Nom(Many1))] }"

View File

@ -0,0 +1,20 @@
/*
namespace: Compile
expectation: Fail
*/
program test.aleo {
record test_credits {
owner: address,
gates: u64,
amount: u64
}
transition mint_credits(to: address, amount: u64) {
return test_credits {
owner: self.caller,
gates: 0u64,
amount
};
}
}