Skip to content

Conversation

@eugeneepshteyn
Copy link
Contributor

@eugeneepshteyn eugeneepshteyn commented Oct 22, 2025

#162722 introduced a regression that started creating initializers for CDEFINED variables. CDEFINED variables cannot have initializers, because their storage is expected come from elsewhere, likely outside of Fortran. Fixed the regression and improved the regression test to catch the incorrect initialization case.

Also, based on the code review feedback, made CDEFINED variable initialization a hard error and updated tests accordingly.

llvm#162722 introduced a regression that
started creating initializers for CDEFINED variables. CDEFINED variables
cannot have initializers, because their storage is expected come from
elsewhere, likely outside of Fortran. Fixed the regression and improved the
regression test to catch the incorrect initialization case.
@eugeneepshteyn eugeneepshteyn marked this pull request as ready for review October 22, 2025 13:05
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:semantics labels Oct 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 22, 2025

@llvm/pr-subscribers-flang-fir-hlfir

@llvm/pr-subscribers-flang-semantics

Author: Eugene Epshteyn (eugeneepshteyn)

Changes

#162722 introduced a regression that started creating initializers for CDEFINED variables. CDEFINED variables cannot have initializers, because their storage is expected come from elsewhere, likely outside of Fortran. Fixed the regression and improved the regression test to catch the incorrect initialization case.


Full diff: https://github.com/llvm/llvm-project/pull/164616.diff

3 Files Affected:

  • (modified) flang/lib/Semantics/resolve-names.cpp (+5-4)
  • (modified) flang/test/Lower/cdefined.f90 (+1)
  • (modified) flang/test/Semantics/cdefined.f90 (+1-1)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index db75437708a6c..5cb1bc636a582 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -9194,11 +9194,12 @@ bool DeclarationVisitor::CheckNonPointerInitialization(
               "'%s' has already been initialized"_err_en_US);
         } else if (IsAllocatable(ultimate)) {
           Say(name, "Allocatable object '%s' cannot be initialized"_err_en_US);
+        } else if (details->isCDefined()) {
+          // CDEFINED variables cannot have initializer, because their storage
+          // may come outside of Fortran.
+          context().Warn(common::UsageWarning::CdefinedInit, name.source,
+              "CDEFINED variable cannot be initialized"_warn_en_US);
         } else {
-          if (details->isCDefined()) {
-            context().Warn(common::UsageWarning::CdefinedInit, name.source,
-                "CDEFINED variable should not have an initializer"_warn_en_US);
-          }
           return true;
         }
       } else {
diff --git a/flang/test/Lower/cdefined.f90 b/flang/test/Lower/cdefined.f90
index 89599442589eb..87a01b51460fc 100644
--- a/flang/test/Lower/cdefined.f90
+++ b/flang/test/Lower/cdefined.f90
@@ -6,4 +6,5 @@ module m
   integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 42
   ! CHECK: fir.global @c_global : i32
   ! CHECK-NOT: fir.zero_bits 
+  ! CHECK-NOT: arith.constant 42
 end
diff --git a/flang/test/Semantics/cdefined.f90 b/flang/test/Semantics/cdefined.f90
index 84103ce661d0b..9e0b0a485ee15 100644
--- a/flang/test/Semantics/cdefined.f90
+++ b/flang/test/Semantics/cdefined.f90
@@ -1,6 +1,6 @@
 ! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
 module m
   use iso_c_binding
-  !WARNING: CDEFINED variable should not have an initializer [-Wcdefined-init]
+  !WARNING: CDEFINED variable cannot be initialized [-Wcdefined-init]
   integer(c_int), bind(C, name='c_global', CDEFINED) :: c  = 42
 end

} else if (details->isCDefined()) {
// CDEFINED variables cannot have initializer, because their storage
// may come outside of Fortran.
context().Warn(common::UsageWarning::CdefinedInit, name.source,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they can't be initialized, this should be a hard error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@eugeneepshteyn eugeneepshteyn merged commit 9702ec0 into llvm:main Oct 27, 2025
10 checks passed
@eugeneepshteyn eugeneepshteyn deleted the fix-cdefined branch October 27, 2025 22:26
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
llvm#162722 introduced a regression
that started creating initializers for CDEFINED variables. CDEFINED
variables cannot have initializers, because their storage is expected
come from elsewhere, likely outside of Fortran. Fixed the regression and
improved the regression test to catch the incorrect initialization case.

Also, based on the code review feedback, made CDEFINED variable
initialization a hard error and updated tests accordingly.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
llvm#162722 introduced a regression
that started creating initializers for CDEFINED variables. CDEFINED
variables cannot have initializers, because their storage is expected
come from elsewhere, likely outside of Fortran. Fixed the regression and
improved the regression test to catch the incorrect initialization case.

Also, based on the code review feedback, made CDEFINED variable
initialization a hard error and updated tests accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:fir-hlfir flang:semantics flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants