[flang-commits] [flang] [flang] To issue error message for unsupported usage of vector type. (PR #215248)
via flang-commits
flang-commits at lists.llvm.org
Mon Aug 10 04:25:23 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics
Author: Daniel Chen (DanielCChen)
<details>
<summary>Changes</summary>
As discussed in PR #<!-- -->214219, we don't plan to support any usage that requires runtime descriptor for vector type object.
Flang has already disallowed ALLOCATABLE, POINTER, ASSUMED-SHAPE and ASSUMED-RANK for vector types.
The test reducer posted by @<!-- -->eugeneepshteyn should also be flagged by the compiler as an error.
This PR is to add semantic check for that case. It covers
1. ALLOCATE statement where SOURCE= or MOLD= has a vector type object.
2. Auto-allocation intrinsic assignment where the RHS is a vector type object.
3. A vector type object being used as an argument of SAME_TYPE_AS or EXTENDs_TYPE_OF.
---
Full diff: https://github.com/llvm/llvm-project/pull/215248.diff
4 Files Affected:
- (modified) flang/lib/Evaluate/tools.cpp (+2-1)
- (modified) flang/lib/Semantics/check-allocate.cpp (+6)
- (modified) flang/lib/Semantics/expression.cpp (+10)
- (added) flang/test/Semantics/PowerPC/ppc-vector-poly-checks.f90 (+45)
``````````diff
diff --git a/flang/lib/Evaluate/tools.cpp b/flang/lib/Evaluate/tools.cpp
index 34deaaea289c1..a1536194242f4 100644
--- a/flang/lib/Evaluate/tools.cpp
+++ b/flang/lib/Evaluate/tools.cpp
@@ -2761,7 +2761,8 @@ bool IsLenTypeParameter(const Symbol &symbol) {
}
bool IsExtensibleType(const DerivedTypeSpec *derived) {
- return !IsSequenceOrBindCType(derived) && !IsIsoCType(derived);
+ return !IsSequenceOrBindCType(derived) && !IsIsoCType(derived) &&
+ !(derived && derived->IsVectorType());
}
bool IsSequenceOrBindCType(const DerivedTypeSpec *derived) {
diff --git a/flang/lib/Semantics/check-allocate.cpp b/flang/lib/Semantics/check-allocate.cpp
index 7f099d51221c0..2ac242abd5788 100644
--- a/flang/lib/Semantics/check-allocate.cpp
+++ b/flang/lib/Semantics/check-allocate.cpp
@@ -238,6 +238,12 @@ static std::optional<AllocateCheckerInfo> CheckAllocateOptions(
info.sourceExprLoc = parserSourceExpr->source;
if (const DerivedTypeSpec *
derived{evaluate::GetDerivedTypeSpec(info.sourceExprType)}) {
+ if (derived->IsVectorType()) {
+ context.Say(at,
+ "SOURCE or MOLD expression must not be a vector type '%s'"_err_en_US,
+ info.sourceExprType.value().AsFortran());
+ return std::nullopt;
+ }
// C949
if (auto it{FindCoarrayUltimateComponent(*derived)}) {
context
diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index fc57cc43e981c..1cc3715330b64 100644
--- a/flang/lib/Semantics/expression.cpp
+++ b/flang/lib/Semantics/expression.cpp
@@ -3852,6 +3852,16 @@ const Assignment *ExpressionAnalyzer::Analyze(const parser::AssignmentStmt &x) {
Warn(common::UsageWarning::IgnoredNoReallocateLHS,
"-fno-realloc-lhs is ignored for assignment to polymorphic allocatable"_warn_en_US);
}
+ const Expr<SomeType> &rhs{analyzer.GetExpr(1)};
+ if (auto rhsType{rhs.GetType()}) {
+ if (const auto *rhsDerived{GetDerivedTypeSpec(*rhsType)}) {
+ if (rhsDerived->IsVectorType()) {
+ Say(rhsExpr.source,
+ "Vector type '%s' may not be used as the right-hand side of a polymorphic intrinsic assignment"_err_en_US,
+ rhsType->AsFortran());
+ }
+ }
+ }
}
if (auto *derived{GetDerivedTypeSpec(*dyType)}) {
if (auto iter{FindAllocatableUltimateComponent(*derived)}) {
diff --git a/flang/test/Semantics/PowerPC/ppc-vector-poly-checks.f90 b/flang/test/Semantics/PowerPC/ppc-vector-poly-checks.f90
new file mode 100644
index 0000000000000..e047cf838f9ea
--- /dev/null
+++ b/flang/test/Semantics/PowerPC/ppc-vector-poly-checks.f90
@@ -0,0 +1,45 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1
+! REQUIRES: target=powerpc{{.*}}
+
+! Test that vector types are rejected in polymorphic contexts:
+! 1. ALLOCATE SOURCE=/MOLD= must not have a vector type expression
+! 2. RHS of intrinsic assignment must not be a vector type when LHS is polymorphic
+! (uses Fortran standard auto-allocation, no prior ALLOCATE)
+! 3. SAME_TYPE_AS / EXTENDS_TYPE_OF must not receive a vector type argument
+! (enforced via IsExtensibleType returning false for vector types)
+
+subroutine test_allocate_source()
+ vector(integer(4)) :: vi(2)
+ class(*), allocatable :: x(:)
+ !ERROR: SOURCE or MOLD expression must not be a vector type 'vector(integer(4))'
+ allocate(x(2), source=vi)
+end subroutine
+
+subroutine test_allocate_mold()
+ vector(real(4)) :: vr(2)
+ class(*), allocatable :: x(:)
+ !ERROR: SOURCE or MOLD expression must not be a vector type 'vector(real(4))'
+ allocate(x(2), mold=vr)
+end subroutine
+
+subroutine test_poly_assign_vector_rhs()
+ vector(real(4)) :: vr(2)
+ class(*), allocatable :: x(:)
+ ! x is unallocated; auto-allocation from RHS type must be rejected
+ !ERROR: Vector type 'vector(real(4))' may not be used as the right-hand side of a polymorphic intrinsic assignment
+ x = vr
+end subroutine
+
+subroutine test_same_type_as()
+ vector(integer(4)) :: vi(2)
+ logical :: res
+ !ERROR: Actual argument for 'a=' has type 'vector(integer(4))', but was expected to be an extensible or unlimited polymorphic type
+ res = same_type_as(vi, vi)
+end subroutine
+
+subroutine test_extends_type_of()
+ vector(real(4)) :: vr(2)
+ logical :: res
+ !ERROR: Actual argument for 'a=' has type 'vector(real(4))', but was expected to be an extensible or unlimited polymorphic type
+ res = extends_type_of(vr, vr)
+end subroutine
``````````
</details>
https://github.com/llvm/llvm-project/pull/215248
More information about the flang-commits
mailing list