[flang-commits] [flang] 12da120 - [flang][OpenACC] Fix crash on ATTACH/DETACH with an unresolved name (#216801)
via flang-commits
flang-commits at lists.llvm.org
Mon Aug 17 11:49:03 PDT 2026
Author: Ron Green [NVIDIA]
Date: 2026-08-17T11:48:58-07:00
New Revision: 12da120df184ab620c70deb9c4d7be29990a4fdf
URL: https://github.com/llvm/llvm-project/commit/12da120df184ab620c70deb9c4d7be29990a4fdf
DIFF: https://github.com/llvm/llvm-project/commit/12da120df184ab620c70deb9c4d7be29990a4fdf.diff
LOG: [flang][OpenACC] Fix crash on ATTACH/DETACH with an unresolved name (#216801)
`AccAttributeVisitor::EnsureAllocatableOrPointer()` passes the last name of the
designator to `IsAllocatableOrObjectPointer()`, which accepts a null `Symbol`
pointer and returns `false` for it. When the argument of an `ATTACH` or `DETACH`
clause fails name resolution -- for example a component reference whose derived
type could not be resolved -- that name's symbol is null, so control reaches the
error-reporting path, which then dereferences the same null symbol and crashes
the compiler.
Because the crash happens during name resolution, it precedes the point where
semantic diagnostics are emitted, so the compiler dies with no output at all
rather than reporting the errors that caused the name to be unresolved.
Reproducer:
```fortran
subroutine test_attach_unresolved
type :: ty
integer :: i
end type ty
type(ty) :: x
!$acc enter data attach(x%bad)
end subroutine test_attach_unresolved
```
`flang -fsyntax-only -fopenacc` on the above segfaults. With this change it
reports the expected `Component 'bad' not found in derived type 'ty'`.
Fix: skip the check when the name is unresolved. Name resolution has already
reported an error for it, so there is nothing left to diagnose.
A regression test is added covering both `ATTACH` and `DETACH`.
Added:
flang/test/Semantics/OpenACC/acc-attach-unresolved.f90
Modified:
flang/lib/Semantics/resolve-directives.cpp
Removed:
################################################################################
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 15bb84d7e486f..532f1f821b8f2 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -1774,6 +1774,11 @@ void AccAttributeVisitor::EnsureAllocatableOrPointer(
common::visitors{
[&](const parser::Designator &designator) {
const auto &lastName{GetLastName(designator)};
+ if (!lastName.symbol) {
+ // Name resolution failed for this designator and has already
+ // emitted an error; there is nothing left to check.
+ return;
+ }
if (!IsAllocatableOrObjectPointer(lastName.symbol)) {
context_.Say(designator.source,
"Argument `%s` on the %s clause must be a variable or "
diff --git a/flang/test/Semantics/OpenACC/acc-attach-unresolved.f90 b/flang/test/Semantics/OpenACC/acc-attach-unresolved.f90
new file mode 100644
index 0000000000000..cbe7ff5881155
--- /dev/null
+++ b/flang/test/Semantics/OpenACC/acc-attach-unresolved.f90
@@ -0,0 +1,17 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenacc
+
+! Check that an ATTACH/DETACH argument whose name could not be resolved is
+! diagnosed instead of crashing the compiler.
+
+subroutine test_attach_unresolved
+ type :: ty
+ integer :: i
+ end type ty
+ type(ty) :: x
+
+ !ERROR: Component 'bad' not found in derived type 'ty'
+ !$acc enter data attach(x%bad)
+
+ !ERROR: Component 'bad' not found in derived type 'ty'
+ !$acc exit data detach(x%bad)
+end subroutine test_attach_unresolved
More information about the flang-commits
mailing list