<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/81413>81413</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Assertion `mutableBox && "expression could not be lowered to mutable box"' failed.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          k-arrows
      </td>
    </tr>
</table>

<pre>
    Crash itself is reproducible on Godbolt.
https://godbolt.org/z/b6hs1Pe3q


Reproducer
```
program p
  implicit none

  type :: base
  end type

  class(*), pointer   :: ptr
  type(base), target  :: tgt

  select type ( ptr => ptr )
    type is (base)
      ptr => tgt
  end select

end
```


Stack dump
```console
 #0 0x00000000035328b8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x35328b8)
 #1 0x000000000353022c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f1cd9642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x0000000001c269b7 Fortran::lower::convertExprToMutableBox(mlir::Location, Fortran::lower::AbstractConverter&, Fortran::evaluate::Expr<Fortran::evaluate::SomeType> const&, Fortran::lower::SymMap&) (.cold) ConvertExprToHLFIR.cpp:0:0
 #4 0x00000000038b2a4a (anonymous namespace)::FirConverter::genExprMutableBox(mlir::Location, Fortran::evaluate::Expr<Fortran::evaluate::SomeType> const&) Bridge.cpp:0:0
 #5 0x00000000038fc760 (anonymous namespace)::FirConverter::genPointerAssignment(mlir::Location, Fortran::evaluate::Assignment const&, std::vector<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, std::allocator<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>> const&) Bridge.cpp:0:0
 #6 0x00000000038fcb47 std::__detail::__variant::__gen_vtable_impl<true, std::__detail::__variant::_Multi_array<void (*)(Fortran::common::visitors<(anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&)::'lambda'(Fortran::evaluate::Assignment::Intrinsic const&), (anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&)::'lambda0'(Fortran::evaluate::ProcedureRef const&), (anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&)::'lambda1'(std::vector<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, std::allocator<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>> const&), (anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&)::'lambda2'(std::vector<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>, std::allocator<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>>> const&)>&&, std::variant<Fortran::evaluate::Assignment::Intrinsic, Fortran::evaluate::ProcedureRef, std::vector<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, std::allocator<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>>, std::vector<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>, std::allocator<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>>>> const&)>, std::tuple<std::variant<Fortran::evaluate::Assignment::Intrinsic, Fortran::evaluate::ProcedureRef, std::vector<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, std::allocator<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>>, std::vector<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>, std::allocator<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>>>> const&>, std::integer_sequence<unsigned long, 2ul>>::__visit_invoke(Fortran::common::visitors<(anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&)::'lambda'(Fortran::evaluate::Assignment::Intrinsic const&), (anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&)::'lambda0'(Fortran::evaluate::ProcedureRef const&), (anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&)::'lambda1'(std::vector<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, std::allocator<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>> const&), (anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&)::'lambda2'(std::vector<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>, std::allocator<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>>> const&)>&&, std::variant<Fortran::evaluate::Assignment::Intrinsic, Fortran::evaluate::ProcedureRef, std::vector<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, std::allocator<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>>, std::vector<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>, std::allocator<std::pair<Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>, Fortran::evaluate::Expr<Fortran::evaluate::Type<(Fortran::common::TypeCategory)0, 8>>>>>> const&) Bridge.cpp:0:0
 #7 0x00000000038f8754 (anonymous namespace)::FirConverter::genAssignment(Fortran::evaluate::Assignment const&) Bridge.cpp:0:0
 #8 0x00000000038e9016 (anonymous namespace)::FirConverter::genFIR(Fortran::lower::pft::Evaluation&, bool) (.constprop.0) Bridge.cpp:0:0
 #9 0x0000000003911935 (anonymous namespace)::FirConverter::genFIR(Fortran::parser::SelectTypeConstruct const&) Bridge.cpp:0:0
#10 0x000000000390612a (anonymous namespace)::FirConverter::lowerFunc(Fortran::lower::pft::FunctionLikeUnit&) Bridge.cpp:0:0
#11 0x00000000038fa358 Fortran::lower::LoweringBridge::lower(Fortran::parser::Program const&, Fortran::semantics::SemanticsContext const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x38fa358)
#12 0x000000000388d4f7 Fortran::frontend::CodeGenAction::beginSourceFileAction() (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x388d4f7)
#13 0x0000000003574cb5 Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x3574cb5)
#14 0x0000000003564b77 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x3564b77)
#15 0x000000000357af1d Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x357af1d)
#16 0x0000000001d5f4a8 fc1_main(llvm::ArrayRef<char const*>, char const*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x1d5f4a8)
#17 0x0000000001c65d5d main (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x1c65d5d)
#18 0x00007f1cd9629d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#19 0x00007f1cd9629e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#20 0x0000000001d5e40e _start (/opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin/flang-new+0x1d5e40e)
flang-new: error: unable to execute command: Segmentation fault (core dumped)
flang-new: error: flang frontend command failed due to signal (use -v to see invocation)
flang-new version 19.0.0git (https://github.com/llvm/llvm-project.git b17348c3b541d7fc7ec441c98db75c18d8959910)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-llvmflang-trunk-20240211/bin
```

When I used the flang-new with assertion enabled, the compilation resulted in the following assertion failure.
```
/path_to_llvm_project/llvm-project/flang/lib/Lower/ConvertExprToHLFIR.cpp:1972: fir::MutableBoxValue Fortran::lower::convertExprToMutableBox(mlir::Location, Fortran::lower::AbstractConverter &, const Fortran::lower::SomeExpr &, Fortran::lower::SymMap &): Assertion `mutableBox && "expression could not be lowered to mutable box"' failed.
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsW99zozgS_muUF1VcIMDAQx48zng3VTNVqcnc3aNLiDbWRUisJBzn_vorCWyDN86vmezs1XnKk4AkWt_3dasRoU2N4ZUEuELJJ5RcX9DWrpW-ur-kWqsHc1Go8vFqrqlZY24NiBXmBmtotCpbxgsBWEn8myoLJewEBdcomK2tbQyKZogsE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