Skip to content

[5.7][ConstraintSystem] Retrieve contextual type from a solution for ambiguities #60533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ ERROR(no_candidates_match_result_type,none,
"no '%0' candidates produce the expected contextual result type %1",
(StringRef, Type))

ERROR(no_overloads_have_result_type_conformance,none,
"no '%0' overloads produce result type that conforms to %1",
(StringRef, Type))

ERROR(no_candidates_match_argument_type,none,
"no '%0' candidates produce the expected type %1 for parameter #%2",
(StringRef, Type, unsigned))
Expand Down Expand Up @@ -559,6 +563,10 @@ NOTE(cannot_convert_candidate_result_to_contextual_type,none,
"%0 produces %1, not the expected contextual result type %2",
(DeclName, Type, Type))

NOTE(overload_result_type_does_not_conform,none,
"result type %1 of %0 does not conform to %2",
(DeclName, Type, Type))

// for ... in expression
ERROR(cannot_convert_sequence_element_value,none,
"cannot convert sequence element type %0 to expected type %1",
Expand Down
9 changes: 9 additions & 0 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,15 @@ class Solution {
/// types where possible.
Type resolveInterfaceType(Type type) const;

Type getContextualType(ASTNode anchor) const {
for (const auto &entry : contextualTypes) {
if (entry.first == anchor) {
return simplifyType(entry.second.getType());
}
}
return Type();
}

/// For a given locator describing a function argument conversion, or a
/// constraint within an argument conversion, returns information about the
/// application of the argument to its parameter. If the locator is not
Expand Down
26 changes: 15 additions & 11 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4219,27 +4219,31 @@ static bool diagnoseAmbiguityWithContextualType(

auto anchor = locator->getAnchor();
auto name = result->choices.front().getName();
DE.diagnose(getLoc(anchor), diag::no_candidates_match_result_type,
name.getBaseName().userFacingName(),
cs.getContextualType(anchor, /*forConstraint=*/false));
auto contextualTy = solution.getContextualType(anchor);

assert(contextualTy);

DE.diagnose(getLoc(anchor),
contextualTy->is<ProtocolType>()
? diag::no_overloads_have_result_type_conformance
: diag::no_candidates_match_result_type,
name.getBaseName().userFacingName(), contextualTy);

for (const auto &solution : solutions) {
auto overload = solution.getOverloadChoice(calleeLocator);
if (auto *decl = overload.choice.getDeclOrNull()) {
auto loc = decl->getLoc();
if (loc.isInvalid())
continue;

auto type = solution.simplifyType(overload.boundType);

if (isExpr<ApplyExpr>(anchor) || isExpr<SubscriptExpr>(anchor)) {
auto fnType = type->castTo<FunctionType>();
DE.diagnose(
loc, diag::cannot_convert_candidate_result_to_contextual_type,
decl->getName(), fnType->getResult(),
cs.getContextualType(anchor, /*forConstraint=*/false));
decl,
contextualTy->is<ProtocolType>()
? diag::overload_result_type_does_not_conform
: diag::cannot_convert_candidate_result_to_contextual_type,
decl->getName(), fnType->getResult(), contextualTy);
} else {
DE.diagnose(loc, diag::found_candidate_type, type);
DE.diagnose(decl, diag::found_candidate_type, type);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/Constraints/overload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,30 @@ func rdar79672230() {
var t: MyType = MyType()
test(&t) // expected-error {{no exact matches in call to local function 'test'}}
}

// rdar://97396399 - crash in swift::DiagnosticEngine::formatDiagnosticText
func rdar97396399() {
// Has to be overloaded to make sure that contextual type is not recorded during constraint generation
func test(_: () -> Void) {}
func test(_: (Int) -> Void) {}

// Multiple different overloads, none of which conform to Sequence
func fn(_: Int) -> Int {}
// expected-note@-1 {{found candidate with type '(Int) -> Int'}}
// expected-note@-2 {{result type 'Int' of 'fn' does not conform to 'Sequence'}}
func fn(_: Int) -> Double {}
// expected-note@-1 {{found candidate with type '(Int) -> Double'}}
// expected-note@-2 {{result type 'Double' of 'fn' does not conform to 'Sequence'}}

test {
for x in fn { // expected-error {{no 'fn' overloads produce result type that conforms to 'Sequence'}}
print(x)
}
}

test {
for x in fn(42) { // expected-error {{no 'fn' overloads produce result type that conforms to 'Sequence'}}
print(x)
}
}
}