From a850ff2c52ad1f84ee2ed71cb60e687d870bd591 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 9 Mar 2024 02:24:17 +0800 Subject: [PATCH] TestErrStringLit --- cl/error_msg_test.go | 6 ++++++ cl/expr.go | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cl/error_msg_test.go b/cl/error_msg_test.go index ff7dd02b6..cc27271a3 100644 --- a/cl/error_msg_test.go +++ b/cl/error_msg_test.go @@ -509,6 +509,12 @@ func (p *[]byte) foo() { `) } +func TestErrStringLit(t *testing.T) { + codeErrorTest(t, `bar.gop:2:9: [].string undefined (type []interface{} has no field or method string)`, ` +echo "${[]}" +`) +} + func TestErrStructLit(t *testing.T) { codeErrorTest(t, `bar.gop:3:39: too many values in struct{x int; y string}{...}`, ` diff --git a/cl/expr.go b/cl/expr.go index d81cdf4c5..7a49b06ca 100644 --- a/cl/expr.go +++ b/cl/expr.go @@ -971,7 +971,12 @@ func compileStringLitEx(ctx *blockCtx, cb *gogen.CodeBuilder, lit *ast.BasicLit) compileExpr(ctx, v, flags) t := cb.Get(-1).Type if t.Underlying() != types.Typ[types.String] { - cb.Member("string", gogen.MemberFlagAutoProperty) + if _, err := cb.Member("string", gogen.MemberFlagAutoProperty); err != nil { + if e, ok := err.(*gogen.CodeError); ok { + err = ctx.newCodeErrorf(v.Pos(), "%s.string%s", ctx.LoadExpr(v), e.Msg) + } + ctx.handleErr(err) + } } pos = v.End() default: