From 642515161514e6a17ea54a3bafae60b72a6140bb Mon Sep 17 00:00:00 2001 From: xushiwei Date: Thu, 2 Nov 2023 22:31:12 +0800 Subject: [PATCH] MatchError.Message --- ast.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/ast.go b/ast.go index 3838bf9b..5f529bbd 100644 --- a/ast.go +++ b/ast.go @@ -1164,20 +1164,22 @@ func strval(at interface{}) string { } } -func (p *MatchError) Error() string { - at := token.NoPos +func (p *MatchError) Message(fileLine string) string { + if p.fstmt { + return fmt.Sprintf( + "%scannot use %v value as type %v in %s", fileLine, p.Arg, p.Param, strval(p.At)) + } src := "" if p.Src != nil { - at = p.Src.Pos() src = p.intr.LoadExpr(p.Src) } - pos := p.Fset.Position(at) - if p.fstmt { - return fmt.Sprintf( - "%v: cannot use %v value as type %v in %s", pos, p.Arg, p.Param, strval(p.At)) - } return fmt.Sprintf( - "%v: cannot use %s (type %v) as type %v in %s", pos, src, p.Arg, p.Param, strval(p.At)) + "%scannot use %s (type %v) as type %v in %s", fileLine, src, p.Arg, p.Param, strval(p.At)) +} + +func (p *MatchError) Error() string { + pos := p.Fset.Position(getSrcPos(p.Src)) + return p.Message(pos.String() + ": ") } // TODO: use matchType to all assignable check