Carp icon indicating copy to clipboard operation
Carp copied to clipboard

Borrow checker is confused by using a match-ref with a let block

Open TimDeve opened this issue 5 years ago • 2 comments

The following code produces a borrow checker error even though there is no shared values between the two match-ref:

(deftype Sum One Two)

(defn main []
  (do
   (let []
    (match-ref &(Sum.One)
     Sum.One ()
     Sum.Two ()))
   (match-ref &(Sum.Two)
    Sum.One ()
    Sum.Two ())))
The reference '(ref (Sum.Two))' (depending on the variable '_9') isn't alive at line 9, column 15 in '/tmp/main.carp'. at /tmp/main.carp:3:1.

TimDeve avatar Dec 12 '20 12:12 TimDeve

The following code works fine so it seems related to the scope of the let:

(deftype Sum One Two)

(defn main []
  (do
   (match-ref &(Sum.One)
    Sum.One ()
    Sum.Two ())
   (match-ref &(Sum.Two)
    Sum.One ()
    Sum.Two ())))

TimDeve avatar Dec 12 '20 12:12 TimDeve

This version has slightly more readable error message:

(deftype Sum One Two)

(defn main []
  (let-do [sum-two (Sum.Two)]
   (let [sum-one (Sum.One)]
    (match-ref &sum-one
     Sum.One ()
     Sum.Two ()))
   (match-ref &sum-two
    Sum.One ()
    Sum.Two ())))
The reference '(ref sum-two)' (depending on the variable 'sum_MINUS_one') isn't alive at line 9, column 15 in '/tmp/main.carp'. at /tmp/main.carp:3:1.

TimDeve avatar Dec 12 '20 13:12 TimDeve