From 3dae44db7328072d0d11936e0d4f618747cd2374 Mon Sep 17 00:00:00 2001 From: zakuro Date: Tue, 5 Jan 2021 09:46:32 +0900 Subject: [PATCH] checker: do not allow empty enums (#7848) --- vlib/v/checker/checker.v | 3 +++ vlib/v/checker/tests/enum_empty.out | 3 +++ vlib/v/checker/tests/enum_empty.vv | 1 + 3 files changed, 7 insertions(+) create mode 100644 vlib/v/checker/tests/enum_empty.out create mode 100644 vlib/v/checker/tests/enum_empty.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 9f8a2f21da..8b42e8e484 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2194,6 +2194,9 @@ pub fn (mut c Checker) const_decl(mut node ast.ConstDecl) { pub fn (mut c Checker) enum_decl(decl ast.EnumDecl) { c.check_valid_pascal_case(decl.name, 'enum name', decl.pos) mut seen := []i64{} + if decl.fields.len == 0 { + c.error('enum cannot be empty', decl.pos) + } for i, field in decl.fields { if !c.pref.experimental && util.contains_capital(field.name) { // TODO C2V uses hundreds of enums with capitals, remove -experimental check once it's handled diff --git a/vlib/v/checker/tests/enum_empty.out b/vlib/v/checker/tests/enum_empty.out new file mode 100644 index 0000000000..b2b432dcff --- /dev/null +++ b/vlib/v/checker/tests/enum_empty.out @@ -0,0 +1,3 @@ +vlib/v/checker/tests/enum_empty.vv:1:1: error: enum cannot be empty + 1 | enum Empty {} + | ~~~~~~~~~~ diff --git a/vlib/v/checker/tests/enum_empty.vv b/vlib/v/checker/tests/enum_empty.vv new file mode 100644 index 0000000000..7afdbc006a --- /dev/null +++ b/vlib/v/checker/tests/enum_empty.vv @@ -0,0 +1 @@ +enum Empty {}