From e4af29508033ecd5945d6b5bffac86898ddfd909 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sun, 23 Aug 2020 12:24:03 +0300 Subject: [PATCH] compiler: process constant first in BinaryExpr handling --- pkg/compiler/codegen.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 8efdb57e4..71baf838b 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -689,6 +689,19 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil case *ast.BinaryExpr: + // The AST package will try to resolve all basic literals for us. + // If the typeinfo.Value is not nil we know that the expr is resolved + // and needs no further action. e.g. x := 2 + 2 + 2 will be resolved to 6. + // NOTE: Constants will also be automatically resolved be the AST parser. + // example: + // const x = 10 + // x + 2 will results into 12 + tinfo := c.typeAndValueOf(n) + if tinfo.Value != nil { + c.emitLoadConst(tinfo) + return nil + } + switch n.Op { case token.LAND: end := c.newLabel() @@ -711,19 +724,6 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil default: - // The AST package will try to resolve all basic literals for us. - // If the typeinfo.Value is not nil we know that the expr is resolved - // and needs no further action. e.g. x := 2 + 2 + 2 will be resolved to 6. - // NOTE: Constants will also be automatically resolved be the AST parser. - // example: - // const x = 10 - // x + 2 will results into 12 - tinfo := c.typeAndValueOf(n) - if tinfo.Value != nil { - c.emitLoadConst(tinfo) - return nil - } - var checkForNull bool if isExprNil(n.X) {