Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:ZeroLit for alias #489

Merged
merged 1 commit into from
Apr 12, 2025
Merged

feat:ZeroLit for alias #489

merged 1 commit into from
Apr 12, 2025

Conversation

luoliwoshang
Copy link
Contributor

@luoliwoshang luoliwoshang commented Apr 10, 2025

fix unexpected zero lit for alias type


without alias support in ZeroLit,will cause unexpect zerolit for a bulitin type's alias,like use goplus/llgo/c.Double

Error: ./cJSON.go:129:9: invalid composite literal type c.Double
Error: ./cJSON.go:406:9: invalid composite literal type c.Double
Error: /Users/runner/work/llcppg/llcppg/_llcppgtest/cjson/out/cjson/cJSON.go:129:9: invalid composite literal type float64
Error: /Users/runner/work/llcppg/llcppg/_llcppgtest/cjson/out/cjson/cJSON.go:406:9: invalid composite literal type float64

https://github.com/goplus/llcppg/actions/runs/14375203222/job/40305873425?pr=244

support goplus/llcppg#244

reproduce

package main

import (
	"go/token"
	"go/types"
	"os"

	"github.com/goplus/gogen"
)

func main() {
	pkg := gogen.NewPackage("", "temp", &gogen.Config{
		EnableTypesalias: true,
	})
	clib := pkg.Import("github.com/goplus/llgo/c")
	cdouble := clib.Ref("Double").Type()
	signature := types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewVar(token.NoPos, pkg.Types, "", cdouble)), false)
	pkg.NewFuncDecl(token.NoPos, "test", signature).BodyStart(pkg).
		ZeroLit(cdouble).Return(1).End()
	pkg.WriteTo(os.Stdout)
}

go.mod

module test

go 1.23.4

require github.com/goplus/gogen v1.16.9

require (
	github.com/goplus/llgo v0.10.0 // indirect
	golang.org/x/tools v0.19.0 // indirect
)

replace golang.org/x/tools => golang.org/x/tools v0.30.0

note:use replace golang.org/x/tools => golang.org/x/tools v0.30.0 to ref a alias's name instead it's origin type.
without this replace the hello

package temp

type hello float64

with this replace,we can refer the actual alias type

package temp

import "github.com/goplus/llgo/c"

type hello c.Double

but in this case,we will get a error zerolit of c.Double

package temp

import "github.com/goplus/llgo/c"

type hello c.Double

func test() c.Double {
        return c.Double{}
}
invalid composite literal type c.Double compiler[InvalidLit](https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#InvalidLit)

with this pr,we can got a correct alias zerolit
image

Copy link

codecov bot commented Apr 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.00%. Comparing base (ba6f4f0) to head (f2fbfc3).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #489   +/-   ##
=======================================
  Coverage   96.00%   96.00%           
=======================================
  Files          21       21           
  Lines        7156     7159    +3     
=======================================
+ Hits         6870     6873    +3     
  Misses        222      222           
  Partials       64       64           

☔ View full report in Codecov by Sentry.
📢 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.

codebuild.go Outdated
@@ -768,6 +768,9 @@ retry:
case *types.Named:
typ = p.getUnderlying(t)
goto retry
case *types.Alias:
Copy link
Member

Choose a reason for hiding this comment

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

typesutil.Alias

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -51,6 +51,9 @@ jobs:
go work init .
go work edit -replace golang.org/x/tools=golang.org/x/[email protected]

- name: Set types alias env
Copy link
Member

Choose a reason for hiding this comment

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

上面的 go work init 自动加入了 go 1.23/1.24 默认为 typesalias 环境。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@luoliwoshang luoliwoshang force-pushed the alias branch 2 times, most recently from 82e12c7 to fcbd255 Compare April 10, 2025 15:05
@xushiwei xushiwei merged commit 42e7875 into goplus:main Apr 12, 2025
22 checks passed
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.

3 participants