[flang-commits] [flang] [flang][Lower] Implement lowering for new expression kind used in explicit-shape-bounds-spec (PR #215403)

via flang-commits flang-commits at lists.llvm.org
Wed Oct 14 01:13:09 PDT 2026


================
@@ -895,11 +895,21 @@ llvm::raw_ostream &DescriptorInquiry::AsFortran(llvm::raw_ostream &o) const {
 }
 
 llvm::raw_ostream &RankOneBoundElement::AsFortran(llvm::raw_ostream &o) const {
-  // A RankOneBoundElement extracts a single element from a rank-1 array that
-  // was used as an array bound in a declaration; it has no true Fortran
-  // surface syntax.  Render it in an internal, clearly-synthetic form.
-  base().AsFortran(o << "rank1BoundElement(")
-      << ",dim=" << (dimension_ + 1) << ')';
+  if (originatingArray_) {
+    // This element was produced by folding an UBOUND()/LBOUND() inquiry of an
+    // explicit-shape array declared with a rank-1 array bound.  Serialize it as
+    // the originating intrinsic so the mod file round-trips as real Fortran.
+    o << (isUpperBound_ ? "ubound(" : "lbound(");
----------------
MattPD wrote:

The module file now spells the inquiry as plain `ubound`, so the module file reader resolves it by name. A user procedure or type named `ubound` then replaces the intrinsic, even though the source used `size`.

You can reproduce this by saving the following as `provider.f90`:

```fortran
module m_shadow_function
contains
  pure integer(8) function ubound(a,dim,kind) result(r)
    real, intent(in) :: a(:,:)
    integer, intent(in) :: dim,kind
    r = 99_8
  end function
  function f(n,m,mask,a) result(b)
    integer, intent(in) :: n(2),m(2)
    logical, intent(in) :: mask(2)
    real, intent(in) :: a(merge(n,m,mask))
    real :: b(size(a,2))
    b = 1.0
  end function
end module
```

Save the following as `consumer.f90`:

```fortran
program test
  use m_shadow_function
  real :: a(2,3)
  a = 1.0
  print *, size(f([2,3],[4,5],[.true.,.true.],a))
end program
```

Then run `flang -c provider.f90 && flang consumer.f90 provider.o && ./a.out`. The command prints `99`. At the previous PR head 14764c2f, the same command prints `3`.

With `type :: ubound` instead, the consumer is rejected with `Keyword 'dim=' does not name a component of derived type 'ubound'`.

https://github.com/llvm/llvm-project/pull/215403


More information about the flang-commits mailing list