Segmentation Fault (Compiler Bug?)

I am encountering a segmentation fault in an application (compiler) that I am writing in crystal after a large refactor. The relevant code appears to be in src/parser/cast.cr on lines 28-37:

def get_cast_to( type : AST::Type, expr : AST::Expression )
    return expr if expr.type == type
    type.assert_no_type_system_breakdown( expr.type, make_error_report_state() )

    unless res = type.make_cast( expr, make_error_report_state() )
        compile_error( "Unable to cast #{expr.type.name} (#{expr.type.class}) to #{type.name} (#{type.class})" )$
    end$

    return res$
end

If it follows the return on the first line inside the function, I get a segmentation fault shortly thereafter when trying to access the value returned from the function. You can verify this by changing line 315 in src/parser/expression.cr to:

puts "casted="
casted = get_cast_to( target.type, rhs )
pp casted
puts "after"
return AST::VariableAssignment.new( target, casted )

Note that AST::Type (src/ast/type.cr) and AST::Expression (src/ast/expression.cr) are both modules.

System information:

$ crystal --version
Crystal 1.4.1 (2022-04-23)

LLVM: 13.0.1
Default target: x86_64-pc-linux-gnu

$ uname -a
Linux artemis.polaris.lan 5.17.5-arch1-1 #1 SMP PREEMPT Wed, 27 Apr 2022 20:56:11 +0000 x86_64 GNU/Linux

Steps to reproduce:

$ git clone https://git.polaris-1.work/orb-compiler.git/
$ cd orb-compiler/
$ git checkout 1b47359b18742bdc1d0d95acc5a8392864403dc0
$ mkdir build
$ ./build.sh

I have a work around for the moment (lifting the conditional out of the function), but this forces me to duplicate code and may just be masking the issue. An initial attempt to duplicate this issue in a smaller source file failed (likely because I don’t understand why crystal compiler is generating an application with this segmentation fault).

If you need more information, please let me know.

I suppose the error might be related to the fact that AST::Type is included into both class and struct types.

That appears to have been leading to the segmentation fault. Changing the items that were structs to classes stopped the segmentation fault from occurring.

Thanks for the help.