Skip to content

release/21.x: [Flang] Fix crash with parametrized derived types usage (#150289) #151937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: release/21.x
Choose a base branch
from

Conversation

llvmbot
Copy link
Member

@llvmbot llvmbot commented Aug 4, 2025

Backport 89e4d9f

Requested by: @tblah

The current mangleName implementation doesn't take a FoldingContext,
which prevents the proper evaluation of expressions containing parameter
references to an integer constant. Since parametrized derived types are
not yet implemented, the compiler will crash there in some cases (see
example in issue llvm#127424).

This is a workaround so that doesn't happen until the feature is
properly implemented.

Fixes llvm#127424

(cherry picked from commit 89e4d9f)
@llvmbot
Copy link
Member Author

llvmbot commented Aug 4, 2025

@tblah What do you think about merging this PR to the release branch?

@llvmbot llvmbot requested a review from tblah August 4, 2025 10:07
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Aug 4, 2025
@llvmbot
Copy link
Member Author

llvmbot commented Aug 4, 2025

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

Author: None (llvmbot)

Changes

Backport 89e4d9f

Requested by: @tblah


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

2 Files Affected:

  • (modified) flang/lib/Lower/Mangler.cpp (+12-2)
  • (added) flang/test/Lower/parametrized-derived-types.f90 (+19)
diff --git a/flang/lib/Lower/Mangler.cpp b/flang/lib/Lower/Mangler.cpp
index 1333e3fe349d1..e1ae86a1b5bb2 100644
--- a/flang/lib/Lower/Mangler.cpp
+++ b/flang/lib/Lower/Mangler.cpp
@@ -224,8 +224,18 @@ std::string Fortran::lower::mangle::mangleName(
       assert(paramExpr && "derived type kind param not explicit");
       std::optional<int64_t> init =
           Fortran::evaluate::ToInt64(paramValue->GetExplicit());
-      assert(init && "derived type kind param is not constant");
-      kinds.emplace_back(*init);
+      // TODO: put the assertion check back when parametrized derived types
+      // are supported:
+      // assert(init && "derived type kind param is not constant");
+      //
+      // The init parameter above will require a FoldingContext for proper
+      // expression evaluation to an integer constant, otherwise the
+      // compiler may crash here (see example in issue #127424).
+      if (!init) {
+        TODO_NOLOC("parameterized derived types");
+      } else {
+        kinds.emplace_back(*init);
+      }
     }
   }
   return fir::NameUniquer::doType(modules, procs, blockId, symbolName, kinds);
diff --git a/flang/test/Lower/parametrized-derived-types.f90 b/flang/test/Lower/parametrized-derived-types.f90
new file mode 100644
index 0000000000000..97a40c9169d2b
--- /dev/null
+++ b/flang/test/Lower/parametrized-derived-types.f90
@@ -0,0 +1,19 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! XFAIL: *
+program main
+  TYPE ty(k1,k2)
+     INTEGER ,KIND::k1,k2=5
+     INTEGER::arr(k1:k2)=10
+     CHARACTER(LEN=k2)::CHARACTER
+  END TYPE ty
+  TYPE,EXTENDS(ty)::ty1(k3)
+     INTEGER,KIND ::k3=4
+     TYPE(ty(2,k3+1))::cmp_ty = ty(2,k3+1)(55,'HI')
+  END TYPE ty1
+  TYPE ty2(l1, l2)
+  !ERROR: not yet implemented: parameterized derived types
+     INTEGER,LEN ::l1,l2
+     TYPE(ty1(2,5)), ALLOCATABLE::ty1_cmp(:)
+  END TYPE ty2
+  TYPE(ty2(4,8)) ::ty2_obj
+end program main

@tblah tblah requested review from ceseo and removed request for tblah August 4, 2025 10:07
@tru tru moved this from Needs Triage to Needs Review in LLVM Release Status Aug 5, 2025
@tru
Copy link
Collaborator

tru commented Aug 5, 2025

Needs to be reviewed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

3 participants