https://github.com/akash-manna-sky updated 
https://github.com/llvm/llvm-project/pull/224331

>From 9e0fb0218d1ef91769f60126bb6732a78a7cd01c Mon Sep 17 00:00:00 2001
From: Akash Manna <[email protected]>
Date: Thu, 17 Sep 2026 20:30:29 +0530
Subject: [PATCH] [Clang] Fix assertion when instantiating a matrix type with
 an invalid element type

A matrix_type with constant dimensions and a dependent element type is
built as a ConstantMatrixType, with the element type check deferred to
instantiation. TreeTransform::RebuildConstantMatrixType then called
ASTContext::getConstantMatrixType directly, so the check never ran and
an invalid element type (e.g. another matrix) hit the "need a valid
element type" assertion.

Rebuild the type through Sema::BuildMatrixType instead, as
RebuildDependentSizedMatrixType already does, so the instantiated
element type is validated and diagnosed like in the non-template case.

Fixes #202744
---
 clang/docs/ReleaseNotes.md              |  4 ++++
 clang/lib/Sema/TreeTransform.h          | 19 ++++++++++++++-----
 clang/test/SemaTemplate/matrix-type.cpp | 18 ++++++++++++++++++
 3 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index f4a34a37aff52..5c1cfddb60c63 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -665,6 +665,10 @@ features cannot lower the translation-unit ABI level;
 - Fixed an assertion when an invalid statement appeared in a ``switch``
   statement nested inside a C++26 expansion statement. (#GH210575)
 
+- Fixed an assertion when a ``matrix_type`` with constant dimensions and a
+  dependent element type was instantiated with an invalid element type.
+  (#GH202744)
+
 - Fixed friend declarations sometimes making non-visible default arguments
   incorrectly visible to default argument redefinition checks across modules.
 
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index e482d15f31cc5..eb0be1ee352b9 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -1045,7 +1045,8 @@ class TreeTransform {
 
   /// Build a new matrix type given the element type and dimensions.
   QualType RebuildConstantMatrixType(QualType ElementType, unsigned NumRows,
-                                     unsigned NumColumns);
+                                     unsigned NumColumns,
+                                     SourceLocation AttributeLoc);
 
   /// Build a new matrix type given the type and dependently-defined
   /// dimensions.
@@ -6289,7 +6290,7 @@ 
TreeTransform<Derived>::TransformConstantMatrixType(TypeLocBuilder &TLB,
   QualType Result = TL.getType();
   if (getDerived().AlwaysRebuild() || ElementType != T->getElementType()) {
     Result = getDerived().RebuildConstantMatrixType(
-        ElementType, T->getNumRows(), T->getNumColumns());
+        ElementType, T->getNumRows(), T->getNumColumns(), TL.getAttrNameLoc());
     if (Result.isNull())
       return QualType();
   }
@@ -18171,9 +18172,17 @@ 
TreeTransform<Derived>::RebuildDependentSizedExtVectorType(QualType ElementType,
 
 template <typename Derived>
 QualType TreeTransform<Derived>::RebuildConstantMatrixType(
-    QualType ElementType, unsigned NumRows, unsigned NumColumns) {
-  return SemaRef.Context.getConstantMatrixType(ElementType, NumRows,
-                                               NumColumns);
+    QualType ElementType, unsigned NumRows, unsigned NumColumns,
+    SourceLocation AttributeLoc) {
+  ASTContext &Ctx = SemaRef.Context;
+  QualType SizeTy = Ctx.getSizeType();
+  unsigned SizeWidth = Ctx.getIntWidth(SizeTy);
+  IntegerLiteral *RowExpr = IntegerLiteral::Create(
+      Ctx, llvm::APInt(SizeWidth, NumRows), SizeTy, AttributeLoc);
+  IntegerLiteral *ColumnExpr = IntegerLiteral::Create(
+      Ctx, llvm::APInt(SizeWidth, NumColumns), SizeTy, AttributeLoc);
+  return SemaRef.BuildMatrixType(ElementType, RowExpr, ColumnExpr,
+                                 AttributeLoc);
 }
 
 template <typename Derived>
diff --git a/clang/test/SemaTemplate/matrix-type.cpp 
b/clang/test/SemaTemplate/matrix-type.cpp
index 1843c0a1a6ed6..a2ee6fdb3e08d 100644
--- a/clang/test/SemaTemplate/matrix-type.cpp
+++ b/clang/test/SemaTemplate/matrix-type.cpp
@@ -152,6 +152,24 @@ int test_make6() {
   make6<2, 2>::type y;
 }
 
+namespace GH202744 {
+template <typename Y>
+using matrix_5_5 = Y __attribute__((matrix_type(5, 5))); // 
expected-error{{invalid matrix element type 'matrix_5_5<float>'}}
+
+template <typename T>
+struct make7 {
+  typedef T __attribute__((matrix_type(3, 3))) type; // 
expected-error{{invalid matrix element type 's'}}
+};
+
+void CastDoubleMatrixToIntCStyle() {
+  matrix_5_5<float> f;
+  make7<int>::type m;
+  matrix_5_5<matrix_5_5<float>> d; // expected-note{{in instantiation of 
template type alias 'matrix_5_5' requested here}}
+  i = (matrix_5_5<int>)d; // expected-error{{use of undeclared identifier 'i'}}
+  make7<s> x; // expected-note{{in instantiation of}}
+}
+} // namespace GH202744
+
 namespace Deduction {
 template <typename T>
 struct X0;

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to