[clang] [ObjC] Fix Assertion failure when merging declarations with different lifetime qualifiers (PR #203272)
Hendrik Hübner via cfe-commits
cfe-commits at lists.llvm.org
Fri Jun 12 01:56:26 PDT 2026
https://github.com/HendrikHuebner updated https://github.com/llvm/llvm-project/pull/203272
>From 9b78ed33016420997619a0cda5d318ef01aa15b5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hendrik=20H=C3=BCbner?= <hhuebner at MacBookPro.localdomain>
Date: Thu, 11 Jun 2026 15:55:55 +0200
Subject: [PATCH 1/3] [ObjC] Fix Assertion failure when merging declarations
with different lifetime qualifiers
Fixes #150403
---
clang/lib/AST/ASTContext.cpp | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index abf0cd5e18c2b..e936572458444 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -12356,7 +12356,8 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
if (LQuals != RQuals) {
// If any of these qualifiers are different, we have a type mismatch.
if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
- LQuals.getAddressSpace() != RQuals.getAddressSpace())
+ LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
+ LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
return {};
// Exactly one GC qualifier difference is allowed: __strong is
>From 7b37b95ce45e0b3d8243e4a2624d42db3d35c795 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hendrik=20H=C3=BCbner?= <hhuebner at MacBookPro.localdomain>
Date: Thu, 11 Jun 2026 16:06:06 +0200
Subject: [PATCH 2/3] add test
---
clang/test/SemaObjC/arc-repeated-weak.mm | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/test/SemaObjC/arc-repeated-weak.mm b/clang/test/SemaObjC/arc-repeated-weak.mm
index aaf8256d314ee..45b9816b42057 100644
--- a/clang/test/SemaObjC/arc-repeated-weak.mm
+++ b/clang/test/SemaObjC/arc-repeated-weak.mm
@@ -340,6 +340,9 @@ - (void)distinctFromOther:(Test *)other {
}
@end
+extern id foo; // expected-note {{previous declaration is here}}
+extern __weak id foo; // expected-error {{redeclaration of 'foo' with a different type}}
+
@interface Base1
@end
@interface Sub1 : Base1
>From e309724911cf3074b6973304850acc056137c33d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hendrik=20H=C3=BCbner?= <hhuebner at MacBookPro.lan>
Date: Fri, 12 Jun 2026 10:54:40 +0200
Subject: [PATCH 3/3] Fix test and logic
---
clang/lib/AST/ASTContext.cpp | 13 +++++--------
clang/test/SemaObjC/arc-repeated-weak.mm | 6 +++---
2 files changed, 8 insertions(+), 11 deletions(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index e936572458444..a4d7ded4bf50e 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -12339,7 +12339,7 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
// id foo(); ... __strong id foo(); or: __strong id foo(); ... id foo();
// In either case, use OldReturnType to build the new function type.
const auto *F = LHS->castAs<FunctionType>();
- if (const auto *FPT = cast<FunctionProtoType>(F)) {
+ if (const auto *FPT = cast_or_null<FunctionProtoType>(F)) {
FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo();
EPI.ExtInfo = getFunctionExtInfo(LHS);
QualType ResultType =
@@ -12354,12 +12354,6 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
Qualifiers LQuals = LHSCan.getLocalQualifiers();
Qualifiers RQuals = RHSCan.getLocalQualifiers();
if (LQuals != RQuals) {
- // If any of these qualifiers are different, we have a type mismatch.
- if (LQuals.getCVRQualifiers() != RQuals.getCVRQualifiers() ||
- LQuals.getAddressSpace() != RQuals.getAddressSpace() ||
- LQuals.getObjCLifetime() != RQuals.getObjCLifetime())
- return {};
-
// Exactly one GC qualifier difference is allowed: __strong is
// okay if the other type has no GC qualifier but is an Objective
// C object pointer (i.e. implicitly strong by default). We fix
@@ -12367,7 +12361,10 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
// qualified __strong.
Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
- assert((GC_L != GC_R) && "unequal qualifier sets had only equal elements");
+ if (GC_L == GC_R) {
+ // Some non-GC qualifiers differ, so merging fails.
+ return {};
+ }
if (GC_L == Qualifiers::Weak || GC_R == Qualifiers::Weak)
return {};
diff --git a/clang/test/SemaObjC/arc-repeated-weak.mm b/clang/test/SemaObjC/arc-repeated-weak.mm
index 45b9816b42057..ecffd77faa203 100644
--- a/clang/test/SemaObjC/arc-repeated-weak.mm
+++ b/clang/test/SemaObjC/arc-repeated-weak.mm
@@ -340,9 +340,6 @@ - (void)distinctFromOther:(Test *)other {
}
@end
-extern id foo; // expected-note {{previous declaration is here}}
-extern __weak id foo; // expected-error {{redeclaration of 'foo' with a different type}}
-
@interface Base1
@end
@interface Sub1 : Base1
@@ -512,3 +509,6 @@ -(void)m {
(void)self.nd[@""]; // no warning
}
@end
+
+extern id mergeQualsVar; // expected-note {{previous declaration is here}}
+extern __weak id mergeQualsVar; // expected-error {{redeclaration of 'mergeQualsVar' with a different type}}
More information about the cfe-commits
mailing list