Skip to content

Commit

Permalink
[Sema] Improves diagnostic when passing an argument as property of an…
Browse files Browse the repository at this point in the history
… optional class instance (swiftlang#57437)
  • Loading branch information
Rajveer100 committed Sep 11, 2023
1 parent a5607cc commit a5e35fa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6465,6 +6465,15 @@ bool ConstraintSystem::repairFailures(
// diagnostics.
if (flags.contains(TMF_MatchingGenericArguments))
break;

if (!path.empty()) {
if (path.back().is<LocatorPathElt::ApplyArgToParam>())
conversionsOrFixes.push_back(AllowArgumentMismatch::create(
*this, lhs, rhs, getConstraintLocator(anchor, path)));
else if (path.back().is<LocatorPathElt::ContextualType>())
conversionsOrFixes.push_back(IgnoreContextualType::create(
*this, lhs, rhs, getConstraintLocator(anchor, path)));
}

Type fromType;
Type toType;
Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/parameterized_existentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func g3(x: G<any P<Int>>) -> G<any P<String>> {
}

func h1(x: (any P)?) -> (any P<Int>)? {
return x // expected-error {{cannot convert return expression of type '(any P)?' to return type '(any P<Int>)?'}}
return x // expected-error {{cannot convert return expression of type 'any P' to return type 'any P<Int>'}}
}

func h2(x: (any P<Int>)?) -> (any P)? {
return x // okay
}

func h3(x: (any P<Int>)?) -> (any P<String>)? {
return x // expected-error {{cannot convert return expression of type '(any P<Int>)?' to return type '(any P<String>)?'}}
return x // expected-error {{cannot convert return expression of type 'String' to return type 'Int'}}
}

0 comments on commit a5e35fa

Please sign in to comment.