Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Pointer type is not converted correctly when assigned to a struct literal #1240

Open
@nevkontakte

Description

@nevkontakte

Example:

package main

import (
	"fmt"
	"reflect"
)

type strPtr *string

func main() {
	var s string = "abc"

	var s1 *string = &s
	var s2 strPtr = &s
	fmt.Println(reflect.TypeOf(s1), reflect.TypeOf(s2))

	v := struct {
		s1 *string
		s2 strPtr
	}{
		s1: &s,
		s2: &s,
	}
	fmt.Println(reflect.TypeOf(v.s1), reflect.TypeOf(v.s2))
}

Expected output:

*string main.strPtr
*string main.strPtr

Actual output:

*string main.strPtr
*string *string

Proximate cause:

Composite literal compilation calls fc.translateImplicitConversionWithCloning() (example), which handles pointer type conversion as a no-op, which is probably incorrect.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions