[clang] Reapply "[Clang][Sema] Always use latest redeclaration of primary template" (PR #114569)

Lei Wang via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 7 14:13:21 PST 2024


wlei-llvm wrote:

Hi, here is the reduced repro (not perfect, but was as far as I can get), thanks!

```
namespace std
{
  template<typename _Tp>
    _Tp
    __declval(long);
  template<typename _Tp>
    auto declval() noexcept -> decltype(__declval<_Tp>(0));
  template<typename _From, typename _To>
    class __is_convertible_helper
    {
      template<typename _From1, typename _To1,
        typename = decltype(__test_aux(std::declval<_From1>()))>
 static int
 __test;
      typedef decltype(__test<_From, _To>0) type;
    };
  template<typename _From, typename _To>
    struct is_convertible
    : public __is_convertible_helper<_From, _To>::type
    ;
template <typename _Tp, typename _Up>
  inline constexpr bool is_same_v = __is_same(_Tp, _Up);
}
  enum _Lock_policy { _S_single, _S_mutex, _S_atomic };
  static const _Lock_policy __default_lock_policy =
  _S_atomic;
  template<typename _Tp, _Lock_policy _Lp = __default_lock_policy>
    class __shared_ptr;
  template<typename _Yp, typename _Tp>
    struct __sp_compatible_with
    : is_convertible<_Yp*, _Tp*>::type
    ;
  template<typename _Tp, _Lock_policy _Lp>
    class __shared_ptr
    {
 using _SafeConv
   = typename enable_if::type;
      template<typename _Yp, typename _Res = void>
 using _Compatible = typename
   enable_if__sp_compatible_with<_Yp*, _Tp*>::value, _Res::type;
    public:
 __shared_ptr0 noexcept
 : _M_ptr0, _M_refcount0
      __shared_ptr0 noexcept = default;
      __shared_ptr& operator=0 noexcept = default;
      ~__shared_ptr0 = default;
      template<typename _Yp, typename = _Compatible<_Yp>>
 __shared_ptr(const __shared_ptr<_Yp, _Lp>& __r) noexcept
    };
namespace storage {
class S ;
}
namespace storage 
template <typename T>
concept StatState =
    std::is_same_v<T, storage::S>;
namespace storage 
template <StatState StatState>
class Tracker;
template <StatState T = S>
std::shared_ptr<Tracker<T>> createTracker() ;
template <StatState T>
class Tracker {
 public:
  template <StatState K>
  std::shared_ptr<Tracker<K>> friend createTracker();
};
namespace storage 
class Pool {
 public:
  Pool() {
    g = createTracker();
  }
 private:
  std::shared_ptr<int> g;
};
void create0  () {
  auto tracker = createTracker();
}

```
please ignore the irrelevant syntax error, the real error is :
```
clang++ -ferror-limit=99999999 -std=gnu++20 -c test.cpp 

clang++: /home/wlei/local/upstream/llvm-project/clang/lib/AST/ExprConstant.cpp:16601: bool clang::Expr::EvaluateAsConstantExpr(EvalResult &, const ASTContext &, ConstantExprKind) const: Assertion `!isValueDependent() && "Expression evaluator can't be called on a dependent expression."' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: /home/wlei/local/upstream/llvm-release/bin/clang++ -ferror-limit=99999999 -std=gnu++20 -c test.cpp
1.      test.cpp:78:32: current parser token ')'
2.      test.cpp:77:18: parsing function body 'create0'
3.      test.cpp:77:18: in compound statement ('{}')
 #0 0x00007fcf547fb128 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/wlei/local/upstream/llvm-project/llvm/lib/Support/Unix/Signals.inc:723:13
 #1 0x00007fcf547f9040 llvm::sys::RunSignalHandlers() /home/wlei/local/upstream/llvm-project/llvm/lib/Support/Signals.cpp:106:18
 #2 0x00007fcf5473c7b6 (anonymous namespace)::CrashRecoveryContextImpl::HandleCrash(int, unsigned long) /home/wlei/local/upstream/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:73:5
 #3 0x00007fcf5473c7b6 CrashRecoverySignalHandler(int) /home/wlei/local/upstream/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:390:51
 #4 0x00007fcf53e3e730 __restore_rt (/lib64/libc.so.6+0x3e730)
 #5 0x00007fcf53e8bacc __pthread_kill_implementation (/lib64/libc.so.6+0x8bacc)
 #6 0x00007fcf53e3e686 gsignal (/lib64/libc.so.6+0x3e686)
 #7 0x00007fcf53e28833 abort (/lib64/libc.so.6+0x28833)
 #8 0x00007fcf53e2875b _nl_load_domain.cold (/lib64/libc.so.6+0x2875b)
 #9 0x00007fcf53e373c6 (/lib64/libc.so.6+0x373c6)
#10 0x00007fcf523da5b1 clang::Expr::EvaluateAsConstantExpr(clang::Expr::EvalResult&, clang::ASTContext const&, clang::Expr::ConstantExprKind) const /home/wlei/local/upstream/llvm-project/clang/lib/AST/ExprConstant.cpp:0:0
#11 0x00007fcf50f9b41d clang::ActionResult<clang::Expr*, true> calculateConstraintSatisfaction<calculateConstraintSatisfaction(clang::Sema&, clang::NamedDecl const*, clang::SourceLocation, clang::MultiLevelTemplateArgumentList const&, clang::Expr const*, clang::ConstraintSatisfaction&)::ConstraintEvaluator>(clang::Sema&, clang::Expr const*, clang::ConstraintSatisfaction&, calculateConstraintSatisfaction(clang::Sema&, clang::NamedDecl const*, clang::SourceLocation, clang::MultiLevelTemplateArgumentList const&, clang::Expr const*, clang::ConstraintSatisfaction&)::ConstraintEvaluator const&) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaConcept.cpp:389:71
#12 0x00007fcf50f941eb clang::ActionResult<clang::Expr*, true>::isInvalid() const /home/wlei/local/upstream/llvm-project/clang/include/clang/Sema/Ownership.h:199:41
#13 0x00007fcf50f941eb CheckConstraintSatisfaction(clang::Sema&, clang::NamedDecl const*, llvm::ArrayRef<clang::Expr const*>, llvm::SmallVectorImpl<clang::Expr*>&, clang::MultiLevelTemplateArgumentList const&, clang::SourceRange, clang::ConstraintSatisfaction&) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaConcept.cpp:600:13
#14 0x00007fcf50f93f6a clang::Sema::CheckConstraintSatisfaction(clang::NamedDecl const*, llvm::ArrayRef<clang::Expr const*>, llvm::SmallVectorImpl<clang::Expr*>&, clang::MultiLevelTemplateArgumentList const&, clang::SourceRange, clang::ConstraintSatisfaction&) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaConcept.cpp:658:7
#15 0x00007fcf50f974fe clang::Sema::CheckInstantiatedFunctionTemplateConstraints(clang::SourceLocation, clang::FunctionDecl*, llvm::ArrayRef<clang::TemplateArgument>, clang::ConstraintSatisfaction&) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaConcept.cpp:1151:10
#16 0x00007fcf5176de46 clang::Sema::FinishTemplateArgumentDeduction(clang::FunctionTemplateDecl*, llvm::SmallVectorImpl<clang::DeducedTemplateArgument>&, unsigned int, clang::FunctionDecl*&, clang::sema::TemplateDeductionInfo&, llvm::SmallVectorImpl<clang::Sema::OriginalCallArg> const*, bool, llvm::function_ref<bool ()>) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaTemplateDeduction.cpp:0:9
#17 0x00007fcf517e59b9 clang::Sema::DeduceTemplateArguments(clang::FunctionTemplateDecl*, clang::TemplateArgumentListInfo*, llvm::ArrayRef<clang::Expr*>, clang::FunctionDecl*&, clang::sema::TemplateDeductionInfo&, bool, bool, clang::QualType, clang::Expr::Classification, llvm::function_ref<bool (llvm::ArrayRef<clang::QualType>)>)::$_2::operator()() const /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaTemplateDeduction.cpp:4647:5
#18 0x00007fcf517e59b9 void llvm::function_ref<void ()>::callback_fn<clang::Sema::DeduceTemplateArguments(clang::FunctionTemplateDecl*, clang::TemplateArgumentListInfo*, llvm::ArrayRef<clang::Expr*>, clang::FunctionDecl*&, clang::sema::TemplateDeductionInfo&, bool, bool, clang::QualType, clang::Expr::Classification, llvm::function_ref<bool (llvm::ArrayRef<clang::QualType>)>)::$_2>(long) /home/wlei/local/upstream/llvm-project/llvm/include/llvm/ADT/STLFunctionalExtras.h:45:12
#19 0x00007fcf56794b4f clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) /home/wlei/local/upstream/llvm-project/clang/lib/Basic/StackExhaustionHandler.cpp:21:1
#20 0x00007fcf51770350 clang::Sema::DeduceTemplateArguments(clang::FunctionTemplateDecl*, clang::TemplateArgumentListInfo*, llvm::ArrayRef<clang::Expr*>, clang::FunctionDecl*&, clang::sema::TemplateDeductionInfo&, bool, bool, clang::QualType, clang::Expr::Classification, llvm::function_ref<bool (llvm::ArrayRef<clang::QualType>)>) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaTemplateDeduction.cpp:4654:10
#21 0x00007fcf51615037 clang::Sema::AddTemplateOverloadCandidate(clang::FunctionTemplateDecl*, clang::DeclAccessPair, clang::TemplateArgumentListInfo*, llvm::ArrayRef<clang::Expr*>, clang::OverloadCandidateSet&, bool, bool, bool, clang::CallExpr::ADLCallKind, clang::OverloadCandidateParamOrder, bool) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaOverload.cpp:7788:14
#22 0x00007fcf516265d2 AddOverloadedCallCandidate(clang::Sema&, clang::DeclAccessPair, clang::TemplateArgumentListInfo*, llvm::ArrayRef<clang::Expr*>, clang::OverloadCandidateSet&, bool, bool) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaOverload.cpp:13588:7
#23 0x00007fcf5162642c clang::Sema::AddOverloadedCallCandidates(clang::UnresolvedLookupExpr*, llvm::ArrayRef<clang::Expr*>, clang::OverloadCandidateSet&, bool) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaOverload.cpp:0:5
#24 0x00007fcf5162696e clang::Sema::getLangOpts() const /home/wlei/local/upstream/llvm-project/clang/include/clang/Sema/Sema.h:524:51
#25 0x00007fcf5162696e clang::Sema::buildOverloadedCallSet(clang::Scope*, clang::Expr*, clang::UnresolvedLookupExpr*, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::OverloadCandidateSet*, clang::ActionResult<clang::Expr*, true>*) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaOverload.cpp:13950:7
#26 0x00007fcf51626d8b clang::Sema::BuildOverloadedCallExpr(clang::Scope*, clang::Expr*, clang::UnresolvedLookupExpr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaOverload.cpp:14156:7
#27 0x00007fcf5119cddc clang::Sema::BuildCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*, bool, bool) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaExpr.cpp:6548:16
#28 0x00007fcf511b2d9b clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*) /home/wlei/local/upstream/llvm-project/clang/lib/Sema/SemaExpr.cpp:6434:7
#29 0x00007fcf531627ae clang::Parser::ParsePostfixExpressionSuffix(clang::ActionResult<clang::Expr*, true>) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseExpr.cpp:2242:23
#30 0x00007fcf53164860 clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, bool&, clang::Parser::TypeCastState, bool, bool*) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseExpr.cpp:1944:9
#31 0x00007fcf531603ef clang::Parser::ParseCastExpression(clang::Parser::CastParseKind, bool, clang::Parser::TypeCastState, bool, bool*) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseExpr.cpp:710:20
#32 0x00007fcf531603ef clang::Parser::ParseAssignmentExpression(clang::Parser::TypeCastState) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseExpr.cpp:184:20
#33 0x00007fcf5312d5eb clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) /home/wlei/local/upstream/llvm-project/clang/include/clang/Parse/Parser.h:0:14
#34 0x00007fcf5312a7f2 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseDecl.cpp:2550:7
#35 0x00007fcf53129977 clang::Parser::ParseSimpleDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, bool, clang::Parser::ForRangeInit*, clang::SourceLocation*) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseDecl.cpp:0:10
#36 0x00007fcf5312935d clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseDecl.cpp:0:0
#37 0x00007fcf531c77d6 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseStmt.cpp:0:16
#38 0x00007fcf531c5aa4 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseStmt.cpp:125:20
#39 0x00007fcf531cf511 clang::Parser::ParseCompoundStatementBody(bool) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseStmt.cpp:1259:11
#40 0x00007fcf531d0362 clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseStmt.cpp:2565:21
#41 0x00007fcf531ea59b clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/Parser.cpp:0:0
#42 0x00007fcf5312b8e5 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) /home/wlei/local/upstream/llvm-project/clang/lib/Parse/ParseDecl.cpp:0:0
```



https://github.com/llvm/llvm-project/pull/114569


More information about the cfe-commits mailing list