-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[Clang] Added explanation why is_constructible
evaluated to false.
#143309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ed1f379
ef79556
cb89df9
bd3cefd
172cee8
bd3cfca
bd1e468
124e9f1
e18073b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/AST/DeclCXX.h" | ||
#include "clang/AST/TemplateBase.h" | ||
#include "clang/AST/Type.h" | ||
#include "clang/Basic/DiagnosticParse.h" | ||
#include "clang/Basic/DiagnosticSema.h" | ||
|
@@ -1945,6 +1946,7 @@ static std::optional<TypeTrait> StdNameToTypeTrait(StringRef Name) { | |
TypeTrait::UTT_IsCppTriviallyRelocatable) | ||
.Case("is_replaceable", TypeTrait::UTT_IsReplaceable) | ||
.Case("is_trivially_copyable", TypeTrait::UTT_IsTriviallyCopyable) | ||
.Case("is_constructible", TypeTrait::TT_IsConstructible) | ||
.Default(std::nullopt); | ||
} | ||
|
||
|
@@ -1981,8 +1983,16 @@ static ExtractedTypeTraitInfo ExtractTypeTraitFromExpression(const Expr *E) { | |
Trait = StdNameToTypeTrait(Name); | ||
if (!Trait) | ||
return std::nullopt; | ||
for (const auto &Arg : VD->getTemplateArgs().asArray()) | ||
Args.push_back(Arg.getAsType()); | ||
for (const auto &Arg : VD->getTemplateArgs().asArray()) { | ||
if (Arg.getKind() == TemplateArgument::ArgKind::Pack) { | ||
for (const auto &InnerArg : Arg.pack_elements()) | ||
Args.push_back(InnerArg.getAsType()); | ||
} else if (Arg.getKind() == TemplateArgument::ArgKind::Type) { | ||
Args.push_back(Arg.getAsType()); | ||
} else { | ||
llvm_unreachable("Unexpected kind"); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the kind is neither, we should probably do SOMETHING about it, even if it is just an assert. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added assert |
||
return {{Trait.value(), std::move(Args)}}; | ||
} | ||
|
||
|
@@ -2253,6 +2263,60 @@ static void DiagnoseNonTriviallyCopyableReason(Sema &SemaRef, | |
} | ||
} | ||
|
||
static void DiagnoseNonConstructibleReason( | ||
Sema &SemaRef, SourceLocation Loc, | ||
const llvm::SmallVector<clang::QualType, 1> &Ts) { | ||
if (Ts.empty()) { | ||
return; | ||
} | ||
|
||
bool ContainsVoid = false; | ||
for (const QualType &ArgTy : Ts) { | ||
ContainsVoid |= ArgTy->isVoidType(); | ||
} | ||
|
||
if (ContainsVoid) | ||
SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) | ||
<< diag::TraitNotSatisfiedReason::CVVoidType; | ||
|
||
QualType T = Ts[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we know that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added early-exit, thanks |
||
if (T->isFunctionType()) | ||
SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) | ||
<< diag::TraitNotSatisfiedReason::FunctionType; | ||
|
||
if (T->isIncompleteArrayType()) | ||
SemaRef.Diag(Loc, diag::note_unsatisfied_trait_reason) | ||
<< diag::TraitNotSatisfiedReason::IncompleteArrayType; | ||
|
||
const CXXRecordDecl *D = T->getAsCXXRecordDecl(); | ||
if (!D || D->isInvalidDecl() || !D->hasDefinition()) | ||
return; | ||
|
||
llvm::BumpPtrAllocator OpaqueExprAllocator; | ||
SmallVector<Expr *, 2> ArgExprs; | ||
ArgExprs.reserve(Ts.size() - 1); | ||
for (unsigned I = 1, N = Ts.size(); I != N; ++I) { | ||
QualType ArgTy = Ts[I]; | ||
if (ArgTy->isObjectType() || ArgTy->isFunctionType()) | ||
ArgTy = SemaRef.Context.getRValueReferenceType(ArgTy); | ||
ArgExprs.push_back( | ||
new (OpaqueExprAllocator.Allocate<OpaqueValueExpr>()) | ||
OpaqueValueExpr(Loc, ArgTy.getNonLValueExprType(SemaRef.Context), | ||
Expr::getValueKindForType(ArgTy))); | ||
} | ||
|
||
EnterExpressionEvaluationContext Unevaluated( | ||
SemaRef, Sema::ExpressionEvaluationContext::Unevaluated); | ||
Sema::ContextRAII TUContext(SemaRef, | ||
SemaRef.Context.getTranslationUnitDecl()); | ||
InitializedEntity To(InitializedEntity::InitializeTemporary(T)); | ||
InitializationKind InitKind(InitializationKind::CreateDirect(Loc, Loc, Loc)); | ||
InitializationSequence Init(SemaRef, To, InitKind, ArgExprs); | ||
|
||
Init.Diagnose(SemaRef, To, InitKind, ArgExprs); | ||
SemaRef.Diag(D->getLocation(), diag::note_defined_here) << D; | ||
} | ||
|
||
static void DiagnoseNonTriviallyCopyableReason(Sema &SemaRef, | ||
SourceLocation Loc, QualType T) { | ||
SemaRef.Diag(Loc, diag::note_unsatisfied_trait) | ||
|
@@ -2292,6 +2356,9 @@ void Sema::DiagnoseTypeTraitDetails(const Expr *E) { | |
case UTT_IsTriviallyCopyable: | ||
DiagnoseNonTriviallyCopyableReason(*this, E->getBeginLoc(), Args[0]); | ||
break; | ||
case TT_IsConstructible: | ||
DiagnoseNonConstructibleReason(*this, E->getBeginLoc(), Args); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IS this a valid assumption here (that this is a type?). Presumably this could be an NTTP pack as well...
Though
getAsType
would assert for us?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getAsType would assert for us