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

CSFixとラベルマッチ

 CSFixとラベルマッチ

omochimetaru

May 22, 2020
Tweet

More Decks by omochimetaru

Other Decks in Programming

Transcript

  1. ϛεͷ͋Δίʔυ1 func f(aa: Int, bb: Int) {} func main() {

    // ❌ `bb:` Λ෇͚๨Ε͍ͯΔ f(aa: 1, 2) } 9
  2. Swift 5.1.5, Swift 5.2.2 (Xcode11.4.1) $ alias swift515='xcrun --toolchain org.swift.515120200323a

    swift' $ swift515 a.swift a.swift:4:6: error: missing argument label 'bb:' in call f(aa: 1, 2) ^ bb: ! 10
  3. ϛεͷ͋Δίʔυ2 func f(aa: Int, bb: Int) {} // fͷΦʔόʔϩʔυ͕௥Ճ͞Ε͍ͯΔ func

    f(cc: Int, dd: Int) {} func main() { // ❌ `bb:` Λ෇͚๨Ε͍ͯΔ f(aa: 1, 2) } 11
  4. Swift 5.1.5 $ swift515 a.swift a.swift:6:5: error: argument labels '(aa:,

    _:)' do not match any available overloads f(aa: 1, 2) ^~~~~~~~~~~ a.swift:6:5: note: overloads for 'f' exist with these partially matching parameter lists: (aa: Int, bb: Int), (cc: Int, dd: Int) f(aa: 1, 2) ^ ! 12
  5. • ܕਪ࿦ʹ͓͍ͯΦʔόʔϩʔυ͸Disjunction Constraintͱ͠ ͯѻΘΕΔ <disjunction> { $T1 <bind OL> (aa:

    Int, bb: Int) -> Void $T1 <bind OL> (cc: Int, dd: Int) -> Void } $T1 <appfn> (aa: Int, _: Int) -> Void 17
  6. Disjunctionۭؒͷ୳ࡧ • ީิ੍໿ΛҰͭͣͭਪ࿦ثʹࢼ͠ʹ౤ೖ͢Δ • શ෦ࣦഊ → Τϥʔ: Ϛον͢ΔΦʔόʔϩʔυ͕ແ͍ • Ұͭ੒ޭ

    → ͦΕΛܕਪ࿦݁Ռͱͯ͠࠾༻ • ෳ਺੒ޭ → ੒ޭͨ͠ղಉ࢜Λൺֱͯ͠ɺ࠷ྑͷ΋ͷΛબ୒ • ൺֱ݁Ռ͕ಉ཰ҰҐ → Τϥʔ: ᐆດͳίʔυ 18
  7. ղͷൺֱ(෮श) func f(_ a: Int?) { print("Int?") } func f(_

    a: Any) { print("Any") } func main() { // 1 ͸ Int? Ͱ΋͋Δ͠ɺ Any Ͱ΋͋Δɻ f(1) } main() 19
  8. ղͷൺֱͷঢ়گͷ؍࡯ $ swift -Xfrontend -debug-constraints b.swift Comparing 2 viable solutions

    --- Solution #0 --- Fixed score: 0 0 0 0 0 0 0 0 1 0 0 0 Type variables: $T0 as (Int?) -> () @ locator@0x7ff8a0808800 [[email protected]:6:5] --- Solution #1 --- Fixed score: 0 0 0 0 0 0 0 0 0 1 0 0 Type variables: $T0 as (Any) -> () @ locator@0x7ff8a0808800 [[email protected]:6:5] 22
  9. ղͷείΞ(ίετ) 0 0 0 0 0 0 0 0 1

    0 0 0 ^ SK_ValueToOptional ͸ Level4 0 0 0 0 0 0 0 0 0 1 0 0 ^ SK_EmptyExistentialConversion ͸ Level3 • Any΁ͷม׵ͷํ͕͍҆ 23
  10. म෮ͷঢ়گͷ؍࡯ // (౎߹্ɺఆٛॱΛೖΕସ͑) func f(cc: Int, dd: Int) {} func

    f(aa: Int, bb: Int) {} func main() { f(aa: 1, 2) } $ swift -Xfrontend -debug-constraints a.swift 28
  11. म෮ͷঢ়گͷ؍࡯ $ swift -Xfrontend -debug-constraints a.swift --- Solution #0 ---

    Fixed score: 4 0 0 0 0 0 0 0 0 0 0 0 Fixes: [fix: re-label argument(s)] @ locator@0x7ff8e08cf100 [[email protected]:4:5 -> apply argument] --- Solution #1 --- Fixed score: 1 0 0 0 0 0 0 0 0 0 0 0 Fixes: [fix: re-label argument(s)] @ locator@0x7ff8e08cf100 [[email protected]:4:5 -> apply argument] 29
  12. Fixed score: 4 0 0 0 0 0 0 0

    0 0 0 0 ^ Level12ͷSK_Fix͕4఺ = ϥϕϧΤϥʔͷجຊ఺͕1఺ + aa: → cc: ͷwrong͕3఺ + _: → dd: ͷmissing͕0఺ Fixed score: 1 0 0 0 0 0 0 0 0 0 0 0 ^ Level12ͷSK_Fix͕1఺ = ϥϕϧΤϥʔͷجຊ఺͕1఺ + _: → bb: ͷmissing͕0఺ • (aa:_:)͔Βͷम෮͸(aa:bb:)ͷํ͕(cc:dd:)ΑΓ͍҆ 30
  13. ܕΤϥʔ΋म෮Ͱ͖Δ func f(aa: String) {} func f(bb: Int) {} func

    main() { // ϥϕϧͷؒҧ͍۩߹͸ಉ཰͚ͩͲɾɾɾ f(cc: 1) } 31
  14. $ swift c.swift c.swift:5:6: error: incorrect argument label in call

    (have 'cc:', expected 'bb:') f(cc: 1) ^~~ bb • (bb: Int) ͷํΛݺͼ͔ͨͬͨͩΖ͏ͱ൑அ͍ͯ͠Δ 32
  15. ܕΤϥʔम෮ͷ؍࡯ --- Solution #0 --- Fixed score: 6 0 0

    0 0 0 0 0 0 0 0 0 Fixes: [fix: re-label argument(s)] @ locator@0x7fc7638cedf8 [[email protected]:5:5 -> apply argument] [fix: allow argument to parameter type conversion mismatch] @ locator@0x7fc7638cee48 [[email protected]:5:5 -> apply argument -> comparing call argument #0 to parameter #0] --- Solution #1 --- Fixed score: 4 0 0 0 0 0 0 0 0 0 0 0 Fixes: [fix: re-label argument(s)] @ locator@0x7fc7638cedf8 [[email protected]:5:5 -> apply argument] 33
  16. • (cc: 1) to (aa: String) ͸ ϥϕϧΤϥʔجຊ఺1఺ + cc:→aa:

    wrong 3఺ + ܕΤϥʔ2఺ Ͱ߹ܭ6఺ • (cc: 1) to (bb: Int) ͸ ϥϕϧΤϥʔجຊ఺1఺ + cc:→bb: wrong 3఺ Ͱ߹ܭ4఺Ͱͪ͜Β͕͍҆ 34
  17. ϥϕϧؒҧ͍ͱॱ൪ؒҧ͍ func f(aa: Int, bb: Int, cc: Int) {} f(aax:

    1, bb: 2, cc: 3) // incorrect argument label in call (have 'aax:bb:cc:', expected 'aa:bb:cc:') f(bb: 2, cc: 3, aa: 1) // argument 'aa' must precede argument 'bb' 39
  18. ϥϕϧ๨Εͱϥϕϧա৒ func f(_ aa: Int, _ bb: Int, cc: Int)

    {} f(1, 2, 3) // missing argument label 'cc:' in call f(1, bb: 2, cc: 3) // extraneous argument label 'bb:' in call 41
  19. Ҿ਺ա৒ͱҾ਺ෆ଍ func f(aa: Int, bb: Int, cc: Int) {} f(aa:

    1, bb: 2, cc: 3, dd: 4) // extra argument 'dd' in call f(aa: 1, bb: 2) // missing argument for parameter 'cc' in call 42
  20. ϥϕϧͳ͠Ҿ਺ͱҾ਺ෆ଍ͷ૊Έ߹Θͤ func f(_ aa: Int, bb: Int, _ cc: Int)

    {} f(bb: 2, 3) // missing argument for parameter #1 in call • bb:Λج४ʹ3Λ_ cc:ʹϚονɺ_ aa:͕ෆ଍ͱ൑அ 43
  21. Մม௕Ҿ਺ func f(aa: Int, bb: Int..., cc: Int) {} f(aa:

    1, bb: 21, 22, cc: 3) f(aa: 1, cc: 3) 44
  22. Մม௕Ҿ਺ͱϥϕϧͳ͠Ҿ਺ func f(aa: Int, bb: Int..., _ cc: Int) {}

    // a parameter following a variadic parameter requires a label • Մม௕Ҿ਺ͷޙଓ͸ϥϕϧ͕ඞਢ • ҎԼͷ৔߹ʹ3͕ᐆດʹͳΔ͔ΒͩΖ͏ f(aa: 1, bb: 2, 3) 45
  23. Մม௕Ҿ਺͸1ͭ·Ͱ func f(aa: Int..., bb: Int...) {} // only a

    single variadic parameter '...' is permitted • ͜ͷ੍ݶΛ֎͢ఏҊ͕͋Δ • Lifting the 1 variadic param per function restriction https://forums.swift.org/t/lifting-the-1-variadic- param-per-function-restriction/33787 46
  24. σϑΥϧτ෇͖Ҿ਺ func f(aa: Int, bb: Int = 0, _ cc:

    Int) {} f(aa: 1, bb: 2, 3) f(aa: 1, 3) 47
  25. ϥϕϧແ͠σϑΥϧτ෇͖Ҿ਺ func f(aa: Int, _ bb: Int = 0, _

    cc: Int) {} f(aa: 1, 2, 3) // OK f(aa: 1, 3) ΍ͬͯΈͯʂ 48
  26. ػೳ͠ͳ͍ϥϕϧແ͠σϑΥϧτ෇͖Ҿ਺ func f(aa: Int, _ bb: Int = 0, _

    cc: Int) {} f(aa: 1, 2, 3) // OK f(aa: 1, 3) // missing argument for parameter #3 in call • σϑΥϧτ෇͖ϥϕϧແ͠Ҿ਺ʹɺϥϕϧແ͠Ҿ਺͕ଓ͘ͱɺ ͜ͷσϑΥϧτ஋͕࢖ΘΕΔࣄ͸ͳ͍ • ޙଓͷϥϕϧແ͠Ҿ਺͚ͩ౉ͨͯ͘͠΋ɺ खલͷσϑΥϧτ෇͖Ҿ਺ʹٵ͍ࠐ·ΕΔ 49
  27. ϥϕϧ෇͖σϑΥϧτ෇͖Ҿ਺ func f(aa: Int, bb: Int = 0, bb cc:

    Int) {} f(aa: 1, bb: 2, bb: 3) // OK f(aa: 1, bb: 3) ΍ͬͯΈͯʂ 50
  28. ػೳ͠ͳ͍ϥϕϧ෇͖σϑΥϧτ෇͖Ҿ਺ func f(aa: Int, bb: Int = 0, bb cc:

    Int) {} f(aa: 1, bb: 2, bb: 3) // OK f(aa: 1, bb: 3) // missing argument for parameter 'bb' in call • ಉ͡ϥϕϧΛ࢖͏ࣄ͕Ͱ͖ΔͷͰɺ σϑΥϧτ෇͖ϥϕϧ෇͖Ҿ਺ʹɺಉ͡ϥϕϧҾ਺͕ଓ͘ͱɺ ΍͸Γ࢖ΘΕͳ͍σϑΥϧτ஋͕ੜ͡Δ 51
  29. Մม௕Ҿ਺ͷޙଓͷػೳ͠ͳ͍ϥϕϧ෇͖σϑΥ ϧτ෇͖Ҿ਺ func f(aa: Int, bb: Int..., cc: Int =

    0, _ dd: Int) {} f(aa: 1, bb: 2, cc: 3, 4) // OK f(aa: 1, bb: 2, 4) // missing argument for parameter #4 in call • Մม௕Ҿ਺ͷޙʹσϑΥϧτ෇͖Ҿ਺Λஔ͍ͯɺ ͞Βʹͦͷޙʹϥϕϧແ͠Ҿ਺Λஔ͘ͱɺ ػೳ͠ͳ͍σϑΥϧτ஋͕ੜ͡Δ • σϑΥϧτ෇͖Ҿ਺Λলུͨ͠৔߹ɺ Մม௕Ҿ਺ͷޙଓͷ஋΋Մม௕Ҿ਺ʹٵ͍ࠐ·ΕΔͨΊɺ ϥϕϧແ͠Ҿ਺ʹ஋Λ༩͑Δࣄ͕Ͱ͖ͳ͍ 52
  30. ෆਖ਼ͳ຤ඌΫϩʔδϟ౉͠ func f(aa: Int, bb: Int, cc: Int) {} f(aa:

    1, bb: 2) { } // trailing closure passed to parameter of type 'Int' that does not accept a closure 54
  31. δΣωϦΫεͱ຤ඌΫϩʔδϟ func f<T>(type: T.Type, aa: Int, bb: Int, cc: T)

    {} f(type: (() -> Void).self, aa: 1, bb: 2) { } 55
  32. δΣωϦΫεͱෆਖ਼ͳ຤ඌΫϩʔδϟ౉͠ func f<T>(type: T.Type, aa: Int, bb: Int, cc: T)

    {} f(type: Int.self, aa: 1, bb: 2) { } // cannot convert value of type '() -> ()' to expected argument type 'Int' • ຤ඌΫϩʔδϟࣗମ͸ڐ༰͞Εͯ͠·ͬͯɺͦͷઌͰܕΤ ϥʔʹͳΔ 56
  33. ຤ඌΫϩʔδϟͱޙଓͷෳ਺ͷσϑΥϧτ෇͖Ҿ ਺ func f(aa: Int, bb: () -> Void, cc:

    Int = 0, dd: Int = 0) {} f(aa: 1) { } // trailing closure passed to parameter of type 'Int' that does not accept a closure // missing argument for parameter 'bb' in call • Ͱ͖ͳ͍(ͳΜͰ΍) • ๻͕PRΛग़ͯ͠Ϛʔδ͞Εɺ࣮૷͞Εͨ https://github.com/apple/swift/pull/29845 • ͔͠͠ɺ͍Ζ͍Ζ͋ͬͯrevert͞Εͨ ! https://github.com/apple/swift/pull/30656 62
  34. Մม௕Ҿ਺ͱ຤ඌΫϩʔδϟ func f(aa: Int, bb: Int, cc: () -> Void...)

    {} f(aa: 1, bb: 2) {} // OK f(aa: 1, bb: 2, cc: {}, {}) // OK f(aa: 1, bb: 2, cc: {}) {} ΍ͬͯΈͯʂ 63
  35. Մม௕Ҿ਺ͱ຤ඌΫϩʔδϟ func f(aa: Int, bb: Int, cc: () -> Void...)

    {} f(aa: 1, bb: 2) {} // OK f(aa: 1, bb: 2, cc: {}, {}) // OK f(aa: 1, bb: 2, cc: {}) {} // extra argument 'cc' in call • ຤ඌΫϩʔδϟͷϚον͕͞ΕΔͱՄม௕ػೳ͕ޮ͔ͳ͍ • όάʁ 64
  36. ϥϕϧͷtypoݕग़Ϛον func f(aa: Int, bb: Int, cc: Int) {} f(aa:

    1, dd: 4, bbx: 2, cc: 3) // extra argument 'dd' in call f(aa: 1, dd: 4, ee: 5, cc: 3) // extra arguments at positions #2, #3 in call // missing argument for parameter 'bb' in call • ฤूڑ཭͕͋Δ৚݅Λຬͨ࣌͢ɺtypoѻ͍͢Δ • 1݅໨͸bbx:͕bb:ͷtypoͩͱ൑ఆ͞Ε͍ͯΔͷͰɺ2݅໨ͱ͸ҟͳΔΤϥʔʹͳ Δ 65
  37. ෳ਺ͷ຤ඌΫϩʔδϟ UIView.animate(withDuration: 0.3) { self.view.alpha = 0 } completion: {

    _ in self.view.removeFromSuperview() } • ࠷ۙAccept͞ΕͨSE-0279 • ͞ΒʹෳࡶʹͳͬͨͶʂ ! 66
  38. ෆ଍Ҿ਺ͷಛघڍಈ func f(_ aa: Int, _ bb: Int) {} f(1)

    // missing argument for parameter #2 in call • ͜Ε͸ී௨ 67
  39. ෆ଍Ҿ਺ͷಛघڍಈ func f(_ aa: String, _ bb: Int) {} f(1)

    // missing argument for parameter #1 in call • Ҿ਺ͷ1͕_ bb:ʹ౉͞Εͨࣄʹͳ͍ͬͯΔ • ϚονϯάͰܕΛݟ͍ͯΔʁ 69
  40. ෆ଍Ҿ਺ͷಛघڍಈ func f(_ aa: String, _ bb: Int, cc: Int)

    {} f(1, cc: 3) // missing argument for parameter #2 in call // cannot convert value of type 'Int' to expected argument type 'String' • 3Ҿ਺ͩͱൃಈ͠ͳ͍(ͳΜͰ΍) 72
  41. ෆ଍Ҿ਺ͷಛघڍಈ func f(aa: String, bb: Int) {} f(aa: 1) //

    missing argument for parameter 'aa' in call • ϥϕϧ͕͋ͬͯ΋ൃಈ͢Δ • ҙຯෆ໌ͳৼΔ෣͍ʹͳΔɺόάʁ 74
  42. ෆ଍Ҿ਺ͷಛघڍಈ func f(aa: String = "", bb: Int) {} f(aa:

    1) // missing argument for parameter 'aa' in call • σϑΥϧτҾ਺Λ༩͑ΔࣄͰ׬શʹҙຯෆ໌ʹ 76