https://github.com/frederick-vs-ja updated https://github.com/llvm/llvm-project/pull/142554
>From 4a98b75bcabd0f524988fbd63caedd0ecbad5810 Mon Sep 17 00:00:00 2001 From: "A. Jiang" <d...@live.cn> Date: Tue, 3 Jun 2025 17:40:30 +0800 Subject: [PATCH] [Clang] Implement LWG3819 for `__reference_meows_from_temporary` Also fix use cases for function reference binding ( `__reference_binds_to_temporary` is also affected despite being deprecated). --- clang/docs/ReleaseNotes.rst | 9 +++++++++ clang/lib/Sema/SemaTypeTraits.cpp | 15 +++++++++++++- clang/test/SemaCXX/type-traits.cpp | 32 ++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index a73c71cdd7201..eccee1aeef846 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -703,6 +703,15 @@ Bug Fixes to Compiler Builtins are ``__builtin_is_cpp_trivially_relocatable``. It is recommended to use ``__builtin_trivially_relocate`` instead. +- ``__reference_binds_to_temporary``, ``__reference_constructs_from_temporary`` + and ``__reference_converts_from_temporary`` intrinsics no longer consider + function references can bind to temporary objects. (#GH114344) + +- ``__reference_constructs_from_temporary`` and + ``__reference_converts_from_temporary`` intrinsics detect reference binding + to prvalue instead of xvalue now if the second operand is an object type, per + `LWG3819 <https://cplusplus.github.io/LWG/issue3819>`_. + Bug Fixes to Attribute Support ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Fixed crash when a parameter to the ``clang::annotate`` attribute evaluates to ``void``. See #GH119125 diff --git a/clang/lib/Sema/SemaTypeTraits.cpp b/clang/lib/Sema/SemaTypeTraits.cpp index 04f54d7044e4f..b849bbe94af62 100644 --- a/clang/lib/Sema/SemaTypeTraits.cpp +++ b/clang/lib/Sema/SemaTypeTraits.cpp @@ -1343,12 +1343,21 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind, if (RD && RD->isAbstract()) return false; + // LWG3819: For reference_meows_from_temporary traits, && is not added to + // the source object type. + // Otherwise, compute the result of add_rvalue_reference_t. + bool UseRawObjectType = + Kind == clang::BTT_ReferenceBindsToTemporary || + Kind == clang::BTT_ReferenceConstructsFromTemporary || + Kind == clang::BTT_ReferenceConvertsFromTemporary; + llvm::BumpPtrAllocator OpaqueExprAllocator; SmallVector<Expr *, 2> ArgExprs; ArgExprs.reserve(Args.size() - 1); for (unsigned I = 1, N = Args.size(); I != N; ++I) { QualType ArgTy = Args[I]->getType(); - if (ArgTy->isObjectType() || ArgTy->isFunctionType()) + if ((ArgTy->isObjectType() && !UseRawObjectType) || + ArgTy->isFunctionType()) ArgTy = S.Context.getRValueReferenceType(ArgTy); ArgExprs.push_back( new (OpaqueExprAllocator.Allocate<OpaqueValueExpr>()) @@ -1386,6 +1395,10 @@ static bool EvaluateBooleanTypeTrait(Sema &S, TypeTrait Kind, if (!T->isReferenceType()) return false; + // A function reference never binds to a temporary object. + if (T.getNonReferenceType()->isFunctionType()) + return false; + if (!Init.isDirectReferenceBinding()) return true; diff --git a/clang/test/SemaCXX/type-traits.cpp b/clang/test/SemaCXX/type-traits.cpp index 56cca05402f45..3f0124755c674 100644 --- a/clang/test/SemaCXX/type-traits.cpp +++ b/clang/test/SemaCXX/type-traits.cpp @@ -3143,6 +3143,10 @@ void reference_binds_to_temporary_checks() { static_assert(!(__reference_binds_to_temporary(int, long))); static_assert((__reference_binds_to_temporary(const int &, long))); + + // Test that function references are never considered bound to temporaries. + static_assert(!__reference_binds_to_temporary(void(&)(), void())); + static_assert(!__reference_binds_to_temporary(void(&&)(), void())); } @@ -3156,6 +3160,14 @@ struct ExplicitConversionRef { explicit operator int&(); }; +struct NonMovable { + NonMovable(NonMovable&&) = delete; +}; + +struct ConvertsFromNonMovable { + ConvertsFromNonMovable(NonMovable); +}; + void reference_constructs_from_temporary_checks() { static_assert(!__reference_constructs_from_temporary(int &, int &)); static_assert(!__reference_constructs_from_temporary(int &, int &&)); @@ -3193,6 +3205,16 @@ void reference_constructs_from_temporary_checks() { static_assert(__reference_constructs_from_temporary(const int &, long)); + // Test that function references are never considered bound to temporaries. + static_assert(!__reference_constructs_from_temporary(void(&&)(), void())); + static_assert(!__reference_constructs_from_temporary(void(&)(), void())); + + // LWG3819: reference_meows_from_temporary should not use is_meowible + static_assert(__reference_constructs_from_temporary(ConvertsFromNonMovable&&, NonMovable) == __cplusplus >= 201703L); + // For scalar types, cv-qualifications are dropped first for prvalues. + static_assert(__reference_constructs_from_temporary(int&&, const int)); + static_assert(__reference_constructs_from_temporary(int&&, volatile int)); + // Additional checks static_assert(__reference_constructs_from_temporary(POD const&, Derives)); static_assert(__reference_constructs_from_temporary(int&&, int)); @@ -3250,6 +3272,16 @@ void reference_converts_from_temporary_checks() { static_assert(__reference_converts_from_temporary(const int &, long)); + // Test that function references are never considered bound to temporaries. + static_assert(!__reference_converts_from_temporary(void(&)(), void())); + static_assert(!__reference_converts_from_temporary(void(&&)(), void())); + + // LWG3819: reference_meows_from_temporary should not use is_meowible + static_assert(__reference_converts_from_temporary(ConvertsFromNonMovable&&, NonMovable) == __cplusplus >= 201703L); + // For scalar types, cv-qualifications are dropped first for prvalues. + static_assert(__reference_converts_from_temporary(int&&, const int)); + static_assert(__reference_converts_from_temporary(int&&, volatile int)); + // Additional checks static_assert(__reference_converts_from_temporary(POD const&, Derives)); static_assert(__reference_converts_from_temporary(int&&, int)); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits