https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/224331
Fixes #202744 `Sema::BuildMatrixType` skips the element type check while the element type is dependent, so `template <typename Y> using matrix_5_5 = Y __attribute__((matrix_type(5, 5)));` gives a `ConstantMatrixType` whose element is still unchecked. On instantiation, `TreeTransform::RebuildConstantMatrixType` called `ASTContext::getConstantMatrixType` directly, so nothing ever validated the substituted type. `matrix_5_5<matrix_5_5<float>>` then hit the "need a valid element type" assertion. The dependent-dimension case (`matrix_type(R, C)`) was fine because `RebuildDependentSizedMatrixType` already goes through `BuildMatrixType`. `RebuildConstantMatrixType` now goes through `Sema::BuildMatrixType` as well, the same way `RebuildExtVectorType` does for vectors: it takes the attribute location and wraps the dimensions in integer literals. An invalid instantiated element type now gets the usual `invalid matrix element type` error instead of crashing, and the `_BitInt` element check that this path also skipped is applied too. >From 8e0410fe99bd1731d4a5b5dd058909b777707aeb 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 3cca316a91d4d3..cec77fafe37723 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -626,6 +626,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 c8458fda58a88e..5ee5ee4b249b1f 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. @@ -6288,7 +6289,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(); } @@ -18158,9 +18159,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 1843c0a1a6ed62..a2ee6fdb3e08d4 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
