Skip to content

Commit 88baa17

Browse files
authored
Merge pull request #10151 from slavapestov/fix-easy-crashers
Fix easy crashers
2 parents 46ed599 + 0099713 commit 88baa17

16 files changed

+62
-24
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,8 +642,10 @@ namespace {
642642
void printAbstractTypeParamCommon(AbstractTypeParamDecl *decl,
643643
const char *name) {
644644
printCommon(decl, name);
645-
if (auto superclassTy = decl->getSuperclass()) {
646-
OS << " superclass='" << superclassTy->getString() << "'";
645+
if (decl->getDeclContext()->getGenericEnvironmentOfContext()) {
646+
if (auto superclassTy = decl->getSuperclass()) {
647+
OS << " superclass='" << superclassTy->getString() << "'";
648+
}
647649
}
648650
}
649651

lib/AST/DeclContext.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,12 @@ DeclContext *DeclContext::getParentForLookup() const {
445445
// outer types.
446446
return getModuleScopeContext();
447447
}
448+
if (isa<NominalTypeDecl>(this)) {
449+
// If we are inside a nominal type that is inside a protocol,
450+
// skip the protocol.
451+
if (isa<ProtocolDecl>(getParent()))
452+
return getModuleScopeContext();
453+
}
448454
return getParent();
449455
}
450456

lib/AST/Type.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3906,24 +3906,25 @@ case TypeKind::Id:
39063906

39073907
case TypeKind::ProtocolComposition: {
39083908
auto pc = cast<ProtocolCompositionType>(base);
3909-
SmallVector<Type, 4> members;
3909+
SmallVector<Type, 4> substMembers;
3910+
auto members = pc->getMembers();
39103911
bool anyChanged = false;
39113912
unsigned index = 0;
3912-
for (auto member : pc->getMembers()) {
3913+
for (auto member : members) {
39133914
auto substMember = member.transformRec(fn);
39143915
if (!substMember)
39153916
return Type();
39163917

39173918
if (anyChanged) {
3918-
members.push_back(substMember);
3919+
substMembers.push_back(substMember);
39193920
++index;
39203921
continue;
39213922
}
39223923

39233924
if (substMember.getPointer() != member.getPointer()) {
39243925
anyChanged = true;
3925-
members.append(members.begin(), members.begin() + index);
3926-
members.push_back(substMember);
3926+
substMembers.append(members.begin(), members.begin() + index);
3927+
substMembers.push_back(substMember);
39273928
}
39283929

39293930
++index;
@@ -3932,7 +3933,8 @@ case TypeKind::Id:
39323933
if (!anyChanged)
39333934
return *this;
39343935

3935-
return ProtocolCompositionType::get(Ptr->getASTContext(), members,
3936+
return ProtocolCompositionType::get(Ptr->getASTContext(),
3937+
substMembers,
39363938
pc->hasExplicitAnyObject());
39373939
}
39383940
}

lib/Sema/ITCDecl.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,6 @@ void IterativeTypeChecker::processResolveTypeDecl(
342342
if (auto typeAliasDecl = dyn_cast<TypeAliasDecl>(typeDecl)) {
343343
if (typeAliasDecl->getDeclContext()->isModuleScopeContext() &&
344344
typeAliasDecl->getGenericParams() == nullptr) {
345-
typeAliasDecl->setValidationStarted();
346-
347345
TypeResolutionOptions options = TR_TypeAliasUnderlyingType;
348346
if (typeAliasDecl->getFormalAccess() <= Accessibility::FilePrivate)
349347
options |= TR_KnownNonCascadingDependency;

lib/Sema/TypeCheckNameLookup.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,14 @@ LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
414414
continue;
415415
}
416416
}
417+
418+
// Nominal type members of protocols cannot be accessed with an
419+
// archetype base, because we have no way to recover the correct
420+
// substitutions.
421+
if (type->is<ArchetypeType>() &&
422+
isa<NominalTypeDecl>(typeDecl)) {
423+
continue;
424+
}
417425
}
418426

419427
// Substitute the base into the member's type.
@@ -451,6 +459,13 @@ LookupTypeResult TypeChecker::lookupMemberType(DeclContext *dc,
451459
// Use the type witness.
452460
auto concrete = conformance->getConcrete();
453461
Type memberType = concrete->getTypeWitness(assocType, this);
462+
463+
// This is the only case where NormalProtocolConformance::
464+
// getTypeWitnessAndDecl() returns a null type.
465+
if (concrete->getState() ==
466+
ProtocolConformanceState::CheckingTypeWitnesses)
467+
continue;
468+
454469
assert(memberType && "Missing type witness?");
455470

456471
// If we haven't seen this type result yet, add it to the result set.

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,15 +3286,21 @@ ResolveWitnessResult ConformanceChecker::resolveWitnessViaDefault(
32863286
///
32873287
/// \returns an empty result on success, or a description of the error.
32883288
static CheckTypeWitnessResult checkTypeWitness(TypeChecker &tc, DeclContext *dc,
3289+
ProtocolDecl *proto,
32893290
AssociatedTypeDecl *assocType,
32903291
Type type) {
3291-
if (auto superclass = assocType->getSuperclass()) {
3292+
auto *moduleDecl = dc->getParentModule();
3293+
auto *reqtSig = assocType->getProtocol()->getRequirementSignature();
3294+
auto *depTy = DependentMemberType::get(proto->getSelfInterfaceType(),
3295+
assocType);
3296+
3297+
if (auto superclass = reqtSig->getSuperclassBound(depTy, *moduleDecl)) {
32923298
if (!superclass->isExactSuperclassOf(type))
32933299
return superclass->getAnyNominal();
32943300
}
32953301

32963302
// Check protocol conformances.
3297-
for (auto reqProto : assocType->getConformingProtocols()) {
3303+
for (auto reqProto : reqtSig->getConformsTo(depTy, *moduleDecl)) {
32983304
if (!tc.conformsToProtocol(type, reqProto, dc, None))
32993305
return reqProto;
33003306

@@ -3323,6 +3329,11 @@ static CheckTypeWitnessResult checkTypeWitness(TypeChecker &tc, DeclContext *dc,
33233329
/// Attempt to resolve a type witness via member name lookup.
33243330
ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
33253331
AssociatedTypeDecl *assocType) {
3332+
if (!Proto->isRequirementSignatureComputed()) {
3333+
Conformance->setInvalid();
3334+
return ResolveWitnessResult::Missing;
3335+
}
3336+
33263337
// Look for a member type with the same name as the associated type.
33273338
auto candidates = TC.lookupMemberType(DC, Adoptee, assocType->getName(),
33283339
NameLookupFlags::ProtocolMembers);
@@ -3342,7 +3353,7 @@ ResolveWitnessResult ConformanceChecker::resolveTypeWitnessViaLookup(
33423353
continue;
33433354

33443355
// Check this type against the protocol requirements.
3345-
if (auto checkResult = checkTypeWitness(TC, DC, assocType,
3356+
if (auto checkResult = checkTypeWitness(TC, DC, Proto, assocType,
33463357
candidate.second)) {
33473358
auto reqProto = checkResult.getProtocolOrClass();
33483359
nonViable.push_back({candidate.first, reqProto});
@@ -3603,7 +3614,7 @@ ConformanceChecker::inferTypeWitnessesViaValueWitnesses(
36033614
if (!canInferFromOtherAssociatedType) {
36043615
// Check that the type witness meets the
36053616
// requirements on the associated type.
3606-
if (auto failed = checkTypeWitness(TC, DC, result.first,
3617+
if (auto failed = checkTypeWitness(TC, DC, Proto, result.first,
36073618
result.second)) {
36083619
witnessResult.NonViable.push_back(
36093620
std::make_tuple(result.first,result.second,failed));
@@ -4196,7 +4207,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
41964207
if (!defaultType)
41974208
return Type();
41984209

4199-
if (auto failed = checkTypeWitness(TC, DC, assocType, defaultType)) {
4210+
if (auto failed = checkTypeWitness(TC, DC, Proto, assocType, defaultType)) {
42004211
// Record the failure, if we haven't seen one already.
42014212
if (!failedDefaultedAssocType) {
42024213
failedDefaultedAssocType = assocType;
@@ -4231,7 +4242,7 @@ void ConformanceChecker::resolveTypeWitnesses() {
42314242
return Type();
42324243

42334244
// Make sure that the derived type is sane.
4234-
if (checkTypeWitness(TC, DC, assocType, derivedType)) {
4245+
if (checkTypeWitness(TC, DC, Proto, assocType, derivedType)) {
42354246
diagnoseOrDefer(assocType, true,
42364247
[derivedType](NormalProtocolConformance *conformance) {
42374248
// FIXME: give more detail here?

test/decl/nested/protocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ protocol Racoon {
4040
associatedtype Stripes
4141
class Claw<T> { // expected-error{{type 'Claw' cannot be nested in protocol 'Racoon'}}
4242
func mangle(_ s: Stripes) {}
43+
// expected-error@-1 {{use of undeclared type 'Stripes'}}
4344
}
4445
struct Fang<T> { // expected-error{{type 'Fang' cannot be nested in protocol 'Racoon'}}
4546
func gnaw(_ s: Stripes) {}
47+
// expected-error@-1 {{use of undeclared type 'Stripes'}}
4648
}
4749
enum Fur { // expected-error{{type 'Fur' cannot be nested in protocol 'Racoon'}}
4850
case Stripes

test/decl/typealias/protocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ struct S7 : P7 {
202202

203203
func inExpressionContext() {
204204
_ = Y.self
205+
_ = T5.T1.self
206+
_ = T5.T2.self
205207
}
206208
}
207209

validation-test/compiler_crashers/28728-d-isbeingvalidated-d-hasvalidsignature.swift renamed to validation-test/compiler_crashers_fixed/28728-d-isbeingvalidated-d-hasvalidsignature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
typealias e:a
1111
struct A:a{}typealias a=A
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol P{class a:Self.a

validation-test/compiler_crashers/28748-genericenv-nullptr-too-much-circularity.swift renamed to validation-test/compiler_crashers_fixed/28748-genericenv-nullptr-too-much-circularity.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol A:a{{}typealias e:a}{}class a:A{typealias e:b

validation-test/compiler_crashers/28751-membertype-missing-type-witness.swift renamed to validation-test/compiler_crashers_fixed/28751-membertype-missing-type-witness.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
{class a:P{{}typealias a:Self.a}protocol P{typealias a

validation-test/compiler_crashers/28753-conforms-equivclass-conformsto-end.swift renamed to validation-test/compiler_crashers_fixed/28753-conforms-equivclass-conformsto-end.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol A:A{{}class a{let ca{a{

validation-test/compiler_crashers/28765-inprotocol-isrequirementsignaturecomputed-missing-signature.swift renamed to validation-test/compiler_crashers_fixed/28765-inprotocol-isrequirementsignaturecomputed-missing-signature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
protocol P{{}class a:A{}protocol A:P.a{typealias a{}func a:a
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

88
// REQUIRES: asserts
9-
// RUN: not --crash %target-swift-frontend %s -emit-ir
9+
// RUN: not %target-swift-frontend %s -emit-ir
1010
Indexable
1111
& ManagedBuffer

validation-test/compiler_crashers/28773-unreachable-executed-at-swift-lib-sema-cssimplify-cpp-4808.swift renamed to validation-test/compiler_crashers_fixed/28773-unreachable-executed-at-swift-lib-sema-cssimplify-cpp-4808.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
// See https://swift.org/LICENSE.txt for license information
66
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
77

8-
// RUN: not --crash %target-swift-frontend %s -emit-ir
8+
// RUN: not %target-swift-frontend %s -emit-ir
99
@objc protocol P{{}typealias a{}class a{let c=a{

0 commit comments

Comments
 (0)