From af07e726a7573304b957a3712984f6ecf6167872 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 10 Aug 2023 22:52:42 -0700 Subject: unuse-sym: fix in face of use-sym-as. * lib.c (unuse_sym): A used symbol may now appear in a package under a different name. So if we don't find a symbol under the symbol's name, or find a different symbol, we must try a reverse hash search before giving up. * txr.1: Add notes to use-sym-as that unuse-sym must be used to undo its effect. Add notes to unuse-sym discussing similarities and differences versus unintern. * tests/012/use-as.tl: New test cases. --- lib.c | 10 ++++++++++ tests/012/use-as.tl | 8 ++++++++ txr.1 | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/lib.c b/lib.c index d7a7ce66..e5d01387 100644 --- a/lib.c +++ b/lib.c @@ -7359,6 +7359,16 @@ val unuse_sym(val symbol, val package_in) val visible = cdr(found_visible); val hidden = cdr(found_hidden); + if (!found_visible || visible != symbol) { + name = hash_revget(package->pk.symhash, symbol, eq_f, identity_f); + if (name) { + found_visible = gethash_e(self, package->pk.symhash, name); + found_hidden = gethash_e(self, package->pk.hidhash, name); + visible = cdr(found_visible); + hidden = cdr(found_hidden); + } + } + if (!found_visible || visible != symbol) return nil; diff --git a/tests/012/use-as.tl b/tests/012/use-as.tl index 681a968d..eb736d9d 100644 --- a/tests/012/use-as.tl +++ b/tests/012/use-as.tl @@ -29,3 +29,11 @@ (use-sym-as 3 '#:foo) :error (use-sym-as 'ldraw 3) :error (use-sym-as 'x 'x) x) + +(mtest + (find-symbol "ldraw") lottery:draw + (find-symbol "gdraw") graphics:draw + (unuse-sym 'ldraw) lottery:draw + (unuse-sym 'gdraw) graphics:draw + (find-symbol "ldraw") nil + (find-symbol "gdraw") nil) diff --git a/txr.1 b/txr.1 index 9a7f8886..61355fff 100644 --- a/txr.1 +++ b/txr.1 @@ -67308,6 +67308,15 @@ clauses and .codn :use-syms-as . +Note: +if +.code use-sym-as +is used to introduce a foreign symbol into a package under a different +name, that symbol cannot be removed with +.codn unintern . +It can only be removed using +.codn unuse-sym . + .coNP Function @ unuse-sym .synb .mets (unuse-sym < symbol <> [ package ]) @@ -67355,6 +67364,39 @@ then .meta symbol itself is returned. +There are close similarities between the function +.code unintern +and +.codn unuse-sym , +but the two are significantly different. + +Firstly, +.code unuse-sym +cannot be used to remove a symbol from its home package. As noted +above, this requires +.codn unintern . + +Secondly, +.code unuse-sym +can be used to undo the effect of +.code use-sym-as +whereby a foreign symbol is introduced into a package under a +different name. If +.meta symbol +is not found under its name, +.code unuse-sym +will search the package for that symbol to discover whether it is +present under a different name, and proceed with the removal +using that name. The +.code unintern +function performs no such secondary check; if +.meta symbol +is not found in the package under its own name, the operation fails, +and so +.code unintern +cannot be used for undoing the effect of +.codn use-sym-as . + .coNP Functions @ use-package and @ unuse-package .synb .mets (use-package < package-list <> [ package ]) -- cgit v1.2.3