Cannot return value referencing function parameter 'data'


#1

Hello,

My code is something like this (incomplete, of course, the whole code is very long):
image





However, on line 246, I’m getting the error “cannot return value referencing function parameter ‘data’”. Any ideas?

Thanks!


#2

Viewing that your code is in images (Markdown is supported here) I see that the return type in next_node is Self and not AdaptiveTransitionFunctionMapNode, sometimes you can but like this time you ain’t, the only thing afaik that you can do is change in line 246 Ref<'b, Self> to Ref<'b, AdaptiveTransitionFunctionMapNode> so the return types will match. Hope it helps ya!


#3

This isn’t making very much sense. Here’s what I can gather:

  • Because Ref<'a, T> doesn’t have a method named get_cell_ref<'b>, I presume that the second image with a function names get_cell_ref<'b>() is from a trait implementation on Ref<'a, T>.
  • But then I look at the first line of get_cell_ref<'b> and I find self.is_link_get_vec() which, looking at the second function in the second image, confuses me. is_link_get_vec() is an impl on the same Self (Which we have thus far determined to be Ref<'a, T>) and therefore the if let statement makes no sense, unless there’s some kind of magical type aliasing trickery.
  • Also, ignoring this, there is no as_ref() function on Rc but I will assume that it returns a Ref<'b, Adaptive...Node> (Which is supposed to be a Self as we established that it was Ref<'b, T>)
  • Which leads to the question of how these mystery methods are implemented and where they get their data from, because the error either lies in there, or their logic is sound, and the error is in next_node (Which I have henceforth presumed to be the cause of the compiler error due to its red highlighting)

#4

Those methods are all custom methods I declared on AdaptiveTransitionFunctionMapNode, I’m using the auto deref to call them


#5

data.get_cell_ref returns a Ref<'b, AdaptiveTransitionFunctionMapNode>. The issue is that the compiler says that I can’t return a value that references a function parameter (data).

NOTE - I have tried changing the 3rd screnshot to be (data : &'b Ref<'b, Self>, however, still running into a few issues.

The issue is in another function:


On line 193, “cannot assign to ‘old’ because it is borrowed”. Also, on the same line (regarding the middle of line 193, “&old”, temporary value dropped while borrowed)


#6

Hmm, what happens if you swap out lines 190-194 with this:

let old = (1..input.len()).iter().fold(self.get_cell_ref(input[0]), 
    |acc, counter| {
        Self::next_node(&acc, input[counter])
    }
);

#8

Thanks! But still:

I’m thinking of creating a function that removes the RefCell (yes, there is a RefCell. The data stored in the enum is of type Vec<Option<Rc<RefCell>>>. I have code that works already if there wasn’t the RefCell (see the question “How to deep clone an Arc”, also by me)