Compile time segmentation fault when using a variable bound to a procedure

Hi,

Bellow I’ve provided simplified code that produces the segmentation whenever I invoke a procedure bound to a variable, parse. (Sorry if it is a little verbose, just want to make sure that if I’m doing something wrong that it is visible.)

package main

import "base:runtime"
import "core:fmt"

Unexpected_Token :: struct {
  loc: runtime.Source_Code_Location,
  reason: string,
}

Parse_Error :: union {
  Unexpected_Token,
}

Parser :: struct { cursor: int }

Parse_Proc :: proc(p: Parser, loc: runtime.Source_Code_Location) -> (Parse_Proc, Parse_Error)

parse_definition :: proc(p: Parser, loc := #caller_location) -> (Parse_Proc, Parse_Error) {
	return nil, nil
}

main :: proc() {
  err: Parse_Error
  parse: Parse_Proc = parse_definition
  
  p := Parser{}
  for parse != nil {
    parse, err = parse(p, #location())
    if err != nil do break
  }

  if err != nil do switch v in err {
  case Unexpected_Token:
    fmt.println(v.loc, v.reason)
  }
}

Trying to build this on my linux machine (I have no other machines :pensive:) produces the following.

$ odin build src -out:build/ssp
[1]    160168 segmentation fault (core dumped)  odin build src -out:build/ssp

The issue does not seem to only be invocation, using parse at all seems to cause the segfault.

parse: Parse_Proc
  
if parse != nil {
  fmt.println("We do in fact have parse defined!")
}

I’m not entirely sure this is a bug, maybe I’m doing something I’m not supposed to?

Thanks in advance.

I can confirm this crash on Linux and have reduced it to the following:

package main

Parse_Proc :: proc() -> (Parse_Proc, int)

parse_definition :: proc() -> (Parse_Proc, int) {
	return nil, 0
}

main :: proc() {
	parse := parse_definition
}

I suspected that the recursive proc definition was the cause, but if I remove int from the return signature, it compiles.

2 Likes