Skip to content

Commit 5b35f45

Browse files
committed
Fix the fixit for specialized non-generic types
If a non-generic type is specialized a fixit is provided to remove the generic arguments. The DiagnosticEngine turns a SourceRange into a CharSourceRange by re-lexing the token at `SourceRange::End`. The problem solved by this change occurs, if the non-generic type is nested in an other generic type like this: let a: GenericType<NongenericType<NongenericType>> The lexer doesn't know that the closing angle brackets are individual tokens leading to the removal of both of them. We can work around this by directly specifying the start and end locations.
1 parent 4153928 commit 5b35f45

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,13 @@ Type TypeChecker::applyGenericArguments(Type type, TypeDecl *decl,
495495

496496
// Don't add fixit on module type; that isn't the right type regardless
497497
// of whether it had generic arguments.
498-
if (!type->is<ModuleType>())
499-
diag.fixItRemove(generic->getAngleBrackets());
498+
if (!type->is<ModuleType>()) {
499+
// When turning a SourceRange into CharSourceRange the closing angle
500+
// brackets on nested generics are lexed as one token.
501+
SourceRange angles = generic->getAngleBrackets();
502+
diag.fixItRemoveChars(angles.Start,
503+
angles.End.getAdvancedLocOrInvalid(1));
504+
}
500505

501506
generic->setInvalid();
502507
return type;

test/Generics/generic_types.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func useRangeOfPrintables(_ roi : RangeOfPrintables<[Int]>) {
167167

168168
var dfail : Dictionary<Int> // expected-error{{generic type 'Dictionary' specialized with too few type parameters (got 1, but expected 2)}}
169169
var notgeneric : Int<Float> // expected-error{{cannot specialize non-generic type 'Int'}}{{21-28=}}
170+
var notgenericNested : Array<Int<Float>> // expected-error{{cannot specialize non-generic type 'Int'}}{{33-40=}}
170171

171172
// Make sure that redundant typealiases (that map to the same
172173
// underlying type) don't break protocol conformance or use.

0 commit comments

Comments
 (0)