Skip to content

Commit 9a6d2b5

Browse files
author
li3zhen1
committed
[CSDiagnostics] Add callee side note for holes in generic pack element
1 parent a5e04b5 commit 9a6d2b5

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4462,6 +4462,10 @@ ERROR(could_not_infer_placeholder,none,
44624462

44634463
ERROR(could_not_infer_pack_element,none,
44644464
"could not infer pack element #%0 from context", (unsigned))
4465+
4466+
NOTE(note_in_opening_pack_element,none,
4467+
"in infering pack element #%0 of '%1'", (unsigned,StringRef))
4468+
44654469

44664470
ERROR(type_of_expression_is_ambiguous,none,
44674471
"type of expression is ambiguous without a type annotation", ())

lib/Sema/CSDiagnostics.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8193,31 +8193,44 @@ bool UnableToInferGenericPackElementType::diagnoseAsError() {
81938193
auto path = locator->getPath();
81948194

81958195
if (path.size() > 1) {
8196+
81968197
const auto applyArgToParamElt = (path.end() - 2)->getAs<LocatorPathElt::ApplyArgToParam>();
8198+
const auto packElementElt = (path.end() - 1)->getAs<LocatorPathElt::PackElement>();
81978199

81988200
if (!applyArgToParamElt)
81998201
return false;
82008202

8201-
unsigned eltIdx = applyArgToParamElt->getArgIdx();
82028203
auto anchor = getAnchor();
82038204

8204-
82058205
// `nil` appears as an element of generic pack params, let's record a
82068206
// specify contextual type for nil fix.
82078207
if (isExpr<NilLiteralExpr>(anchor)) {
82088208
emitDiagnostic(diag::unresolved_nil_literal);
8209-
return true;
8209+
}
8210+
else {
8211+
// unable to infer the type of an element of generic pack params
8212+
emitDiagnostic(diag::could_not_infer_pack_element, packElementElt->getIndex());
8213+
}
8214+
8215+
// emit callee side diagnostics
8216+
auto applyExpr = castToExpr<ApplyExpr>(locator->getAnchor());
8217+
if (auto* Fn = applyExpr->getFn()) {
8218+
if (const auto DeclRef = Fn->getReferencedDecl()) {
8219+
auto paramDecl = getParameterAt(DeclRef, applyArgToParamElt->getParamIdx());
8220+
emitDiagnosticAt(
8221+
paramDecl->getLoc(), diag::note_in_opening_pack_element,
8222+
packElementElt->getIndex() + 1, paramDecl->getNameStr());
8223+
}
82108224
}
82118225

8212-
// unable to infer the type of an element of generic pack params
8213-
emitDiagnostic(diag::could_not_infer_pack_element, eltIdx);
82148226
return true;
82158227
}
82168228

82178229
return false;
82188230
}
82198231

82208232

8233+
82218234
static std::pair<StringRef, StringRef>
82228235
getImportModuleAndDefaultType(const ASTContext &ctx,
82238236
const ObjectLiteralExpr *expr) {

0 commit comments

Comments
 (0)