Skip to content

Refuse install_unit() before anything is mapped - #447

Closed
vorpalvorpal wants to merge 2 commits into
r-quantities:mainfrom
vorpalvorpal:refuse-install-before-mapping
Closed

vorpalvorpal wants to merge 2 commits into
r-quantities:mainfrom
vorpalvorpal:refuse-install-before-mapping

Conversation

@vorpalvorpal

@vorpalvorpal vorpalvorpal commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #448.

A refused install_unit() leaves the new symbol or name mapped to the existing unit, and remove_unit() on that leftover then unmaps the existing unit's own symbol or name:

library(units)
install_unit("zzdeg", "1 degree")
#> Error : Unit already maps to "°"
set_units(1, "zzdeg")
#> 1 [°]          # the refused call left `zzdeg` mapped
remove_unit(symbol = "zzdeg")
set_units(1, "degree")
#> 1 [��]         # degree has lost its symbol

ud_map_symbols() maps each symbol to the unit and only then calls ut_map_unit_to_symbol(), which is the step that fails; ud_map_names() has the same order.

This PR makes install_unit() check everything before it maps anything. A new ud_check_free(symbol, name, ut_unit) refuses when

  • the unit already has another symbol or another name, or
  • one of the symbols or names already belongs to a different unit.

The messages are the ones raised today; they now come before any mapping, so a refused call leaves nothing to remove. Nothing changes for calls that succeed.

The new test covers a refused symbol, a refused name, a refused name with a symbol in the same call, and a refused later symbol in a vector. It fails on main and passes here. The suite passes on main at a823fef built against udunits 2.2.28 (macOS arm64, R 4.4.3): 118 tests, 0 failing, 6 skipped.

🤖 Generated with Claude Code

https://claude.ai/code/session_01RzNVuCMZyYuDk4M1NMkB5Z

A refused install_unit() used to leave the new symbol or name mapped
to the existing unit. remove_unit() on that leftover then unmapped
the existing unit's own symbol or name, so that e.g. degree lost its
symbol for the rest of the session.

install_unit() now runs every check first: the unit must not already
have another symbol or name, and no symbol or name may belong to a
different unit. The messages are the ones raised before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RzNVuCMZyYuDk4M1NMkB5Z
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RzNVuCMZyYuDk4M1NMkB5Z
@Enchufa2

Copy link
Copy Markdown
Member

Thanks a lot for this, @vorpalvorpal, and for the very clear reproducible example in #448! Looking into it, we found several related issues in how units are installed and removed (refusals leaving partial mappings depending on the unit's encodings, and remove_unit() stripping the symbol of existing units, also through aliases), so we are redesigning this part as a whole in #449. Closing this one in favor of that PR.

@Enchufa2 Enchufa2 closed this Nov 13, 2026
@vorpalvorpal
vorpalvorpal deleted the refuse-install-before-mapping branch November 13, 2026 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A refused install_unit() leaves the symbol mapped, and remove_unit() then strips the existing unit's symbol

2 participants