lfortran / lfortran

Official main repository for LFortran
https://lfortran.org/
Other
906 stars 137 forks source link

Bug in `allocate()` with `source` keyword #3640

Open Shaikh-Ubaid opened 4 months ago

Shaikh-Ubaid commented 4 months ago
% cat examples/expr9.f90 
program expr2
    implicit none

    character(len=:), allocatable :: x
    character(len=:), allocatable :: y
    allocate(character(len=5) :: x)
    x = "what!"

    ! call move_alloc(x, y)
    allocate(y, source=x)
    deallocate(x)

    print *, allocated(x)
    print *, allocated(y), y

end program
% gfortran examples/expr9.f90
% ./a.out 
 F
 T what!
% lfortran_in_main examples/expr9.f90 
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/bin/lfortran.cpp", line 2391
    LCompilers::print_stack_on_segfault();
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/bin/lfortran.cpp", line 2355
    err = compile_to_object_file(arg_file, tmp_o, false,
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/bin/lfortran.cpp", line 949
    res = fe.get_llvm3(*asr, lpm, diagnostics, infile);
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/lfortran/fortran_evaluator.cpp", line 360
    compiler_options, run_fn, infile);
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/libasr/codegen/asr_to_llvm.cpp", line 9745
    v.visit_asr((ASR::asr_t&)asr);
  File "../libasr/asr.h", line 5136
  File "../libasr/asr.h", line 5112
  File "../libasr/asr.h", line 5137
  File "../libasr/asr.h", line 4842
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/libasr/codegen/asr_to_llvm.cpp", line 945
    visit_symbol(*item.second);
  File "../libasr/asr.h", line 5139
  File "../libasr/asr.h", line 4850
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/libasr/codegen/asr_to_llvm.cpp", line 3071
    this->visit_stmt(*x.m_body[i]);
  File "../libasr/asr.h", line 5156
  File "../libasr/asr.h", line 4873
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/libasr/codegen/asr_to_llvm.cpp", line 1023
    visit_AllocateUtil(x, x.m_stat, false);
  File "/Users/ubaid/Desktop/OpenSource/lfortran_in_main/src/libasr/codegen/asr_to_llvm.cpp", line 972
    LCOMPILERS_ASSERT(curr_arg.m_len_expr != nullptr);
AssertFailed: curr_arg.m_len_expr != nullptr
certik commented 4 months ago

Let's try making the frontend transform allocate(y, source=x) into MoveAllocation(x, y).

Let's figure out all the use cases for source in allocate, but if the above syntax is the only one, then the above seems like the way to go.