Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit b1ebb0c

Browse files
committed
Merge pull request #104 from antongulenko/master
Fixed handling of nil-literals
2 parents 3eb1a52 + 19dcebd commit b1ebb0c

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (c *compiler) Resolve(ident *ast.Ident) Value {
136136
value = c.NewConstValue(obj.Val(), obj.Type())
137137

138138
default:
139-
panic(fmt.Sprintf("unreachable (%T)", obj))
139+
panic(fmt.Sprintf("Could not handle type in compiler.Resolve(): (%T)", obj))
140140
}
141141

142142
data.Value = value

expr.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package llgo
66

77
import (
88
"code.google.com/p/go.tools/go/types"
9+
"code.google.com/p/go.tools/go/exact"
910
"fmt"
1011
"github.com/axw/gollvm/llvm"
1112
"go/ast"
@@ -534,12 +535,19 @@ func (c *compiler) VisitTypeAssertExpr(expr *ast.TypeAssertExpr) Value {
534535

535536
func (c *compiler) VisitExpr(expr ast.Expr) Value {
536537
c.setDebugLine(expr.Pos())
538+
537539
// Before all else, check if we've got a constant expression.
538540
// go/types performs constant folding, and we store the value
539541
// alongside the expression's type.
540542
if constval := c.typeinfo.Values[expr]; constval != nil {
541543
return c.NewConstValue(constval, c.typeinfo.Types[expr])
542544
}
545+
// nil-literals are parsed to Ident-nodes for some reason,
546+
// treat them like constant values here.
547+
// TODO nil literals should be represented more appropriately.
548+
if identval, valid := expr.(*ast.Ident); valid && identval.Name == "nil" {
549+
return c.NewConstValue(exact.MakeUnknown(), c.typeinfo.Types[expr])
550+
}
543551

544552
switch x := expr.(type) {
545553
case *ast.BinaryExpr:

value.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func (c *compiler) NewValue(v llvm.Value, t types.Type) *LLVMValue {
5656

5757
func (c *compiler) NewConstValue(v exact.Value, typ types.Type) *LLVMValue {
5858
switch {
59-
case v.Kind() == exact.Nil:
59+
case v.Kind() == exact.Unknown:
60+
// TODO nil literals should be represented more appropriately once the exact-package supports it.
6061
llvmtyp := c.types.ToLLVM(typ)
6162
return c.NewValue(llvm.ConstNull(llvmtyp), typ)
6263

0 commit comments

Comments
 (0)