Repository: VKCOM/noverify
Pull request #700 (2020-08-21 22:07:43)

constant: rename constant value

quasilyte: Looks good, but I would bump the cache version.

Patch:
diff --git a/src/constfold/binary_op.go b/src/constfold/binary_op.go
index 2253608..120bbc5 100644
--- a/src/constfold/binary_op.go
+++ b/src/constfold/binary_op.go
@@ -5,98 +5,98 @@ import (
 )
 
 // Plus performs arithmetic "+".
-func Plus(x, y meta.ConstantValue) meta.ConstantValue {
+func Plus(x, y meta.ConstValue) meta.ConstValue {
 	switch x.Type {
 	case meta.Integer:
 		if y.Type == meta.Integer {
-			return meta.ConstantIntValue(x.GetInt() + y.GetInt())
+			return meta.NewIntConst(x.GetInt() + y.GetInt())
 		}
 	case meta.Float:
 		if y.Type == meta.Float {
-			return meta.ConstantFloatValue(x.GetFloat() + y.GetFloat())
+			return meta.NewFloatConst(x.GetFloat() + y.GetFloat())
 		}
 	}
 	return meta.UnknownValue
 }
 
 // Minus performs arithmetic "-".
-func Minus(x, y meta.ConstantValue) meta.ConstantValue {
+func Minus(x, y meta.ConstValue) meta.ConstValue {
 	switch x.Type {
 	case meta.Integer:
 		if y.Type == meta.Integer {
-			return meta.ConstantIntValue(x.GetInt() - y.GetInt())
+			return meta.NewIntConst(x.GetInt() - y.GetInt())
 		}
 	case meta.Float:
 		if y.Type == meta.Float {
-			return meta.ConstantFloatValue(x.GetFloat() - y.GetFloat())
+			return meta.NewFloatConst(x.GetFloat() - y.GetFloat())
 		}
 	}
 	return meta.UnknownValue
 }
 
 // Mul performs arithmetic "*".
-func Mul(x, y meta.ConstantValue) meta.ConstantValue {
+func Mul(x, y meta.ConstValue) meta.ConstValue {
 	switch x.Type {
 	case meta.Integer:
 		if y.Type == meta.Integer {
-			return meta.ConstantIntValue(x.GetInt() * y.GetInt())
+			return meta.NewIntConst(x.GetInt() * y.GetInt())
 		}
 	case meta.Float:
 		if y.Type == meta.Float {
-			return meta.ConstantFloatValue(x.GetFloat() * y.GetFloat())
+			return meta.NewFloatConst(x.GetFloat() * y.GetFloat())
 		}
 	}
 	return meta.UnknownValue
 }
 
 // Concat performs string "." operation.
-func Concat(x, y meta.ConstantValue) meta.ConstantValue {
+func Concat(x, y meta.ConstValue) meta.ConstValue {
 	v1, ok1 := x.ToString()
 	v2, ok2 := y.ToString()
 	if ok1 && ok2 {
-		return meta.ConstantStringValue(v1 + v2)
+		return meta.NewStringConstant(v1 + v2)
 	}
 	return meta.UnknownValue
 }
 
 // Or performs logical "||".
 // Also works for "or" operator.
-func Or(x, y meta.ConstantValue) meta.ConstantValue {
+func Or(x, y meta.ConstValue) meta.ConstValue {
 	v1, ok1 := x.ToBool()
 	v2, ok2 := y.ToBool()
 	if ok1 && ok2 {
-		return meta.ConstantBoolValue(v1 || v2)
+		return meta.NewBoolConstant(v1 || v2)
 	}
 	return meta.UnknownValue
 }
 
 // And performs logical "&&".
 // Also works for "and" operator.
-func And(x, y meta.ConstantValue) meta.ConstantValue {
+func And(x, y meta.ConstValue) meta.ConstValue {
 	v1, ok1 := x.ToBool()
 	v2, ok2 := y.ToBool()
 	if ok1 && ok2 {
-		return meta.ConstantBoolValue(v1 && v2)
+		return meta.NewBoolConstant(v1 && v2)
 	}
 	return meta.UnknownValue
 }
 
 // BitOr performs bitwise "|".
-func BitOr(x, y meta.ConstantValue) meta.ConstantValue {
+func BitOr(x, y meta.ConstValue) meta.ConstValue {
 	v1, ok1 := x.ToInt()
 	v2, ok2 := y.ToInt()
 	if ok1 && ok2 {
-		return meta.ConstantIntValue(v1 | v2)
+		return meta.NewIntConst(v1 | v2)
 	}
 	return meta.UnknownValue
 }
 
 // BitAnd performs bitwise "&".
-func BitAnd(x, y meta.ConstantValue) meta.ConstantValue {
+func BitAnd(x, y meta.ConstValue) meta.ConstValue {
 	v1, ok1 := x.ToInt()
 	v2, ok2 := y.ToInt()
 	if ok1 && ok2 {
-		return meta.ConstantIntValue(v1 & v2)
+		return meta.NewIntConst(v1 & v2)
 	}
 	return meta.UnknownValue
 }
diff --git a/src/constfold/eval.go b/src/constfold/eval.go
index 1d5872b..6f122d9 100644
--- a/src/constfold/eval.go
+++ b/src/constfold/eval.go
@@ -10,7 +10,7 @@ import (
 
 // Eval tries to compute the e using the constant expressions folding.
 // In case of failure, meta.UnknownValue is returned.
-func Eval(st *meta.ClassParseState, e ir.Node) meta.ConstantValue {
+func Eval(st *meta.ClassParseState, e ir.Node) meta.ConstValue {
 	// TODO: support more operators and some builtin PHP functions like strlen.
 
 	switch e := e.(type) {
@@ -83,17 +83,17 @@ func Eval(st *meta.ClassParseState, e ir.Node) meta.ConstantValue {
 		if err != nil {
 			return meta.UnknownValue
 		}
-		return meta.ConstantValue{Type: meta.Integer, Value: value}
+		return meta.ConstValue{Type: meta.Integer, Value: value}
 
 	case *ir.Dnumber:
 		value, err := strconv.ParseFloat(e.Value, 64)
 		if err != nil {
 			return meta.UnknownValue
 		}
-		return meta.ConstantValue{Type: meta.Float, Value: value}
+		return meta.ConstValue{Type: meta.Float, Value: value}
 
 	case *ir.String:
-		return meta.ConstantValue{Value: e.Value, Type: meta.String}
+		return meta.ConstValue{Value: e.Value, Type: meta.String}
 	}
 
 	return meta.UnknownValue
diff --git a/src/constfold/unary_op.go b/src/constfold/unary_op.go
index 939a51d..5fc958f 100644
--- a/src/constfold/unary_op.go
+++ b/src/constfold/unary_op.go
@@ -5,21 +5,21 @@ import (
 )
 
 // Not performs unary "!".
-func Not(x meta.ConstantValue) meta.ConstantValue {
+func Not(x meta.ConstValue) meta.ConstValue {
 	v, ok := x.ToBool()
 	if !ok {
 		return meta.UnknownValue
 	}
-	return meta.ConstantBoolValue(!v)
+	return meta.NewBoolConstant(!v)
 }
 
 // Neg performs unary "-".
-func Neg(x meta.ConstantValue) meta.ConstantValue {
+func Neg(x meta.ConstValue) meta.ConstValue {
 	switch x.Type {
 	case meta.Integer:
-		return meta.ConstantIntValue(-x.GetInt())
+		return meta.NewIntConst(-x.GetInt())
 	case meta.Float:
-		return meta.ConstantFloatValue(-x.GetFloat())
+		return meta.NewFloatConst(-x.GetFloat())
 	}
 	return meta.UnknownValue
 }
diff --git a/src/ir/normalize/normalize.go b/src/ir/normalize/normalize.go
index 5b1abf7..9922723 100644
--- a/src/ir/normalize/normalize.go
+++ b/src/ir/normalize/normalize.go
@@ -489,7 +489,7 @@ func encapsedPartToConcatArg(n ir.Node) ir.Node {
 	}
 }
 
-func constToIR(v meta.ConstantValue) ir.Node {
+func constToIR(v meta.ConstValue) ir.Node {
 	value := v.Value
 	switch v.Type {
 	case meta.Integer:
diff --git a/src/linter/cache.go b/src/linter/cache.go
index 04c3f40..66b27a0 100644
--- a/src/linter/cache.go
+++ b/src/linter/cache.go
@@ -42,7 +42,8 @@ import (
 //     41 - const-folding affected const definition values
 //     42 - bool-typed consts are now stored in meta info
 //     43 - define'd const values stored in cache
-const cacheVersion = 43
+//     44 - rename ConstantInfo => ConstInfo
+const cacheVersion = 44
 
 var (
 	errWrongVersion = errors.New("Wrong cache version")
diff --git a/src/linter/root.go b/src/linter/root.go
index 0a4a1f5..4fd3956 100644
--- a/src/linter/root.go
+++ b/src/linter/root.go
@@ -942,7 +942,7 @@ func (d *RootWalker) enterClassConstList(s *ir.ClassConstListStmt) bool {
 		value := constfold.Eval(d.ctx.st, c.Expr)
 
 		// TODO: handle duplicate constant
-		cl.Constants[nm] = meta.ConstantInfo{
+		cl.Constants[nm] = meta.ConstInfo{
 			Pos:         d.getElementPos(c),
 			Typ:         typ.Immutable(),
 			AccessLevel: accessLevel,
@@ -1613,7 +1613,7 @@ func (d *RootWalker) enterFunctionCall(s *ir.FunctionCallExpr) bool {
 
 	value := constfold.Eval(d.ctx.st, valueArg)
 
-	d.meta.Constants[`\`+strings.TrimFunc(str.Value, isQuote)] = meta.ConstantInfo{
+	d.meta.Constants[`\`+strings.TrimFunc(str.Value, isQuote)] = meta.ConstInfo{
 		Pos:   d.getElementPos(s),
 		Typ:   solver.ExprTypeLocal(d.scope(), d.ctx.st, valueArg.Expr),
 		Value: value,
@@ -1704,7 +1704,7 @@ func (d *RootWalker) enterConstList(lst *ir.ConstListStmt) bool {
 		id := s.ConstantName
 		nm := d.ctx.st.Namespace + `\` + id.Value
 
-		d.meta.Constants[nm] = meta.ConstantInfo{
+		d.meta.Constants[nm] = meta.ConstInfo{
 			Pos:   d.getElementPos(s),
 			Typ:   solver.ExprTypeLocal(d.scope(), d.ctx.st, s.Expr),
 			Value: value,
diff --git a/src/meta/const_value.go b/src/meta/const_value.go
index fef2e19..61094a4 100644
--- a/src/meta/const_value.go
+++ b/src/meta/const_value.go
@@ -6,11 +6,11 @@ import (
 	"strconv"
 )
 
-type ConstantValueType uint8
+type ConstValueType uint8
 
-//go:generate stringer -type=ConstantValueType
+//go:generate stringer -type=ConstValueType
 const (
-	Undefined ConstantValueType = iota
+	Undefined ConstValueType = iota
 	Integer
 	Float
 	String
@@ -18,55 +18,55 @@ const (
 )
 
 var (
-	UnknownValue = ConstantValue{Type: Undefined}
-	TrueValue    = ConstantValue{Type: Bool, Value: true}
-	FalseValue   = ConstantValue{Type: Bool, Value: false}
+	UnknownValue = ConstValue{Type: Undefined}
+	TrueValue    = ConstValue{Type: Bool, Value: true}
+	FalseValue   = ConstValue{Type: Bool, Value: false}
 )
 
-type ConstantValue struct {
-	Type  ConstantValueType
+type ConstValue struct {
+	Type  ConstValueType
 	Value interface{}
 }
 
-func ConstantIntValue(v int64) ConstantValue {
-	return ConstantValue{Type: Integer, Value: v}
+func NewIntConst(v int64) ConstValue {
+	return ConstValue{Type: Integer, Value: v}
 }
 
-func ConstantFloatValue(v float64) ConstantValue {
-	return ConstantValue{Type: Float, Value: v}
+func NewFloatConst(v float64) ConstValue {
+	return ConstValue{Type: Float, Value: v}
 }
 
-func ConstantStringValue(v string) ConstantValue {
-	return ConstantValue{Type: String, Value: v}
+func NewStringConstant(v string) ConstValue {
+	return ConstValue{Type: String, Value: v}
 }
 
-func ConstantBoolValue(v bool) ConstantValue {
-	return ConstantValue{Type: Bool, Value: v}
+func NewBoolConstant(v bool) ConstValue {
+	return ConstValue{Type: Bool, Value: v}
 }
 
 // GetInt returns the value stored in c.Value cast to int type.
-func (c ConstantValue) GetInt() int64 {
+func (c ConstValue) GetInt() int64 {
 	return c.Value.(int64)
 }
 
 // GetFloat returns the value stored in c.Value cast to float type.
-func (c ConstantValue) GetFloat() float64 {
+func (c ConstValue) GetFloat() float64 {
 	return c.Value.(float64)
 }
 
 // GetString returns the value stored in c.Value cast to string type.
-func (c ConstantValue) GetString() string {
+func (c ConstValue) GetString() string {
 	return c.Value.(string)
 }
 
 // GetBool returns the value stored in c.Value cast to bool type.
-func (c ConstantValue) GetBool() bool {
+func (c ConstValue) GetBool() bool {
 	return c.Value.(bool)
 }
 
 // ToBool converts x constant to boolean constants following PHP conversion rules.
 // Second bool result tells whether that conversion was successful.
-func (c ConstantValue) ToBool() (bool, bool) {
+func (c ConstValue) ToBool() (bool, bool) {
 	switch c.Type {
 	case Bool:
 		return c.GetBool(), true
@@ -84,7 +84,7 @@ func (c ConstantValue) ToBool() (bool, bool) {
 
 // ToInt converts x constant to int constants following PHP conversion rules.
 // Second bool result tells whether that conversion was successful.
-func (c ConstantValue) ToInt() (int64, bool) {
+func (c ConstValue) ToInt() (int64, bool) {
 	switch c.Type {
 	case Bool:
 		if c.GetBool() {
@@ -101,7 +101,7 @@ func (c ConstantValue) ToInt() (int64, bool) {
 
 // ToString converts x constant to string constants following PHP conversion rules.
 // Second bool result tells whether that conversion was successful.
-func (c ConstantValue) ToString() (string, bool) {
+func (c ConstValue) ToString() (string, bool) {
 	switch c.Type {
 	case Bool:
 		if c.GetBool() {
@@ -116,7 +116,7 @@ func (c ConstantValue) ToString() (string, bool) {
 	return "", false
 }
 
-func (c ConstantValue) GobEncode() ([]byte, error) {
+func (c ConstValue) GobEncode() ([]byte, error) {
 	switch c.Type {
 	case Float:
 		val, ok := c.Value.(float64)
@@ -155,12 +155,12 @@ func (c ConstantValue) GobEncode() ([]byte, error) {
 	return nil, fmt.Errorf("unhandeled type")
 }
 
-func (c *ConstantValue) GobDecode(buf []byte) error {
+func (c *ConstValue) GobDecode(buf []byte) error {
 	if len(buf) < 1 {
 		return fmt.Errorf("data corrupted")
 	}
 
-	tp := ConstantValueType(buf[0])
+	tp := ConstValueType(buf[0])
 	buf = buf[1:]
 	val := string(buf)
 
@@ -195,7 +195,7 @@ func (c *ConstantValue) GobDecode(buf []byte) error {
 	return nil
 }
 
-func (c ConstantValue) String() string {
+func (c ConstValue) String() string {
 	if c.Type == Undefined {
 		return "Undefined type"
 	}
@@ -203,7 +203,7 @@ func (c ConstantValue) String() string {
 	return fmt.Sprintf("%s(%v)", c.Type, c.Value)
 }
 
-func (c ConstantValue) IsEqual(v ConstantValue) bool {
+func (c ConstValue) IsEqual(v ConstValue) bool {
 	if v.Type == Undefined || c.Type == Undefined {
 		return false
 	}
@@ -211,9 +211,9 @@ func (c ConstantValue) IsEqual(v ConstantValue) bool {
 	return c.Value == v.Value
 }
 
-type ConstantInfo struct {
+type ConstInfo struct {
 	Pos         ElementPosition
 	Typ         TypesMap
 	AccessLevel AccessLevel
-	Value       ConstantValue
+	Value       ConstValue
 }
diff --git a/src/meta/constantvaluetype_string.go b/src/meta/constantvaluetype_string.go
deleted file mode 100644
index 418f896..0000000
--- a/src/meta/constantvaluetype_string.go
+++ /dev/null
@@ -1,27 +0,0 @@
-// Code generated by "stringer -type=ConstantValueType"; DO NOT EDIT.
-
-package meta
-
-import "strconv"
-
-func _() {
-	// An "invalid array index" compiler error signifies that the constant values have changed.
-	// Re-run the stringer command to generate them again.
-	var x [1]struct{}
-	_ = x[Undefined-0]
-	_ = x[Integer-1]
-	_ = x[Float-2]
-	_ = x[String-3]
-	_ = x[Bool-4]
-}
-
-const _ConstantValueType_name = "UndefinedIntegerFloatStringBool"
-
-var _ConstantValueType_index = [...]uint8{0, 9, 16, 21, 27, 31}
-
-func (i ConstantValueType) String() string {
-	if i >= ConstantValueType(len(_ConstantValueType_index)-1) {
-		return "ConstantValueType(" + strconv.FormatInt(int64(i), 10) + ")"
-	}
-	return _ConstantValueType_name[_ConstantValueType_index[i]:_ConstantValueType_index[i+1]]
-}
diff --git a/src/meta/constvaluetype_string.go b/src/meta/constvaluetype_string.go
new file mode 100644
index 0000000..0505b63
--- /dev/null
+++ b/src/meta/constvaluetype_string.go
@@ -0,0 +1,27 @@
+// Code generated by "stringer -type=ConstValueType"; DO NOT EDIT.
+
+package meta
+
+import "strconv"
+
+func _() {
+	// An "invalid array index" compiler error signifies that the constant values have changed.
+	// Re-run the stringer command to generate them again.
+	var x [1]struct{}
+	_ = x[Undefined-0]
+	_ = x[Integer-1]
+	_ = x[Float-2]
+	_ = x[String-3]
+	_ = x[Bool-4]
+}
+
+const _ConstValueType_name = "UndefinedIntegerFloatStringBool"
+
+var _ConstValueType_index = [...]uint8{0, 9, 16, 21, 27, 31}
+
+func (i ConstValueType) String() string {
+	if i >= ConstValueType(len(_ConstValueType_index)-1) {
+		return "ConstValueType(" + strconv.FormatInt(int64(i), 10) + ")"
+	}
+	return _ConstValueType_name[_ConstValueType_index[i]:_ConstValueType_index[i+1]]
+}
diff --git a/src/meta/metainfo.go b/src/meta/metainfo.go
index cc54764..c7aa922 100644
--- a/src/meta/metainfo.go
+++ b/src/meta/metainfo.go
@@ -65,7 +65,7 @@ type PerFile struct {
 	Constants ConstantsMap
 }
 
-func (i *info) GetConstant(nm string) (res ConstantInfo, ok bool) {
+func (i *info) GetConstant(nm string) (res ConstInfo, ok bool) {
 	res, ok = i.allConstants[nm]
 	return res, ok
 }
@@ -404,7 +404,7 @@ type ClassParseState struct {
 
 type FunctionsOverrideMap map[string]FuncInfoOverride
 type PropertiesMap map[string]PropertyInfo
-type ConstantsMap map[string]ConstantInfo
+type ConstantsMap map[string]ConstInfo
 
 type ElementPosition struct {
 	Filename  string
diff --git a/src/solver/oop.go b/src/solver/oop.go
index 4cc92e1..7037f4b 100644
--- a/src/solver/oop.go
+++ b/src/solver/oop.go
@@ -83,10 +83,10 @@ func GetClassName(cs *meta.ClassParseState, classNode ir.Node) (className string
 }
 
 // GetConstant searches for specified constant in const fetch.
-func GetConstant(cs *meta.ClassParseState, constNode ir.Node) (constName string, ci meta.ConstantInfo, ok bool) {
+func GetConstant(cs *meta.ClassParseState, constNode ir.Node) (constName string, ci meta.ConstInfo, ok bool) {
 	nm, ok := constNode.(*ir.Name)
 	if !ok {
-		return "", meta.ConstantInfo{}, false
+		return "", meta.ConstInfo{}, false
 	}
 
 	nameStr := nm.Value
@@ -111,5 +111,5 @@ func GetConstant(cs *meta.ClassParseState, constNode ir.Node) (constName string,
 		}
 	}
 
-	return "", meta.ConstantInfo{}, false
+	return "", meta.ConstInfo{}, false
 }
diff --git a/src/solver/solver.go b/src/solver/solver.go
index b376a42..b43bbd4 100644
--- a/src/solver/solver.go
+++ b/src/solver/solver.go
@@ -532,12 +532,12 @@ func interfaceExtends(orig string, parent string, visited map[string]struct{}) b
 }
 
 // FindConstant searches for a costant in specified class and returns actual class that contains the constant.
-func FindConstant(className string, constName string) (res meta.ConstantInfo, implClassName string, ok bool) {
+func FindConstant(className string, constName string) (res meta.ConstInfo, implClassName string, ok bool) {
 	visitedClasses := make(map[string]struct{}, 8) // expecting to be not so many inheritance levels
 	return findConstant(className, constName, visitedClasses)
 }
 
-func findConstant(className string, constName string, visitedClasses map[string]struct{}) (res meta.ConstantInfo, implClassName string, ok bool) {
+func findConstant(className string, constName string, visitedClasses map[string]struct{}) (res meta.ConstInfo, implClassName string, ok bool) {
 	for {
 		// check for inheritance loops
 		if _, ok := visitedClasses[className]; ok {


Test patch:
diff --git a/src/linter/cache_test.go b/src/linter/cache_test.go
index 061fe7e..a219fa7 100644
--- a/src/linter/cache_test.go
+++ b/src/linter/cache_test.go
@@ -11,8 +11,9 @@ import (
 	"strings"
 	"testing"
 
-	"github.com/VKCOM/noverify/src/linttest/assert"
 	"github.com/google/go-cmp/cmp"
+
+	"github.com/VKCOM/noverify/src/linttest/assert"
 )
 
 func TestCache(t *testing.T) {
@@ -119,7 +120,7 @@ main();
 		//
 		// If cache encoding changes, there is a very high chance that
 		// encoded data lengh will change as well.
-		wantLen := 4846
+		wantLen := 4843
 		haveLen := buf.Len()
 		if haveLen != wantLen {
 			t.Errorf("cache len mismatch:\nhave: %d\nwant: %d", haveLen, wantLen)
@@ -128,7 +129,7 @@ main();
 		// 2. Check cache "strings" hash.
 		//
 		// It catches new fields in cached types, field renames and encoding of additional named attributes.
-		wantStrings := "cc4141d5dc69f8f49dfaa717c59596ee5e6c490b8274fe2742393bea87c1fcd0cc4be443127b117cdd8e58aeef09603c548e6461d2f45ff7be7fb6834b40533c"
+		wantStrings := "a1509317129f7b0556e2ecf6b52c3319ffcde53180f81def1f28be025d437171026e908ec87ee8ddd44bc315295c5f51620f58838f759e289131e79d0307c8e1"
 		haveStrings := collectCacheStrings(buf.String())
 		if haveStrings != wantStrings {
 			t.Errorf("cache strings mismatch:\nhave: %q\nwant: %q", haveStrings, wantStrings)
diff --git a/src/meta/encoding_test.go b/src/meta/encoding_test.go
index 418e70e..ab7e64f 100644
--- a/src/meta/encoding_test.go
+++ b/src/meta/encoding_test.go
@@ -96,7 +96,7 @@ func TestTypeEncoding(t *testing.T) {
 }
 
 func TestConstantValueDecodeEncode(t *testing.T) {
-	testCases := []ConstantValue{
+	testCases := []ConstValue{
 		{Type: String, Value: "world"},
 		{Type: Integer, Value: int64(5)},
 		{Type: Float, Value: 5.56},
@@ -113,7 +113,7 @@ func TestConstantValueDecodeEncode(t *testing.T) {
 		}
 
 		// decode this
-		decoded := ConstantValue{}
+		decoded := ConstValue{}
 		err = decoded.GobDecode(encoded)
 		if err != nil {
 			t.Errorf("unexpected error \"%s\"", err)