Skip to content

protoc: strange inconsistency in message literals in custom options for when a colon is needed #9551

@jhump

Description

@jhump

What version of protobuf and what language are you using?
3.19.0

What operating system (Linux, Windows, ...) and version?
OS X

What runtime / compiler are you using (e.g., python version or gcc version)
N/A, just using protoc to compile descriptors

What did you do?
I tried to compile the following file, via protoc -o /dev/null test.proto:

// test.proto
syntax = "proto3";

import "google/protobuf/descriptor.proto";

message Foo {
  Foo f = 1;
  string s = 2;
  repeated Foo fs = 3;
  repeated string ss = 4;
}

extend google.protobuf.MessageOptions {
  Foo foo = 10101;
}

message Test1 {
  option (foo) = {
    // using a colon to separate field and value always works
    f: {s:"a"}
    s: "a"
    fs: [{s:"a"}, {s:"b"}, {s:"c"}]
    ss: ["a", "b", "c"]
  };
}

message Test2 {
  option (foo) = {
    // but it's inconsistent when a colon can be omitted
   
    // safe to omit when value is a message because '<' or '{'
    // unambiguously indicates start of value
    f {s: "a"}
    
    // also safe to omit for repeated field of messages using
    // array literal notation
    fs [{s:"a"}, {s:"b"}, {s:"c"}]

    // but NOT safe to omit for repeated scalar in array literal
    // notation, even though '[' unambiguously indicates start of value
    ss ["a", "b", "c"]
  };
}

What did you expect to see
Success

What did you see instead?
An error that a colon is required on that last example value:

test.proto:28:18: Error while parsing option value for "foo": Expected ":", found "[".

If I add a colon just to that last one (ss) or comment out that line, the file is accepted.

Anything else we should know about your project / environment
Nope

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions