Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix two bugs related to type conversion #1199

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

Merged
merged 2 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions 15 compiler/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func (fc *funcContext) translateExpr(expr ast.Expr) *expression {
return fc.translateExpr(e.X)
}

elemType := exprType.(*types.Pointer).Elem()

switch x := astutil.RemoveParens(e.X).(type) {
case *ast.CompositeLit:
return fc.formatExpr("$newDataPointer(%e, %s)", x, fc.typeName(fc.pkgCtx.TypeOf(e)))
Expand All @@ -214,13 +216,13 @@ func (fc *funcContext) translateExpr(expr ast.Expr) *expression {
if fc.pkgCtx.escapingVars[obj] {
return fc.formatExpr("(%1s.$ptr || (%1s.$ptr = new %2s(function() { return this.$target[0]; }, function($v) { this.$target[0] = $v; }, %1s)))", fc.pkgCtx.objectNames[obj], fc.typeName(exprType))
}
return fc.formatExpr(`(%1s || (%1s = new %2s(function() { return %3s; }, function($v) { %4s })))`, fc.varPtrName(obj), fc.typeName(exprType), fc.objectName(obj), fc.translateAssign(x, fc.newIdent("$v", exprType), false))
return fc.formatExpr(`(%1s || (%1s = new %2s(function() { return %3s; }, function($v) { %4s })))`, fc.varPtrName(obj), fc.typeName(exprType), fc.objectName(obj), fc.translateAssign(x, fc.newIdent("$v", elemType), false))
case *ast.SelectorExpr:
sel, ok := fc.pkgCtx.SelectionOf(x)
if !ok {
// qualified identifier
obj := fc.pkgCtx.Uses[x.Sel].(*types.Var)
return fc.formatExpr(`(%1s || (%1s = new %2s(function() { return %3s; }, function($v) { %4s })))`, fc.varPtrName(obj), fc.typeName(exprType), fc.objectName(obj), fc.translateAssign(x, fc.newIdent("$v", exprType), false))
return fc.formatExpr(`(%1s || (%1s = new %2s(function() { return %3s; }, function($v) { %4s })))`, fc.varPtrName(obj), fc.typeName(exprType), fc.objectName(obj), fc.translateAssign(x, fc.newIdent("$v", elemType), false))
}
newSel := &ast.SelectorExpr{X: fc.newIdent("this.$target", fc.pkgCtx.TypeOf(x.X)), Sel: x.Sel}
fc.setType(newSel, exprType)
Expand Down Expand Up @@ -1183,7 +1185,7 @@ func (fc *funcContext) translateConversion(expr ast.Expr, desiredType types.Type
//
// TODO(nevkontakte): Should this only apply when exprType is a pointer to a
// struct as well?
return fc.formatExpr("$pointerOfStructConversion(%e, %s)", expr, fc.typeName(t))
return fc.formatExpr("$pointerOfStructConversion(%e, %s)", expr, fc.typeName(desiredType))
}

if types.Identical(exprType, types.Typ[types.UnsafePointer]) {
Expand Down Expand Up @@ -1213,12 +1215,7 @@ func (fc *funcContext) translateConversion(expr ast.Expr, desiredType types.Type
func (fc *funcContext) translateImplicitConversionWithCloning(expr ast.Expr, desiredType types.Type) *expression {
switch desiredType.Underlying().(type) {
case *types.Struct, *types.Array:
switch expr.(type) {
case nil, *ast.CompositeLit:
// nothing
default:
return fc.formatExpr("$clone(%e, %s)", expr, fc.typeName(desiredType))
}
return fc.formatExpr("$clone(%e, %s)", expr, fc.typeName(desiredType))
}

return fc.translateImplicitConversion(expr, desiredType)
Expand Down
2 changes: 1 addition & 1 deletion 2 compiler/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ func (fc *funcContext) translateAssign(lhs, rhs ast.Expr, define bool) string {
}

lhsType := fc.pkgCtx.TypeOf(lhs)
rhsExpr := fc.translateImplicitConversion(rhs, lhsType)
rhsExpr := fc.translateConversion(rhs, lhsType)
if _, ok := rhs.(*ast.CompositeLit); ok && define {
return fmt.Sprintf("%s = %s;", fc.translateExpr(lhs), rhsExpr) // skip $copy
}
Expand Down
32 changes: 32 additions & 0 deletions 32 tests/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ func TestPointerOfStructConversion(t *testing.T) {

type B A

type AP *A

a1 := &A{Value: 1}
b1 := (*B)(a1)
b1.Value = 2
Expand All @@ -197,6 +199,10 @@ func TestPointerOfStructConversion(t *testing.T) {
if a1 != a2 || b1 != b2 || a1.Value != 4 || a2.Value != 4 || b1.Value != 4 || b2.Value != 4 {
t.Fail()
}

if got := reflect.TypeOf((AP)(&A{Value: 1})); got.String() != "tests.AP" {
t.Errorf("Got: reflect.TypeOf((AP)(&A{Value: 1})) = %v. Want: tests.AP.", got)
}
}

func TestCompareStruct(t *testing.T) {
Expand Down Expand Up @@ -888,3 +894,29 @@ func TestReflectSetForEmbed(t *testing.T) {
t.Fatalf("relfect.Set got %v, want %v", f0, e.Field(0))
}
}

func TestAssignImplicitConversion(t *testing.T) {
type S struct{}
type SP *S

t.Run("Pointer to named type", func(t *testing.T) {
var sp SP = &S{}
if got := reflect.TypeOf(sp); got.String() != "tests.SP" {
t.Errorf("Got: reflect.TypeOf(sp) = %v. Want: tests.SP", got)
}
})

t.Run("Anonymous struct to named type", func(t *testing.T) {
var s S = struct{}{}
if got := reflect.TypeOf(s); got.String() != "tests.S" {
t.Errorf("Got: reflect.TypeOf(s) = %v. Want: tests.S", got)
}
})

t.Run("Named type to anonymous type", func(t *testing.T) {
var x struct{} = S{}
if got := reflect.TypeOf(x); got.String() != "struct {}" {
t.Errorf("Got: reflect.TypeOf(x) = %v. Want: struct {}", got)
}
})
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.