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

interp: fix type assertion for wrapped empty interface #1467

Merged
merged 3 commits into from
Oct 3, 2022
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
56 changes: 56 additions & 0 deletions _test/cli7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
)

type T struct {
http.ResponseWriter
}

type mw1 struct {
}

var obj = map[string]interface{}{}

func (m *mw1) ServeHTTP(rw http.ResponseWriter, rq *http.Request) {
t := &T{
ResponseWriter: rw,
}
x := t.Header()
i := obj["m1"].(*mw1)
fmt.Fprint(rw, "Welcome to my website!", x, i)
}

func main() {
m1 := &mw1{}

obj["m1"] = m1

mux := http.NewServeMux()
mux.HandleFunc("/", m1.ServeHTTP)

server := httptest.NewServer(mux)
defer server.Close()

client(server.URL)
}

func client(uri string) {
resp, err := http.Get(uri)
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(body))
}

// Output:
// Welcome to my website!map[] &{}
6 changes: 3 additions & 3 deletions _test/type24.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func assertValue() {
}

// Output:
// interface conversion: interface {} is int, not string
// interface conversion: interface {} is nil, not string
// interface conversion: *httptest.ResponseRecorder is not http.Pusher: missing method Push
// 22:10: interface conversion: interface {} is int, not string
// 32:10: interface conversion: interface {} is nil, not string
// 42:10: interface conversion: *httptest.ResponseRecorder is not http.Pusher: missing method Push
1 change: 1 addition & 0 deletions interp/interp_consistent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestInterpConsistencyBuild(t *testing.T) {
file.Name() == "range9.go" || // expect error
file.Name() == "unsafe6.go" || // needs go.mod to be 1.17
file.Name() == "unsafe7.go" || // needs go.mod to be 1.17
file.Name() == "type24.go" || // expect error
file.Name() == "type27.go" || // expect error
file.Name() == "type28.go" || // expect error
file.Name() == "type29.go" || // expect error
Expand Down
5 changes: 4 additions & 1 deletion interp/interp_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/token"
"os"
"path/filepath"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -76,7 +77,9 @@ func runCheck(t *testing.T, p string) {
t.Fatal(err)
}

if res := strings.TrimSpace(stdout.String()); res != wanted {
// Remove path in output, to have results independent of location.
re := regexp.MustCompile(p + ":")
if res := re.ReplaceAllString(strings.TrimSpace(stdout.String()), ""); res != wanted {
t.Errorf("\ngot: %q,\nwant: %q", res, wanted)
}
}
Expand Down
24 changes: 14 additions & 10 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,15 @@ func typeAssert(n *node, withResult, withOk bool) {
ok = v.IsValid()
if !ok {
if !withOk {
panic(fmt.Sprintf("interface conversion: interface {} is nil, not %s", rtype.String()))
panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String()))
}
return next
}
ok = canAssertTypes(leftType, rtype)
if !ok {
if !withOk {
method := firstMissingMethod(leftType, rtype)
panic(fmt.Sprintf("interface conversion: %s is not %s: missing method %s", leftType.String(), rtype.String(), method))
panic(n.cfgErrorf("interface conversion: %s is not %s: missing method %s", leftType.String(), rtype.String(), method))
}
return next
}
Expand All @@ -430,14 +430,18 @@ func typeAssert(n *node, withResult, withOk bool) {
concrete := val.Interface()
ctyp := reflect.TypeOf(concrete)

if vv, ok := concrete.(valueInterface); ok {
ctyp = vv.value.Type()
concrete = vv.value.Interface()
}
ok = canAssertTypes(ctyp, rtype)
if !ok {
if !withOk {
// TODO(mpl): think about whether this should ever happen.
if ctyp == nil {
panic(fmt.Sprintf("interface conversion: interface {} is nil, not %s", rtype.String()))
panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String()))
}
panic(fmt.Sprintf("interface conversion: interface {} is %s, not %s", ctyp.String(), rtype.String()))
panic(n.cfgErrorf("interface conversion: interface {} is %s, not %s", ctyp.String(), rtype.String()))
}
return next
}
Expand All @@ -462,7 +466,7 @@ func typeAssert(n *node, withResult, withOk bool) {
}
if !ok {
if !withOk {
panic(fmt.Sprintf("interface conversion: interface {} is nil, not %s", rtype.String()))
panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String()))
}
return next
}
Expand All @@ -475,7 +479,7 @@ func typeAssert(n *node, withResult, withOk bool) {
if !ok {
if !withOk {
method := firstMissingMethod(v.Type(), rtype)
panic(fmt.Sprintf("interface conversion: %s is not %s: missing method %s", v.Type().String(), rtype.String(), method))
panic(n.cfgErrorf("interface conversion: %s is not %s: missing method %s", v.Type().String(), rtype.String(), method))
}
return next
}
Expand All @@ -495,15 +499,15 @@ func typeAssert(n *node, withResult, withOk bool) {
if !ok || !v.value.IsValid() {
ok = false
if !withOk {
panic(fmt.Sprintf("interface conversion: interface {} is nil, not %s", rtype.String()))
panic(n.cfgErrorf("interface conversion: interface {} is nil, not %s", rtype.String()))
}
return next
}

ok = canAssertTypes(v.value.Type(), rtype)
if !ok {
if !withOk {
panic(fmt.Sprintf("interface conversion: interface {} is %s, not %s", v.value.Type().String(), rtype.String()))
panic(n.cfgErrorf("interface conversion: interface {} is %s, not %s", v.value.Type().String(), rtype.String()))
}
return next
}
Expand Down Expand Up @@ -567,7 +571,7 @@ func convert(n *node) {
n.exec = func(f *frame) bltn {
n, ok := value(f).Interface().(*node)
if !ok || !n.typ.convertibleTo(c.typ) {
panic("cannot convert")
panic(n.cfgErrorf("cannot convert to %s", c.typ.id()))
}
n1 := *n
n1.typ = c.typ
Expand Down Expand Up @@ -3611,7 +3615,7 @@ func convertConstantValue(n *node) {
case constant.Int:
i, x := constant.Int64Val(c)
if !x {
panic(fmt.Sprintf("constant %s overflows int64", c.ExactString()))
panic(n.cfgErrorf("constant %s overflows int64", c.ExactString()))
}
v = reflect.ValueOf(int(i))
case constant.Float:
Expand Down