-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Support for Compact Enums #686
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ pub fn validate_enum(enum_def: &Enum, diagnostics: &mut Diagnostics) { | |
enumerator_values_are_unique(enum_def, diagnostics); | ||
underlying_type_cannot_be_optional(enum_def, diagnostics); | ||
nonempty_if_checked(enum_def, diagnostics); | ||
check_compact_modifier(enum_def, diagnostics); | ||
compact_enums_cannot_contain_tags(enum_def, diagnostics); | ||
|
||
// If the enum wasn't defined in a Slice1 file, validate whether fields or explicit values are allowed, | ||
// based on whether it has an underlying type. Fields in Slice1 files are already rejected by `encoding_patcher`. | ||
|
@@ -173,3 +175,53 @@ fn cannot_contain_explicit_values(enum_def: &Enum, diagnostics: &mut Diagnostics | |
} | ||
} | ||
} | ||
|
||
/// Validate that compact enums do not have an underlying type and are not marked as 'unchecked'. | ||
fn check_compact_modifier(enum_def: &Enum, diagnostics: &mut Diagnostics) { | ||
if enum_def.is_compact { | ||
if let Some(underlying) = &enum_def.underlying { | ||
Diagnostic::new(Error::CannotBeCompact { | ||
kind: enum_def.kind(), | ||
identifier: enum_def.identifier().to_owned(), | ||
}) | ||
.set_span(enum_def.span()) | ||
.add_note( | ||
"compact enums cannot also have underlying types\ntry removing either the 'compact' modifier, or the underlying type", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should try to avoid using newlines in these notes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I'll change them all to other punctuation. What's the problem with newlines though? |
||
Some(underlying.span()), | ||
) | ||
.push_into(diagnostics); | ||
} | ||
|
||
if enum_def.is_unchecked { | ||
Diagnostic::new(Error::CannotBeCompact { | ||
kind: enum_def.kind(), | ||
identifier: enum_def.identifier().to_owned(), | ||
}) | ||
.set_span(enum_def.span()) | ||
.add_note( | ||
"An enum cannot be both unchecked and compact - try removing the 'compact' modifier", | ||
None, | ||
) | ||
.push_into(diagnostics); | ||
} | ||
} | ||
} | ||
|
||
/// Validate that tags cannot be used in compact enums. | ||
fn compact_enums_cannot_contain_tags(enum_def: &Enum, diagnostics: &mut Diagnostics) { | ||
if enum_def.is_compact { | ||
for enumerator in enum_def.enumerators() { | ||
for field in enumerator.fields() { | ||
if field.is_tagged() { | ||
Diagnostic::new(Error::CompactTypeCannotContainTaggedFields { kind: enum_def.kind() }) | ||
.set_span(field.span()) | ||
.add_note( | ||
format!("enum '{}' is declared compact here", enum_def.identifier()), | ||
Some(enum_def.span()), | ||
) | ||
.push_into(diagnostics); | ||
} | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @externl on the new lines in the error descriptions. Do we do this anywhere else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a few scattered around notes and error messages.
Just whenever something seemed more readable on two lines.