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

Don't allow prerequisite values for inlined functions #60

Closed
marick opened this issue Nov 4, 2011 · 7 comments
Closed

Don't allow prerequisite values for inlined functions #60

marick opened this issue Nov 4, 2011 · 7 comments
Labels

Comments

@marick
Copy link
Owner

marick commented Nov 4, 2011

It doesn't work to override functions like #'+ because they're inlined. Processing of provided should consider an attempt to do so a user error.

(defn doubler [n] (+ n n))
(fact (doubler 3) => 0 (provided (+ 3 3) => 0))   ;;; Should be rejected

In Clojure 1.3 (at least), the var for a function that will be inlined has an :inline key in its metadata.

@AlexBaranosky
Copy link
Collaborator

Would this be adding to (defmethod validate "fake" ...) to have it check that the function's metadata doesn't identify it is inlinable?

@AlexBaranosky
Copy link
Collaborator

So something like this?:

;; tst
(defn doubler [n] (+ n n))

(after-silently
  (expect (doubler 3) => 0
    (fake (+ 3 3) => 0))
  (expect @reported => (user-error-with-notes #"cannot fake the function")))

;; src
(defn- compiler-will-inline-fn? [[fn & args :as funcall]]
  (contains? (meta (var fn)) :inline))

(defmethod validate "fake" [[_fake_ & fake-form :as form]]
  (let [funcall (first fake-form)]
    (cond (not (list? funcall))
          (user-error-report-form
            form
            "The left-hand-side of a prerequisite must look like a function call."
            (cl-format nil "`~S` doesn't." funcall))

          (compiler-will-inline-fn? funcall)
          (user-error-report-form
            form
            (cl-format nil "You cannot fake the function `~S` because it is inlined by the Clojure compiler." (first funcall)))

          :else
          fake-form)))

@marick
Copy link
Owner Author

marick commented Nov 7, 2011

That seems a reasonable way to do it. Are you going to? Otherwise I will.

(I wouldn't use "fake" in the error message, since that's not a word midje.sweet exposes. Perhaps "you cannot override a call to + because the Clojure compiler inlines it.")

On Nov 5, 2011, at 8:08 PM, AlexBaranosky wrote:

So something like this?:

;; tst
(defn doubler [n] (+ n n))

(after-silently
 (expect (doubler 3) => 0
   (fake (+ 3 3) => 0))
 (expect @reported => (user-error-with-notes #"cannot fake the function")))

;; src
(defn- compiler-will-inline-fn? [[fn & args :as funcall]]
 (contains? (meta (var fn)) :inline))

(defmethod validate "fake" [[_fake_ & fake-form :as form]]
 (let [funcall (first fake-form)]
   (cond (not (list? funcall))
         (user-error-report-form
           form
           "The left-hand-side of a prerequisite must look like a function call."
           (cl-format nil "`~S` doesn't." funcall))

         (compiler-will-inline-fn? funcall)
         (user-error-report-form
           form
           (cl-format nil "You cannot fake the function `~S` because it is inlined by the Clojure compiler." (first funcall)))

         :else
         fake-form)))

Reply to this email directly or view it on GitHub:
#60 (comment)


Brian Marick, Artisanal Labrador
Now working at http://path11.com
Contract programming in Ruby and Clojure
Occasional consulting on Agile

@AlexBaranosky
Copy link
Collaborator

I tried it out and wasn't quite sure if I had it right. You can finish it off if you'd like.

@marick
Copy link
Owner Author

marick commented Nov 7, 2011

OK, I'll take a look at it.

On Nov 6, 2011, at 6:29 PM, AlexBaranosky wrote:

I tried it out and wasn't quite sure if I had it right. You can finish it off if you'd like.


Reply to this email directly or view it on GitHub:
#60 (comment)


Brian Marick, Artisanal Labrador
Now working at http://path11.com
Contract programming in Ruby and Clojure
Occasional consulting on Agile

@marick
Copy link
Owner Author

marick commented Nov 9, 2011

In 1.3-alpha5

@marick marick closed this as completed Nov 9, 2011
@AlexBaranosky
Copy link
Collaborator

Great! Looks like resolve was the piece I was missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants