Skip to content

Commit

Permalink
Merge pull request #28193 from ProvableHQ/fix/empty-struct
Browse files Browse the repository at this point in the history
[Fix] Informative error on empty struct.
  • Loading branch information
evan-schott authored Jul 8, 2024
2 parents 5ff127c + 72d09ff commit 475f6d3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/passes/src/type_checking/check_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ impl<'a, N: Network> ProgramVisitor<'a> for TypeChecker<'a, N> {
};
check_has_field(sym::owner, Type::Address);
}
// For structs, check that there is at least one member.
else if input.members.is_empty() {
self.emit_err(TypeCheckerError::empty_struct(input.span()));
}

if !(input.is_record && self.scope_state.is_stub) {
for Member { mode, identifier, type_, span, .. } in input.members.iter() {
Expand Down
7 changes: 7 additions & 0 deletions errors/src/errors/type_checker/type_checker_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,4 +879,11 @@ create_messages!(
msg: format!("The async function `{name}` does not exist."),
help: Some(format!("Ensure that `{name}` is defined as an async function in the current program.")),
}

@formatted
empty_struct {
args: (),
msg: "A struct must have at least one member.".to_string(),
help: None,
}
);
5 changes: 5 additions & 0 deletions tests/expectations/compiler/structs/empty_struct_fail.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
namespace: Compile
expectation: Fail
outputs:
- "Error [ETYC0372112]: A struct must have at least one member.\n --> compiler-test:4:5\n |\n 4 | struct Bar {}\n | ^^^^^^^^^^^^^\nError [ETYC0372083]: A program must have at least one transition function.\n --> compiler-test:1:1\n |\n 1 | \n 2 | \n 3 | program test.aleo {\n | ^^^^^^^^^^^^\n"
12 changes: 12 additions & 0 deletions tests/tests/compiler/structs/empty_struct_fail.leo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
namespace: Compile
expectation: Fail
*/

program test.aleo {
struct Bar {}

function main() -> bool {
return true;
}
}

0 comments on commit 475f6d3

Please sign in to comment.