#Not sure how to solve this borrow issue

33 messages · Page 1 of 1 (latest)

indigo jungle
#

The type signature for .get_function is:

pub fn get_function(&self, name: &str) -> Option<&dyn Function2>

while .type_check is:

fn type_check(&self, args: &[PreExp], context: &mut TypeCheckerContext) -> Result<(), TransformError>

Is this because i'm getting the function ref from the context (immutable) and then calling the type_check which is mutable?

but i'm not using the ref afterwards, what could be the issue?

and also, how would i fix this

#

basically the context has inside of it all the data of my language + builtin functions and user defined functions.

By using the get_function() i get the function by it's name from the context, and then i call the function itself to execute type checking (some builtin functions need some complex type checking so i had to move it to the function itself)

hidden field
#

Which is a no go

#

Basically the solution is have f not be in context while the call is happening

indigo jungle
indigo jungle
hidden field
hidden field
# indigo jungle hm

You could also move type_check to context such that it doesn't require this passing to itself

#

This may or may not be feasible

indigo jungle
#

so i'd have to store the functions map outside of the context and pass that along as a parameter to the type_check function as immutable?

indigo jungle
#

if i can get away with passing one parameter less it might be better

hidden field
hidden field
#

Oh it also needs the args param

indigo jungle
#
    pub fn type_check_fn(
        &mut self,
        name: &str,
        args: &[PreExp],
    ) -> Result<(), TransformError> {
        //builtin_functions is:
        //&'a IndexMap<String, Box<dyn Function2>>
        match self.builtin_functions.get(name) {
            Some(f) => {
                let f = f.as_ref();
                f.type_check(args, self)?;
                Ok(())
            }
        //while this is:
        //IndexMap<String, Box<dyn Function2>>
            None => match self.functions.get(name) {
                Some(f) => {
                    let f = f.as_ref();
                    //borrow error here
                    f.type_check(args, self)?;
                    Ok(())
                }
                None => Err(TransformError::NonExistentFunction(name.to_string())),
            },
        }
    }
#

still getting the cannot borrow *self as mutable because it is also borrowed as immutable in the None branch

#

is this what you meant? @hidden field, it's inside the context

indigo jungle
#

lemme try that

hidden field
#

The new one must completely replace it

hidden field
indigo jungle
#

maybe i should just pass along another parameter for the functions map

hidden field
#

Context or standalone

indigo jungle
#

i'll make another context for the functions, and have that be immutable

#

since i'm also sharing the logic with another place, this is better

indigo jungle