From 7a980a9ba952bac42d1ec86c09cbeb11fd3a0b88 Mon Sep 17 00:00:00 2001 From: zhangziyao Date: Tue, 26 Aug 2025 14:39:32 +0800 Subject: [PATCH] [borrow] change the check order and fix the bug of unexpected diagnostic message --- clang/lib/Sema/SemaExpr.cpp | 19 ++++++++----------- .../borrow_call_arg/borrow_call_arg.cbs | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 clang/test/BSC/Negative/Ownership/Borrow/borrow_call_arg/borrow_call_arg.cbs diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 08b7853f06e8..ddc38107a9ea 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -10462,10 +10462,16 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &CallerRHS, if (!IsSafeConversion(LHSType, RHS.get())) { return IncompatibleBSCSafeZone; } - if (RHS.get()->getType().getCanonicalType().isOwnedQualified() || LHSType.getCanonicalType().isOwnedQualified()) { + QualType LHSCanType = LHSType.getCanonicalType(); + QualType RHSCanType = RHS.get()->getType().getCanonicalType(); + if (RHSCanType.isOwnedQualified() || LHSCanType.isOwnedQualified()) { if (!CheckOwnedQualTypeAssignment(LHSType, RHS.get())) return IncompatibleOwnedPointer; } + if (RHSCanType.isBorrowQualified() || LHSCanType.isBorrowQualified()) { + if (!CheckBorrowQualTypeAssignment(LHSType, RHS.get())) + return IncompatibleBorrowPointer; + } if (const auto *LHSPtrType = LHSType->getAs()) { if (const auto *RHSPtrType = RHS.get()->getType()->getAs()) { if (LHSPtrType->hasOwnedFields() || RHSPtrType->hasOwnedFields()) { @@ -10473,18 +10479,9 @@ Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &CallerRHS, return IncompatibleOwnedPointer; } } - } - } - - if (RHS.get()->getType().getCanonicalType().isBorrowQualified() || LHSType.getCanonicalType().isBorrowQualified()) { - if (!CheckBorrowQualTypeAssignment(LHSType, RHS.get())) - return IncompatibleOwnedPointer; - } - if (const auto *LHSPtrType = LHSType->getAs()) { - if (const auto *RHSPtrType = RHS.get()->getType()->getAs()) { if (LHSPtrType->hasBorrowFields() || RHSPtrType->hasBorrowFields()) { if (!CheckBorrowQualTypeAssignment(LHSType, RHS.get())) { - return IncompatibleOwnedPointer; + return IncompatibleBorrowPointer; } } } diff --git a/clang/test/BSC/Negative/Ownership/Borrow/borrow_call_arg/borrow_call_arg.cbs b/clang/test/BSC/Negative/Ownership/Borrow/borrow_call_arg/borrow_call_arg.cbs new file mode 100644 index 000000000000..2c3e0f1f4dda --- /dev/null +++ b/clang/test/BSC/Negative/Ownership/Borrow/borrow_call_arg/borrow_call_arg.cbs @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -verify %s + +owned struct Rc { +}; + +owned struct Weak { +}; + +Weak Weak::new(const Rc *borrow rc); + +owned struct S { +}; + +void test(Rc rc) { + Weak wk1 = Weak::new(&const rc); // expected-error {{incompatible borrow types, cannot cast 'const owned Rc *borrow' to 'const owned Rc *borrow'}} +} -- Gitee