Skip to content
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

Reject/error on base declarations that appear after field declarations #4553

Merged
merged 5 commits into from
Nov 21, 2024

Conversation

dwblaikie
Copy link
Contributor

No description provided.

@dwblaikie
Copy link
Contributor Author

Open to feedback on the diagnostic phrasing, where the handling is being done (too early? too late?) etc.

@dwblaikie dwblaikie marked this pull request as ready for review November 20, 2024 19:12
Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you asked for comments on the message. :)

toolchain/check/handle_class.cpp Outdated Show resolved Hide resolved
@@ -512,6 +512,15 @@ auto HandleParseNode(Context& context, Parse::BaseDeclId node_id) -> bool {
return true;
}

if (!context.struct_type_fields_stack().PeekArray().empty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!context.struct_type_fields_stack().PeekArray().empty()) {
if (auto fields = context.struct_type_fields_stack().PeekArray(); !fields.empty()) {

(see note suggestion below)

Comment on lines 519 to 520
context.emitter().Emit(node_id, BaseDeclAfterFieldDecl,
Lex::TokenKind::Base);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work to add a note for the first field location? i.e., something like:

Suggested change
context.emitter().Emit(node_id, BaseDeclAfterFieldDecl,
Lex::TokenKind::Base);
CARBON_DIAGNOSTIC(FirstFieldDecl, Note,
"First field declared here");
context.emitter().Build(node_id, BaseDeclAfterFieldDecl,
Lex::TokenKind::Base).Note(fields.front(), FirstFieldDecl).Emit();

(I think FirstFieldDecl is a bad name, but I'm struggling with better naming)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now the StructTypeFields don't have source locations for this extra note, but once we track the FieldDecls separately in #4514 I can revisit this to add the note. I'll add a TODO for this.

"`{0}` declaration appears after field declaration(s)",
Lex::TokenKind);
context.emitter().Emit(node_id, BaseDeclAfterFieldDecl,
Lex::TokenKind::Base);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you pass in Base here, versus putting base directly in the message?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inspired by some nearby code (DiagnoseClassSpecificDeclRepeated - which admittedly is actually parameterized, with one caller using Lex::TokenKind::Base and another using Lex::TokenKind::Adapt, whereas this case only has the one value). Smallest argument in favor of type safety/write-it-once - there's one bit of code for mapping this keyword to text, rather than including freeform text that is consistent today, but isn't checked in any way...
But I certainly don't feel strongly. I see there are at least several cases where it's written literally/in the string, so I'll change to that.

Copy link
Contributor

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG

dwblaikie and others added 5 commits November 20, 2024 23:18
Co-authored-by: Jon Ross-Perkins <[email protected]>
(sounded better to me and we have one prior diagnostic example of
`appear before` and none for `come before`)

(admittedly we also have one example of "come after" and none for
"appear after"... )
@jonmeow jonmeow added this pull request to the merge queue Nov 20, 2024
Merged via the queue into carbon-language:trunk with commit ffbcfc4 Nov 21, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants