Skip to content

Commit 5f24005

Browse files
authored
Merge pull request swiftlang#19830 from gregomni/8813
[Sema] When resolving type declarations, if there is an error with a typealias,...
2 parents 9d131fd + c7ae3e6 commit 5f24005

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,11 @@ ERROR(use_nonmatching_operator,none,
689689
"%0 is not a %select{binary|prefix unary|postfix unary}1 operator",
690690
(DeclName, unsigned))
691691
ERROR(unsupported_recursion_in_associated_type_reference,none,
692-
"unsupported recursion for reference to associated type %0 of type %1",
693-
(DeclName, Type))
692+
"unsupported recursion for reference to %select{associated type|type alias}0 %1 of type %2",
693+
(bool, DeclName, Type))
694694
ERROR(broken_associated_type_witness,none,
695-
"reference to invalid associated type %0 of type %1", (DeclName, Type))
695+
"reference to invalid %select{associated type|type alias}0 %1 of type %2",
696+
(bool, DeclName, Type))
696697

697698
ERROR(unspaced_binary_operator_fixit,none,
698699
"missing whitespace between %0 and %1 operators",

lib/Sema/TypeCheckType.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,15 @@ static void diagnoseUnboundGenericType(Type ty, SourceLoc loc) {
841841
static void maybeDiagnoseBadConformanceRef(DeclContext *dc,
842842
Type parentTy,
843843
SourceLoc loc,
844-
AssociatedTypeDecl *assocType) {
844+
TypeDecl *typeDecl) {
845+
auto protocol = dyn_cast<ProtocolDecl>(typeDecl->getDeclContext());
846+
if (!protocol)
847+
return;
848+
845849
// If we weren't given a conformance, go look it up.
846850
ProtocolConformance *conformance = nullptr;
847851
if (auto conformanceRef = TypeChecker::conformsToProtocol(
848-
parentTy, assocType->getProtocol(), dc,
852+
parentTy, protocol, dc,
849853
(ConformanceCheckFlags::InExpression |
850854
ConformanceCheckFlags::SuppressDependencyTracking |
851855
ConformanceCheckFlags::AllowUnavailableConditionalRequirements))) {
@@ -864,7 +868,7 @@ static void maybeDiagnoseBadConformanceRef(DeclContext *dc,
864868
? diag::unsupported_recursion_in_associated_type_reference
865869
: diag::broken_associated_type_witness;
866870

867-
ctx.Diags.diagnose(loc, diagCode, assocType->getFullName(), parentTy);
871+
ctx.Diags.diagnose(loc, diagCode, isa<TypeAliasDecl>(typeDecl), typeDecl->getFullName(), parentTy);
868872
}
869873

870874
/// \brief Returns a valid type or ErrorType in case of an error.
@@ -910,10 +914,11 @@ static Type resolveTypeDecl(TypeDecl *typeDecl, SourceLoc loc,
910914
return ErrorType::get(ctx);
911915
}
912916

913-
if (type->hasError() && isa<AssociatedTypeDecl>(typeDecl)) {
917+
if (type->hasError() && foundDC &&
918+
(isa<AssociatedTypeDecl>(typeDecl) || isa<TypeAliasDecl>(typeDecl))) {
914919
maybeDiagnoseBadConformanceRef(fromDC,
915920
foundDC->getDeclaredInterfaceType(),
916-
loc, cast<AssociatedTypeDecl>(typeDecl));
921+
loc, typeDecl);
917922
}
918923

919924
if (generic) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// SR-8813
4+
// By itself in this file because this particular expected error is omitted if there have been any other diagnostics.
5+
protocol BaseProtocol {
6+
associatedtype Value
7+
typealias Closure = () -> Value
8+
9+
init(closure: Closure)
10+
}
11+
12+
struct Base<Value>: BaseProtocol {
13+
private let closure: Closure
14+
15+
init(closure: Closure) { //expected-error {{reference to invalid type alias 'Closure' of type 'Base<Value>'}}
16+
self.closure = closure
17+
}
18+
}

validation-test/compiler_crashers_2/0127-sr5546.swift renamed to validation-test/compiler_crashers_2_fixed/0127-sr5546.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: not --crash %target-swift-frontend %s -typecheck
1+
// RUN: not %target-swift-frontend %s -typecheck
22
// REQUIRES: asserts
33

44
public struct Foo<A, B, C> {}

0 commit comments

Comments
 (0)