Skip to content

cl loadConsts: if not untyped, convert value to the enum type#2784

Merged
xushiwei merged 1 commit into
goplus:mainfrom
xushiwei:q
Jun 21, 2026
Merged

cl loadConsts: if not untyped, convert value to the enum type#2784
xushiwei merged 1 commit into
goplus:mainfrom
xushiwei:q

Conversation

@xushiwei

Copy link
Copy Markdown
Member

No description provided.

@codecov

codecov Bot commented Jun 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.13%. Comparing base (90efe7f) to head (315ae2e).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2784   +/-   ##
=======================================
  Coverage   94.13%   94.13%           
=======================================
  Files          32       32           
  Lines       10177    10186    +9     
=======================================
+ Hits         9580     9589    +9     
  Misses        427      427           
  Partials      170      170           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for enum types with custom underlying types, along with a corresponding test case. In cl/compile.go, the compiler is updated to convert typed enum constant values to the target enum type. The feedback suggests adding safety checks to prevent potential nil pointer dereferences when inspecting the stack element's type, and avoiding redundant type conversions if the expression's type is already identical to the target enum type.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread cl/compile.go
@xushiwei xushiwei merged commit 27a591d into goplus:main Jun 21, 2026
11 checks passed

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The change is correct and targeted: introducing explicit type conversion for typed enum constant values (e.g. byte(iota)) so the generated Go output properly uses the enum type. The isEnum extraction and loadConstSpecs parameter rename both improve readability.

Two substantive concerns, plus one minor doc note:

1. Missing nil guard before e.Type dereferencestk.Get(-1) can return a nil or zero-value element if compileExpr failed silently. Accessing e.Type on such an element panics. Although the default enableRecover=true wrapper will catch and convert such a panic to a compile error, it is better to guard explicitly. The gemini-code-assist thread on this file flags the same issue.

2. Redundant conversion when value already has the enum type — when an enum const value expression already produces a value of the enum type (e.g. a reference to an earlier enum constant), the current condition still wraps it in another typ(...), emitting Weekday(Weekday(...)) in the generated Go. A types.Identical check would suppress this. Use types.Identical, not ==, for type comparison.

3. Test coverage gap — the new enum4 fixture only exercises the byte(iota) typed path. A fixture confirming that plain iota values (untyped) are not wrapped would lock in the critical untyped-passthrough behavior that the IsUntyped check depends on.

Minor — the // typ = enum type for enum consts, or nil for normal consts comment above loadConstSpecs still refers to the old parameter name typ after the rename to enumType.

Comment thread cl/compile.go
compileExpr(ctx, 1, val)
if isEnum {
stk := cb.InternalStack()
e := stk.Get(-1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Missing nil guard on stack element. stk.Get(-1) may return nil or a zero-value element if compileExpr pushes nothing (error path). Dereferencing e.Type without a nil check will panic. Add guards — and while here, a types.Identical check avoids generating redundant double-conversions like Weekday(Weekday(...)) when the value already has the target type:

e := stk.Get(-1)
if e != nil && e.Type != nil && !types.Identical(e.Type, typ) {
    if t, ok := e.Type.(*types.Basic); !ok || t.Info()&types.IsUntyped == 0 {
        // not untyped, convert value to the enum type
        stk.Pop()
        cb.Typ(typ)
        stk.Push(e)
        cb.Call(1)
    }
}

Note: use types.Identical rather than == for type comparison.

Comment thread cl/compile.go
@@ -1655,10 +1655,10 @@ func inferEnumUnderlyingType(ctx *blockCtx, enumType *ast.EnumType) types.Type {
}

// typ = enum type for enum consts, or nil for normal consts

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor: the comment // typ = enum type for enum consts, or nil for normal consts still uses the old parameter name typ after the rename of loadConstSpecs's parameter to enumType. Consider updating to enumType for consistency.

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.

1 participant