1

I'm creating a hash map and I'd like to use markers as keys. I set the hash map equality test to "equal", but the last line from the below code is returning ("bye" "fly") instead of ("bazooka" "joe"). Any idea what I'm doing wrong?

(let* ((tmp (make-hash-table :test 'equal))
       (marker-1 (make-marker))
       (marker-2 (make-marker)))
  (set-marker marker-1 1)
  (set-marker marker-2 1)
  (message "markers equal 1?: %s" (equal marker-1 marker-2))
  (message "markerp 1: %s, 2: %s" (markerp marker-1) (markerp marker-2))
  (puthash marker-1 (list "hi") tmp)
  (puthash marker-2 (list "bye" "fly") tmp)
  (message "markers equal 2?: %s" (equal marker-1 marker-2))
  (puthash marker-1 (list "bazooka" "joe") tmp)
  (gethash marker-2 tmp))
Drew
  • 75,699
  • 9
  • 109
  • 225
rclark
  • 113
  • 5
  • 2
    Looks like a bug, to me. The doc says that if the objects are `equal` then their `sxhash-equal` values are the same. But in this case the markers are `equal` and they have different `sxhash-equal` values. If someone doesn't provide a good answer here, consider filing a bug report: `M-x report-emacs-bug`. – Drew May 02 '22 at 15:20
  • 1
    From [(elisp) Defining Hash](https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Hash.html): " If two objects OBJ1 and OBJ2 are ‘equal’, then ‘(sxhash-equal OBJ1)’ and ‘(sxhash-equal OBJ2)’ are the same integer." But that doesn't seem to be true here. – Drew May 02 '22 at 15:30
  • 1
    @Drew Indeed, looking briefly at the source code of 27.2, `sxhash` has a comment about `Lisp_Vectorlike` objects “Others are `equal' if they are `eq'”, but that doesn't match with `internal_equal` which several extra cases with comparisons by value: markers, overlays and window configurations. – Gilles 'SO- stop being evil' May 02 '22 at 16:27
  • 2
    Note that using markers as keys in a hash table with equality by value is fragile: it can only work if the markers' positions don't change throughout the lifetime of the hash table. So you need to make sure the hash table isn't kept around past a call to `set-marker` or a modification of the buffers or other things that can change the markers' positions. – Gilles 'SO- stop being evil' May 02 '22 at 16:29
  • @Gilles'SO-stopbeingevil': Yes, your last comment is a good one. (But I assume the OP was giving a test case, just to ask about the `equal` test when they do have the same position.) – Drew May 02 '22 at 16:36
  • Thanks everyone. For now, I will just use position instead of marker, and consider filing a bug – rclark May 03 '22 at 01:13

1 Answers1

1

I reported this problem to emacs-devel@gnu.org.

@Stefan Monnier replied that this seems to be fixed in the development version (master) for Emacs 29.

Drew
  • 75,699
  • 9
  • 109
  • 225