Skip to content

Commit 35bce55

Browse files
committed
When diagnosing some problems with associated types, mark the decl invalid. When forming a type reference to an invalid type decl, have validateType return ErrorType instead of an apparently valid type. This silences some bogus downstream errors in code that references the decl.
This exposes some wierdness with while_parsing_as_left_angle_bracket where one case the note is being is when resolveType returns a failure. However, resolveType can produce a failure without emitting a diagnostic, and this can lead to us generating a note unattached to an error. Just remove this case.
1 parent f1aac08 commit 35bce55

File tree

5 files changed

+16
-10
lines changed

5 files changed

+16
-10
lines changed

lib/AST/ArchetypeBuilder.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,15 +1065,19 @@ bool ArchetypeBuilder::addAbstractTypeParamRequirements(
10651065
Diags.diagnose(assocType->getLoc(),
10661066
diag::recursive_requirement_reference);
10671067

1068-
// Mark all associatedtypes in this protocol as recursive (and error-type) to avoid later
1069-
// crashes dealing with this invalid protocol in other contexts.
1068+
// Mark all associatedtypes in this protocol as recursive (and error-type)
1069+
// to avoid later crashes dealing with this invalid protocol in other
1070+
// contexts.
10701071
auto containingProto = assocType->getDeclContext()->isProtocolOrProtocolExtensionContext();
10711072
for (auto member : containingProto->getMembers())
10721073
if (auto assocType = dyn_cast<AssociatedTypeDecl>(member))
10731074
assocType->setIsRecursive();
10741075
}
10751076
pa->setIsRecursive();
10761077

1078+
// Silence downstream errors referencing this associated type.
1079+
assocType->setInvalid();
1080+
10771081
// FIXME: Drop this protocol.
10781082
pa->addConformance(proto, RequirementSource(kind, loc), *this);
10791083
};

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,8 @@ namespace {
683683
// Type check the type parameters in an UnresolvedSpecializeExpr.
684684
if (auto us = dyn_cast<UnresolvedSpecializeExpr>(expr)) {
685685
for (TypeLoc &type : us->getUnresolvedParams()) {
686-
if (TC.validateType(type, DC)) {
687-
TC.diagnose(us->getLAngleLoc(),
688-
diag::while_parsing_as_left_angle_bracket);
686+
if (TC.validateType(type, DC))
689687
return nullptr;
690-
}
691688
}
692689

693690
// If this is a reference type a specialized type, form a TypeExpr.

lib/Sema/TypeCheckType.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,11 @@ static Type resolveNestedIdentTypeComponent(
10691069
TC, memberType, comp->getIdLoc(), DC, genComp,
10701070
options.contains(TR_GenericSignature), resolver);
10711071

1072+
// If we found a reference to an associated type or other member type that
1073+
// was marked invalid, just return ErrorType to silence downstream errors.
1074+
if (member && member->isInvalid())
1075+
memberType = ErrorType::get(TC.Context);
1076+
10721077
if (member)
10731078
comp->setValue(member);
10741079
return memberType;

test/Parse/generic_disambiguation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ var a, b, c, d : Int
2626
a < b
2727
(a < b, c > d)
2828
// Parses as generic because of lparen after '>'
29-
(a < b, c > (d)) // expected-error{{use of undeclared type 'b'}} expected-note{{while parsing this '<' as a type parameter bracket}}
29+
(a < b, c > (d)) // expected-error{{use of undeclared type 'b'}}
3030
// Parses as generic because of lparen after '>'
31-
(a<b, c>(d)) // expected-error{{use of undeclared type 'b'}} expected-note{{while parsing this '<' as a type parameter bracket}}
31+
(a<b, c>(d)) // expected-error{{use of undeclared type 'b'}}
3232
a>(b)
3333
a > (b)
3434

test/decl/protocol/recursive_requirement.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct X<T: P> {
2020
}
2121

2222
func f<T : P>(z: T) {
23-
_ = X<T.A>() // expected-error{{expression type 'X<T.A>' is ambiguous without more context}}
23+
_ = X<T.A>()
2424
}
2525

2626
// -----
@@ -60,7 +60,7 @@ struct Y3 : DeclaredP {
6060
struct X3<T:P4> {}
6161

6262
func f2<T:P4>(a: T) {
63-
_ = X3<T.A>() // expected-error{{expression type 'X3<T.A>' is ambiguous without more context}}
63+
_ = X3<T.A>()
6464
}
6565

6666
f2(Y3())

0 commit comments

Comments
 (0)