Skip to content

Generated C compilation error with function pointer #618

@zyedidia

Description

@zyedidia
module body Example.CodegenBug is
    function double(x: Int64): Int64 is
        return x * 2;
    end;

    function consume(x: Int64): Unit is
        printLn(x);
        return nil;
    end;

    function main(): ExitCode is
        let f: Fn[Int64, Int64] := double;
        -- Bug: calling a function pointer as an argument to another function
        -- generates a spurious semicolon in the C output.
        consume(f(10));
        return ExitSuccess();
    end;
end module body.

When compiled with the Austral compiler this produces invalid C code:

Compiler call tree printed to calltree.html
Error:
  Title: Command Line Arguments Error
  Module:
    [unknown]
  Location:
    [no span available]
  Description:
    Failed to run C compiler.
    
    Command: `cc /tmp/austral_fa8934.c -fwrapv -lm -o main`
    
    Exit code: 1
    
    Standard output:
    
    
    Standard error:
    /tmp/austral_fa8934.c: In function 'decl_281':
    /tmp/austral_fa8934.c:4819:79: error: expected ')' before ';' token
     4819 |     decl_283(((au_int64_t) ((au_int64_t(*)(au_int64_t))(f))(((au_int64_t) 10));));
          |              ~                                                                ^
          |                                                                               )
  Code:
    [no span available]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions