Skip to content

Commit

Permalink
constants can't be accessed via index
Browse files Browse the repository at this point in the history
  • Loading branch information
mbellotti committed Apr 25, 2023
1 parent 3e53fd8 commit 78f4eeb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
8 changes: 7 additions & 1 deletion listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,6 @@ func (l *FaultListener) ExitRunInit(c *parser.RunInitContext) {
l.push(inst)
}


func (l *FaultListener) ExitRunSwap(c *parser.SwapContext) {
token := util.GenerateToken("SWAP", "SWAP", c.GetStart(), c.GetStop())

Expand Down Expand Up @@ -1384,6 +1383,13 @@ func (l *FaultListener) ExitAssertion(c *parser.AssertionContext) {
}
case *ast.InvariantClause:
con = e
case *ast.IndexExpression:
con = &ast.InvariantClause{
Token: e.Token,
Left: e,
Operator: "==",
Right: &ast.Boolean{Value: true},
}
}

l.push(&ast.AssertionStatement{
Expand Down
21 changes: 16 additions & 5 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var COMPARE = map[string]bool{

type Checker struct {
SpecStructs map[string]*preprocess.SpecRecord
Constants map[string]map[string]ast.Node
Instances map[string]*ast.StructInstance
inStock string
temps map[string]*ast.Type
Expand All @@ -35,7 +34,6 @@ func NewTypeChecker(Processer *preprocess.Processor) *Checker {
return &Checker{
SpecStructs: Processer.Specs,
Instances: Processer.Instances,
Constants: make(map[string]map[string]ast.Node),
temps: make(map[string]*ast.Type),
Preprocesser: Processer,
}
Expand Down Expand Up @@ -358,6 +356,8 @@ func (c *Checker) isValue(exp interface{}) bool {
return true
case *ast.ComponentLiteral:
return true
case *ast.IndexExpression:
return true
default:
return false
}
Expand Down Expand Up @@ -461,6 +461,20 @@ func (c *Checker) infer(exp interface{}) (ast.Node, error) {
}
return node, nil
case *ast.IndexExpression:
rawid := node.Left.(ast.Nameable).RawId()
spec := c.SpecStructs[rawid[0]]
con, _ := spec.FetchConstant(rawid[1])
if con != nil {
return nil, fmt.Errorf("variable %s is a constant cannot access by index", node.Left.String())
}

if node.InferredType == nil {
t, err := c.LookupType(node.Left)
if err != nil {
return nil, err
}
node.InferredType = t
}
return node, nil
case *ast.ComponentLiteral:
if node.InferredType == nil {
Expand Down Expand Up @@ -770,8 +784,6 @@ func (c *Checker) inferFunction(f ast.Expression) (ast.Expression, error) {
node.InferredType = node.Consequence.InferredType // This is probably an incorrect approach. Need to think about it.
return node, err

case *ast.IndexExpression:
return node, err
case *ast.PrefixExpression:
var nr ast.Node
if c.isValue(node.Right) {
Expand Down Expand Up @@ -997,7 +1009,6 @@ func (c *Checker) lookupReference(base ast.Node) (ast.Node, error) {
}

// Assume it's referencing a constant then
spec = c.SpecStructs[b.Spec]
n, _ := spec.FetchConstant(b.Value)
if n != nil {
return n, err
Expand Down
14 changes: 14 additions & 0 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,20 @@ func TestSwapError3(t *testing.T) {
}
}

func TestIndexError(t *testing.T) {
test := `spec test;
const a = 10;
assert a[2];
`

_, err := prepTest(test, true)

actual := "variable a is a constant cannot access by index"

if err == nil || err.Error() != actual {
t.Fatalf("Type checking failed to catch invalid expression. got=%s", err)
}
}

func prepTest(test string, specType bool) (*Checker, error) {
flags := make(map[string]bool)
Expand Down

0 comments on commit 78f4eeb

Please sign in to comment.