Skip to content

Commit

Permalink
Handle case where LookupVar() returns nil, false, nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Dec 29, 2023
1 parent edab73f commit 99e659b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions compiler/ast/ssagen.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,14 @@ func (ast *Assign) SSA(block *ssa.Block, ctx *Codegen,
}
switch arr := lv.Expr.(type) {
case *VariableRef:
lrv, _, err := ctx.LookupVar(block, gen, block.Bindings, arr)
lrv, ok, err := ctx.LookupVar(block, gen, block.Bindings, arr)
if err != nil {
return nil, nil, err
}
if !ok {
return nil, nil,
ctx.Errorf(arr, "value %v not constant", arr)
}
valueType := lrv.ValueType()
if valueType.Type == types.TPtr {
valueType = *valueType.ElementType
Expand Down Expand Up @@ -1787,7 +1791,7 @@ func (ast *Unary) SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator) (
return nil, nil, err
}
if !ok {
return nil, nil, err
return nil, nil, ctx.Errorf(v, "value %v not constant", v)
}

valueType := lrv.ValueType()
Expand Down Expand Up @@ -1839,13 +1843,17 @@ func (ast *Unary) addrIndex(block *ssa.Block, ctx *Codegen, gen *ssa.Generator,
index *Index) (
lrv *LRValue, ptrType *types.Info, offset types.Size, err error) {

var ok bool
switch indexed := index.Expr.(type) {
case *VariableRef:
lrv, _, err = ctx.LookupVar(block, gen, block.Bindings,
indexed)
lrv, ok, err = ctx.LookupVar(block, gen, block.Bindings, indexed)
if err != nil {
return
}
if !ok {
err = ctx.Errorf(indexed, "value %v not constant", indexed)
return
}
vt := lrv.ValueType()
if vt.Type != types.TArray {
return nil, nil, 0, ctx.Errorf(indexed,
Expand Down Expand Up @@ -2163,10 +2171,13 @@ func (ast *Index) index(block *ssa.Block, ctx *Codegen, gen *ssa.Generator,
func (ast *VariableRef) SSA(block *ssa.Block, ctx *Codegen,
gen *ssa.Generator) (*ssa.Block, []ssa.Value, error) {

lrv, _, err := ctx.LookupVar(block, gen, block.Bindings, ast)
lrv, ok, err := ctx.LookupVar(block, gen, block.Bindings, ast)
if err != nil {
return nil, nil, err
}
if !ok {
return nil, nil, ctx.Errorf(ast, "value %v not constant", ast)
}

value := lrv.RValue()
if value.Const {
Expand Down

0 comments on commit 99e659b

Please sign in to comment.