errWrap to a internal interface
The following program sample.gop triggers an unexpected result
// add a sample
import "gop/x/gopprojs"
projs := gopprojs.parseAll(pattern...)!
for proj in projs {
// ...
}
Expected result
OK
Got
cannot use gopprojs.ParseAll(pattern...) (value of type []interface{projObj()}) as []interface{projObj()} value in assignment
Gop Version
v1.3.7
Additional Notes
No response
/code
Analysis of errWrap with internal interface issue
I've investigated this issue extensively and found that it's a fundamental limitation in how Go's type system handles type aliases with internal interfaces.
Problem Summary:
xgoprojs.Projis defined astype Proj = interface{projObj()}whereprojObj()is unexported- When errWrap creates a closure, it must specify return types in the closure signature
- The type system gives us
[]interface{projObj()}, but this cannot be written literally in generated code from packagemainbecauseprojObj()is not accessible - Type aliases in Go are completely transparent - there's no
*types.Namedrepresentation for them - Creating a new
*types.Namedwrapper breaks type compatibility during assignment
Why This Is Hard:
- We need a type that: (a) prints as
[]xgoprojs.Projin generated code, AND (b) is considered identical to[]interface{projObj()}for type checking - Go's type system doesn't provide a way to satisfy both requirements for type aliases
- The only solution would be to modify gogen's code generator to detect interfaces with unexported methods from other packages and automatically use qualified type names
Proposed Solutions:
- Fix in gogen (recommended): Modify gogen's AST/printer to detect when an interface type contains unexported methods from another package, search for a type alias in that package's scope, and use the qualified name instead of the interface literal
- Fix in xgoprojs: Change the API design to not use internal interfaces as public return types (breaking change)
- Workaround in errWrap: Detect this specific case and generate different code (e.g., not use inline closures)
This issue requires deeper changes to either gogen or the package design. @xushiwei - what approach would you prefer?
Branch: codeagent/claude/issue-2236-1760236688 | Job Run
@xgopilot I choose to fix in gogen. Please create an issue for gogen to describe this program and suggested solution.
Task Completed
I've created issue goplus/gogen#547 describing the problem and suggested solution.
Summary:
- Problem: When using errWrap with type aliases wrapping interfaces with unexported methods, the code generator produces type expressions that reference unexported method names from other packages
- Root Cause: Type aliases are transparent in Go's type system, but interface literals with unexported methods cannot be written in code outside their defining package
- Suggested Solution: Modify gogen's AST/printer to detect interfaces with unexported methods from other packages and automatically use the qualified type alias name instead of the interface literal
The issue includes:
- Complete problem description with the sample code from this issue
- Root cause analysis explaining why this happens
- Detailed suggested implementation approach
- Alternative workarounds for consideration
Please review the issue and let me know if you'd like any adjustments to the description or proposed solution.