Skip to content

Commit 689a723

Browse files
committed
Warn user about precompilation of symbolic units
1 parent 7d229fd commit 689a723

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/symbolic_dimensions.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ module SymbolicUnitsParse
240240
import ...DEFAULT_VALUE_TYPE
241241
import ...DEFAULT_DIM_BASE_TYPE
242242

243+
_is_precompiling() = ccall(:jl_generating_output, Cint, ()) == 1
244+
243245
# Lazily create unit symbols (since there are so many)
244246
module Constants
245247
import ..CONSTANT_SYMBOLS
@@ -249,12 +251,14 @@ module SymbolicUnitsParse
249251
import ..Quantity
250252
import ..DEFAULT_VALUE_TYPE
251253
import ..DEFAULT_DIM_BASE_TYPE
254+
import .._is_precompiling
252255

253256
import ...Constants as EagerConstants
254257

255258
const CONSTANT_SYMBOLS_EXIST = Ref{Bool}(false)
256259
const CONSTANT_SYMBOLS_LOCK = Threads.SpinLock()
257260
function _generate_unit_symbols()
261+
_is_precompiling() && return nothing
258262
CONSTANT_SYMBOLS_EXIST[] || lock(CONSTANT_SYMBOLS_LOCK) do
259263
CONSTANT_SYMBOLS_EXIST[] && return nothing
260264
for unit in setdiff(CONSTANT_SYMBOLS, SYMBOL_CONFLICTS)
@@ -273,7 +277,19 @@ module SymbolicUnitsParse
273277

274278
const UNIT_SYMBOLS_EXIST = Ref{Bool}(false)
275279
const UNIT_SYMBOLS_LOCK = Threads.SpinLock()
276-
function _generate_unit_symbols()
280+
function _generate_unit_symbols(; testing=Val(false))
281+
if _is_precompiling() || testing === Val(true)
282+
@warn (
283+
"Creating SymbolicDimensions objects during precompilation is not allowed, "
284+
* "as symbolic units are not created until the first runtime call. "
285+
* "You should use regular `Dimensions` instead for computations, "
286+
* "and generally use `SymbolicDimensions` for display purposes. "
287+
* "If needed, you can manually create `SymbolicDimensions` by calling "
288+
* "the constructor explicitly, such as `Quantity(1.5, SymbolicDimensions; km=1, s=-1)`, "
289+
* "which is equivalent to `us\"km/s\"`."
290+
)
291+
return nothing
292+
end
277293
UNIT_SYMBOLS_EXIST[] || lock(UNIT_SYMBOLS_LOCK) do
278294
UNIT_SYMBOLS_EXIST[] && return nothing
279295
for unit in UNIT_SYMBOLS

test/unittests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,14 @@ end
589589
sym5 = dimension(us"km/s")
590590
VERSION >= v"1.8" &&
591591
@test_throws "rad is not available as a symbol" sym5.rad
592+
593+
# Extra test coverage
594+
@test_warn(
595+
"Creating SymbolicDimensions objects",
596+
DynamicQuantities.SymbolicUnitsParse._generate_unit_symbols(
597+
testing=Val(true)
598+
)
599+
)
592600
end
593601

594602
@testset "Test ambiguities" begin

0 commit comments

Comments
 (0)