diff --git a/compiler/passes/src/type_checking/check_program.rs b/compiler/passes/src/type_checking/check_program.rs index 0c35db12d5..39c859fe03 100644 --- a/compiler/passes/src/type_checking/check_program.rs +++ b/compiler/passes/src/type_checking/check_program.rs @@ -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() { diff --git a/errors/src/errors/type_checker/type_checker_error.rs b/errors/src/errors/type_checker/type_checker_error.rs index 6f67a8ecc0..37486d0784 100644 --- a/errors/src/errors/type_checker/type_checker_error.rs +++ b/errors/src/errors/type_checker/type_checker_error.rs @@ -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, + } ); diff --git a/tests/expectations/compiler/structs/empty_struct_fail.out b/tests/expectations/compiler/structs/empty_struct_fail.out new file mode 100644 index 0000000000..d9815695c9 --- /dev/null +++ b/tests/expectations/compiler/structs/empty_struct_fail.out @@ -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" diff --git a/tests/tests/compiler/structs/empty_struct_fail.leo b/tests/tests/compiler/structs/empty_struct_fail.leo new file mode 100644 index 0000000000..020290cc35 --- /dev/null +++ b/tests/tests/compiler/structs/empty_struct_fail.leo @@ -0,0 +1,12 @@ +/* +namespace: Compile +expectation: Fail +*/ + +program test.aleo { + struct Bar {} + + function main() -> bool { + return true; + } +}