kowainik / stan

🕵️ Haskell STatic ANalyser
https://kowainik.github.io/projects/stan
Mozilla Public License 2.0
565 stars 48 forks source link

Don't consider embedFile to be unsafe #356

Open DanBurton opened 4 years ago

DanBurton commented 4 years ago

Stan helpfully considers unsafePerformIO to be unsafe.

However, stan is marking uses of the Template Haskell macro embedFile as unsafe. This is because at compile time, the macro expands to an expression which includes unsafePerformIO. However, usage of this function is completely runtime safe, because file embedding happens at compile time, and unsafePerformIO is only used to efficiently allocate the embedded ByteString.

Can there be a way for me to tell stan that uses of embedFile are safe, while still getting warned about other uses of unsafePerformIO?

chshersh commented 4 years ago

Unfortunately, we can't do a lot with this at the moment. Stan uses the HIE files for its analysis, and the HIE files don't keep the information about original splices, they only contain the expanded TH splices with some very generic annotations. Annotations do tell that the unsafePerformIO is from some TH splice in this particular case, but they don't tell that it is from embedFile exactly. So the only thing that we can do is to not warn on unsafe functions in all TH splices. Will that work for you?

However, once GHC is improved and is capable of providing more context, it should be possible to improve Stan analysis even further and cover such cases. I've created a note about this possible improvement opportunity in our Backlog of GHC features that can help Stan to provide better analysis:

Current workaround is to disable STAN-0212 only in modules with embedFile in .stan.toml config (or alternatively via CLI):

[[check]]
type = "Exclude"
file = "src/Module/With/EmbedFile.hs"
id   = "STAN-0212"
DanBurton commented 4 years ago

So the only thing that we can do is to not warn on unsafe functions in all TH splices. Will that work for you?

Yes, that would work for me.

Current workaround is to disable STAN-0212 only in modules with embedFile in .stan.toml config (or alternatively via CLI)

Thank you for the info!