PR46377: Fix dependence calculation for function types and typedef

types.

We previously did not treat a function type as dependent if it had a
parameter pack with a non-dependent type -- such a function type depends
on the arity of the pack so is dependent even though none of the
parameter types is dependent. In order to properly handle this, we now
treat pack expansion types as always being dependent types (depending on
at least the pack arity), and always canonically being pack expansion
types, even in the unusual case when the pattern is not a dependent
type. This does mean that we can have canonical types that are pack
expansions that contain no unexpanded packs, which is unfortunate but
not inaccurate.

We also previously did not treat a typedef type as
instantiation-dependent if its canonical type was not
instantiation-dependent. That's wrong because instantiation-dependence
is a property of the type sugar, not of the type; an
instantiation-dependent type can have a non-instantiation-dependent
canonical type.
This commit is contained in:
Richard Smith 2020-07-28 12:09:16 -07:00
parent b46176bbb0
commit 740a164dec
12 changed files with 61 additions and 40 deletions

View file

@ -1459,8 +1459,16 @@ public:
void getInjectedTemplateArgs(const TemplateParameterList *Params,
SmallVectorImpl<TemplateArgument> &Args);
/// Form a pack expansion type with the given pattern.
/// \param NumExpansions The number of expansions for the pack, if known.
/// \param ExpectPackInType If \c false, we should not expect \p Pattern to
/// contain an unexpanded pack. This only makes sense if the pack
/// expansion is used in a context where the arity is inferred from
/// elsewhere, such as if the pattern contains a placeholder type or
/// if this is the canonical type of another pack expansion type.
QualType getPackExpansionType(QualType Pattern,
Optional<unsigned> NumExpansions);
Optional<unsigned> NumExpansions,
bool ExpectPackInType = true);
QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
ObjCInterfaceDecl *PrevDecl = nullptr) const;

View file

@ -4383,11 +4383,7 @@ class TypedefType : public Type {
protected:
friend class ASTContext; // ASTContext creates these.
TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can)
: Type(tc, can, can->getDependence() & ~TypeDependence::UnexpandedPack),
Decl(const_cast<TypedefNameDecl *>(D)) {
assert(!isa<TypedefType>(can) && "Invalid canonical type");
}
TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can);
public:
TypedefNameDecl *getDecl() const { return Decl; }
@ -5624,7 +5620,8 @@ class PackExpansionType : public Type, public llvm::FoldingSetNode {
PackExpansionType(QualType Pattern, QualType Canon,
Optional<unsigned> NumExpansions)
: Type(PackExpansion, Canon,
(Pattern->getDependence() | TypeDependence::Instantiation) &
(Pattern->getDependence() | TypeDependence::Dependent |
TypeDependence::Instantiation) &
~TypeDependence::UnexpandedPack),
Pattern(Pattern) {
PackExpansionTypeBits.NumExpansions =
@ -5645,8 +5642,8 @@ public:
return None;
}
bool isSugared() const { return !Pattern->isDependentType(); }
QualType desugar() const { return isSugared() ? Pattern : QualType(this, 0); }
bool isSugared() const { return false; }
QualType desugar() const { return QualType(this, 0); }
void Profile(llvm::FoldingSetNodeID &ID) {
Profile(ID, getPattern(), getNumExpansions());

View file

@ -100,7 +100,7 @@ def DeducedTemplateSpecializationType : TypeNode<DeducedType>;
def InjectedClassNameType : TypeNode<Type>, AlwaysDependent, LeafType;
def DependentNameType : TypeNode<Type>, AlwaysDependent;
def DependentTemplateSpecializationType : TypeNode<Type>, AlwaysDependent;
def PackExpansionType : TypeNode<Type>, NeverCanonicalUnlessDependent;
def PackExpansionType : TypeNode<Type>, AlwaysDependent;
def ObjCTypeParamType : TypeNode<Type>, NeverCanonical;
def ObjCObjectType : TypeNode<Type>;
def ObjCInterfaceType : TypeNode<ObjCObjectType>, LeafType;

View file

@ -4817,37 +4817,27 @@ ASTContext::getInjectedTemplateArgs(const TemplateParameterList *Params,
}
QualType ASTContext::getPackExpansionType(QualType Pattern,
Optional<unsigned> NumExpansions) {
Optional<unsigned> NumExpansions,
bool ExpectPackInType) {
assert((!ExpectPackInType || Pattern->containsUnexpandedParameterPack()) &&
"Pack expansions must expand one or more parameter packs");
llvm::FoldingSetNodeID ID;
PackExpansionType::Profile(ID, Pattern, NumExpansions);
// A deduced type can deduce to a pack, eg
// auto ...x = some_pack;
// That declaration isn't (yet) valid, but is created as part of building an
// init-capture pack:
// [...x = some_pack] {}
assert((Pattern->containsUnexpandedParameterPack() ||
Pattern->getContainedDeducedType()) &&
"Pack expansions must expand one or more parameter packs");
void *InsertPos = nullptr;
PackExpansionType *T
= PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
PackExpansionType *T = PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
if (T)
return QualType(T, 0);
QualType Canon;
if (!Pattern.isCanonical()) {
Canon = getCanonicalType(Pattern);
// The canonical type might not contain an unexpanded parameter pack, if it
// contains an alias template specialization which ignores one of its
// parameters.
if (Canon->containsUnexpandedParameterPack()) {
Canon = getPackExpansionType(Canon, NumExpansions);
Canon = getPackExpansionType(getCanonicalType(Pattern), NumExpansions,
/*ExpectPackInType=*/false);
// Find the insert position again, in case we inserted an element into
// PackExpansionTypes and invalidated our insert position.
PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
}
// Find the insert position again, in case we inserted an element into
// PackExpansionTypes and invalidated our insert position.
PackExpansionTypes.FindNodeOrInsertPos(ID, InsertPos);
}
T = new (*this, TypeAlignment)

View file

@ -1187,9 +1187,6 @@ public:
T->getTypeConstraintArguments());
}
// FIXME: Non-trivial to implement, but important for C++
SUGARED_TYPE_CLASS(PackExpansion)
QualType VisitObjCObjectType(const ObjCObjectType *T) {
QualType baseType = recurse(T->getBaseType());
if (baseType.isNull())
@ -3348,6 +3345,12 @@ void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID,
getExtProtoInfo(), Ctx, isCanonicalUnqualified());
}
TypedefType::TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can)
: Type(tc, can, D->getUnderlyingType()->getDependence()),
Decl(const_cast<TypedefNameDecl *>(D)) {
assert(!isa<TypedefType>(can) && "Invalid canonical type");
}
QualType TypedefType::desugar() const {
return getDecl()->getUnderlyingType();
}

View file

@ -3252,7 +3252,6 @@ llvm::DIType *CGDebugInfo::CreateTypeNode(QualType Ty, llvm::DIFile *Unit) {
case Type::TypeOf:
case Type::Decltype:
case Type::UnaryTransform:
case Type::PackExpansion:
break;
}

View file

@ -2075,7 +2075,6 @@ void CodeGenFunction::EmitVariablyModifiedType(QualType type) {
case Type::UnaryTransform:
case Type::Attributed:
case Type::SubstTemplateTypeParm:
case Type::PackExpansion:
case Type::MacroQualified:
// Keep walking after single level desugaring.
type = type.getSingleStepDesugaredType(getContext());

View file

@ -4345,7 +4345,6 @@ static void captureVariablyModifiedType(ASTContext &Context, QualType T,
case Type::UnaryTransform:
case Type::Attributed:
case Type::SubstTemplateTypeParm:
case Type::PackExpansion:
case Type::MacroQualified:
// Keep walking after single level desugaring.
T = T.getSingleStepDesugaredType(Context);

View file

@ -803,7 +803,8 @@ QualType Sema::buildLambdaInitCaptureInitialization(
Diag(EllipsisLoc, getLangOpts().CPlusPlus20
? diag::warn_cxx17_compat_init_capture_pack
: diag::ext_init_capture_pack);
DeductType = Context.getPackExpansionType(DeductType, NumExpansions);
DeductType = Context.getPackExpansionType(DeductType, NumExpansions,
/*ExpectPackInType=*/false);
TLB.push<PackExpansionTypeLoc>(DeductType).setEllipsisLoc(EllipsisLoc);
} else {
// Just ignore the ellipsis for now and form a non-pack variable. We'll

View file

@ -614,7 +614,8 @@ QualType Sema::CheckPackExpansion(QualType Pattern, SourceRange PatternRange,
return QualType();
}
return Context.getPackExpansionType(Pattern, NumExpansions);
return Context.getPackExpansionType(Pattern, NumExpansions,
/*ExpectPackInType=*/false);
}
ExprResult Sema::ActOnPackExpansion(Expr *Pattern, SourceLocation EllipsisLoc) {

View file

@ -5516,7 +5516,7 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
<< T << D.getSourceRange();
D.setEllipsisLoc(SourceLocation());
} else {
T = Context.getPackExpansionType(T, None);
T = Context.getPackExpansionType(T, None, /*ExpectPackInType=*/false);
}
break;
case DeclaratorContext::TemplateParamContext:

View file

@ -0,0 +1,24 @@
// RUN: %clang_cc1 -std=c++20 -verify %s
namespace PR46377 {
template<typename> using IntPtr = int*;
template<typename ...T> auto non_dependent_typedef() {
typedef int(*P)(IntPtr<T>...);
return P();
}
template<typename ...T> auto non_dependent_alias() {
using P = int(*)(IntPtr<T>...);
return P();
}
template<typename ...T> auto non_dependent_via_sizeof() {
using P = int(*)(int(...pack)[sizeof(sizeof(T))]); // expected-error {{invalid application of 'sizeof'}}
return P();
}
using a = int (*)(int*, int*);
using a = decltype(non_dependent_typedef<void, void>());
using a = decltype(non_dependent_alias<void, void>());
using a = decltype(non_dependent_via_sizeof<float, float>());
using b = decltype(non_dependent_via_sizeof<float, void>()); // expected-note {{instantiation of}}
}