令人困惑的SBCL编译器消息

时间:2017-04-28 23:06:47

标签: sorting common-lisp compiler-warnings sbcl

我收到了SBCL编译器警告消息,我不明白:

; file: D:/Users Data/Dave/SW Library/AI/Planning/DAB Planner/support.lisp
; in: DEFUN UPDATE-HAPPENINGS
;     (SORT PLANNER-PKG::HAP-UPDATES #'< :KEY #'CAR)
; ==>
;   (SB-IMPL::STABLE-SORT-LIST LIST
;                              (SB-KERNEL:%COERCE-CALLABLE-TO-FUN
;                               SB-C::PREDICATE)
;                              (IF SB-C::KEY
;                                  (SB-KERNEL:%COERCE-CALLABLE-TO-FUN SB-C::KEY)
;                                  #'IDENTITY))
;
; caught STYLE-WARNING:
;   The return value of STABLE-SORT-LIST should not be discarded.
;
; caught STYLE-WARNING:
;   The return value of STABLE-SORT-LIST should not be discarded.

生成它的源函数如下:

(defun update-happenings (state act-state)
  "Updates act-state with happenings up through the action completion time
  for all happenings, and checks for constraint violation along the way."
  (declare (problem-state state act-state) (special *happenings*))
  (when (null *happenings*)
    (return-from update-happenings act-state))
  (let* ((hap-state (copy-problem-state state))  ;initialization
         (net-state (copy-problem-state act-state))  ;initialization
         (obj-hap-updates (iter (for object in *happenings*)
                                (collect (get-object-happening-update object state
                                                    (problem-state-time act-state)))))
         (next-hap-update (iter (for obj-update in obj-hap-updates)
                                (finding obj-update maximizing (second (car obj-update)))))
         (hap-updates (iter (for obj-update in obj-hap-updates)
                            (append (cdr obj-update)))))
    (setf (problem-state-happenings net-state) (car next-hap-update))
    (setf hap-updates (sort hap-updates #'< :key #'car))
    (iter (for hap-update in hap-updates)  ;compute final net-state
          (revise (problem-state-db net-state) (cdr hap-update)))
    (iter (for hap-update in hap-updates)
          (revise (problem-state-db hap-state) (cdr hap-update))
          (when (constraint-violated act-state hap-state net-state)
            (return-from update-happenings nil))  ;cancel action and exit
          (finally (return net-state)))))  ;no constraint violations encountered

所有编译器优化参数都设置为1.是否有人对此进行故障排除有解释或建议?

注意:已编辑为包含整个功能。

1 个答案:

答案 0 :(得分:3)

排序列表时,SORT可能会产生破坏性和不必要的副作用。

例如,我们在此处对列表(10 10 9 10)

进行排序
CL-USER> (funcall (lambda (&aux (a (list 10 10 9 10))) 
                     (sort a #'<)
                     a))
; in: FUNCALL (LAMBDA (&AUX (A (LIST 10 10 9 10))) (SORT A #'<) A)
;     (SORT A #'<)
; ==>
;   (SB-IMPL::STABLE-SORT-LIST LIST
;                              (SB-KERNEL:%COERCE-CALLABLE-TO-FUN
;                               SB-C::PREDICATE)
;                              (IF SB-C::KEY
;                                  (SB-KERNEL:%COERCE-CALLABLE-TO-FUN SB-C::KEY)
;                                  #'IDENTITY))
; 
; caught STYLE-WARNING:
;   The return value of STABLE-SORT-LIST should not be discarded.
; 
; caught STYLE-WARNING:
;   The return value of STABLE-SORT-LIST should not be discarded.
; 
; compilation unit finished
;   caught 2 STYLE-WARNING conditions
(10 10 10)

您可以看到a指向的列表不是结果,其内容已被更改。

您可以看到差异:

CL-USER> (funcall (lambda (&aux (a (list 10 10 9 10))) 
            (values (sort a #'<)
                    a)))
(9 10 10 10)     ;    this is the result from SORT
(10 10 10)       ;    this is the side-effected value of A

SBCL警告说代码不使用结果值。排序后使用副作用列表也是错误的。

您应该检查代码并查看是否是这种情况。