Skip to content

Commit da799eb

Browse files
committed
[Diagnostics] Add a note if missing parameter is associated with generic subscript
1 parent 5e35c3e commit da799eb

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,8 @@ NOTE(note_call_to_operator,none,
927927
"in call to operator %0", (DeclName))
928928
NOTE(note_call_to_func,none,
929929
"in call to function %0", (DeclName))
930+
NOTE(note_call_to_subscript,none,
931+
"in call to %0", (DeclName))
930932
NOTE(note_call_to_initializer,none,
931933
"in call to initializer", ())
932934
NOTE(note_init_parameter,none,

lib/Sema/CSDiagnostics.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,6 +2918,11 @@ bool MissingGenericArgumentsFailure::diagnoseForAnchor(
29182918
return false;
29192919

29202920
auto *DC = getDeclContext();
2921+
if (auto *SD = dyn_cast<SubscriptDecl>(DC)) {
2922+
emitDiagnostic(SD, diag::note_call_to_subscript, SD->getFullName());
2923+
return true;
2924+
}
2925+
29212926
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
29222927
if (isa<ConstructorDecl>(AFD)) {
29232928
emitDiagnostic(AFD, diag::note_call_to_initializer);
@@ -2974,11 +2979,8 @@ bool MissingGenericArgumentsFailure::diagnoseParameter(
29742979
} else if (auto *TAD = dyn_cast<TypeAliasDecl>(DC)) {
29752980
baseTyForNote = TAD->getUnboundGenericType();
29762981
} else {
2977-
baseTyForNote = DC->getDeclaredInterfaceType();
2978-
}
2979-
2980-
if (!baseTyForNote)
29812982
return true;
2983+
}
29822984

29832985
emitDiagnostic(GP->getDecl(), diag::archetype_declared_in_type, GP,
29842986
baseTyForNote);

test/Constraints/generics.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,14 @@ func sr_7003() {
747747
// expected-error@-1 {{generic parameter 'T' could not be inferred}}
748748
// expected-note@-2 {{explicitly specify the generic arguments to fix this issue}} {{17-17=<Any>}}
749749
}
750+
751+
func test_generic_subscript_with_missing_arg() {
752+
struct S<T> {
753+
subscript<U>(_: T) -> S<U> { fatalError() }
754+
// expected-note@-1 {{in call to 'subscript(_:)'}}
755+
}
756+
757+
func test(_ s: S<Int>) {
758+
_ = s[0] // expected-error {{generic parameter 'U' could not be inferred}}
759+
}
760+
}

0 commit comments

Comments
 (0)