Skip to content

Fixing error when trying to extend any google native definition.#288

Open
rogersguedes wants to merge 1 commit intostarwing:masterfrom
rogersguedes:bugfix/google-extension-error
Open

Fixing error when trying to extend any google native definition.#288
rogersguedes wants to merge 1 commit intostarwing:masterfrom
rogersguedes:bugfix/google-extension-error

Conversation

@rogersguedes
Copy link

I was getting the following error when trying to extend a Google Native definition:

Protobuf definition

extend google.protobuf.MessageOptions {
    optional PersistanceMessageOptions persistance = 50000;
}

Error

proto3 disallow 'optional' label

This issue is similar to the one reported in #282 , but in this case it happens only when extending already parsed definitions.

So I made that change for fixing the issue but I'm not sure if it's the best approach and came here to share it with You all.

@rogersguedes rogersguedes changed the title Fixing erro when trying to extend any google native definition. Fixing error when trying to extend any google native definition. Mar 18, 2026
@sonarqubecloud
Copy link

@starwing
Copy link
Owner

starwing commented Mar 19, 2026

This is not that easy - Even you makes protoc.lua support optional in extend, the pb.h itself do not support it:

  • A extended field will be load into the extendee type
  • But that type lacks oneof_index for this field
  • That means the field _persistance can not set even if a optional field is set.

If you do not care about full support for optional extend, that's just work, but the type information in memory is not complete: a field with oneof_idx can not find the corresponding oneof name.

To full support it, you should change pb.h, or load the patched types twice to workaround this issue.

FYI below is my modify to protoc.lua to makes compiler itself work:

diff --git a/protoc.lua b/protoc.lua
old mode 100644
new mode 100755
index 55fd067..fd3a3e6
--- a/protoc.lua
+++ b/protoc.lua
@@ -405,7 +405,7 @@ local function map_info(lex)
    local kt, ktn = type_info(lex, keyt)
    local vt, vtn = type_info(lex, valt)
    return name, types.message, ident, {
-      name = ident,
+      name = ident, type = types.message,
       field = {
          {
             name      = "key",
@@ -447,7 +447,7 @@ local function field(self, lex, ident)
       name, typ, type_name, map_entry = map_info(lex)
       self.locmap[map_entry.field[1]] = lex.pos
       self.locmap[map_entry.field[2]] = lex.pos
-      register_type(self, lex, type_name, types.message)
+      register_type(self, lex, type_name, map_entry)
    else
       typ, type_name = type_info(lex, ident)
       name = lex:ident()
@@ -471,7 +471,7 @@ local function field(self, lex, ident)
    return info, map_entry
 end

-local function label_field(self, lex, ident, parent)
+local function label_field(self, lex, ident)
    local label = labels[ident]
    local info, map_entry
    if not label then
@@ -481,17 +481,14 @@ local function label_field(self, lex, ident, parent)
       return field(self, lex, ident)
    end
    local proto3_optional = label == labels.optional and self.syntax == "proto3"
-   if proto3_optional and not (self.proto3_optional and parent) then
+   if proto3_optional and not self.proto3_optional then
       return lex:error("proto3 disallow 'optional' label")
    end
    info, map_entry = field(self, lex, lex:type_name())
    if proto3_optional then
-      local ot = default(parent, "oneof_decl")
-      info.oneof_index = #ot
-      ot[#ot+1] = { name = "_" .. info.name }
-   else
-      info.label = label
+      info.proto3_optional = true
    end
+   info.label = label
    return info, map_entry
 end

@@ -636,8 +633,8 @@ end

 function toplevel:message(lex, info)
    local name = lex:ident 'message name'
-   local typ = { name = name }
-   register_type(self, lex, name, types.message)
+   local typ = { name = name, type = types.message }
+   register_type(self, lex, name, typ)
    local prefix = self.prefix
    self.prefix = prefix..name.."."
    lex:expected "{"
@@ -648,7 +645,7 @@ function toplevel:message(lex, info)
          body_parser(self, lex, typ)
       else
          local fs = default(typ, 'field')
-         local f, t = label_field(self, lex, ident, typ)
+         local f, t = label_field(self, lex, ident)
          self.locmap[f] = pos
          insert_tab(fs, f)
          if t then
@@ -669,9 +666,9 @@ end

 function toplevel:enum(lex, info)
    local name, pos = lex:ident 'enum name'
-   local enum = { name = name }
+   local enum = { name = name, type = types.enum }
    self.locmap[enum] = pos
-   register_type(self, lex, name, types.enum)
+   register_type(self, lex, name, enum)
    lex:expected "{"
    while not lex:test "}" do
       local ident, pos = lex:ident 'enum constant name'
@@ -802,8 +799,12 @@ local function make_context(self, lex)
    ctx.loaded  = self.loaded
    ctx.typemap = self.typemap
    ctx.paths   = self.paths
-   ctx.proto3_optional =
-      self.proto3_optional or self.experimental_allow_proto3_optional
+   if self.proto3_optional == nil and self.experimental_allow_proto3_optional == nil then
+      ctx.proto3_optional = true
+   else
+      ctx.proto3_optional =
+         self.proto3_optional or self.experimental_allow_proto3_optional
+   end
    ctx.unknown_type = self.unknown_type
    ctx.unknown_import = self.unknown_import
    ctx.on_import = self.on_import
@@ -898,7 +899,7 @@ local function check_type(self, lex, tname)
    if tn then
       t = types[t or "message"]
       if tn == true then tn = "."..tname end
-      return t, tn
+      return { name = tn, type = t }, tn
    end
    return lex:error("unknown type '%s'", tname)
 end
@@ -906,14 +907,20 @@ end
 local function check_field(self, lex, info)
    if info.extendee then
       local t, tn = check_type(self, lex, info.extendee)
-      if t ~= types.message then
+      if t.type ~= types.message then
          lex:error("message type expected in extension")
       end
       info.extendee = tn
+      if info.proto3_optional then
+         local ot = default(t, "oneof_decl")
+         local index = #ot + 1
+         ot[index] = { name = "_" .. info.name }
+         info.oneof_index = index
+      end
    end
    if info.type_name then
       local t, tn = check_type(self, lex, info.type_name)
-      info.type      = t
+      info.type      = t.type
       info.type_name = tn
    end
 end
@@ -955,12 +962,12 @@ local function check_service(self, lex, info)
       check_dup(self, lex, 'rpc name', names, 'name', v)
       local t, tn = check_type(self, lex, v.input_type)
       v.input_type = tn
-      if t ~= types.message then
+      if t.type ~= types.message then
          lex:error "message type expected in parameter"
       end
       t, tn = check_type(self, lex, v.output_type)
       v.output_type = tn
-      if t ~= types.message then
+      if t.type ~= types.message then
          lex:error "message type expected in return"
       end
    end

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