Skip to content

[PrintAsObjC] Handle circularities introduced by ObjC generics. #4155

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 9, 2016

Conversation

jrose-apple
Copy link
Contributor

Like Swift generics, Objective-C generics may have constraints; unlike Swift generics, Objective-C doesn't do separate parsing and type-checking passes. This means that any generic arguments for constrained generic parameters must be fully-defined, in order to check that they satisfy the constraints.

This commit addresses this problem with three different mechanisms, one for each kind of declaration that might run into this issue:

  • For classes, if a member references a type with constrained generic parameter, and the corresponding argument type hasn't been printed yet, that member is "delayed", which means it is put into a category at the end of the file.
  • Protocols cannot have categories, so for protocols we instead see if we can print the definition of the other type first. To break circular dependencies, the printer will not attempt this if both the type and the protocol are already being depended on. This isn't perfect (see below).
  • Rather than delaying members of extensions, we just delay them wholesale. This keeps related members together, but also has problems (see below).

These approaches solve the most common cases while still not crashing in the uncommon ones. However, there are still a number of problems:

  • The protocol heuristic is overly negative, which means we may generate an invalid header even when there's a reasonable ordering. For example, a single class might inherit from a class A and conform to protocol P, and protocol P depends on class A as a generic argument. In this case, defining class A first is the right thing to do, but it's possible for the printer to decide that there's circularity here and just forward-declare A instead.
  • Protocols really can be circular. This can be fixed by printing a forward-declared protocol alongside the generic constraints, i.e. id <MoreThanNSCopying, NSCopying> instead of just id <MoreThanNSCopying>.
  • Extensions can introduce protocols as well. This is not modeled at all; if a member depends on a protocol conformance, it's assumed that simply printing the class would be sufficient. This could be fixed by checking how a generic argument satisfies its constraints, possibly delaying individual members from extensions in order to print them sooner.
  • More cases I haven't thought about.

Test cases for some of these problems are in the new circularity-errors.swift file, mostly to make sure the ObjC printer doesn't crash when it encounters them. I intend to file bugs for these issues.

rdar://problem/27109377


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

A smoke test on macOS does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library only for macOS. Simulator standard libraries and
    device standard libraries are not built.
  3. lldb is not built.
  4. The test and validation-test targets are run only for macOS. The optimized
    version of these tests are not run.

A smoke test on Linux does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library incrementally.
  3. lldb is built incrementally.
  4. The swift test and validation-test targets are run. The optimized version of these
    tests are not run.
  5. lldb is tested.

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

Like Swift generics, Objective-C generics may have constraints; unlike
Swift generics, Objective-C doesn't do separate parsing and
type-checking passes. This means that any generic arguments for
constrained generic parameters must be fully-defined, in order to
check that they satisfy the constraints.

This commit addresses this problem with three different mechanisms,
one for each kind of declaration that might run into this issue:

- For classes, if a member references a type with constrained generic
  parameter, and the corresponding argument type hasn't been printed
  yet, that member is "delayed", which means it is put into a category
  at the end of the file.

- Protocols cannot have categories, so for protocols we instead see if
  we can print the definition of the other type first. To break
  circular dependencies, the printer will not attempt this if both the
  type and the protocol are already being depended on. This isn't
  perfect (see below).

- Rather than delaying members of extensions, we just delay them
  wholesale. This keeps related members together, but also has
  problems (see below).

These approaches solve the most common cases while still not crashing
in the uncommon ones. However, there are still a number of problems:

- The protocol heuristic is overly negative, which means we may generate
  an invalid header even when there's a reasonable ordering. For example,
  a single class might inherit from a class A and conform to protocol P,
  and protocol P depends on class A as a generic argument. In this case,
  defining class A first is the right thing to do, but it's possible for
  the printer to decide that there's circularity here and just forward-
  declare A instead.

- Protocols really can be circular. This can be fixed by printing a
  forward-declared protocol alongside the generic constraints, i.e.
  'id <MoreThanNSCopying, NSCopying>' instead of just
  'id <MoreThanNSCopying>'.

- Extensions can introduce protocols as well. This is not modeled at
  all; if a member depends on a protocol conformance, it's assumed
  that simply printing the class would be sufficient. This could be
  fixed by checking how a generic argument satisfies its constraints,
  possibly delaying individual members from extensions in order to
  print them sooner.

- More cases I haven't thought about.

Test cases for some of these problems are in the new
circularity-errors.swift file, mostly to make sure the ObjC printer
doesn't crash when it encounters them.

rdar://problem/27109377
@jrose-apple
Copy link
Contributor Author

@jckarter, @bitjammer, or @atrick, can you review this?

@swift-ci Please test

@jckarter
Copy link
Contributor

jckarter commented Aug 9, 2016

LGTM.

@jrose-apple
Copy link
Contributor Author

jrose-apple commented Aug 9, 2016

Linux failures unrelated (and really I shouldn't have tested Linux at all, since this only affects ObjC).

@tkremenek
Copy link
Member

Majority of CI testing has passed. Merging.

@tkremenek tkremenek merged commit 3b315d1 into swiftlang:master Aug 9, 2016
@jrose-apple jrose-apple deleted the PrintAsObjC-circularity branch August 9, 2016 21:06
kateinoigakukun pushed a commit that referenced this pull request Aug 31, 2022
[pull] swiftwasm from main
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.

4 participants