Inspiration

Generating Diagnostics for Unintentional Optionals in String Interpolation https://github.com/apple/swift/pull/5248/files

I'd like to learn to implement similar diagnostics checks.

What it does

compiler generates warnings for with some specific special conditions:

  • try Swift()
  • try? Swift()

but does not generate warnings for this case:

  • try! Swift()

(It's not a simple string or regex matching!) Swift is not always literally Swift. but conforming to SwiftConf protocol is needed to be checked by this diagnostics.

A full example input swift file.

protocol Conf {}

struct Swift: Conf {
  init() throws {  }
}

struct Riko {
  init() throws {  }
}

_ = try Swift() // diagnostics warning: use try! Swift
_ = try? Swift() // diagnostics warning: use try! Swift
_ = try! Swift()
_ = try? Riko()

func f() throws {
  _ = try Swift() // diagnostics warning: use try! Swift
  _ = try? Swift() // diagnostics warning: use try! Swift
  _ = try! Swift()
  _ = try? Riko()
}
/Users/banjun/Downloads/tryswiftchecked.swift:11:5: warning: use try! Swift
_ = try Swift()
    ^~~~~~~~~~~
/Users/banjun/Downloads/tryswiftchecked.swift:12:5: warning: use try! Swift
_ = try? Swift()
    ^~~~~~~~~~~~
/Users/banjun/Downloads/tryswiftchecked.swift:17:7: warning: use try! Swift
  _ = try Swift()
      ^~~~~~~~~~~
/Users/banjun/Downloads/tryswiftchecked.swift:18:7: warning: use try! Swift
  _ = try? Swift()
      ^~~~~~~~~~~~

How I built it

  • clone apple/swift
  • build
  • understand the PR in the inspiration section
  • try, try, try implementation with learning methods around the diagnostics

Challenges I ran into

  • learn to walk through swift expressions
  • understand what informations can be used at that point
  • use protocol conformance for diagnostics
  • C++

Accomplishments that I'm proud of

  • it works

What I learned

  • how to generate diagnostics on compile time based on swift expression type and protocols
  • swift build is too slow for preparing for hackathon

What's next for Extra typechecks or simple diagnostics on Swift code

  • fix-it
  • provide diagnostics for everyone(?), for a team, or only for me (very specific condition)!

Built With

  • apple/swift
Share this project:
×

Updates