Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Contributing to Swift

D3c5f7382824133c2718c8effb5894fb?s=47 Ayaka Nonaka
November 08, 2016

Contributing to Swift

A few months ago, I somehow ended up contributing a non-trivial diff to the Swift compiler without much prior experience with C++ and absolutely zero with compilers, and a lot of people have been asking me how I managed to pull that off. This talk will go over how to get up and running with the Swift compiler and how you can contribute too even if it feels completely new and scary!

D3c5f7382824133c2718c8effb5894fb?s=128

Ayaka Nonaka

November 08, 2016
Tweet

Transcript

  1. Contributing to Swift @ayanonagon

  2. Hi! I’m Ayaka.

  3. Workflow

  4. June 30, 2016

  5. #if DC_TARGET_MOBILE #import <UIKit/UIKit.h> @compatibility_alias DCColor UIColor; #else #import <Cocoa/Cocoa.h>

    @compatibility_alias DCColor NSColor; #endif
  6. None
  7. None
  8. None
  9. None
  10. My job here is done. !

  11. None
  12. None
  13. None
  14. Contributing to Swift @ayanonagon

  15. 0. Make sure you don’t need to write a proposal

    for it.
  16. 1.Check swift-evolution@swift.org archives 2.Email swift-evolution@swift.org 3.Get a ! from Swift

    team member 4.Get SR assigned to you.
  17. None
  18. Now what?

  19. Fears 1.New codebases 2.Compilers 3.C++

  20. I have no idea what I’m doing.

  21. Proceed fearlessly!

  22. 0.5. Find a mentor My mentor: Robert (aka @_CodaFi)

  23. 1. Clone the repo github.com/apple/swift

  24. 1. Clone the repo 1.mkdir swift-source (or wherever) 2.cd into

    swift-source & clone repo 3../swift/utils/update-checkout --clone
  25. None
  26. 2. Get CMake and Ninja

  27. » CMake: manages build configurations » Ninja: does the building

  28. None
  29. None
  30. Homebrew ! brew install cmake ninja

  31. 3. Build the project

  32. ./swift/utils/build-script -x

  33. None
  34. None
  35. build/Xcode-DebugAssert/swift-macosx-x86_64/ Swift.xcodeproj

  36. None
  37. 4. Implement the thing

  38. 4. Implement the thing !

  39. I really have no idea what I’m doing.

  40. But… 1.There’s a lot of great existing example code. 2.What’s

    the worst that can happen?
  41. Import @compatibility_alia s as typealias

  42. “Where are similar things implemented?”

  43. “Where are other Objective-C things pulled into Swift?”

  44. ImportDecl.cpp

  45. ImportDecl.cpp Decl * VisitObjCCompatibleAliasDecl(const clang::ObjCCompatibleAliasDecl *decl) { // Like C++

    using declarations, name lookup simply looks through // Objective-C compatibility aliases. They are not imported directly. return nullptr; }
  46. “What behaves similarly to @compatibility_ali as?”

  47. “What also gets imported as typealias?”

  48. typedef typedef NSString StringCheese;

  49. ! " ✨$ !"#$

  50. … It builds. !

  51. !?

  52. !

  53. 4. Tests!

  54. 1.Write some Objective-C code. 2.Import it from Swift. 3.Make some

    assertions based on what you would expect.
  55. 1.Write some Objective-C code. import Foundation; @compatibility_alias StringCheese NSString;

  56. None
  57. 1.Write some Objective-C code. ✅ 2.Import it from Swift. 3.Make

    some assertions based on what you would expect.
  58. 1.Import it from Swift import CompatibilityAlias

  59. None
  60. None
  61. test/IDE/Inputs/custom-modules/module.map module CompatibilityAlias { header "CompatibilityAlias.h" export * }

  62. None
  63. 1.Import it from Swift import CompatibilityAlias func dontMove(cheese: StringCheese) ->

    StringCheese { return cheese }
  64. 1.Write some Objective-C code. ✅ 2.Import it from Swift. ✅

    3.Make some assertions based on what you would expect.
  65. lit testing It’s lit !

  66. // RUN: rm -rf %t // RUN: mkdir -p %t

    // RUN: %build-clang-importer-objc-overlays // RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk-nosource) -I %t -I %S/Inputs/custom-modules // -print-module -source-filename %s -module-to-print=CompatibilityAlias > %t.printed.CompatibilityAlias.txt // RUN: FileCheck %s -check-prefix=PRINT -strict-whitespace < %t.printed.CompatibilityAlias.txt // RUN: %target-parse-verify-swift -sdk %clang-importer-sdk -I %S/Inputs/custom-modules -I %t %s // REQUIRES: objc_interop // PRINT: typealias StringCheese = NSString import CompatibilityAlias func dontMove(cheese: StringCheese) -> StringCheese { return cheese }
  67. // PRINT: typealias StringCheese = NSString import CompatibilityAlias func dontMove(cheese:

    StringCheese) -> StringCheese { return cheese }
  68. // RUN: rm -rf %t // RUN: mkdir -p %t

    // RUN: %build-clang-importer-objc-overlays // RUN: %target-swift-ide-test(mock-sdk: %clang-importer-sdk-nosource) -I %t -I %S/Inputs/custom-modules // -print-module -source-filename %s -module-to-print=CompatibilityAlias > %t.printed.CompatibilityAlias.txt // RUN: FileCheck %s -check-prefix=PRINT -strict-whitespace < %t.printed.CompatibilityAlias.txt // RUN: %target-parse-verify-swift -sdk %clang-importer-sdk -I %S/Inputs/custom-modules -I %t %s // REQUIRES: objc_interop // PRINT: typealias StringCheese = NSString
  69. “What also links with an Objective-C module?”

  70. swift_newtype

  71. None
  72. None
  73. Running the Tests

  74. None
  75. None
  76. None
  77. None
  78. None
  79. None
  80. None
  81. None
  82. ./llvm/utils/lit/lit.py -sv ./build/Ninja- DebugAssert/swift-macosx-x86_64/test-macosx-x86_64/ --filter=compatibility_alias.swift

  83. Tests Failed !

  84. 6. Debugging

  85. LLDB

  86. ./llvm/utils/lit/lit.py -sv ./build/Ninja- DebugAssert/swift-macosx-x86_64/test-macosx-x86_64/ --filter=compatibility_alias.swift

  87. None
  88. None
  89. lldb -- /Users/ayanonagon/swift-source/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swiftc -frontend -target x86_64-apple-macosx10.9 -module-cache-path '/var/folders/g9/jwhqvm3x1rx65dtfhdrb00dc0000gn/T/swift-testsuite-clang-module-cacheK6GoT_' -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk

    -parse -verify -disable-objc-attr-requires-foundation-module /Users/ayanonagon/swift-source/swift/test/IDE/compatibility_alias.swift -sdk -enable-source-import -sdk '/Users/ayanonagon/swift-source/swift/test'/Inputs/clang-importer-sdk -I '/Users/ayanonagon/swift-source/swift/test'/Inputs/clang-importer-sdk/swift-modules -I /Users/ayanonagon/swift-source/swift/test/IDE/Inputs/custom-modules -I /Users/ayanonagon/swift-source/build/Ninja-DebugAssert/swift-macosx-x86_64/test-macosx-x86_64/IDE/Output/compatibility_alias.swift.tmp /Users/ayanonagon/swift-source/swift/test/IDE/compatibility_alias.swift
  90. !

  91. None
  92. None
  93. None
  94. None
  95. » The secret life of types in Swift by Slava

    Pestov » Porting the Swift Runtime to Android by Brian Gesiak » Contributing to Open Source Swift by Jesse Squires » Contributing to Swift by Russ Bishop
  96. » The secret life of types in Swift by Slava

    Pestov » Porting the Swift Runtime to Android by Brian Gesiak » Contributing to Open Source Swift by Jesse Squires » Contributing to Swift by Russ Bishop » Contributing to Swift by YOU <3
  97. Proceed Fearlessly!

  98. Thanks! ? → @ayanonagon