Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: loop variable identifier not found #4318

Closed
glihm opened this issue Oct 29, 2023 · 0 comments
Closed

bug: loop variable identifier not found #4318

glihm opened this issue Oct 29, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@glihm
Copy link

glihm commented Oct 29, 2023

Bug Report

Cairo version:

v2.3.0

Current behavior:

When writing a loop, we're facing a weird issue from the compiler saying that Identifier not found for a variable that is actually defined in the loop itself:

impl BattleImpl of BattleTrait {
    fn battle_loop(ref self: Battle) {
        loop {
            let mut entity: u32 = 5;
            if !self.isAlly(entity) {
                break;
            }
        };
    }

    fn is_ally(ref self: Battle, entity_index: u32) -> bool {
        return true;
    }
}

This fails with:

error: Identifier not found.
 --> /home/glihm7a/Downloads/mre/src/Battle.cairo:20:29
            if !self.isAlly(entity) {
                            ^****^

But defining the variable outside the loop, or adding a statement solves the problem:

impl BattleImpl of BattleTrait {
    fn battle_loop(ref self: Battle) {
        loop {
            // Moving this out of the loop makes the code compile.
            let mut entity: u32 = 5;
            if !self.isAlly(entity) {
                break;
            }

            // This makes the code compile.
            let a = 2;
        };
    }

    fn is_ally(ref self: Battle, entity_index: u32) -> bool {
        return true;
    }
}

Expected behavior:

Expecting the variable defined inside the loop to be found without adding an extra statement.

Steps to reproduce:

Here is a MRE:

#[derive(Destruct)]
struct Battle {
}

fn new() -> Battle {
    let mut battle = Battle {};
    return battle;
}

trait BattleTrait {
    fn battleLoop(ref self: Battle);
    fn isAlly(ref self: Battle, entityIndex: u32) -> bool;
}

impl BattleImpl of BattleTrait {
    fn battleLoop(ref self: Battle) {

        loop {
            let mut entity: u32 = 5;
            if !self.isAlly(entity) {
                break;
            }

            //let b = 2;
        };
    }
    fn isAlly(ref self: Battle, entityIndex: u32) -> bool {
        return true;
    }
}

Related code:

Not sure of the compiler code related to that. Any hints on this and I would be happy to investigate.

Other information:

At the beginning I was thinking of the fact that as the entity variable is related to the break, cairo needs to know it outside the loop.
But when we add a statement, it works. So I'm not sure of the behavior here.
Speaking with Oriz on the discord, he is likely a bug, but no clue where on my side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants