NEWTON

NEWTON


Popular tags

    Revoked References exercise from Cairo-lang

    Asked

    3 months ago

    37

    views


    1

    I am currently running this code

    func main() {
    
        let x = [ap];
        [ap] = 1, ap++;
    
        [ap] = 2, ap++;
    
        [ap] = x, ap++;
        jmp rel -1;  // Jump to the previous instruction.
    }
    
    And what is happening is that "x" is alternating between 1 and 2 and I couldn't figure out why.
    
    Also, it wasn't very clear to me what a "Revoked Reference" means. Is it when you cannot access a certain [ap] when running a specific function?
    
    This is the link to the exercise:
    https://www.cairo-lang.org/docs/how_cairo_works/consts.html#revoked-references
    
      cairo

    0x3728...30a956

    asked

    3 months ago


    1 answers

    1

    Accepted answer

    That's actually a nice question! In your example, the compiler is not able to compute the change of ap, and here's why "x" is alternating between 1 and 2

    You can see a similar question there it is explained a bit more clearly explained what is a revoked reference.

    About revoked references

    The scope of some types of references in Cairo (e.g., return values of functions and temporary variables) is somewhat restricted. For example, a call to another function may revoke those references.

    When you write "let" you do not create a variable, instead you create a reference to the memory slot. I may even cause an error, for example:

    let (x) = foo(1); let (y) = foo(2); serialize_word(x + y);

    will cause an error:

    Error: code:43:20: Reference 'x' was revoked. serialize_word(x + y); ^ Reference was defined here: code:41:10 let (x) = foo(1); ^

    vargastartup

    answered

    3 months ago

    Your answer

    NEWTON

    NEWTON