Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal error: Backend.C.FromCore.genFunDefSig: not a function #644

Open
osa1 opened this issue Dec 26, 2024 · 5 comments · May be fixed by #646
Open

internal error: Backend.C.FromCore.genFunDefSig: not a function #644

osa1 opened this issue Dec 26, 2024 · 5 comments · May be fixed by #646

Comments

@osa1
Copy link
Contributor

osa1 commented Dec 26, 2024

import std/os/env
import std/os/file
import std/os/path

fun main()
  val args = get-args()
  val file-path = match args
    Cons(file-path, Nil) -> file-path
    _ -> throw("Usage: program <file path>", ExnError)
  val file-contents = read-text-file(file-path.path)
  val file-contents-chars = file-contents.vector
  ()

value type token
  Int
  Str(size-in-bytes: int)
  True
  False
  Null
  LBracket
  RBracket
  LBrace
  RBrace
  Colon
  Comma

value struct token-iter<h>
  chars: vector<char>
  idx: ref<h, int>

value struct parse-result
  // Character index (not byte index!) of the parse event.
  char-offset: int

  // Nothing means parse error.
  token: maybe<token>

fun token-iter(chars: vector<char>): <alloc<h>> token-iter<h>
  Token-iter(chars = chars, idx = ref(0))

fun token-iter/next-char(token-iter: token-iter<h>): <read<h>, write<h>> maybe<(int, char)>
  val char-idx = !token-iter.idx
  val char = token-iter.chars.at(char-idx)
  token-iter.idx.set(char-idx + 1)
  char.map(fn(c) (char-idx, c))

fun token-iter/peek-char(token-iter: token-iter<h>): <read<h>> maybe<(int, char)>
  token-iter.chars.at(!token-iter.idx).map(fn(c) (!token-iter.idx, c))

fun token-iter/next(token-iter: token-iter<h>): <div, read<h>, write<h>> maybe<(int, token)>
  // Strange code below to work around issues with early returns in Koka 3.1.2.
  // (https://github.com/koka-lang/koka/discussions/643)

  // Skip whitespace
  val next-char0 =
    match token-iter.next-char
      Just((idx, c)) ->
        if c.is-white then
          return token-iter.next
        else
          Just((idx, c))
      Nothing -> Nothing

  val (char-idx, char) =
    match next-char0
      Nothing -> return Nothing
      Just(c) -> c

  match char
    '"' -> ()


    _ -> ()

  Nothing

Output:

parse-iter(1, 1): internal error: Backend.C.FromCore.genFunDefSig: not a function: (parse-iter/#token-iter/next,val _ : ((maybe :: V -> V)<(int, parse-iter/token)>) -> (int, char)
      = std/core/types/@dup(@return);
fn<<(div :: X),(read :: H -> X)<(h :: H)>,(write :: H -> X)<(h :: H)>>>(token-iter@0: (parse-iter/token-iter :: H -> V)<(674 :: H)>){
  val  : ();
  val @match-x64 : (maybe :: V -> V)<(int, char)>;
  match (@match-x64) {
    (std/core/types/Just(((@skip std/core/types/@Box(((@skip std/core/types/Tuple2(((@skip std/core/types/@Box((idx: int) : int) : @Box ) as @box-x59: @Box) : int, ((@skip std/core/types/@Box((c@0: char) : char) : @Box ) as @box-x60: @Box) : char) : (int, char) ) as @pat@5: (int, char)) : (int, char)) : @Box ) as @box-x58: @Box) : (int, char)) : (maybe :: V -> V)<(int, char)> )
       -> val  : ();
          val @match-x66 : bool;
      (match (@match-x66) {
        (std/core/types/True() : bool )
           -> parse-iter/token-iter/next(token-iter@0);
        (@skip std/core/types/False() : bool )
           -> val _ : ()
                    = std/core/types/@drop(token-iter@0);
          std/core/types/Nothing;
      });
    (@skip std/core/types/Nothing() : ((maybe :: V -> V)<(int, char)>) )
       -> val  : ();
          val @match-x65 : (int, char);
      (match (@match-x65) {
        (@skip std/core/types/Tuple2(((@skip std/core/types/@Box((char-idx@0: int) : int) : @Box ) as @box-x62: @Box) : int, ((@skip std/core/types/@Box((char@0: char) : char) : @Box ) as @box-x63: @Box) : char) : (int, char) )
           -> val _ : ()
                    = std/core/types/@drop(@match-x65);
          std/core/types/Nothing;
      });
  };
})
CallStack (from HasCallStack):
  error, called at src/Backend/C/FromCore.hs:326:42 in koka-3.1.2-2NpoSf9Uv2JEsgjfT7jQ0Q:Backend.C.FromCore

Failed to compile parse-iter.kk

Happens with the dev branch and the latest release.

@anfelor
Copy link
Collaborator

anfelor commented Dec 27, 2024

Likely the same issue as #186

@osa1
Copy link
Contributor Author

osa1 commented Dec 27, 2024

That seems likely, however I get the same error with #637 which is supposed to fix #186.

@TimWhiting
Copy link
Collaborator

This is similar but different, and I've run into it recently as well. Thanks for the smallish repro.
#186 is related to shadowing.

However, this seems at first glance related to the drop / dup pass adding a dup above a top level function's lambda definition. Not sure the exact root cause, but looking at the output with the --showcore flag, it looks like the unreturn pass hasn't fully translated the code to remove "return".

@TimWhiting
Copy link
Collaborator

TimWhiting commented Dec 27, 2024

Okay, I tracked down the root cause:

 val (char-idx, char) =
    match next-char0
      Nothing -> return Nothing
      Just(c) -> c
 ....

Desugars to

 match (
          match next-char0
            Nothing -> return Nothing
            Just(c) -> c
      )
   (char-idx, char) -> ....

But the grammar technically disallows returns in match scrutinees, so there is part of the unreturn pass that makes that assumption.

For now you can work around this by doing:

let tmp = 
          match next-char0
            Nothing -> return Nothing
            Just(c) -> c
let (char-idx, char) = tmp
....

@TimWhiting TimWhiting linked a pull request Dec 27, 2024 that will close this issue
@TimWhiting
Copy link
Collaborator

PR open with fix for this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants