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

Swiftのレキシカルスコープ管理

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for kntk kntk
May 25, 2026

 Swiftのレキシカルスコープ管理

わいわいswiftc #45

Avatar for kntk

kntk

May 25, 2026

More Decks by kntk

Other Decks in Programming

Transcript

  1. ͸͡Ίʹ let a = 1 do { let a =

    2 print(a) } print(a)
  2. ͸͡Ίʹ let a = 1 do { let a =

    2 print(a) // 2 } print(a) // 1
  3. ͸͡Ίʹ let a = 1 do { let a =

    2 print(a) } print(a) B B
  4. ͸͡Ίʹ let a = 1 if let a = Optional(2)

    { print(a) } guard let a = Optional(3) else { print(a) return } print(a)
  5. ͸͡Ίʹ let a = 1 if let a = Optional(2)

    { print(a) // 2 } guard let a = Optional(3) else { print(a) // 1 return } print(a) // 3
  6. ͸͡Ίʹ let a = 1 if let a = Optional(2)

    { print(a) } guard let a = Optional(3) else { print(a) return } print(a) B B B
  7. /BNF-PPLVQ let a = 1 do { let a =

    2 print(a) } print(a) 2͔͜͜Βݟ͑ΔBͷఆٛ͸ͲΕʁ
  8. /BNF-PPLVQ let a = 1 do { let a =

    2 print(a) } print(a) 2͔͜͜Βݟ͑ΔBͷఆٛ͸ͲΕʁ "͜Ε
  9. 2VBMJGJFE-PPLVQʢΏΔ;Θઆ໌ʣ protocol A {} extension A { static var a:

    Int { 1 } } class B { static let a = 2 } class C: B, A {} 2RVBMJ fi FS$ͷOBNFBΛ୳ͯ͠
  10. 2VBMJGJFE-PPLVQʢΏΔ;Θઆ໌ʣ protocol A {} extension A { static var a:

    Int { 1 } } class B { static let a = 2 } class C: B, A {} 2RVBMJ fi FS$ͷOBNFBΛ୳ͯ͠
  11. 2VBMJGJFE-PPLVQʢΏΔ;Θઆ໌ʣ protocol A {} extension A { static var a:

    Int { 1 } } class B { static let a = 2 } class C: B, A {} 2RVBMJ fi FS$ͷOBNFBΛ୳ͯ͠
  12. -FYJDBM-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) }
  13. -FYJDBM-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } 2ᶃͷҐஔ͔Βݟ͑ΔʮBʯͷఆٛ͸ͲΕͰ͔͢ʁ ᶃ
  14. -FYJDBM-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } 2ᶃͷҐஔ͔Βݟ͑ΔʮBʯͷఆٛ͸ͲΕͰ͔͢ʁ ᶃ "ᶄᶅͷҐஔͷ7BS%FDMͩΑ ᶄ ᶅ
  15. "454DPQF໦ߏ଄ swift -frontend -dump-scope-maps expanded Source.swift ASTSourceFileScope 0x738faf000, [1:1 -

    13:1] 'Source.swift' `-AbstractFunctionDeclScope 0x738fe5ee0, [1:1 - 11:1] 'f()' `-FunctionBodyScope 0x738fe5ee0, [1:10 - 11:1] `-BraceStmtScope 0x73842a900, [1:10 - 11:1] `-PatternEntryDeclScope 0x73842a0f0, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope 0x73842a0f0, [2:13 - 2:13] entry 0 'a' |-IfStmtScope 0x73842a478, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] let a? |-ConditionalClauseInitializerScope, [3:16 - 3:26] `-BraceStmtScope 0x73842a450, [3:28 - 5:5] `-GuardStmtScope 0x73842a7e8, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] let a? |-ConditionalClauseInitializerScope, [6:19 - 6:29] `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope 0x73842a7b8, [6:36 - 9:5] func f() { let a = 1 if let a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) }
  16. "454DPQF໦ߏ଄ ASTSourceFileScope 0x738faf000, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope 0x738fe5ee0, [1:1

    - 11:1] 'f()' `-FunctionBodyScope 0x738fe5ee0, [1:10 - 11:1] `-BraceStmtScope 0x73842a900, [1:10 - 11:1] `-PatternEntryDeclScope 0x73842a0f0, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope 0x73842a0f0, [2:13 - 2:13] entry 0 'a' |-IfStmtScope 0x73842a478, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] let a? |-ConditionalClauseInitializerScope, [3:16 - 3:26] `-BraceStmtScope 0x73842a450, [3:28 - 5:5] `-GuardStmtScope 0x73842a7e8, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] let a? |-ConditionalClauseInitializerScope, [6:19 - 6:29] `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope 0x73842a7b8, [6:36 - 9:5]
  17. "454DPQF໦ߏ଄ ASTSourceFileScope 0x738faf000, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope 0x738fe5ee0, [1:1

    - 11:1] 'f()' `-FunctionBodyScope 0x738fe5ee0, [1:10 - 11:1] `-BraceStmtScope 0x73842a900, [1:10 - 11:1] `-PatternEntryDeclScope 0x73842a0f0, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope 0x73842a0f0, [2:13 - 2:13] entry 0 'a' |-IfStmtScope 0x73842a478, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] let a? |-ConditionalClauseInitializerScope, [3:16 - 3:26] `-BraceStmtScope 0x73842a450, [3:28 - 5:5] `-GuardStmtScope 0x73842a7e8, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] let a? |-ConditionalClauseInitializerScope, [6:19 - 6:29] `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope 0x73842a7b8, [6:36 - 9:5] 4DPQFͷ໊લ
  18. "454DPQF໦ߏ଄ ASTSourceFileScope 0x738faf000, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope 0x738fe5ee0, [1:1

    - 11:1] 'f()' `-FunctionBodyScope 0x738fe5ee0, [1:10 - 11:1] `-BraceStmtScope 0x73842a900, [1:10 - 11:1] `-PatternEntryDeclScope 0x73842a0f0, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope 0x73842a0f0, [2:13 - 2:13] entry 0 'a' |-IfStmtScope 0x73842a478, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] let a? |-ConditionalClauseInitializerScope, [3:16 - 3:26] `-BraceStmtScope 0x73842a450, [3:28 - 5:5] `-GuardStmtScope 0x73842a7e8, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] let a? |-ConditionalClauseInitializerScope, [6:19 - 6:29] `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope 0x73842a7b8, [6:36 - 9:5] ରԠ͢Δ"45ͷ/PEF
  19. "454DPQF໦ߏ଄ ASTSourceFileScope 0x738faf000, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope 0x738fe5ee0, [1:1

    - 11:1] 'f()' `-FunctionBodyScope 0x738fe5ee0, [1:10 - 11:1] `-BraceStmtScope 0x73842a900, [1:10 - 11:1] `-PatternEntryDeclScope 0x73842a0f0, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope 0x73842a0f0, [2:13 - 2:13] entry 0 'a' |-IfStmtScope 0x73842a478, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] let a? |-ConditionalClauseInitializerScope, [3:16 - 3:26] `-BraceStmtScope 0x73842a450, [3:28 - 5:5] `-GuardStmtScope 0x73842a7e8, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] let a? |-ConditionalClauseInitializerScope, [6:19 - 6:29] `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope 0x73842a7b8, [6:36 - 9:5] 4DPQFͷ࡞༻ൣғ
  20. "454DPQF໦ߏ଄ ASTSourceFileScope 0x738faf000, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope 0x738fe5ee0, [1:1

    - 11:1] 'f()' `-FunctionBodyScope 0x738fe5ee0, [1:10 - 11:1] `-BraceStmtScope 0x73842a900, [1:10 - 11:1] `-PatternEntryDeclScope 0x73842a0f0, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope 0x73842a0f0, [2:13 - 2:13] entry 0 'a' |-IfStmtScope 0x73842a478, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] let a? |-ConditionalClauseInitializerScope, [3:16 - 3:26] `-BraceStmtScope 0x73842a450, [3:28 - 5:5] `-GuardStmtScope 0x73842a7e8, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] let a? |-ConditionalClauseInitializerScope, [6:19 - 6:29] `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope 0x73842a7b8, [6:36 - 9:5] ͦͷଞิ଍৘ใ
  21. "454DPQF໦ߏ଄ ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1]

    'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 'a' |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] let a? |-ConditionalClauseInitializerScope, [3:16 - 3:26] `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] let a? |-ConditionalClauseInitializerScope, [6:19 - 6:29] `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5]
  22. "454DPQF໦ߏ଄ ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1]

    'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] func f() { let a = 1 if let a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) }
  23. "454DPQF໦ߏ଄ ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1]

    'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] func f() { let a = 1 if let a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) }
  24. "454DPQF໦ߏ଄ ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1]

    'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] func f() { let a = 1 if let a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) }
  25. "454DPQF໦ߏ଄ ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1]

    'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] func f() { let a = 1 if let a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } *GMFUB͸MFUBͷࢠ
  26. "454DPQF໦ߏ଄ ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1]

    'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] func f() { let a = 1 if let a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } *GMFUBͱHVBSEMFUB͸MFUBͷࢠ *GMFUBͱHVBSEMFUB͸ܑఋ
  27. "454DPQF-PPLVQ static void unqualifiedLookup( SourceFile *sourceFile, const SourceLoc loc, DeclConsumer

    consumer) "45ͷSPPU -PPLVQ͢Δ৔ॴ ܧଓ൑ఆ౳ͷڍಈΛૢ࡞͢Δܕ
  28. "454DPQF-PPLVQ static void unqualifiedLookup( SourceFile *sourceFile, const SourceLoc loc, DeclConsumer

    consumer) "45ͷSPPU -PPLVQ͢Δ৔ॴ ܧଓ൑ఆ౳ͷڍಈΛૢ࡞͢Δܕ $POTVNFSʹ݁Ռ͕౉͞ΕΔ $POTVNFS͕ݕࡧ໊Λ࣋ͪɺܧଓ൑ఆʹར༻͢Δ class AbstractASTScopeDeclConsumer { virtual bool consume(ArrayRef<ValueDecl *> values, NullablePtr<DeclContext> baseDC = nullptr)
  29. "454DPQF-PPLVQ static void unqualifiedLookup( SourceFile *sourceFile, const SourceLoc loc, DeclConsumer

    consumer) "45ͷSPPU -PPLVQ͢Δ৔ॴ ܧଓ൑ఆ౳ͷڍಈΛૢ࡞͢Δܕ static func unqualifiedLookup( sourceFile: SourceFile, at location: SourceLoc, filterBy name: DeclReference ) -> [LookupResult]
  30. "454DPQF-PPLVQ void ASTScopeImpl::unqualifiedLookup( SourceFile *sourceFile, const SourceLoc loc, DeclConsumer consumer)

    { const auto *start = findStartingScopeForLookup(sourceFile, loc); if (start) start->lookup(nullptr, nullptr, consumer); }
  31. "454DPQF-PPLVQ void ASTScopeImpl::unqualifiedLookup( SourceFile *sourceFile, const SourceLoc loc, DeclConsumer consumer)

    { const auto *start = findStartingScopeForLookup(sourceFile, loc); if (start) start->lookup(nullptr, nullptr, consumer); }  MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ  MPPLVQ
  32. "454DPQF-PPLVQ void ASTScopeImpl::unqualifiedLookup( SourceFile *sourceFile, const SourceLoc loc, DeclConsumer consumer)

    { const auto *start = findStartingScopeForLookup(sourceFile, loc); if (start) start->lookup(nullptr, nullptr, consumer); }  MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ  MPPLVQ
  33. "454DPQF-PPLVQ const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup( SourceFile *sourceFile, const SourceLoc loc) {

    auto *fileScope = sourceFile->getScope().impl; (লུ) return fileScope->findInnermostEnclosingScope(loc, nullptr); } MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  34. "454DPQF-PPLVQ const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup( SourceFile *sourceFile, const SourceLoc loc) {

    auto *fileScope = sourceFile->getScope().impl; (লུ) return fileScope->findInnermostEnclosingScope(loc, nullptr); } MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ "45SPPU͔Β"454DPQFSPPUΛऔಘʢ࡞੒PSΩϟογϡʣ
  35. "454DPQF-PPLVQ const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup( SourceFile *sourceFile, const SourceLoc loc) {

    auto *fileScope = sourceFile->getScope().impl; (লུ) return fileScope->findInnermostEnclosingScope(loc, nullptr); } MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ ࠶ؼԼ߱ͰMPPLVQMPDBUJPOΛؚΉ࠷಺TDPQFΛ୳͢ "45SPPU͔Β"454DPQFSPPUΛऔಘʢ࡞੒PSΩϟογϡʣ
  36. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  37. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  38. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  39. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  40. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  41. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  42. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ ෳ਺ࢠ͕͋Δ৔߹͸ೋ෼୳ࡧ
  43. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  44. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  45. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  46. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  47. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ
  48. "454DPQF-PPLVQ void ASTScopeImpl::unqualifiedLookup( SourceFile *sourceFile, const SourceLoc loc, DeclConsumer consumer)

    { const auto *start = findStartingScopeForLookup(sourceFile, loc); if (start) start->lookup(nullptr, nullptr, consumer); }  MPDBUJPO͕ଐ͢Δ*OOFSNPTUTDPQFͷಛఆ  MPPLVQ MPPLVQ
  49. "454DPQF-PPLVQ MPPLVQ void ASTScopeImpl::lookup(const NullablePtr<const ASTScopeImpl> limit, NullablePtr<const GenericParamList> lastListSearched,

    DeclConsumer consumer) const { (লུ) if (lookupLocalsOrMembers(consumer)) return; (লུ) const auto *const lookupParent = getLookupParent().getPtrOrNull(); return lookupParent->lookup(limitForParent, lastListSearched, consumer); }
  50. "454DPQF-PPLVQ MPPLVQ void ASTScopeImpl::lookup(const NullablePtr<const ASTScopeImpl> limit, NullablePtr<const GenericParamList> lastListSearched,

    DeclConsumer consumer) const { (লུ) if (lookupLocalsOrMembers(consumer)) return; (লུ) const auto *const lookupParent = getLookupParent().getPtrOrNull(); return lookupParent->lookup(limitForParent, lastListSearched, consumer); } ਌Λ࠶ؼతʹMPPLVQ
  51. "454DPQF-PPLVQ MPPLVQ void ASTScopeImpl::lookup(const NullablePtr<const ASTScopeImpl> limit, NullablePtr<const GenericParamList> lastListSearched,

    DeclConsumer consumer) const { (লུ) if (lookupLocalsOrMembers(consumer)) return; (লུ) const auto *const lookupParent = getLookupParent().getPtrOrNull(); return lookupParent->lookup(limitForParent, lastListSearched, consumer); } ͜ͷؔ਺Λ֤4DPQF۩৅ܕͰ࣮૷͢Δ
  52. "454DPQF-PPLVQ MPPLVQ1BUUFSO&OUSZ%FDM4DPQF bool PatternEntryDeclScope::lookupLocalsOrMembers(DeclConsumer consumer) const { if (!isLocalBinding) return

    false; return lookupLocalBindingsInPattern(getPattern(), consumer); } bool ASTScopeImpl::lookupLocalBindingsInPattern(const Pattern *p, DeclConsumer consumer) { if (!p) return false; bool isDone = false; p->forEachVariable([&](VarDecl *var) { if (!isDone) { SmallVector<ValueDecl *, 2> vars = { var }; auto abiRole = ABIRoleInfo(var); if (!abiRole.providesABI()) vars.push_back(abiRole.getCounterpart()); isDone = consumer.consume(vars); } }); return isDone; } let a = 1
  53. "454DPQF-PPLVQ MPPLVQ1BUUFSO&OUSZ%FDM4DPQF bool PatternEntryDeclScope::lookupLocalsOrMembers(DeclConsumer consumer) const { if (!isLocalBinding) return

    false; return lookupLocalBindingsInPattern(getPattern(), consumer); } bool ASTScopeImpl::lookupLocalBindingsInPattern(const Pattern *p, DeclConsumer consumer) { if (!p) return false; bool isDone = false; p->forEachVariable([&](VarDecl *var) { if (!isDone) { isDone = consumer.consume({ vars }); } }); return isDone; } 4DPQF͸ରԠ͢Δ"45/PEFΛอ࣋ɾ৘ใநग़ʹར༻ let a = 1
  54. "454DPQF-PPLVQ MPPLVQ1BUUFSO&OUSZ%FDM4DPQF bool PatternEntryDeclScope::lookupLocalsOrMembers(DeclConsumer consumer) const { if (!isLocalBinding) return

    false; return lookupLocalBindingsInPattern(getPattern(), consumer); } bool ASTScopeImpl::lookupLocalBindingsInPattern(const Pattern *p, DeclConsumer consumer) { if (!p) return false; bool isDone = false; p->forEachVariable([&](VarDecl *var) { if (!isDone) { isDone = consumer.consume({ vars }); } }); return isDone; } ϊʔυ͕࣋ͭ7BS%FDMΛશͯDPOTVNFSʹ౉͢ DPOTVNF͕USVFΛฦͤ͹ऴྃɺGBMTF͸ଓߦ let a = 1
  55. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPPLVQ DPOTVNF B
  56. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPPLVQ VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz
  57. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPPLVQ DPOTVNF B VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz
  58. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPPLVQ VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz
  59. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPPLVQ VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz
  60. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPPLVQ VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz
  61. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPPLVQ VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz
  62. "454DPQF-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] MPPLVQ VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz DPOTVNF B DPOTVNF B
  63. "454DPQFHVBSE ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1]

    'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] func f() { let a = 1 if let a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } HVBSEͷCPEZ͕HVBSEMFUBͷࢠʜ 
  64. "454DPQFHVBSE ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1]

    'f()' `-FunctionBodyScope, [1:10 - 11:1] `-BraceStmtScope, [1:10 - 11:1] `-PatternEntryDeclScope, [2:9 - 11:1] entry 0 'a' |-PatternEntryInitializerScope, [2:13 - 2:13] entry 0 |-IfStmtScope, [3:5 - 5:5] `-ConditionalClausePatternUseScope, [3:16 - 5:5] le |-ConditionalClauseInitializerScope, [3:16 - 3:26 `-BraceStmtScope, [3:28 - 5:5] `-GuardStmtScope, [6:5 - 11:1] `-ConditionalClausePatternUseScope, [6:19 - 11:1] l |-ConditionalClauseInitializerScope, [6:19 - 6:29 `-GuardStmtBodyScope, [6:36 - 9:5] `-BraceStmtScope, [6:36 - 9:5] func f() { let a = 1 if let a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } HVBSEͷCPEZ͕HVBSEMFUBͷࢠʜ  HVBSE͚ͩྫ֎తʹQBSFOUͱMPPLVQ1BSFOU͕ҟͳΔ
  65. "454DPQFHVBSE /// The tree is organized by source location and

    for most nodes this is also /// what obtains for scoping. However, guards are different. The scope after /// the guard else must hop into the innermoset scope of the guard condition. virtual NullablePtr<const ASTScopeImpl> getLookupParent() const
  66. "454DPQFTPVSDFSBOHFͷ੍໿ //===----------------------------------------------------------------------===// /// /// This fi le de fi nes

    the ASTScopeImpl class ontology, which /// describes the scopes that exist within a Swift AST. /// /// Each scope has four basic functions: printing for debugging, creation of /// itself and its children, obtaining its SourceRange (for lookup), and looking /// up names accessible from that scope. /// /// Invariants: /// a child's source range is a subset (proper or improper) of its parent's, /// children are ordered by source range, and do not overlap, /// all the names visible within a parent are visible within the child, unless /// the nesting is illegal. For instance, a protocol nested inside of a class /// does not get to see the symbols in the class or its ancestors. /// //===----------------------------------------------------------------------===//
  67. "454DPQFTPVSDFSBOHFͷ੍໿ //===----------------------------------------------------------------------===// /// /// This fi le de fi nes

    the ASTScopeImpl class ontology, which /// describes the scopes that exist within a Swift AST. /// /// Each scope has four basic functions: printing for debugging, creation of /// itself and its children, obtaining its SourceRange (for lookup), and looking /// up names accessible from that scope. /// /// Invariants: /// a child's source range is a subset (proper or improper) of its parent's, /// children are ordered by source range, and do not overlap, /// all the names visible within a parent are visible within the child, unless /// the nesting is illegal. For instance, a protocol nested inside of a class /// does not get to see the symbols in the class or its ancestors. /// //===----------------------------------------------------------------------===// MJC"45"454DPQFI
  68. "454DPQFTPVSDFSBOHFͷ੍໿ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } `-GuardStmtScope, [6:5 - 11:1] ɹ`-ConditionalClausePatternUseScope, [6:19 - 11:1] let ɹɹ|-ConditionalClauseInitializerScope, [6:19 - 6:29] ɹɹ`-GuardStmtBodyScope, [6:36 - 9:5] ɹɹɹ`-BraceStmtScope, [6:36 - 9:5]
  69. "454DPQFTPVSDFSBOHFͷ੍໿ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } `-GuardStmtScope, [6:5 - 11:1] ɹ|-ConditionalClausePatternUseScope, [6:19 - 11:1] let ɹɹ`-ConditionalClauseInitializerScope, [6:19 - 6:29] ɹ`-GuardStmtBodyScope, [6:36 - 9:5] ɹɹ`-BraceStmtScope, [6:36 - 9:5]
  70. "454DPQFTPVSDFSBOHFͷ੍໿ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } `-GuardStmtScope, [6:5 - 11:1] ɹ|-ConditionalClausePatternUseScope, [6:19 - 11:1] let ɹɹ`-ConditionalClauseInitializerScope, [6:19 - 6:29] ɹ`-GuardStmtBodyScope, [6:36 - 9:5] ɹɹ`-BraceStmtScope, [6:36 - 9:5]
  71. "454DPQF-B[JMZ&YQBOTJPO func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift'
  72. "454DPQF-B[JMZ&YQBOTJPO func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' FYQBOE
  73. "454DPQF-B[JMZ&YQBOTJPO func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1]
  74. "454DPQF-B[JMZ&YQBOTJPO func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1] ಛఆͷ/PEFͰల։͕ࢭ·Δʢ%FGFSSFE/PEFʣ
  75. "454DPQF-B[JMZ&YQBOTJPO func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } VORVBMJ fi FE-PPLVQ BU<>  fi MUFS#ZlBz ASTSourceFileScope, [1:1 - 13:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 11:1] 'f()' `-FunctionBodyScope, [1:10 - 11:1]
  76. "454DPQF-B[JMZ&YQBOTJPO struct A { func a() { let a =

    1 } func b() { let a = 1 } func c() { let a = 1 } } ASTSourceFileScope 0xb3ef67000, [1:1 - 14:1] 'Source.swift' `-NominalTypeDeclScope 0xb3ef79eb0, [1:1 - 13:1] 'A' `-NominalTypeBodyScope 0xb3ef79eb0, [1:10 - 13:1] 'A' |-AbstractFunctionDeclScope 0xb3ef8a020, [2:5 - 4:5] 'a()' `-FunctionBodyScope 0xb3ef8a020, [2:14 - 4:5] `-BraceStmtScope 0xb3ef8a268, [2:14 - 4:5] `-PatternEntryDeclScope 0xb3ef8a1f0, [3:13 - 4:5] entry `-PatternEntryInitializerScope 0xb3ef8a1f0, [3:17 - 3: |-AbstractFunctionDeclScope 0xb3ef8a300, [6:5 - 8:5] 'b()' `-FunctionBodyScope 0xb3ef8a300, [6:14 - 8:5] `-BraceStmtScope 0xb3ef8a548, [6:14 - 8:5] `-PatternEntryDeclScope 0xb3ef8a4d0, [7:13 - 8:5] entry `-PatternEntryInitializerScope 0xb3ef8a4d0, [7:17 - 7: `-AbstractFunctionDeclScope 0xb3ef8a5e0, [10:5 - 12:5] 'c()' `-FunctionBodyScope 0xb3ef8a5e0, [10:14 - 12:5] `-BraceStmtScope 0xb3ef8a828, [10:14 - 12:5] `-PatternEntryDeclScope 0xb3ef8a7b0, [11:13 - 12:5] entr `-PatternEntryInitializerScope 0xb3ef8a7b0, [11:17 - 1
  77. 4XJGU-FYJDBM-PPLVQ @_spi(Experimental) public protocol ScopeSyntax: SyntaxProtocol {…} @_spi(Experimental) extension IfExprSyntax:

    ScopeSyntax { … } @_spi(Experimental) extension WhileStmtSyntax: ScopeSyntax { … } …
  78. 4XJGU-FYJDBM-PPLVQ໦ߏ଄ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) }
  79. 4XJGU-FYJDBM-PPLVQ໦ߏ଄ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) }
  80. 4XJGU-FYJDBM-PPLVQ໦ߏ଄ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } ߏจ໦ͱείʔϓ໦ͷߏ଄͕ಉҰͳͷͰਓؒʹ༏͍͠
  81. 4XJGU-FYJDBM-PPLVQ if let a = Optional(2) { // a͕ݟ͑Δ }

    else { // a͕ݟ͑ͳ͍ } ASTSourceFileScope, [1:1 - 11:1] 'Source.swift' `-AbstractFunctionDeclScope, [1:1 - 10:1] 'f()' `-FunctionBodyScope, [1:10 - 10:1] `-BraceStmtScope, [1:10 - 10:1] `-IfStmtScope, [2:5 - 9:5] |-ConditionalClausePatternUseScope, [2:16 - 5: |-ConditionalClauseInitializerScope, [2:16 - `-BraceStmtScope, [2:28 - 5:5] `-BraceStmtScope, [6:10 - 9:5] ໦ߏ଄ʹΑͬͯՄࢹੑΛ੍ޚ *O"454DPQF MPPLVQQPTJUJPOͰಈతʹఆٛͷՄࢹੑΛ੍ޚ
  82. 4XJGU-FYJDBM-PPLVQ if let a = Optional(2) { // a͕ݟ͑Δ }

    else { // a͕ݟ͑ͳ͍ } *O4XJGU-FYJDBM-PPLVQ MPPLVQQPTJUJPOͰಈతʹఆٛͷՄࢹੑΛ੍ޚ
  83. 4XJGU-FYJDBM-PPLVQ @_spi(Experimental) extension IfExprSyntax: ScopeSyntax { @_spi(Experimental) public func lookup(

    _ identifier: Identifier?, at lookUpPosition: AbsolutePosition, with config: LookupConfig ) -> [LookupResult] { if let elseBody, elseBody.range.contains(lookUpPosition) { return lookupInParent(identifier, at: lookUpPosition, with: config) } else { return defaultLookupImplementation(identifier, at: lookUpPosition, with: config) } } MPPLVQQPTJUJPOͰಈతʹఆٛͷՄࢹੑΛ੍ޚ
  84. 4XJGU-FYJDBM-PPLVQ @_spi(Experimental) extension IfExprSyntax: ScopeSyntax { @_spi(Experimental) public func lookup(

    _ identifier: Identifier?, at lookUpPosition: AbsolutePosition, with config: LookupConfig ) -> [LookupResult] { if let elseBody, elseBody.range.contains(lookUpPosition) { return lookupInParent(identifier, at: lookUpPosition, with: config) } else { return defaultLookupImplementation(identifier, at: lookUpPosition, with: config) } } MPPLVQQPTJUJPOͰಈతʹఆٛͷՄࢹੑΛ੍ޚ FMTFCPEZ಺͔ΒͷMPPLVQ͸JGMFUΛݟͤͳ͍
  85. 4XJGU-FYJDBM-PPLVQ-PPLVQ @_spi(Experimental) public protocol ScopeSyntax: SyntaxProtocol { func lookup( _

    identifier: Identifier?, at lookUpPosition: AbsolutePosition, with config: LookupConfig ) -> [LookupResult] }
  86. 4XJGU-FYJDBM-PPLVQ-PPLVQ extension SyntaxProtocol { /// Parent scope of this syntax

    node, or scope introduced by this syntax node. var scope: ScopeSyntax? { if let scopeSyntax = Syntax(self).asProtocol(SyntaxProtocol.self) as? ScopeSyntax { return scopeSyntax } else { return self.parent?.scope } } public func lookup( _ identifier: Identifier?, with config: LookupConfig = LookupConfig() ) -> [LookupResult] { scope?.lookup(identifier, at: self.position, with: config) ?? [] } }
  87. 4XJGU-FYJDBM-PPLVQ-PPLVQ func f() { let a = 1 if let

    a = Optional(2) { print(a) } guard let a = Optional(3) else { print(a) return } print(a) } TPVSDFUPLFO BU<> MPPLVQ lBz
  88. w$PEF#MPDL಺ͷఆٛ͸$PEF#MPDL4DPQF͕؅ཧ 4XJGU-FYJDBM-PPLVQ-PPLVQ @_spi(Experimental) extension CodeBlockSyntax: SequentialScopeSyntax { @_spi(Experimental) public func

    lookup( _ identifier: Identifier?, at lookUpPosition: AbsolutePosition, with config: LookupConfig ) -> [LookupResult] { sequentialLookup( in: statements, identifier, at: lookUpPosition, with: config ) } }
  89. 4XJGU-FYJDBM-PPLVQ-PPLVQ func sequentialLookup(…) -> [LookupResult] { var collectedNamedDecls: [NamedDeclSyntax] =

    [] for codeBlockItem in codeBlockItems.reversed() { if let namedDecl = codeBlockItem.item.asProtocol(NamedDeclSyntax.self) { guard !ignoreNamedDecl else { continue } collectedNamedDecls.append(namedDecl) continue } } for namedDecl in collectedNamedDecls.reversed() { currentChunk += LookupName.getNames( from: namedDecl, accessibleAfter: namedDecl.endPosition ).filter { introducedName in checkIdentifier(identifier, refersTo: introducedName, at: lookUpPosition) } } … }
  90. 4XJGU-FYJDBM-PPLVQ-PPLVQ func sequentialLookup(…) -> [LookupResult] { var collectedNamedDecls: [NamedDeclSyntax] =

    [] for codeBlockItem in codeBlockItems.reversed() { if let namedDecl = codeBlockItem.item.asProtocol(NamedDeclSyntax.self) { guard !ignoreNamedDecl else { continue } collectedNamedDecls.append(namedDecl) continue } } for namedDecl in collectedNamedDecls.reversed() { currentChunk += LookupName.getNames( from: namedDecl, accessibleAfter: namedDecl.endPosition ).filter { introducedName in checkIdentifier(identifier, refersTo: introducedName, at: lookUpPosition) } } … } $PEF#MPDL಺ͷEFDMΛר͖औΔ
  91. 4XJGU-FYJDBM-PPLVQ-PPLVQ func sequentialLookup(…) -> [LookupResult] { var collectedNamedDecls: [NamedDeclSyntax] =

    [] for codeBlockItem in codeBlockItems.reversed() { if let namedDecl = codeBlockItem.item.asProtocol(NamedDeclSyntax.self) { guard !ignoreNamedDecl else { continue } collectedNamedDecls.append(namedDecl) continue } } for namedDecl in collectedNamedDecls.reversed() { currentChunk += LookupName.getNames( from: namedDecl, accessibleAfter: namedDecl.endPosition ).filter { introducedName in checkIdentifier(identifier, refersTo: introducedName, at: lookUpPosition) } } … } ABDDFTTJCMF"GUFSAͰՄࢹੑ৘ใΛ෇༩ ʮ͜ͷQPTJUJPOΑΓޙͰݟ͑Δʯ
  92. 4XJGU-FYJDBM-PPLVQBDDFTTJCMF"GUFS let a = 1 if let a = Optional(2)

    { print(a) } guard let a = Optional(3) else { print(a) return } print(a) B B B
  93. 4XJGU-FYJDBM-PPLVQBDDFTTJCMF"GUFS let a = 1 if let a = Optional(2)

    { print(a) } guard let a = Optional(3) else { print(a) return } print(a) B B B
  94. 4XJGU-FYJDBM-PPLVQ-PPLVQ func sequentialLookup(…) -> [LookupResult] { var collectedNamedDecls: [NamedDeclSyntax] =

    [] for codeBlockItem in codeBlockItems.reversed() { if let namedDecl = codeBlockItem.item.asProtocol(NamedDeclSyntax.self) { guard !ignoreNamedDecl else { continue } collectedNamedDecls.append(namedDecl) continue } } for namedDecl in collectedNamedDecls.reversed() { currentChunk += LookupName.getNames( from: namedDecl, accessibleAfter: namedDecl.endPosition ).filter { introducedName in checkIdentifier(identifier, refersTo: introducedName, at: lookUpPosition) } } … } BDDFTTJCMF"GUFSMPPLVQQPTJUJPO͚ͩϑΟϧλ
  95. ͓·͚2VBMJGJFE-PPLVQ /// Represents result from a speci fi c scope.

    public enum LookupResult { /// Scope and the names that matched lookup. case fromScope(Syntax, withNames: [LookupName]) /// Indicates where to perform member lookup. case lookForMembers(in: Syntax) /// Indicates to lookup generic parameters of extended type. /// /// ### Example /// ```swift /// extension Foo { /// func bar() { /// let a = A() // <-- lookup here /// } /// } /// ``` case lookForGenericParameters(of: ExtensionDeclSyntax) /// Indicates this closure expression could introduce dollar identi fi ers. /// /// ### Example /// ```swift /// func foo() { /// let a = { /// $0 // <-- lookup here /// } /// } /// ``` case lookForImplicitClosureParameters(ClosureExprSyntax)
  96. ͓·͚4XJGU2VBMJGJFE-PPLVQ // In SwiftLexicalLookup result processing for result in someNode.lookup("area",

    with: config) { switch result { case .lookInMembers(let typeSyntax): // Hand off to qualified lookup let memberResults = typeSyntax.qualifiedLookup("area", in: sourceFile) // ... process memberResults default: // ... process unqualified results } }
  97. ग़య w "454DPQF w IUUQTHJUIVCDPNTXJGUMBOHTXJGUUSFFNBJOMJC"45 w 4XJGU-FYJDBM-PPLVQ w IUUQTHJUIVCDPNTXJGUMBOHTXJGUTZOUBYUSFFNBJO4PVSDFT4XJGU-FYJDBM-PPLVQ w

    <(4P$>4XJGU-FYJDBM-PPLVQBOFXMFYJDBMOBNFMPPLVQMJCSBSZ w IUUQTGPSVNTTXJGUPSHUHTPDTXJGUMFYJDBMMPPLVQBOFXMFYJDBMOBNFMPPLVQMJCSBSZ  w <(4P$>2VBMJ fi FE/BNF-PPLVQGPSTXJGUTZOUBY w IUUQTGPSVNTTXJGUPSHUHTPDRVBMJ fi FEOBNFMPPLVQGPSTXJGUTZOUBY