Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guile deprecation warning due to bit-count #79

Closed
attila-lendvai opened this issue Feb 24, 2023 · 9 comments · Fixed by #81
Closed

Guile deprecation warning due to bit-count #79

attila-lendvai opened this issue Feb 24, 2023 · 9 comments · Fixed by #81

Comments

@attila-lendvai
Copy link
Contributor

attila-lendvai commented Feb 24, 2023

here:

(cond-expand

the issue is that the reference to bit-count is not eliminated completely at expand time, it's only guarded by a runtime if.

not the end of the world, but due to this Shepherd services in Guix print this enormous warning, mutiple times:

Some deprecated features have been used.  Set the environment
variable GUILE_WARN_DEPRECATED to "detailed" and rerun the
program to get more information.  Set it to "no" to suppress
this message.
@attila-lendvai
Copy link
Contributor Author

attila-lendvai commented Feb 24, 2023

FTR, the current (apparently failed?) attempt at fixing it was discussed in: #66

@mwette
Copy link

mwette commented Feb 24, 2023

What about replacing guile-3 case of bit-count with (module-variable (resolve-module '(guile)) 'bit-count)?

@attila-lendvai
Copy link
Contributor Author

unfortunately guile is too smart:

x.scm:

#!/usr/bin/env -S guile --no-auto-compile -e 'main' -s
!#

(define (main cli-args)
  ;;(pk (bit-count #t #*101))
  (pk ((variable-ref (module-variable (resolve-module '(guile)) 'bit-count)) #t #*101))
  )
$ export GUILE_WARN_DEPRECATED=detailed
$ /tmp/x.scm 
bit-count is deprecated.  Use bitvector-count, or a loop over array-ref if array support is needed.
scm_bitvector_length is deprecated.  Use scm_c_bitvector_length instead.

;;; (2)

@mwette
Copy link

mwette commented Feb 24, 2023

This code in bitv.scm

(define bitvector-count*
  (if (defined? 'bitvector-count)
      bitvector-count
      (lambda (v)
        ((module-ref (resolve-module '(guile)) 'bit-count) #t v))))

(display (version)) (newline)
(display (bitvector-count* #*101)) (newline)

gives

$ GUILE_WARN_DEPRECATED_DETAILED=detailed guile bitv.scm
3.0.8
2

And on freshly built 3.0.2:

$ GUILE_WARN_DEPRECATED_DETAILED=detailed guile bitv.scm
3.0.2
2

EDIT: There was a bug in the proposed sol'n above: module-variable should be module-ref. [fixed]

@mwette
Copy link

mwette commented Feb 24, 2023

The above might be slower, but only for 3.0, to 3.0.2.

scheme@(guile-user)> ,optimize (lambda (v) ((module-ref (resolve-module '(guile)) 'bit-count) #t v)) 
$1 = (lambda (v)
  ((module-ref (resolve-module '(guile)) 'bit-count)
   #t
   v))

@attila-lendvai
Copy link
Contributor Author

FTR, this in a file:

(pk ((module-ref (resolve-module '(guile)) 'bit-count) #t #*101))

still leads to this:

$ guile --version
guile (GNU Guile) 3.0.9
$ GUILE_WARN_DEPRECATED=detailed guile /tmp/x.scm 
bit-count is deprecated.  Use bitvector-count, or a loop over array-ref if array support is needed.
scm_bitvector_length is deprecated.  Use scm_c_bitvector_length instead.

;;; (2)
$

what we need here is a cond-expand* that dispatches at macroexpand-time:

(define-syntax cond-expand*
  (lambda (x)
    (define (condition-matches? condition)
      (syntax-case condition (and or not)
        ((and c ...)
         (and-map condition-matches? #'(c ...)))
        ((or c ...)
         (or-map condition-matches? #'(c ...)))
        ((not c)
         (if (condition-matches? #'c) #f #t))
        (c
         (eval #'c (current-module)))))

    (define (match clauses alternate)
      (syntax-case clauses ()
        (((condition form ...) . rest)
         (if (condition-matches? #'condition)
             #'(begin form ...)
             (match #'rest alternate)))
        (() (alternate))))

    (syntax-case x (else)
      ((_ clause ... (else form ...))
       (match #'(clause ...)
         (lambda ()
           #'(begin form ...))))
      ((_ clause ...)
       (match #'(clause ...)
         (lambda ()
           (syntax-violation 'cond-expand* "unfulfilled cond-expand*" x)))))))

(define bitvector-count*
  (cond-expand*
   ((defined? 'bitvector-count)
    bitvector-count)
   (else
    (lambda (v) (bit-count #t v)))))

(pk (bitvector-count* #*101))

i'm just not sure where to add it:

  1. lobby for changing cond-expand in guile
  2. lobby for adding cond-expand* to guile
  3. or open a PR for fibers that adds it as a local utility.

@attila-lendvai
Copy link
Contributor Author

or this should also work:

(cond-expand*
 ((not (defined? 'bitvector-count))
  (define (bitvector-count v)
    (bit-count #t v)))
 (else
  (values)))

@civodul
Copy link
Collaborator

civodul commented Apr 18, 2023

@attila-lendvai Or maybe just something like:

(define bitvector-count
  (or (and=> (module-variable the-scm-module  'bitvector-count) variable-ref)
      (lambda (v) (bit-count #t v))))

@attila-lendvai
Copy link
Contributor Author

attila-lendvai commented Apr 19, 2023

@civodul strangely enough that works indeed!

i thought the deprecation warning trap is installed at reading the variable, but your solution would trigger that, too. maybe the trap is ignored at toplevel-form-evaluation/compilation-time?

either way, thanks for pointing out a much simpler solution!

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 a pull request may close this issue.

3 participants