[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
Thu Aug 13 00:27:14 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/6] [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/6] 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/6] 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}}
>From 48f77f587490f484cbefdbe04b063ae9b9030f23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hendrik=20H=C3=BCbner?= <hhuebner at MacBookPro.localdomain>
Date: Tue, 16 Jun 2026 11:28:30 +0200
Subject: [PATCH 4/6] fix the fix
---
clang/lib/AST/ASTContext.cpp | 10 ++++++----
clang/test/SemaObjC/arc-repeated-weak.mm | 6 ++++++
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index a4d7ded4bf50e..79892ef95dc17 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -12353,6 +12353,12 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
// If the qualifiers are different, the types can still be merged.
Qualifiers LQuals = LHSCan.getLocalQualifiers();
Qualifiers RQuals = RHSCan.getLocalQualifiers();
+
+ if (LQuals.withoutObjCGCAttr() != RQuals.withoutObjCGCAttr()) {
+ // Reject immediately, if anything but the GC qualifiers is different.
+ return {};
+ }
+
if (LQuals != RQuals) {
// Exactly one GC qualifier difference is allowed: __strong is
// okay if the other type has no GC qualifier but is an Objective
@@ -12361,10 +12367,6 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
// qualified __strong.
Qualifiers::GC GC_L = LQuals.getObjCGCAttr();
Qualifiers::GC GC_R = RQuals.getObjCGCAttr();
- 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 ecffd77faa203..13cd964a978ce 100644
--- a/clang/test/SemaObjC/arc-repeated-weak.mm
+++ b/clang/test/SemaObjC/arc-repeated-weak.mm
@@ -512,3 +512,9 @@ -(void)m {
extern id mergeQualsVar; // expected-note {{previous declaration is here}}
extern __weak id mergeQualsVar; // expected-error {{redeclaration of 'mergeQualsVar' with a different type}}
+
+extern const id mergeQualsVar2; // expected-note {{previous declaration is here}}
+extern __strong id mergeQualsVar2; // expected-error {{redeclaration of 'mergeQualsVar2' with a different type}}
+
+extern Class mergeQualsVar3; // expected-note {{previous declaration is here}}
+extern __ptrauth(1,1,1) Class mergeQualsVar3; // expected-error {{redeclaration of 'mergeQualsVar3' with a different type}}
>From afac59782a3b9724b4c5601ee3173125c7eb8ab7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hendrik=20H=C3=BCbner?= <hhuebner at hhuebnerMacBookPro.local>
Date: Tue, 28 Jul 2026 15:07:42 +0200
Subject: [PATCH 5/6] Fix the fix of the fix
---
clang/lib/AST/ASTContext.cpp | 2 ++
clang/test/SemaObjC/arc-repeated-weak.mm | 9 ---------
clang/test/SemaObjC/gc-attributes.m | 12 ++++++++++++
3 files changed, 14 insertions(+), 9 deletions(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 79892ef95dc17..cb9536e90dd45 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -12368,6 +12368,8 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
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 == 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 13cd964a978ce..aaf8256d314ee 100644
--- a/clang/test/SemaObjC/arc-repeated-weak.mm
+++ b/clang/test/SemaObjC/arc-repeated-weak.mm
@@ -509,12 +509,3 @@ -(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}}
-
-extern const id mergeQualsVar2; // expected-note {{previous declaration is here}}
-extern __strong id mergeQualsVar2; // expected-error {{redeclaration of 'mergeQualsVar2' with a different type}}
-
-extern Class mergeQualsVar3; // expected-note {{previous declaration is here}}
-extern __ptrauth(1,1,1) Class mergeQualsVar3; // expected-error {{redeclaration of 'mergeQualsVar3' with a different type}}
diff --git a/clang/test/SemaObjC/gc-attributes.m b/clang/test/SemaObjC/gc-attributes.m
index 1b81cafd35a7d..12b3e40029a19 100644
--- a/clang/test/SemaObjC/gc-attributes.m
+++ b/clang/test/SemaObjC/gc-attributes.m
@@ -24,3 +24,15 @@ void test_f1(void) {
// These qualifiers should silently expand to nothing in GC mode.
void test_unsafe_unretained(__unsafe_unretained id *x) {}
void test_autoreleasing(__autoreleasing id *x) {}
+
+extern id mergeQualsVar; // expected-note {{previous declaration is here}}
+extern __weak id mergeQualsVar; // expected-error {{redeclaration of 'mergeQualsVar' with a different type}}
+
+extern const id mergeQualsVar2; // expected-note {{previous declaration is here}}
+extern __strong id mergeQualsVar2; // expected-error {{redeclaration of 'mergeQualsVar2' with a different type}}
+
+extern id mergeQualsVar3; // expected-note {{previous declaration is here}}
+extern volatile id mergeQualsVar3; // expected-error {{redeclaration of 'mergeQualsVar3' with a different type}}
+
+extern id mergeQualsVarOk;
+extern __strong id mergeQualsVarOk; // Should merge just fine
>From 9dd989a81fbf16a58aad98ea83c9d3e422234b3b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hendrik=20H=C3=BCbner?= <hhuebner at hhuebnerMacBookPro.local>
Date: Tue, 28 Jul 2026 15:22:54 +0200
Subject: [PATCH 6/6] fmt
---
clang/lib/AST/ASTContext.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index cb9536e90dd45..9763da0549dbf 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -12355,7 +12355,7 @@ QualType ASTContext::mergeObjCGCQualifiers(QualType LHS, QualType RHS) {
Qualifiers RQuals = RHSCan.getLocalQualifiers();
if (LQuals.withoutObjCGCAttr() != RQuals.withoutObjCGCAttr()) {
- // Reject immediately, if anything but the GC qualifiers is different.
+ // Reject immediately, if anything but the GC qualifiers is different.
return {};
}
@@ -12367,7 +12367,6 @@ 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 == Qualifiers::Weak || GC_R == Qualifiers::Weak)
More information about the cfe-commits
mailing list