SymbolicML / DynamicQuantities.jl

Efficient and type-stable physical quantities in Julia
https://symbolicml.org/DynamicQuantities.jl/dev/
Apache License 2.0
120 stars 15 forks source link

u"Ω" errors in precompilation #105

Closed YingboMa closed 5 months ago

YingboMa commented 6 months ago

MWE: write the literalu"Ω" in a package that precompiles.

caused by: LoadError: Evaluation into the closed module `UnitsParse` breaks incremental compilation because the side effects will not be permanent. This is likely due to some other module mutating `UnitsParse` with `eval` during precompilation - don't do this.
Stacktrace:
  [1] eval
    @ Core ./boot.jl:385 [inlined]
  [2] eval
    @ DynamicQuantities.UnitsParse ~/.julia/packages/DynamicQuantities/5QflN/src/uparse.jl:1 [inlined]
  [3] uparse(s::String)
    @ DynamicQuantities.UnitsParse ~/.julia/packages/DynamicQuantities/5QflN/src/uparse.jl:37
  [4] var"@u_str"(__source__::LineNumberNode, __module__::Module, s::Any)
    @ DynamicQuantities.UnitsParse ~/.julia/packages/DynamicQuantities/5QflN/src/uparse.jl:57
...
MilesCranmer commented 6 months ago

As a quick fix you could do import DynamicQuantities.Units: Ω (or .SymbolicUnits: Ω) and work with the symbols directly.

It's weird that precompilation doesn't like the string version. Does anything in that module jump out to you? This is the file:

https://github.com/SymbolicML/DynamicQuantities.jl/blob/a31609fa9f732ded5a863b0f9a65211d78b7d889/src/uparse.jl#L1

YingboMa commented 6 months ago

It's probably due to the eval in https://github.com/SymbolicML/DynamicQuantities.jl/blob/a31609fa9f732ded5a863b0f9a65211d78b7d889/src/uparse.jl#L37

MilesCranmer commented 6 months ago

Oh I see. What other options are there for implementing this?

YingboMa commented 6 months ago

You can evaluate the expression yourself instead of eval.

MilesCranmer commented 6 months ago

Ah. Of course 🤦‍♂️

Thanks, will fix.

MilesCranmer commented 6 months ago

I guess we’ll need to do something a bit more complex like https://github.com/PainterQubits/Unitful.jl/blob/a09bdaac255d456e6315b77979a48956aca1d3a3/src/user.jl#L692 to get this working without eval.

Working on this in #106.

Edit: fixed!