#Why does renaming a local static in another scope fix this bug?

3 messages · Page 1 of 1 (latest)

placid grotto
#

Ok so I had a bug in some embedded firmware im working on where I have two separate static instances of TimedTrigger (I'm using StaticCell because I need &'static for embassy), and then they each have a processing loop that get spawned into a different async task.

    let spray_trigger: &RefCell<TimedTrigger> = {
        static TRIGGER: StaticCell<RefCell<TimedTrigger>> = StaticCell::new();
        TRIGGER.init_with(|| RefCell::new(TimedTrigger::new("Spray Trigger", pac.spray_trigger.into())))
    };

    let syringe_trigger: &RefCell<TimedTrigger> = {
        static TRIGGER: StaticCell<RefCell<TimedTrigger>> = StaticCell::new();
        TRIGGER.init_with(|| RefCell::new(TimedTrigger::new("Sonic Syringe", pac.syringe_trigger.into())))
    };

The bug was that only whichever loop was spawned last would appear to run.

So, when I change the above code to this, it works:

    let spray_trigger: &RefCell<TimedTrigger> = {
        static SPRAY_TRIGGER: StaticCell<RefCell<TimedTrigger>> = StaticCell::new();
        SPRAY_TRIGGER.init_with(|| RefCell::new(TimedTrigger::new("Spray Trigger", pac.spray_trigger.into())))
    };

    let syringe_trigger: &RefCell<TimedTrigger> = {
        static SYRINGE_TRIGGER: StaticCell<RefCell<TimedTrigger>> = StaticCell::new();
        SYRINGE_TRIGGER.init_with(|| RefCell::new(TimedTrigger::new("Sonic Syringe", pac.syringe_trigger.into())))
    };

But I don't understand why. It contradicts my understanding of static locals. In the first snippet, each static TRIGGER should be a different variable, right?

slow nebula
#

This is pretty weird. Even if they were in the same scope, the latter should shadow the former and there shouldn't be any problems using both.

boreal inlet
#

hmm...maybe a bug where the linker is combining the two static symbols even though it shouldn't?