Skip to content

Reject keywords used as field type or enum value names - #807

Merged
emcfarlane merged 6 commits into
mainfrom
ed/keywordDeclarations
Dec 2, 2026
Merged

emcfarlane merged 6 commits into
mainfrom
ed/keywordDeclarations

Conversation

@emcfarlane

@emcfarlane emcfarlane commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor
syntax = "proto3";
enum message { unset = 0; }
enum optional { unset = 0; }
message group { message Bar {} }

message Foo {
  message a = 1;             // was: field of type `message`
  optional b = 2;            // was: field of type `optional`
  reserved = 3;              // was: field named `reserved`
  optional group c = 4;      // was: field of type `group`
  repeated group.Bar d = 5;  // was: accepted
  oneof o {
    option.X g = 8;          // was: field of type `option.X`
    message h = 9;           // still accepted
  }
  optional .group e = 6;     // still accepted
  optional message f = 7;    // still accepted
}

enum E {
  option = 0;                // was: enum value named `option`
  reserved = 1;              // was: enum value named `reserved`
}

This rejects keywords that protoc reads as the start of a different statement, so we now match protoc.

  • In a message: message, enum, extend, oneof, option, reserved and extensions start their own statement. optional, repeated and required are always labels.
  • In a oneof: option starts an option statement. Labels are not allowed. Other keywords can still be field types.
  • In an extend: only labels and group are special.
  • In a field: group is always the group keyword, including after a label and inside a oneof or extend.
  • In an enum: option and reserved start their own statement.

These still work:

  • Fully qualified names: .message, .group.
  • Keywords that aren't the first part of a name: foo.message.
  • Keywords after a label: optional message f.

A rejected statement is marked as invalid in the AST rather than left as a field or enum value. The formatter, LSP and IR then skip it, and it doesn't cause follow-on errors.

@emcfarlane
emcfarlane requested a review from pkwarren November 10, 2026 14:20
@emcfarlane
emcfarlane requested a review from pkwarren November 12, 2026 17:07
@emcfarlane
emcfarlane merged commit 8292b5d into main Dec 2, 2026
10 checks passed
@emcfarlane
emcfarlane deleted the ed/keywordDeclarations branch December 2, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants