Issue 109268
Summary [flang] Flang crashes compiling a program with a derived type that extends an undefined derived type
Labels flang
Assignees
Reporter inaki-amatria
    - `flang-new` version is: `flang-new version 20.0.0git (https://github.com/llvm/llvm-project.git 999313debe8a87760b128e4469f17ec0ce1a4a8f)`
- Reduced test case:
```f90
! foo.f90
program main
  type :: foo_t
 class(bar_t), pointer :: bar_t_ptr
  end type foo_t
  type, extends(bar_t) :: baz_t
  end type baz_t
end program main
```
- `flang-new` invocation is: `flang-new foo.f90`
- Expected behavior is: The code errors with `The derived type 'bar_t' has not been defined`.
- Actual behavior: Compilation aborts with the following stack trace:
```
 "/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new" -fc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu x86-64 -debug-info-kind=standalone -resource-dir /opt/compiler-explorer/clang-llvmflang-trunk-20240919/lib/clang/20 -mframe-pointer=all -O0 -o CMakeFiles/main.dir/main.f90.o -x f95-cpp-input /app/main.f90
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new -fc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu x86-64 -debug-info-kind=standalone -resource-dir /opt/compiler-explorer/clang-llvmflang-trunk-20240919/lib/clang/20 -mframe-pointer=all -O0 -o CMakeFiles/main.dir/main.f90.o -x f95-cpp-input /app/main.f90
 #0 0x00000000035fd368 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x35fd368)
 #1 0x00000000035fad0c SignalHandler(int) Signals.cpp:0:0
 #2 0x0000754bc4642520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x000000000403a78b Fortran::semantics::DerivedTypeSpec::set_scope(Fortran::semantics::Scope const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x403a78b)
 #4 0x0000000003f8f3de Fortran::semantics::DeclarationVisitor::Post(Fortran::parser::DerivedTypeStmt const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3f8f3de)
 #5 0x0000000003f8f501 void Fortran::parser::Walk<Fortran::parser::DerivedTypeStmt, Fortran::semantics::ResolveNamesVisitor>(Fortran::parser::Statement<Fortran::parser::DerivedTypeStmt> const&, Fortran::semantics::ResolveNamesVisitor&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3f8f501)
 #6 0x0000000003f9ab0a Fortran::semantics::DeclarationVisitor::Pre(Fortran::parser::DerivedTypeDef const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3f9ab0a)
 #7 0x0000000003f9b074 std::enable_if<TupleTrait<Fortran::parser::DerivedTypeDef>, void>::type Fortran::parser::Walk<Fortran::parser::DerivedTypeDef, Fortran::semantics::ResolveNamesVisitor>(Fortran::parser::DerivedTypeDef const&, Fortran::semantics::ResolveNamesVisitor&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3f9b074)
 #8 0x0000000003fa27e6 void Fortran::parser::Walk<Fortran::parser::DeclarationConstruct, Fortran::semantics::ResolveNamesVisitor>(std::__cxx11::list<Fortran::parser::DeclarationConstruct, std::allocator<Fortran::parser::DeclarationConstruct>> const&, Fortran::semantics::ResolveNamesVisitor&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fa27e6)
 #9 0x0000000003fa3e55 Fortran::semantics::ResolveNamesVisitor::Pre(Fortran::parser::SpecificationPart const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fa3e55)
#10 0x0000000003fa1ed9 std::enable_if<TupleTrait<Fortran::parser::SpecificationPart>, void>::type Fortran::parser::Walk<Fortran::parser::SpecificationPart, Fortran::semantics::ResolveNamesVisitor>(Fortran::parser::SpecificationPart const&, Fortran::semantics::ResolveNamesVisitor&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fa1ed9)
#11 0x0000000003fc8c64 Fortran::semantics::ResolveNamesVisitor::ResolveSpecificationParts(Fortran::semantics::ProgramTree&) (.part.0) resolve-names.cpp:0:0
#12 0x0000000003fc9306 Fortran::semantics::ResolveNamesVisitor::Pre(Fortran::parser::ProgramUnit const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fc9306)
#13 0x0000000003fca864 Fortran::semantics::ResolveNames(Fortran::semantics::SemanticsContext&, Fortran::parser::Program const&, Fortran::semantics::Scope&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3fca864)
#14 0x0000000004023459 Fortran::semantics::Semantics::Perform() (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x4023459)
#15 0x00000000036570ad Fortran::frontend::FrontendAction::runSemanticChecks() (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x36570ad)
#16 0x00000000039beafa Fortran::frontend::CodeGenAction::beginSourceFileAction() (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x39beafa)
#17 0x00000000036556ad Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x36556ad)
#18 0x0000000003644fa7 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x3644fa7)
#19 0x000000000365dd9d Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x365dd9d)
#20 0x0000000001cdfc98 fc1_main(llvm::ArrayRef<char const*>, char const*) (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x1cdfc98)
#21 0x0000000001bc020c main (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x1bc020c)
#22 0x0000754bc4629d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#23 0x0000754bc4629e40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#24 0x0000000001cdebfe _start (/opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin/flang-new+0x1cdebfe)
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 20.0.0git (https://github.com/llvm/llvm-project.git 999313debe8a87760b128e4469f17ec0ce1a4a8f)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/compiler-explorer/clang-llvmflang-trunk-20240919/bin
flang-new: note: diagnostic msg: 
```
- Additional info: It looks like the following patch works around the issue, although I can't assess if this is a valid patch so I'd rather open this issue:
```diff
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 0b4e01b76c56..4882e2f624a8 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -5539,7 +5539,7 @@ void DeclarationVisitor::Post(const parser::DerivedTypeStmt &x) {
     // existing.
     const Symbol &extendsSymbol{extendsType->typeSymbol()};
 auto restorer{common::ScopedSet(extendsName->symbol, nullptr)};
- if (OkToAddComponent(*extendsName, &extendsSymbol)) {
+    if (OkToAddComponent(*extendsName, &extendsSymbol) && extendsSymbol.scope()) {
       auto &comp{DeclareEntity<ObjectEntityDetails>(*extendsName, Attrs{})};
       comp.attrs().set(
           Attr::PRIVATE, extendsSymbol.attrs().test(Attr::PRIVATE));
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to