Skip to content

[ConstraintSystem] Further unwrap cleanup #18699

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 3 commits into from
Aug 16, 2018
Merged

Conversation

gregomni
Copy link
Contributor

This was part of PR #18324

If shouldAttemptFixes() then when we are trying to bind type variables, if T? fails, then attempt to bind T for all binding types. This was already happening and valid for subtype bindings. This change tries it for supertype and identity bindings because it allows us the opportunity to matchTypes() a T? value to a T bound variable by diagnosing a missing unwrap.

This was the only situation in which we were previously missing finding that Fix in the constraint system, so with this change all of the unwrap diagnosis can be moved into the new CSDiagnostics.cpp and made static.

@gregomni
Copy link
Contributor Author

cc @xedin @rudkx

@gregomni
Copy link
Contributor Author

@swift-ci Please smoke test

…(previously just allowed for Subtypes). This allows us to always find MissingOptionalUnwrapFailures, so that all the unwrap fixit code can be moved into CSDiagnostics and made static.
@gregomni
Copy link
Contributor Author

@swift-ci Please smoke test

@gregomni
Copy link
Contributor Author

Looks like checks failed because they hadn't included #18324 yet.

@swift-ci Please smoke test.

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

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

As I mentioned in previous PR, this make sense to me! Could you try recording a fix right away? It seems like that should work, and duplicate fix should be ignored (when attempted) later on because it would have the same locator?

@gregomni
Copy link
Contributor Author

I investigated this further, and the issue is slightly different than I originally thought, and I don't think there's any good way to construct the needed fix(es) in advance. The test that addresses this is SR-1069 which is in tests/Constraints/closures.swift.

Even further simplified, the issue arises here:

class C {}
struct S {
  func genericallyNonOptional<T: AnyObject>(_ non: T) {}

  func f(object: C?) {
    genericallyNonOptional(object)
  }
}

T can't be bound to C? because Optional isn't a class type, so we want to bind T to C and emit a missing unwrap.

Interestingly, without this PR the current code handles the above fine, but breaks down with a mix of multiple arguments:

class C {}
struct S {
  func genericallyNonOptional<T: AnyObject>(_ a: T, _ b: T, _ c: T) { }

  func f(_ a: C?, _ b: C?, _ c: C) {
    genericallyNonOptional(a, b, c) // cannot invoke 'genericallyNonOptional' with an argument list of type '(C?, C?, C)' // expected an argument list of type '(T, T, T)'

  }
}

... whereas after this PR, we get the much better suggestion of unwrap fixits for arguments a and b. Will add a test to this effect.

@gregomni
Copy link
Contributor Author

@swift-ci Please smoke test.

@xedin
Copy link
Contributor

xedin commented Aug 15, 2018

Sounds good to me!

@gregomni
Copy link
Contributor Author

@swift-ci Please smoke test

@gregomni gregomni merged commit 34f6a1f into swiftlang:master Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants