[pigeon] Report a clear error for enhanced enums#11880
Conversation
Pigeon's input parser silently ignored the extra syntax in enhanced enums (constructors, fields, methods, or arguments on values), reading only the value names. This produced confusing generated output — the enum's fields were effectively coalesced into the next class — with no indication that the input was unsupported. Detect enhanced enums in visitEnumDeclaration and report a clear, source-located error instead. The enum is still registered by its value names so that classes referencing it don't produce additional cascading 'Unknown type' errors, and the visitor no longer walks the enum's body (whose class-like members it doesn't expect; a getter there previously crashed the parser on a null parameter list). Fixes flutter/flutter#160827
There was a problem hiding this comment.
Code Review
This pull request updates Pigeon to version 27.0.1 and introduces detection for unsupported enhanced enums in Dart input files. When an enhanced enum (defined by having members or constant arguments) is detected, Pigeon now emits a clear error message and avoids visiting its children to prevent parser crashes. Feedback on these changes suggests expanding the enhanced enum detection to also check for type parameters, mixins, and implemented interfaces to ensure all unsupported cases are caught.
| final bool hasEnumMembers = node.body.members.isNotEmpty; | ||
| final bool hasConstantArguments = node.body.constants.any( | ||
| (dart_ast.EnumConstantDeclaration e) => e.arguments != null, | ||
| ); | ||
| final bool isEnhancedEnum = hasEnumMembers || hasConstantArguments; |
There was a problem hiding this comment.
Enhanced enums in Dart can also declare type parameters, implement interfaces, or use mixins (using with or implements clauses). The current check only detects enhanced enums with members or constant arguments. To fully prevent unsupported enhanced enums from causing issues, we should also check for node.typeParameters, node.withClause, and node.implementsClause.
final bool hasEnumMembers = node.body.members.isNotEmpty;
final bool hasConstantArguments = node.body.constants.any(
(dart_ast.EnumConstantDeclaration e) => e.arguments != null,
);
final bool isEnhancedEnum = hasEnumMembers ||
hasConstantArguments ||
node.typeParameters != null ||
node.withClause != null ||
node.implementsClause != null;…implements clauses as enhanced enums These forms were previously still silently reduced to their value names. The error message now lists the full set of unsupported syntax.
Pigeon's input parser silently ignored the extra syntax in enhanced enums (constructors, fields, methods, or arguments on values), reading only the value names. This produced confusing generated output — as described in the linked issue, the enum's fields were effectively coalesced into the next class — with no indication that the input was unsupported.
This change detects enhanced enums in
visitEnumDeclarationand reports a clear, source-located error:Implementation notes:
visitMethodDeclaration'snode.parameters!). One of the new tests covers this case.enum E { a, b; }) are unaffected; a test guards against false positives.The scope follows the approach discussed and agreed in the issue thread (silent failure → clear error; no attempt to actually support enhanced enums).
Fixes flutter/flutter#160827
Pre-Review Checklist
[shared_preferences]///).Four new tests in
pigeon_lib_test.dart: enhanced enum with constructor+fields, enhanced enum with only arguments on values, enhanced enum with a getter (the previous parser-crash case), and a plain enum with a trailing semicolon (false-positive guard). The fullpigeon_lib_test.dartsuite (113 tests) andversion_test.dartpass locally.