Skip to content

Commit 29cdbe8

Browse files
committed
Strip TypeEraserAttr of its TypeLoc
1 parent 80d3a32 commit 29cdbe8

File tree

6 files changed

+64
-39
lines changed

6 files changed

+64
-39
lines changed

include/swift/AST/Attr.h

+15-22
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "swift/AST/PlatformKind.h"
3737
#include "swift/AST/Requirement.h"
3838
#include "swift/AST/TrailingCallArguments.h"
39-
#include "swift/AST/TypeLoc.h"
4039
#include "llvm/ADT/SmallVector.h"
4140
#include "llvm/ADT/StringRef.h"
4241
#include "llvm/Support/ErrorHandling.h"
@@ -1111,47 +1110,41 @@ class DynamicReplacementAttr final
11111110

11121111
/// The \c @_typeEraser(TypeEraserType) attribute.
11131112
class TypeEraserAttr final : public DeclAttribute {
1114-
TypeLoc TypeEraserLoc;
1113+
TypeExpr *TypeEraserExpr;
11151114
LazyMemberLoader *Resolver;
11161115
uint64_t ResolverContextData;
11171116

11181117
friend class ResolveTypeEraserTypeRequest;
11191118

1120-
TypeEraserAttr(SourceLoc atLoc, SourceRange range, TypeLoc typeEraserLoc,
1119+
TypeEraserAttr(SourceLoc atLoc, SourceRange range, TypeExpr *typeEraserExpr,
11211120
LazyMemberLoader *Resolver, uint64_t Data)
11221121
: DeclAttribute(DAK_TypeEraser, atLoc, range, /*Implicit=*/false),
1123-
TypeEraserLoc(typeEraserLoc),
1122+
TypeEraserExpr(typeEraserExpr),
11241123
Resolver(Resolver), ResolverContextData(Data) {}
11251124

11261125
public:
11271126
static TypeEraserAttr *create(ASTContext &ctx,
11281127
SourceLoc atLoc, SourceRange range,
1129-
TypeRepr *typeEraserRepr);
1128+
TypeExpr *typeEraserRepr);
11301129

11311130
static TypeEraserAttr *create(ASTContext &ctx,
11321131
LazyMemberLoader *Resolver,
11331132
uint64_t Data);
11341133

11351134
/// Retrieve the parsed type repr for this attribute, if it
11361135
/// was parsed. Else returns \c nullptr.
1137-
TypeRepr *getParsedTypeEraserTypeRepr() const {
1138-
return TypeEraserLoc.getTypeRepr();
1139-
}
1136+
TypeRepr *getParsedTypeEraserTypeRepr() const;
11401137

11411138
/// Retrieve the parsed location for this attribute, if it was parsed.
1142-
SourceLoc getLoc() const {
1143-
return TypeEraserLoc.getLoc();
1144-
}
1139+
SourceLoc getLoc() const;
11451140

11461141
/// Retrieve the resolved type of this attribute if it has been resolved by a
11471142
/// successful call to \c getResolvedType(). Otherwise,
11481143
/// returns \c Type()
11491144
///
11501145
/// This entrypoint is only suitable for syntactic clients like the
11511146
/// AST printer. Semantic clients should use \c getResolvedType() instead.
1152-
Type getTypeWithoutResolving() const {
1153-
return TypeEraserLoc.getType();
1154-
}
1147+
Type getTypeWithoutResolving() const;
11551148

11561149
/// Returns \c true if the type eraser type has a valid implementation of the
11571150
/// erasing initializer for the given protocol.
@@ -1597,27 +1590,27 @@ class ClangImporterSynthesizedTypeAttr : public DeclAttribute {
15971590
/// Defines a custom attribute.
15981591
class CustomAttr final : public DeclAttribute,
15991592
public TrailingCallArguments<CustomAttr> {
1600-
TypeLoc type;
1593+
TypeExpr *typeExpr;
16011594
Expr *arg;
16021595
PatternBindingInitializer *initContext;
16031596
Expr *semanticInit = nullptr;
16041597

16051598
unsigned hasArgLabelLocs : 1;
16061599
unsigned numArgLabels : 16;
16071600

1608-
CustomAttr(SourceLoc atLoc, SourceRange range, TypeLoc type,
1601+
CustomAttr(SourceLoc atLoc, SourceRange range, TypeExpr *type,
16091602
PatternBindingInitializer *initContext, Expr *arg,
16101603
ArrayRef<Identifier> argLabels, ArrayRef<SourceLoc> argLabelLocs,
16111604
bool implicit);
16121605

16131606
public:
1614-
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
1607+
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
16151608
bool implicit = false) {
16161609
return create(ctx, atLoc, type, false, nullptr, SourceLoc(), { }, { }, { },
16171610
SourceLoc(), implicit);
16181611
}
16191612

1620-
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
1613+
static CustomAttr *create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
16211614
bool hasInitializer,
16221615
PatternBindingInitializer *initContext,
16231616
SourceLoc lParenLoc,
@@ -1630,8 +1623,8 @@ class CustomAttr final : public DeclAttribute,
16301623
unsigned getNumArguments() const { return numArgLabels; }
16311624
bool hasArgumentLabelLocs() const { return hasArgLabelLocs; }
16321625

1633-
TypeRepr *getTypeRepr() const { return type.getTypeRepr(); }
1634-
Type getType() const { return type.getType(); }
1626+
TypeRepr *getTypeRepr() const;
1627+
Type getType() const;
16351628

16361629
Expr *getArg() const { return arg; }
16371630
void setArg(Expr *newArg) { arg = newArg; }
@@ -1647,11 +1640,11 @@ class CustomAttr final : public DeclAttribute,
16471640

16481641
private:
16491642
friend class CustomAttrNominalRequest;
1650-
void resetTypeInformation(TypeRepr *repr) { type = TypeLoc(repr); }
1643+
void resetTypeInformation(TypeExpr *repr);
16511644

16521645
private:
16531646
friend class CustomAttrTypeRequest;
1654-
void setType(Type ty) { type = TypeLoc(getTypeRepr(), ty); }
1647+
void setType(Type ty);
16551648
};
16561649

16571650
/// Relates a property to its projection value property, as described by a property wrapper. For

lib/AST/Attr.cpp

+32-9
Original file line numberDiff line numberDiff line change
@@ -1386,25 +1386,36 @@ SourceLoc DynamicReplacementAttr::getRParenLoc() const {
13861386

13871387
TypeEraserAttr *TypeEraserAttr::create(ASTContext &ctx,
13881388
SourceLoc atLoc, SourceRange range,
1389-
TypeRepr *typeEraserRepr) {
1390-
return new (ctx) TypeEraserAttr(atLoc, range, typeEraserRepr, nullptr, 0);
1389+
TypeExpr *typeEraserExpr) {
1390+
return new (ctx) TypeEraserAttr(atLoc, range, typeEraserExpr, nullptr, 0);
13911391
}
13921392

13931393
TypeEraserAttr *TypeEraserAttr::create(ASTContext &ctx,
13941394
LazyMemberLoader *Resolver,
13951395
uint64_t Data) {
13961396
return new (ctx) TypeEraserAttr(SourceLoc(), SourceRange(),
1397-
TypeLoc(), Resolver, Data);
1397+
nullptr, Resolver, Data);
13981398
}
13991399

1400-
bool
1401-
TypeEraserAttr::hasViableTypeEraserInit(ProtocolDecl *protocol) const {
1400+
bool TypeEraserAttr::hasViableTypeEraserInit(ProtocolDecl *protocol) const {
14021401
return evaluateOrDefault(protocol->getASTContext().evaluator,
14031402
TypeEraserHasViableInitRequest{
14041403
const_cast<TypeEraserAttr *>(this), protocol},
14051404
false);
14061405
}
14071406

1407+
TypeRepr *TypeEraserAttr::getParsedTypeEraserTypeRepr() const {
1408+
return TypeEraserExpr ? TypeEraserExpr->getTypeRepr() : nullptr;
1409+
}
1410+
1411+
SourceLoc TypeEraserAttr::getLoc() const {
1412+
return TypeEraserExpr ? TypeEraserExpr->getLoc() : SourceLoc();
1413+
}
1414+
1415+
Type TypeEraserAttr::getTypeWithoutResolving() const {
1416+
return TypeEraserExpr ? TypeEraserExpr->getInstanceType() : Type();
1417+
}
1418+
14081419
Type TypeEraserAttr::getResolvedType(const ProtocolDecl *PD) const {
14091420
auto &ctx = PD->getASTContext();
14101421
return evaluateOrDefault(ctx.evaluator,
@@ -1847,20 +1858,21 @@ TypeRepr *ImplementsAttr::getProtocolTypeRepr() const {
18471858
return ProtocolType->getTypeRepr();
18481859
}
18491860

1850-
CustomAttr::CustomAttr(SourceLoc atLoc, SourceRange range, TypeLoc type,
1861+
CustomAttr::CustomAttr(SourceLoc atLoc, SourceRange range, TypeExpr *type,
18511862
PatternBindingInitializer *initContext, Expr *arg,
18521863
ArrayRef<Identifier> argLabels,
18531864
ArrayRef<SourceLoc> argLabelLocs, bool implicit)
18541865
: DeclAttribute(DAK_Custom, atLoc, range, implicit),
1855-
type(type),
1866+
typeExpr(type),
18561867
arg(arg),
18571868
initContext(initContext) {
1869+
assert(type);
18581870
hasArgLabelLocs = !argLabelLocs.empty();
18591871
numArgLabels = argLabels.size();
18601872
initializeCallArguments(argLabels, argLabelLocs);
18611873
}
18621874

1863-
CustomAttr *CustomAttr::create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
1875+
CustomAttr *CustomAttr::create(ASTContext &ctx, SourceLoc atLoc, TypeExpr *type,
18641876
bool hasInitializer,
18651877
PatternBindingInitializer *initContext,
18661878
SourceLoc lParenLoc,
@@ -1869,6 +1881,7 @@ CustomAttr *CustomAttr::create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
18691881
ArrayRef<SourceLoc> argLabelLocs,
18701882
SourceLoc rParenLoc,
18711883
bool implicit) {
1884+
assert(type);
18721885
SmallVector<Identifier, 2> argLabelsScratch;
18731886
SmallVector<SourceLoc, 2> argLabelLocsScratch;
18741887
Expr *arg = nullptr;
@@ -1878,7 +1891,7 @@ CustomAttr *CustomAttr::create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
18781891
argLabelsScratch, argLabelLocsScratch);
18791892
}
18801893

1881-
SourceRange range(atLoc, type.getSourceRange().End);
1894+
SourceRange range(atLoc, type->getSourceRange().End);
18821895
if (arg)
18831896
range.End = arg->getEndLoc();
18841897

@@ -1888,6 +1901,16 @@ CustomAttr *CustomAttr::create(ASTContext &ctx, SourceLoc atLoc, TypeLoc type,
18881901
argLabelLocs, implicit);
18891902
}
18901903

1904+
TypeRepr *CustomAttr::getTypeRepr() const { return typeExpr->getTypeRepr(); }
1905+
Type CustomAttr::getType() const { return typeExpr->getInstanceType(); }
1906+
1907+
void CustomAttr::resetTypeInformation(TypeExpr *info) { typeExpr = info; }
1908+
1909+
void CustomAttr::setType(Type ty) {
1910+
assert(ty);
1911+
typeExpr->setType(MetatypeType::get(ty));
1912+
}
1913+
18911914
void swift::simple_display(llvm::raw_ostream &out, const DeclAttribute *attr) {
18921915
if (attr)
18931916
attr->print(out);

lib/AST/NameLookup.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,8 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
23472347
identTypeRepr
23482348
};
23492349

2350-
attr->resetTypeInformation(IdentTypeRepr::create(ctx, components));
2350+
auto *newTE = new (ctx) TypeExpr(IdentTypeRepr::create(ctx, components));
2351+
attr->resetTypeInformation(newTE);
23512352
return nominal;
23522353
}
23532354
}

lib/AST/TypeCheckRequests.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -1410,16 +1410,22 @@ void LookupAllConformancesInContextRequest::writeDependencySink(
14101410
//----------------------------------------------------------------------------//
14111411

14121412
Optional<Type> ResolveTypeEraserTypeRequest::getCachedResult() const {
1413-
auto ty = std::get<1>(getStorage())->TypeEraserLoc.getType();
1414-
if (ty.isNull()) {
1413+
auto *TyExpr = std::get<1>(getStorage())->TypeEraserExpr;
1414+
if (!TyExpr || !TyExpr->getType()) {
14151415
return None;
14161416
}
1417-
return ty;
1417+
return TyExpr->getInstanceType();
14181418
}
14191419

14201420
void ResolveTypeEraserTypeRequest::cacheResult(Type value) const {
14211421
assert(value && "Resolved type erasure type to null type!");
1422-
std::get<1>(getStorage())->TypeEraserLoc.setType(value);
1422+
auto *attr = std::get<1>(getStorage());
1423+
if (attr->TypeEraserExpr) {
1424+
attr->TypeEraserExpr->setType(MetatypeType::get(value));
1425+
} else {
1426+
attr->TypeEraserExpr = TypeExpr::createImplicit(value,
1427+
value->getASTContext());
1428+
}
14231429
}
14241430

14251431
//----------------------------------------------------------------------------//

lib/Parse/ParseDecl.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -2218,7 +2218,8 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
22182218
if (invalid)
22192219
return false;
22202220

2221-
Attributes.add(TypeEraserAttr::create(Context, AtLoc, {Loc, RParenLoc}, ErasedType.get()));
2221+
auto *TE = new (Context) TypeExpr(ErasedType.get());
2222+
Attributes.add(TypeEraserAttr::create(Context, AtLoc, {Loc, RParenLoc}, TE));
22222223
break;
22232224
}
22242225

@@ -2591,7 +2592,8 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc At
25912592
}
25922593

25932594
// Form the attribute.
2594-
auto attr = CustomAttr::create(Context, AtLoc, type.get(), hasInitializer,
2595+
auto *TE = new (Context) TypeExpr(type.get());
2596+
auto attr = CustomAttr::create(Context, AtLoc, TE, hasInitializer,
25952597
initContext, lParenLoc, args, argLabels,
25962598
argLabelLocs, rParenLoc);
25972599
Attributes.add(attr);

lib/Sema/TypeCheckRequestFunctions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Type FunctionBuilderTypeRequest::evaluate(Evaluator &evaluator,
366366
evaluator,
367367
CustomAttrTypeRequest{mutableAttr, dc, CustomAttrTypeKind::NonGeneric},
368368
Type());
369-
if (!type) return Type();
369+
if (!type || type->hasError()) return Type();
370370

371371
auto nominal = type->getAnyNominal();
372372
if (!nominal) {

0 commit comments

Comments
 (0)