[flang] Fix potential null scope when lowering dispatch table op
authorValentin Clement <clementval@gmail.com>
Fri, 3 Feb 2023 09:16:54 +0000 (10:16 +0100)
committerValentin Clement <clementval@gmail.com>
Fri, 3 Feb 2023 09:17:33 +0000 (10:17 +0100)
Similary to D140209, the scope might need to be retrieved
from the typeSymbol. The test code was crashing because the
scope passed to CollectBindings was initially null.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D143188

flang/lib/Lower/Bridge.cpp
flang/test/Lower/dispatch.f90

index 9033ab8..aa844e5 100644 (file)
@@ -204,9 +204,11 @@ public:
           info.loc, dtName,
           parent ? Fortran::lower::mangle::mangleName(*parent) : "");
       auto insertPt = builder.saveInsertionPoint();
-
+      const Fortran::semantics::Scope *scope = info.typeSpec->scope();
+      if (!scope)
+        scope = info.typeSpec->typeSymbol().scope();
       Fortran::semantics::SymbolVector bindings =
-          Fortran::semantics::CollectBindings(*info.typeSpec->scope());
+          Fortran::semantics::CollectBindings(*scope);
 
       if (!bindings.empty())
         builder.createBlock(&dt.getRegion());
index 4328ac7..0331bfb 100644 (file)
@@ -39,6 +39,16 @@ module call_dispatch
     procedure(nopass_defferred), deferred, nopass :: nopassd
   end type
 
+  type :: node
+    type(node_ptr), pointer :: n(:)
+  end type
+  type :: use_node
+    type(node) :: n
+  end type
+  type :: node_ptr
+    type(node_ptr), pointer :: n
+  end type
+
   contains
 
 ! ------------------------------------------------------------------------------
@@ -341,4 +351,8 @@ module call_dispatch
 ! CHECK: fir.call @_QMcall_dispatchPtbp_pass_arg0
 ! CHECK: fir.call @_QMcall_dispatchPtbp_pass_arg1
 
+  subroutine use_node_test(n)
+    type(use_node) :: n
+  end subroutine
+
 end module