Skip to content

Commit 00b0491

Browse files
author
li3zhen1
committed
[CSDiagnostics] Update UnableToInferGenericPackElementType to accept overloads and update test cases
1 parent 4779ddc commit 00b0491

File tree

4 files changed

+42
-44
lines changed

4 files changed

+42
-44
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,14 +2550,12 @@ TypeVariableBinding::fixForHole(ConstraintSystem &cs) const {
25502550
}
25512551

25522552
if (dstLocator->isLastElement<LocatorPathElt::PackElement>()) {
2553-
25542553
// A hole appears as an element of generic pack params
25552554
ConstraintFix *Fix = SpecifyPackElementType::create(cs, dstLocator);
25562555
return std::make_pair(Fix, defaultImpact);
25572556
}
25582557

25592558
return std::nullopt;
2560-
return std::nullopt;
25612559
}
25622560

25632561
bool TypeVariableBinding::attempt(ConstraintSystem &cs) const {

lib/Sema/CSDiagnostics.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8188,49 +8188,46 @@ bool UnableToInferClosureReturnType::diagnoseAsError() {
81888188
}
81898189

81908190
bool UnableToInferGenericPackElementType::diagnoseAsError() {
8191-
8192-
const auto* locator = getLocator();
8191+
auto *locator = getLocator();
81938192
auto path = locator->getPath();
81948193

8195-
if (path.size() > 1) {
8196-
8197-
const auto applyArgToParamElt = (path.end() - 2)->getAs<LocatorPathElt::ApplyArgToParam>();
8198-
const auto packElementElt = (path.end() - 1)->getAs<LocatorPathElt::PackElement>();
8199-
8200-
if (!applyArgToParamElt)
8201-
return false;
8202-
8203-
auto anchor = getAnchor();
8194+
const auto applyArgToParamElt =
8195+
(path.end() - 2)->getAs<LocatorPathElt::ApplyArgToParam>();
8196+
const auto packElementElt =
8197+
(path.end() - 1)->getAs<LocatorPathElt::PackElement>();
8198+
if (!applyArgToParamElt || !packElementElt) {
8199+
return false;
8200+
}
82048201

8202+
if (isExpr<NilLiteralExpr>(getAnchor())) {
82058203
// `nil` appears as an element of generic pack params, let's record a
82068204
// specify contextual type for nil fix.
8207-
if (isExpr<NilLiteralExpr>(anchor)) {
8208-
emitDiagnostic(diag::unresolved_nil_literal);
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-
}
8205+
emitDiagnostic(diag::unresolved_nil_literal);
8206+
} else {
8207+
// unable to infer the type of an element of generic pack params
8208+
emitDiagnostic(diag::could_not_infer_pack_element,
8209+
packElementElt->getIndex());
8210+
}
82148211

8212+
if (isExpr<ApplyExpr>(locator->getAnchor())) {
82158213
// 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());
8214+
if (auto *calleeLocator = getSolution().getCalleeLocator(locator)) {
8215+
if (const auto choice = getOverloadChoiceIfAvailable(calleeLocator)) {
8216+
if (auto *decl = choice->choice.getDeclOrNull()) {
8217+
if (auto paramDecl =
8218+
getParameterAt(decl, applyArgToParamElt->getParamIdx())) {
8219+
emitDiagnosticAt(
8220+
paramDecl->getLoc(), diag::note_in_opening_pack_element,
8221+
packElementElt->getIndex() + 1, paramDecl->getNameStr());
8222+
}
8223+
}
82238224
}
82248225
}
8225-
8226-
return true;
82278226
}
82288227

8229-
return false;
8228+
return true;
82308229
}
82318230

8232-
8233-
82348231
static std::pair<StringRef, StringRef>
82358232
getImportModuleAndDefaultType(const ASTContext &ctx,
82368233
const ObjectLiteralExpr *expr) {

lib/Sema/ConstraintSystem.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5881,28 +5881,24 @@ void constraints::simplifyLocator(ASTNode &anchor,
58815881
if (!elt)
58825882
break;
58835883

5884-
// If the 3rd element is an PackElement, add the index of pack element within
5885-
// packs to locate the correct element.
5886-
bool hasEltPack = false;
5887-
unsigned eltPackIdx = 0;
5884+
// If the 3rd element is an PackElement, add the index of pack element
5885+
// within packs to locate the correct element.
5886+
std::optional<unsigned> eltPackIdx;
58885887
if (path.size() > 2) {
5889-
auto eltPack = path[2].getAs<LocatorPathElt::PackElement>();
5890-
if (eltPack) {
5891-
hasEltPack = true;
5888+
if (auto eltPack = path[2].getAs<LocatorPathElt::PackElement>()) {
58925889
eltPackIdx = eltPack->getIndex();
58935890
}
58945891
}
58955892

58965893
// Extract application argument.
58975894
if (auto *args = anchorExpr->getArgs()) {
5898-
if (hasEltPack) {
5899-
if (elt->getArgIdx() + eltPackIdx < args->size()) {
5900-
anchor = args->getExpr(elt->getArgIdx() + eltPackIdx);
5895+
if (eltPackIdx.has_value()) {
5896+
if (elt->getArgIdx() + eltPackIdx.value() < args->size()) {
5897+
anchor = args->getExpr(elt->getArgIdx() + eltPackIdx.value());
59015898
path = path.slice(3);
59025899
continue;
59035900
}
5904-
}
5905-
else if (elt->getArgIdx() < args->size()) {
5901+
} else if (elt->getArgIdx() < args->size()) {
59065902
anchor = args->getExpr(elt->getArgIdx());
59075903
path = path.slice(2);
59085904
continue;

test/Constraints/variadic_generic_functions.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,11 @@ do {
9797
bar(1, 2, 3, nil, "Hello", u: 3, w: 4, 8, nil) // expected-error {{'nil' requires a contextual type}}
9898
// expected-error@-1 {{'nil' requires a contextual type}}
9999

100+
101+
func fooWithOverload(_ value: Int) {}
102+
func fooWithOverload<each T>(_ value: repeat each T) {}
103+
// expected-note@-1 {{in inferring pack element #5 of 'value'}}
104+
105+
fooWithOverload(0, 1, 2, 3, nil) // expected-error {{'nil' requires a contextual type}}
106+
100107
}

0 commit comments

Comments
 (0)