Skip to content

Commit d43e5e3

Browse files
authored
[Fixit] Add a fixit to remove type alias self-referring declaration. (#3418)
* [Fixit] Add a fixit to remote type alias self-referring definition like typealias A = A. rdar://24869708 * [test] Update existing test.
1 parent 56f0470 commit d43e5e3

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,6 +3013,10 @@ WARNING(extraneous_default_args_in_call, none,
30133013
//------------------------------------------------------------------------------
30143014
ERROR(circular_reference, none,
30153015
"circular reference", ())
3016+
3017+
ERROR(redundant_type_alias_define, none,
3018+
"redundant type alias declaration", ())
3019+
30163020
NOTE(circular_reference_through, none,
30173021
"through reference here", ())
30183022

lib/Sema/IterativeTypeChecker.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,35 @@ void IterativeTypeChecker::satisfy(TypeCheckRequest request) {
112112
}
113113
}
114114

115+
static bool isSelfRedefinedTypeAliasDecl(const TypeCheckRequest &Request) {
116+
if (Request.getKind() == TypeCheckRequest::Kind::ResolveTypeDecl) {
117+
if (auto TAD = dyn_cast<TypeAliasDecl>(Request.getAnchor())) {
118+
SourceRange SR = TAD->getUnderlyingTypeLoc().getSourceRange();
119+
SourceManager &SM = TAD->getASTContext().SourceMgr;
120+
CharSourceRange CR = CharSourceRange(SM, SR.Start,
121+
Lexer::getLocForEndOfToken(SM,
122+
SR.End));
123+
return CR.str() == TAD->getNameStr();
124+
}
125+
}
126+
return false;
127+
}
128+
115129
//===----------------------------------------------------------------------===//
116130
// Diagnostics
117131
//===----------------------------------------------------------------------===//
118132
void IterativeTypeChecker::diagnoseCircularReference(
119133
ArrayRef<TypeCheckRequest> requests) {
120134
bool isFirst = true;
121135
for (const auto &request : requests) {
122-
diagnose(request.getLoc(),
123-
isFirst ? diag::circular_reference
124-
: diag::circular_reference_through);
136+
if (isSelfRedefinedTypeAliasDecl(request)) {
137+
diagnose(request.getLoc(), diag::redundant_type_alias_define).
138+
fixItRemove(request.getAnchor()->getSourceRange());
139+
} else {
140+
diagnose(request.getLoc(),
141+
isFirst ? diag::circular_reference :
142+
diag::circular_reference_through);
143+
}
125144

126145
isFirst = false;
127146
}

test/Sema/diag_typealias.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-parse-verify-swift
2+
3+
struct S {}
4+
5+
typealias S = S // expected-error {{redundant type alias declaration}}{{1-17=}}
6+

test/decl/init/basic_init.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class C {
1010
init() {}
1111
}
1212

13-
typealias t = t // expected-error {{circular reference}}
13+
typealias t = t // expected-error {{redundant type alias declaration}}{{1-17=}}
1414

1515

1616

0 commit comments

Comments
 (0)