Skip to content

Don't use the type of checked TYPED-FORMs to rewrite acode#537

Open
se-mz wants to merge 1 commit intoClozure:masterfrom
se-mz:typed-form-fix
Open

Don't use the type of checked TYPED-FORMs to rewrite acode#537
se-mz wants to merge 1 commit intoClozure:masterfrom
se-mz:typed-form-fix

Conversation

@se-mz
Copy link
Copy Markdown
Contributor

@se-mz se-mz commented Jul 26, 2025

Optimizations that rely on ASSERTED-TYPE might change the value (hence type) of the inner form if it isn't actually of the given type, which would defeat the point of typechecking.

This looks like it may hurt the performance of code that both trusts declarations and generates checked TYPED-FORMs, but this can only happen when the user provides a compiler policy that has both TRUST-DECLARATIONS and DECLARATIONS-TYPECHECK enabled; in the default policy, they're mutually exclusive.

For an example of how this can cause issues, consider the following:

>>> (let ((f (compile nil '(lambda (x)
                            (declare (optimize safety)
                                     (type fixnum x))
                            (the fixnum (ash x 1))))))
      (funcall f most-positive-fixnum))
;; TYPE-ERROR: The value 2305843009213693950 is not of the expected type FIXNUM.
>>> (let ((f (compile nil '(lambda (x)
                            (declare (optimize safety)
                                     (type fixnum x))
                            (the fixnum (ash (the fixnum x) 1))))))
      (funcall f most-positive-fixnum))
-2

(the fixnum x) is necessary to make the acode rewriter for ASH trust that it's working on a fixnum and rewrite to %ILSL; on high safety, it won't trust the unchecked TYPED-FORM generated by the type declaration.

Optimizations that rely on ASSERTED-TYPE might change the value (hence type) of
the inner form if it isn't actually of the given type, which would defeat the
point of typechecking.

This looks like it may hurt the performance of code that both trusts
declarations and generates checked TYPED-FORMs, but at the time of writing, this
can only happen when the user provides a compiler policy that has both
TRUST-DECLARATIONS and DECLARATIONS-TYPECHECK enabled; in the default policy,
they're mutually exclusive.
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.

1 participant