Skip to content

Commit d71ebcd

Browse files
authored
Merge pull request #18392 from slavapestov/error-type-cleanup
Error type cleanup
2 parents 4201f9c + 3454a54 commit d71ebcd

File tree

14 files changed

+76
-90
lines changed

14 files changed

+76
-90
lines changed

include/swift/AST/Decl.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,13 +4392,6 @@ class AbstractStorageDecl : public ValueDecl {
43924392
/// property from the given module?
43934393
bool isResilient(ModuleDecl *M, ResilienceExpansion expansion) const;
43944394

4395-
/// Returns the interface type of elements of storage represented by this
4396-
/// declaration.
4397-
///
4398-
/// For variables, this is the type of the variable itself.
4399-
/// For subscripts, this is the type of the subscript element.
4400-
Type getStorageInterfaceType() const;
4401-
44024395
/// Does the storage use a behavior?
44034396
bool hasBehavior() const {
44044397
return BehaviorInfo.getPointer() != nullptr;

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3528,6 +3528,8 @@ getGenericFunctionRecursiveProperties(Type Input, Type Result) {
35283528
static_assert(RecursiveTypeProperties::BitWidth == 10,
35293529
"revisit this if you add new recursive type properties");
35303530
RecursiveTypeProperties properties;
3531+
if (Input->getRecursiveProperties().hasError())
3532+
properties |= RecursiveTypeProperties::HasError;
35313533
if (Result->getRecursiveProperties().hasDynamicSelf())
35323534
properties |= RecursiveTypeProperties::HasDynamicSelf;
35333535
if (Result->getRecursiveProperties().hasError())

lib/AST/Decl.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4335,7 +4335,7 @@ SourceLoc AbstractStorageDecl::getOverrideLoc() const {
43354335

43364336
Type AbstractStorageDecl::getValueInterfaceType() const {
43374337
if (auto var = dyn_cast<VarDecl>(this))
4338-
return var->getInterfaceType();
4338+
return var->getInterfaceType()->getReferenceStorageReferent();
43394339
return cast<SubscriptDecl>(this)->getElementInterfaceType();
43404340
}
43414341

@@ -5296,11 +5296,12 @@ void AbstractFunctionDecl::computeType(AnyFunctionType::ExtInfo info) {
52965296
} else if (auto ctor = dyn_cast<ConstructorDecl>(this)) {
52975297
auto *dc = ctor->getDeclContext();
52985298

5299-
if (hasSelf)
5300-
resultTy = dc->getSelfInterfaceType();
5301-
5302-
if (!resultTy)
5303-
resultTy = ErrorType::get(ctx);
5299+
if (hasSelf) {
5300+
if (!dc->isTypeContext())
5301+
resultTy = ErrorType::get(ctx);
5302+
else
5303+
resultTy = dc->getSelfInterfaceType();
5304+
}
53045305

53055306
// Adjust result type for failability.
53065307
if (ctor->getFailability() != OTK_None)
@@ -5514,7 +5515,7 @@ Type FuncDecl::getResultInterfaceType() const {
55145515
return nullptr;
55155516

55165517
Type resultTy = getInterfaceType();
5517-
if (resultTy->hasError())
5518+
if (resultTy->is<ErrorType>())
55185519
return resultTy;
55195520

55205521
if (hasImplicitSelfDecl())

lib/AST/NameLookup.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,18 +1511,6 @@ void ClassDecl::recordObjCMethod(AbstractFunctionDecl *method) {
15111511
vec.push_back(method);
15121512
}
15131513

1514-
Type AbstractStorageDecl::getStorageInterfaceType() const {
1515-
if (auto var = dyn_cast<VarDecl>(this)) {
1516-
return var->getInterfaceType();
1517-
}
1518-
1519-
if (auto sub = dyn_cast<SubscriptDecl>(this)) {
1520-
return sub->getElementInterfaceType();
1521-
}
1522-
1523-
llvm_unreachable("unhandled storage decl kind");
1524-
}
1525-
15261514
/// Configure name lookup for the given declaration context and options.
15271515
///
15281516
/// This utility is used by qualified name lookup.

lib/ClangImporter/ImporterImpl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
626626
assert(CD->getInterfaceType());
627627
ty = CD->getResultInterfaceType();
628628
} else {
629-
ty = cast<AbstractStorageDecl>(decl)->getValueInterfaceType()
630-
->getReferenceStorageReferent();
629+
ty = cast<AbstractStorageDecl>(decl)->getValueInterfaceType();
631630
}
632631
#endif
633632

lib/SIL/SILVerifier.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4783,8 +4783,7 @@ void SILProperty::verify(const SILModule &M) const {
47834783
auto sig = dc->getGenericSignatureOfContext();
47844784
auto baseTy = dc->getInnermostTypeContext()->getSelfInterfaceType()
47854785
->getCanonicalType(sig);
4786-
auto leafTy = decl->getStorageInterfaceType()->getReferenceStorageReferent()
4787-
->getCanonicalType(sig);
4786+
auto leafTy = decl->getValueInterfaceType()->getCanonicalType(sig);
47884787
SubstitutionMap subs;
47894788
if (sig) {
47904789
auto env = dc->getGenericEnvironmentOfContext();

lib/SILGen/SILGen.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,14 +1204,13 @@ TypeConverter::canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl) {
12041204
M.getSwiftModule());
12051205
switch (strategy.getKind()) {
12061206
case AccessStrategy::Storage: {
1207-
// If the stored value would need to be reabstracted in fully opaque
1208-
// context, then we have to treat the component as computed.
1209-
auto componentObjTy = decl->getStorageInterfaceType()
1210-
->getWithoutSpecifierType();
12111207
// Keypaths rely on accessors to handle the special behavior of weak or
12121208
// unowned properties.
1213-
if (componentObjTy->is<ReferenceStorageType>())
1209+
if (decl->getInterfaceType()->is<ReferenceStorageType>())
12141210
return false;
1211+
// If the stored value would need to be reabstracted in fully opaque
1212+
// context, then we have to treat the component as computed.
1213+
auto componentObjTy = decl->getValueInterfaceType();
12151214
if (auto genericEnv =
12161215
decl->getInnermostDeclContext()->getGenericEnvironmentOfContext())
12171216
componentObjTy = genericEnv->mapTypeIntoContext(componentObjTy);

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3687,7 +3687,7 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
36873687
if (auto var = dyn_cast<VarDecl>(storage)) {
36883688
CanType componentTy;
36893689
if (!var->getDeclContext()->isTypeContext()) {
3690-
componentTy = storage->getStorageInterfaceType()->getCanonicalType();
3690+
componentTy = var->getInterfaceType()->getCanonicalType();
36913691
} else {
36923692
componentTy =
36933693
GenericEnvironment::mapTypeIntoContext(genericEnv, baseTy)

lib/Sema/CodeSynthesis.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,8 +2169,7 @@ ConstructorDecl *swift::createImplicitConstructor(TypeChecker &tc,
21692169

21702170
auto varType = var->getType()
21712171
->getReferenceStorageReferent();
2172-
auto varInterfaceType = var->getInterfaceType()
2173-
->getReferenceStorageReferent();
2172+
auto varInterfaceType = var->getValueInterfaceType();
21742173

21752174
// If var is a lazy property, its value is provided for the underlying
21762175
// storage. We thus take an optional of the properties type. We only

lib/Sema/TypeCheckDecl.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2620,6 +2620,12 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
26202620

26212621
void visitSubscriptDecl(SubscriptDecl *SD) {
26222622
TC.validateDecl(SD);
2623+
2624+
if (!SD->isInvalid()) {
2625+
TC.checkReferencedGenericParams(SD);
2626+
TC.checkProtocolSelfRequirements(SD);
2627+
}
2628+
26232629
TC.checkDeclAttributes(SD);
26242630

26252631
AccessControlChecker::checkAccessControl(TC, SD);
@@ -3121,6 +3127,15 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
31213127
void visitFuncDecl(FuncDecl *FD) {
31223128
TC.validateDecl(FD);
31233129

3130+
// We get bogus errors here with generic subscript materializeForSet.
3131+
if (!FD->isInvalid()) {
3132+
if (!isa<AccessorDecl>(FD) ||
3133+
!cast<AccessorDecl>(FD)->isMaterializeForSet()) {
3134+
TC.checkReferencedGenericParams(FD);
3135+
TC.checkProtocolSelfRequirements(FD);
3136+
}
3137+
}
3138+
31243139
AccessControlChecker::checkAccessControl(TC, FD);
31253140
UsableFromInlineChecker::checkUsableFromInline(TC, FD);
31263141

@@ -3265,6 +3280,11 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
32653280
void visitConstructorDecl(ConstructorDecl *CD) {
32663281
TC.validateDecl(CD);
32673282

3283+
if (!CD->isInvalid()) {
3284+
TC.checkReferencedGenericParams(CD);
3285+
TC.checkProtocolSelfRequirements(CD);
3286+
}
3287+
32683288
// Check whether this initializer overrides an initializer in its
32693289
// superclass.
32703290
if (!checkOverrides(CD)) {
@@ -3609,13 +3629,13 @@ void checkMemberOperator(TypeChecker &TC, FuncDecl *FD) {
36093629
isProtocol, FD->getFullName());
36103630
}
36113631

3612-
bool checkDynamicSelfReturn(TypeChecker &TC, FuncDecl *func,
3632+
bool checkDynamicSelfReturn(FuncDecl *func,
36133633
TypeRepr *typeRepr,
36143634
unsigned optionalDepth) {
36153635
// Look through parentheses.
36163636
if (auto parenRepr = dyn_cast<TupleTypeRepr>(typeRepr)) {
36173637
if (!parenRepr->isParenType()) return false;
3618-
return checkDynamicSelfReturn(TC, func, parenRepr->getElementType(0),
3638+
return checkDynamicSelfReturn(func, parenRepr->getElementType(0),
36193639
optionalDepth);
36203640
}
36213641

@@ -3624,8 +3644,9 @@ bool checkDynamicSelfReturn(TypeChecker &TC, FuncDecl *func,
36243644
TypeAttributes attrs = attrRepr->getAttrs();
36253645
if (!attrs.empty())
36263646
return false;
3627-
return checkDynamicSelfReturn(TC, func, attrRepr->getTypeRepr(),
3647+
return checkDynamicSelfReturn(func, attrRepr->getTypeRepr(),
36283648
optionalDepth);
3649+
36293650
}
36303651

36313652
// Look through optional types.
@@ -3639,7 +3660,7 @@ bool checkDynamicSelfReturn(TypeChecker &TC, FuncDecl *func,
36393660
if (base) {
36403661
// But only one level.
36413662
if (optionalDepth != 0) return false;
3642-
return checkDynamicSelfReturn(TC, func, base, optionalDepth + 1);
3663+
return checkDynamicSelfReturn(func, base, optionalDepth + 1);
36433664
}
36443665

36453666
// Check whether we have a simple identifier type.
@@ -3648,17 +3669,16 @@ bool checkDynamicSelfReturn(TypeChecker &TC, FuncDecl *func,
36483669
return false;
36493670

36503671
// Check whether it is 'Self'.
3651-
if (simpleRepr->getIdentifier() != TC.Context.Id_Self)
3672+
if (simpleRepr->getIdentifier() != func->getASTContext().Id_Self)
36523673
return false;
36533674

36543675
// Note that the function has a dynamic Self return type and set
36553676
// the return type component to the dynamic self type.
3656-
func->setDynamicSelf(true);
3657-
return false;
3677+
return true;
36583678
}
36593679

36603680
/// Check for methods that return 'DynamicResult'.
3661-
bool checkDynamicSelfReturn(TypeChecker &TC, FuncDecl *func) {
3681+
bool checkDynamicSelfReturn(FuncDecl *func) {
36623682
// Check whether we have a specified result type.
36633683
auto typeRepr = func->getBodyResultTypeLoc().getTypeRepr();
36643684
if (!typeRepr)
@@ -3674,7 +3694,7 @@ bool checkDynamicSelfReturn(TypeChecker &TC, FuncDecl *func) {
36743694
if (isa<AccessorDecl>(func))
36753695
return false;
36763696

3677-
return checkDynamicSelfReturn(TC, func, typeRepr, 0);
3697+
return checkDynamicSelfReturn(func, typeRepr, 0);
36783698
}
36793699

36803700
Type buildAddressorResultType(TypeChecker &TC,
@@ -4097,8 +4117,7 @@ void TypeChecker::validateDecl(ValueDecl *D) {
40974117
validateSelfAccessKind(*this, FD);
40984118

40994119
// Check whether the return type is dynamic 'Self'.
4100-
if (checkDynamicSelfReturn(*this, FD))
4101-
FD->setInvalid();
4120+
FD->setDynamicSelf(checkDynamicSelfReturn(FD));
41024121

41034122
// Accessors should pick up various parts of their type signatures
41044123
// directly from the storage declaration instead of re-deriving them.

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -602,13 +602,12 @@ static bool isSelfDerivedOrConcrete(Type protoSelf, Type type) {
602602

603603
// For a generic requirement in a protocol, make sure that the requirement
604604
// set didn't add any requirements to Self or its associated types.
605-
static bool checkProtocolSelfRequirements(GenericSignature *sig,
606-
ValueDecl *decl,
607-
TypeChecker &TC) {
605+
void TypeChecker::checkProtocolSelfRequirements(ValueDecl *decl) {
608606
// For a generic requirement in a protocol, make sure that the requirement
609607
// set didn't add any requirements to Self or its associated types.
610608
if (auto *proto = dyn_cast<ProtocolDecl>(decl->getDeclContext())) {
611609
auto protoSelf = proto->getSelfInterfaceType();
610+
auto *sig = decl->getInnermostDeclContext()->getGenericSignatureOfContext();
612611
for (auto req : sig->getRequirements()) {
613612
// If one of the types in the requirement is dependent on a non-Self
614613
// type parameter, this requirement is okay.
@@ -622,27 +621,21 @@ static bool checkProtocolSelfRequirements(GenericSignature *sig,
622621
req.getFirstType()->is<GenericTypeParamType>())
623622
continue;
624623

625-
TC.diagnose(decl,
626-
TC.Context.LangOpts.EffectiveLanguageVersion[0] >= 4
627-
? diag::requirement_restricts_self
628-
: diag::requirement_restricts_self_swift3,
629-
decl->getDescriptiveKind(), decl->getFullName(),
630-
req.getFirstType().getString(),
631-
static_cast<unsigned>(req.getKind()),
632-
req.getSecondType().getString());
633-
634-
if (TC.Context.LangOpts.EffectiveLanguageVersion[0] >= 4)
635-
return true;
624+
diagnose(decl,
625+
Context.isSwiftVersion3()
626+
? diag::requirement_restricts_self_swift3
627+
: diag::requirement_restricts_self,
628+
decl->getDescriptiveKind(), decl->getFullName(),
629+
req.getFirstType().getString(),
630+
static_cast<unsigned>(req.getKind()),
631+
req.getSecondType().getString());
636632
}
637633
}
638-
639-
return false;
640634
}
641635

642636
/// All generic parameters of a generic function must be referenced in the
643637
/// declaration's type, otherwise we have no way to infer them.
644-
static void checkReferencedGenericParams(GenericContext *dc,
645-
TypeChecker &TC) {
638+
void TypeChecker::checkReferencedGenericParams(GenericContext *dc) {
646639
auto *genericParams = dc->getGenericParams();
647640
auto *genericSig = dc->getGenericSignatureOfContext();
648641
if (!genericParams)
@@ -798,9 +791,9 @@ static void checkReferencedGenericParams(GenericContext *dc,
798791
continue;
799792
}
800793
// Produce an error that this generic parameter cannot be bound.
801-
TC.diagnose(paramDecl->getLoc(), diag::unreferenced_generic_parameter,
802-
paramDecl->getNameStr());
803-
decl->setInterfaceType(ErrorType::get(TC.Context));
794+
diagnose(paramDecl->getLoc(), diag::unreferenced_generic_parameter,
795+
paramDecl->getNameStr());
796+
decl->setInterfaceType(ErrorType::get(Context));
804797
decl->setInvalid();
805798
}
806799
}
@@ -868,9 +861,6 @@ void TypeChecker::validateGenericFuncSignature(AbstractFunctionDecl *func) {
868861
if (checkGenericFuncSignature(*this, nullptr, func, completeResolver))
869862
invalid = true;
870863

871-
if (!invalid)
872-
invalid = checkProtocolSelfRequirements(sig, func, *this);
873-
874864
if (invalid) {
875865
func->setInterfaceType(ErrorType::get(Context));
876866
func->setInvalid();
@@ -879,15 +869,9 @@ void TypeChecker::validateGenericFuncSignature(AbstractFunctionDecl *func) {
879869

880870
func->computeType();
881871

882-
// We get bogus errors here with generic subscript materializeForSet.
883-
if (!isa<AccessorDecl>(func) ||
884-
!cast<AccessorDecl>(func)->isMaterializeForSet())
885-
checkReferencedGenericParams(func, *this);
886-
887-
// Make sure that there are no unresolved
888-
// dependent types in the generic signature.
889-
assert(func->getInterfaceType()->hasError() ||
890-
!func->getInterfaceType()->findUnresolvedDependentMemberType());
872+
// Make sure that there are no unresolved dependent types in the
873+
// generic signature.
874+
assert(!func->getInterfaceType()->findUnresolvedDependentMemberType());
891875
}
892876

893877
///
@@ -1010,10 +994,6 @@ TypeChecker::validateGenericSubscriptSignature(SubscriptDecl *subscript) {
1010994
CompleteGenericTypeResolver completeResolver(*this, sig);
1011995
if (checkGenericSubscriptSignature(*this, nullptr, subscript, completeResolver))
1012996
invalid = true;
1013-
1014-
if (!invalid)
1015-
invalid = checkProtocolSelfRequirements(sig, subscript, *this);
1016-
1017997
if (invalid) {
1018998
subscript->setInterfaceType(ErrorType::get(Context));
1019999
subscript->setInvalid();
@@ -1022,8 +1002,6 @@ TypeChecker::validateGenericSubscriptSignature(SubscriptDecl *subscript) {
10221002
}
10231003

10241004
subscript->computeType();
1025-
1026-
checkReferencedGenericParams(subscript, *this);
10271005
}
10281006

10291007
///

lib/Sema/TypeChecker.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,14 @@ class TypeChecker final : public LazyResolver {
12941294
/// of a generic subscript.
12951295
void validateGenericSubscriptSignature(SubscriptDecl *subscript);
12961296

1297+
/// For a generic requirement in a protocol, make sure that the requirement
1298+
/// set didn't add any requirements to Self or its associated types.
1299+
void checkProtocolSelfRequirements(ValueDecl *decl);
1300+
1301+
/// All generic parameters of a generic function must be referenced in the
1302+
/// declaration's type, otherwise we have no way to infer them.
1303+
void checkReferencedGenericParams(GenericContext *dc);
1304+
12971305
/// Construct a new generic environment for the given declaration context.
12981306
///
12991307
/// \param genericParams The generic parameters to validate.

test/Parse/init_deinit.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ struct FooStructConstructorB {
1111
struct FooStructConstructorC {
1212
init {} // expected-error {{expected '('}}{{7-7=()}}
1313
init<T> {} // expected-error {{expected '('}} {{10-10=()}}
14-
// expected-error@-1{{generic parameter 'T' is not used in function signature}}
1514
init? { self.init() } // expected-error {{expected '('}} {{8-8=()}}
1615
}
1716

0 commit comments

Comments
 (0)