Is it a good idea to have static (mut) variables in compiler plugin?

I'm designing a compiler plugin which reads external data from disk files, and takes the filename as a #[plugin(...)] parameter. I'm thinking of storing the file handle globally with lazy_static.

I rg'd the rustc source and surprisingly found zero usage of mutable global variables (includes interior mutability). I'm now afraid of this being an anti-pattern. Is it a good idea to store the file handle as global? If not, how should I achieve the thing?

Are you writing or reading the file?

If it is written to that'd be bad as destructors wouldn't run on the global file and so cause buffered writes won't be flushed.

For readonly files that is much less of a problem.

1 Like

Thanks for mentioning the absence of destructors. I'm just using it as an input, so I guess it's fine to use a global.