Carp icon indicating copy to clipboard operation
Carp copied to clipboard

Lifetime is incorrectly inferred as dead

Open hellerve opened this issue 6 years ago • 3 comments

As pointed out in the chat, sometimes the lifetime inferrence will infer that an alive reference is dead. Here’s an example of the bug and a workaround in the test code for URI.

The good news is that this seems to happen pretty rarely, and can be fixed by hoisting an inline reference into a containing let statement. It should still be fixed, of course.

Cheers

hellerve avatar Feb 10 '20 19:02 hellerve

Is there a small reproducible for this?

eriksvedang avatar May 05 '20 13:05 eriksvedang

I just tried coming up with one, but failed. The best I can offer is this:

(load "Test.carp")

(use Test)

(load "[email protected]:carpentry-org/[email protected]")

(def uri "scheme://usr:[email protected]:123/path?query=1#fragment")

(deftest test
  (assert-equal test
                uri
                &(str &(Result.unsafe-from-success (URI.parse uri)))
                "we can convert our representation to a string again correctly"
  )
)

Which depends on the URI library. Sorry about that!

hellerve avatar May 05 '20 16:05 hellerve

There’s also a problem using System.get-arg:

(defn main []
  (for [i 0 (System.get-args-len)]
    (let [x (System.get-arg i)] ; this is inferred as dead
      (if (ends-with? x "Untitled")
        (println* x)
        ()))))

I assume that the Ref there should have a static lifetime, but I’m not sure how to annotate that.

hellerve avatar May 10 '20 10:05 hellerve