You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An unexpected invalid memory address or nil pointer dereference error occurs when performing a nil check (== nil) on a variable within the switch statement cases inside a closure.
This issue appears in all cases of the switch statement except for the first case.
The program successfully executes the first case but panics when attempting to execute any subsequent case.
package main
import (
"fmt"
)
funcgetList(kindint) {
params:= []int{}
visit(kind, func(childKindint) {
switchchildKind {
case1:
fmt.Printf("Case 1:params is nil: %t\n", params==nil)
fmt.Printf("Length of params: %d\n", len(params))
case2:
fmt.Printf("Case 2:params is nil: %t\n", params==nil)
fmt.Printf("Length of params: %d\n", len(params))
case3:
fmt.Printf("Case 3:params is nil: %t\n", params==nil)
fmt.Printf("Length of params: %d\n", len(params))
}
})
}
funcvisit(kindint, visitorfunc(int)) {
visitor(kind)
}
funcmain() {
getList(1)
getList(2)
}
Expected
❯ go run .
Case 1:params is nil: false
Length of params: 0
Case 2:params is nil: false
Length of params: 0
Actual Got
❯ llgo run .
Case 1:params is nil: false
Length of params: 0
panic: runtime error: invalid memory address or nil pointer dereference
This behavior is consistent whether using a switch statement or an if-else structure. For example, the following if-else structure exhibits the same behavior:
if childKind == 1 {
fmt.Printf("Case 1:params is nil: %t\n", params == nil)
fmt.Printf("Length of params: %d\n", len(params))
} else if childKind == 2 {
fmt.Printf("Case 2:params is nil: %t\n", params == nil)
fmt.Printf("Length of params: %d\n", len(params))
} else if childKind == 3 {
fmt.Printf("Case 3:params is nil: %t\n", params == nil)
fmt.Printf("Length of params: %d\n", len(params))
}
In this case, the first if statement (childKind == 1) executes successfully, but the program panics when trying to execute either of the else if statements.
An unexpected invalid memory address or nil pointer dereference error occurs when performing a nil check (== nil) on a variable within the switch statement cases inside a closure.
This issue appears in all cases of the switch statement except for the first case.
The program successfully executes the first case but panics when attempting to execute any subsequent case.
Expected
Actual Got
This behavior is consistent whether using a switch statement or an if-else structure. For example, the following if-else structure exhibits the same behavior:
In this case, the first if statement (childKind == 1) executes successfully, but the program panics when trying to execute either of the else if statements.
Env
llgo version: Latest main branch, commit dbaf12b
The text was updated successfully, but these errors were encountered: