Skip to content

Issue-60730: Addressing the situation where we were saying that (any … #61191

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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
a2d1ef6
Issue-60730: Addressing the situation where we were saying that (any …
dfperry5 Sep 22, 2022
6222099
Issue-60730: Removing assigned of AllowArgumentMismatch fix that woul…
dfperry5 Sep 22, 2022
81c3b3c
Issue-60730: Removing file that was accidentally changed
dfperry5 Sep 22, 2022
41cbed8
Issue-60730: Updating to use simplifyConformsToConstraint - and to cl…
dfperry5 Sep 24, 2022
d5463c8
Merge branch 'apple:main' into Issue-60730-ForceOptional-InsteadOf-No…
dfperry5 Sep 24, 2022
54d99aa
Issue-60730: Updating to pass protocoDecl instead of type2 to simplif…
dfperry5 Sep 28, 2022
c34e5c2
Issue-60730: Updating to pass protocoDecl instead of type2 to simplif…
dfperry5 Sep 28, 2022
7697e87
Issue-60730: Formatting correctly
dfperry5 Sep 28, 2022
66c2284
Issue-60730: Updating to not pass 1 to recordFix, where that is the d…
dfperry5 Sep 28, 2022
c053448
Merge branch 'apple:main' into Issue-60730-ForceOptional-InsteadOf-No…
dfperry5 Oct 4, 2022
f4c2027
Issue-60730: Adding in test to validate that the fix works as expected
dfperry5 Oct 4, 2022
d4e2cb7
Issue-60730: Cleaning up test based on PR Comments
dfperry5 Oct 4, 2022
4e92152
Issue-60730: Correcting typo in test
dfperry5 Oct 5, 2022
f8c6cbc
Merge branch 'apple:main' into Issue-60730-ForceOptional-InsteadOf-No…
dfperry5 Oct 5, 2022
9f9770e
Issue-60730: Adding additional test cases for layers of optionals and…
dfperry5 Oct 7, 2022
0488fe0
Merge branch 'apple:main' into Issue-60730-ForceOptional-InsteadOf-No…
dfperry5 Oct 7, 2022
6811590
Issue-60730: Updating test to include fixme(diagnostics) for multilpe…
dfperry5 Oct 7, 2022
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
19 changes: 15 additions & 4 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3627,18 +3627,29 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,

if (last.is<LocatorPathElt::ApplyArgToParam>()) {
auto proto = protoDecl->getDeclaredInterfaceType();
auto *fix = AllowArgumentMismatch::create(
*this, type1, proto, getConstraintLocator(anchor, path));

// Impact is 2 here because there are two failures
// 1 - missing conformance and 2 - incorrect argument type.
//
// This would make sure that arguments with incorrect
// conformances are not prioritized over general argument
// mismatches.
if (type1->isOptional()) {
auto unwrappedType = type1->lookThroughAllOptionalTypes();
auto result = simplifyConformsToConstraint(
unwrappedType, protoDecl, kind, locator,
subflags | TMF_ApplyingFix);
if (result == SolutionKind::Solved) {
auto fix = ForceOptional::create(*this, type1, proto,
getConstraintLocator(locator));
if (recordFix(fix))
return getTypeMatchFailure(locator);
break;
}
}
auto fix = AllowArgumentMismatch::create(
*this, type1, proto, getConstraintLocator(anchor, path));
if (recordFix(fix, /*impact=*/2))
return getTypeMatchFailure(locator);

break;
}

Expand Down
24 changes: 24 additions & 0 deletions test/Constraints/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,27 @@ func testFunctionContainerMethodCall(container: FunctionContainer?) {
// expected-note@-3 {{coalesce}}
// expected-note@-4 {{force-unwrap}}
}

// Test for https://github.com/apple/swift/issues/60730
// rdar://94037733
do {
struct S: P {}
func takesP(_: any P) {}
func passOptional(value: (any P)?) {
takesP(value)
// expected-error@-1 {{value of optional type '(any P)?' must be unwrapped to a value of type 'any P'}}
// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
}
func passLayersOfOptional(value: (any P)??) {
// FIXME(diagnostics): Consider recording multiple ForceUnwrap fixes based on number of optionals
takesP(value)
// expected-error@-1 {{value of optional type '(any P)??' must be unwrapped to a value of type '(any P)?}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this is not great... Could you please leave a FIXME(diagnostics): here to to record multiple ForceUnwrap fixes based on number of optionals we need to unwrap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do I add the "FIXME"? As a comment above the takesP() call?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's good enough.

// expected-note@-2 {{coalesce using '??' to provide a default when the optional value contains 'nil'}}
// expected-note@-3 {{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
}
func passNonConformingValue(value: (any BinaryInteger)?){
takesP(value)
// expected-error@-1 {{argument type '(any BinaryInteger)?' does not conform to expected type 'P'}}
}
}