Skip to content

[Sema] NFC - Make #15419 work with weak vars #15542

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

Conversation

davezarzycki
Copy link
Contributor

This extends the tuple liternal type checking optimization to handle weak variables.

@davezarzycki davezarzycki requested a review from rudkx March 27, 2018 17:06
@davezarzycki
Copy link
Contributor Author

@swift-ci please smoke test

@rudkx
Copy link
Contributor

rudkx commented Mar 27, 2018

I don't think this makes sense. weak can only be used on class and class-bound protocol types.

@davezarzycki
Copy link
Contributor Author

Can you elaborate? The validation test suite passes on my Linux box.

@davezarzycki
Copy link
Contributor Author

davezarzycki commented Mar 27, 2018

I think I get it now, the #15419 optimization doesn't optimize anything if the expression is not a tuple/dictionary/array literal, and weak variables will never be tuple/array/dictionary literals.

I guess this pull request is superficial. Do you find the resulting code to be prettier? Or would you rather have two switch statements?

And what about weak var (x,y,z) = (a,b,c)? Won't the optimization kick in then?

@rudkx
Copy link
Contributor

rudkx commented Mar 27, 2018

Oh sorry, I just meant that it wouldn't be legal code.

Compiling:

class C {}
weak var x = (C(), C())

Results in:

/tmp/weak.swift:2:10: error: 'weak' may only be applied to class and class-bound protocol types, not '(C, C)'
weak var x = (C(), C())

I had originally tried to make this work, as well as things with inout on the RHS, and all forms of nested patterns. It would be nice to get the latter working.

Now having said that, it would produce a diagnostic faster (or at all the the case of expressions where we would otherwise bail out with "expression was too complex").

When I had a similar change I wasn't really able to convince myself that it was actually correct, though, since the RHS might be inferred to be an optional as well, so whether you're wrapping it in another optional or not really shouldn't depend on whether we knew we had an optional on the RHS before the type checker runs.

@rudkx
Copy link
Contributor

rudkx commented Mar 27, 2018

And what about weak var (x,y,z) = (a,b,c)? Won't the optimization kick in then?

That's a tuple pattern, which already recursively decomposes the elements (see PatternKind::Tuple father down).

@davezarzycki
Copy link
Contributor Author

Oh, duh, yes you're right about tuple patterns. As for weak variables, they're kind of gross. They intentionally let this work, which is why this pull request does what it does:

class C {}
let c = C()
let oc : C? = c
weak var w1 = c
weak var w2 = oc

Are faster weak diagnostics worth it?

@rudkx
Copy link
Contributor

rudkx commented Mar 27, 2018

Are faster weak diagnostics worth it?

Probably not worth the effort. We've only had the exponential tuple type checking issue reported a few times. I happened to be looking into exponential cases involving arrays and dictionaries and noticed that the tuple case was pretty easy to fix for the simple pattern kinds, so I did that, and then spent way too much time trying to generalize it :).

@davezarzycki
Copy link
Contributor Author

What about bypassing the optimization in the unowned or unmanaged cases, which like weak can never be tuples?

@rudkx
Copy link
Contributor

rudkx commented Mar 27, 2018

What about bypassing the optimization in the unowned or unmanaged cases, which like weak can never be tuples?

I think it's fine to leave it as is, since we should report the error faster with the optimization in place.

@davezarzycki
Copy link
Contributor Author

I get the impression that you want me to abandon this pull request then. For the record, it seems lame to me unnecessarily different logic for weak versus unowned/unmanaged, especially when the work to unify the code has already been done in this PR. Please confirm that you want me to abandon this PR. Thanks.

@slavapestov
Copy link
Contributor

I think this PR is reasonable as a cleanup.

@rudkx
Copy link
Contributor

rudkx commented Mar 27, 2018

@davezarzycki I'm not suggesting you abandon the PR. I'm just not sure it's worth trying to fix this case, and as I mentioned I abandoned a similar change because I couldn't convince myself it would work.

I just spent a few minutes putting together a test case, and this is the sort of thing that I think will be broken if you were to use the same type variable that appears on the RHS and then attempt to wrap that type variable in an optional: #15557

There may be some way to generalize this to this case as well as other nested pattern kinds, I'm just not sure it's worth the effort.

@davezarzycki davezarzycki force-pushed the nfc_opt_weak_var_typechecking branch from 1d1707a to 3d276f3 Compare March 28, 2018 01:31
@davezarzycki
Copy link
Contributor Author

I updated this PR to handle #15557.

@swift-ci please smoke test

@davezarzycki
Copy link
Contributor Author

BTW – Whether this PR is "worth the effort" or not, good things have happened because of it. Like your test in #15557, and my understanding of this code improved too (which improved the QoI of my branch). Thanks. :-)

@rudkx
Copy link
Contributor

rudkx commented Mar 28, 2018

Sure, I don’t mean to tell you how to spend your time for sure, and appreciate any efforts towards generalizing things to remove special cases. I’ll take another look at your updated code when I am in front of a keyboard again rather than on my phone.

@davezarzycki davezarzycki force-pushed the nfc_opt_weak_var_typechecking branch from 3d276f3 to 660d1d2 Compare March 28, 2018 02:19
@davezarzycki
Copy link
Contributor Author

@swift-ci please smoke test

@rudkx
Copy link
Contributor

rudkx commented Mar 28, 2018

@swift-ci Please smoke test

@rudkx
Copy link
Contributor

rudkx commented Mar 28, 2018

@swift-ci Please test source compatibility

1 similar comment
@rudkx
Copy link
Contributor

rudkx commented Mar 28, 2018

@swift-ci Please test source compatibility

@davezarzycki
Copy link
Contributor Author

davezarzycki commented Mar 28, 2018

Unless I'm reading the source compatibility results incorrectly, that failure looks unrelated:

UPasses:
  UPASS: https://bugs.swift.org/browse/SR-7287, ReSwift, 3.0, db35c2, ReSwift-watchOS, generic/platform=watchOS
  UPASS: https://bugs.swift.org/browse/SR-7287, ReSwift, 3.0, db35c2, ReSwift-tvOS, generic/platform=tvOS
  UPASS: https://bugs.swift.org/browse/SR-7287, ReSwift, 3.0, db35c2, ReSwift-macOS, generic/platform=macOS
  UPASS: https://bugs.swift.org/browse/SR-7287, ReSwift, 3.0, db35c2, ReSwift-iOS, generic/platform=iOS

Also, I'm going to need to rebase this to fix a merge conflict due to #15545

@davezarzycki davezarzycki force-pushed the nfc_opt_weak_var_typechecking branch from 660d1d2 to f4d4a3d Compare March 28, 2018 11:44
@davezarzycki
Copy link
Contributor Author

@swift-ci please smoke test

@davezarzycki
Copy link
Contributor Author

Hi @rudkx – Out of respect to the tentative "make reviewing mandatory" policy discussion, can you please approve, decline, or actionably comment on this change? Thanks!

@slavapestov slavapestov self-requested a review March 28, 2018 22:11
Copy link
Contributor

@rudkx rudkx left a comment

Choose a reason for hiding this comment

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

One request: Please add a test case showing that we are now fast when weak is used.

@davezarzycki
Copy link
Contributor Author

@rudkx – I'm not familiar with performance sensitive tests. How does one show that a particular semantic scenario is "fast"?

@rudkx
Copy link
Contributor

rudkx commented Mar 28, 2018

Actually I just realized that I didn't do that with my original change. Let's merge your change and then I will set up a validation test with my original test update and a new test for weak.

We have two mechanisms for this, neither of which is perfect. Timer-based testing, and scale-test-based testing. The existing tests sit under validation-test/Sema/type_checker_perf and I inadvertently just left the tests that I had written for these patterns in the pattern test rather than moving them over.

@rudkx
Copy link
Contributor

rudkx commented Mar 28, 2018

Merge at will, thanks!

@davezarzycki davezarzycki merged commit 18abd7e into swiftlang:master Mar 28, 2018
@davezarzycki davezarzycki deleted the nfc_opt_weak_var_typechecking branch March 28, 2018 23:17
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.

3 participants