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

Unexpected "unreachable call" warning in macro polonius_loop usage #9

Closed
ethe opened this issue Aug 23, 2022 · 0 comments · Fixed by #10
Closed

Unexpected "unreachable call" warning in macro polonius_loop usage #9

ethe opened this issue Aug 23, 2022 · 0 comments · Fixed by #10
Assignees
Labels
bug Something isn't working

Comments

@ethe
Copy link

ethe commented Aug 23, 2022

Crate version:

[dependencies]
polonius-the-crab = "0.3.0"

rustc version:

rustc 1.65.0-nightly (015a824f2 2022-08-22)

Show case:

use polonius_the_crab::prelude::*;

trait ContexIterator<'context, Cx> {
    type Item;

    fn next(&mut self) -> Option<(&mut Cx, Self::Item)>;

    fn find<F>(&mut self, f: &mut F) -> Option<(&Cx, Self::Item)>
    where
        F: FnMut(&mut Cx, &Self::Item) -> bool,
    {
        let mut s = self;
        polonius_loop!(|s| -> Option<(&'polonius Cx, Self::Item)> {
            if let Some((cx, item)) = s.next() {
                if f(cx, &item) {
                    polonius_return!(Some((cx, item)));
                }
            } else {
                polonius_break!();
            }
        });
        None
    }
}

#[cfg(test)]
mod tests {
    use crate::ContexIterator;

    #[derive(Debug)]
    pub struct Iter<'context, 'source, Cx, I> {
        inner: &'source [I],
        context: &'context mut Cx,
        index: usize,
    }

    impl<'context, 'source, Cx, I> ContexIterator<'context, Cx> for Iter<'context, 'source, Cx, I> {
        type Item = &'source I;

        fn next(&mut self) -> Option<(&mut Cx, Self::Item)> {
            let item = self
                .inner
                .get(self.index)
                .map(|item| (&mut *self.context, item));
            self.index += 1;
            item
        }
    }

    #[test]
    fn test() {
        let v = vec![1, 2, 3];
        let mut i = Iter {
            inner: &v,
            context: &mut (),
            index: 0,
        };
        println!("{:?}", i.find(&mut |_, i| **i % 2 == 0));
    }
}

cargo check output:

 ↳ cargo check
warning: unreachable call
  --> src/lib.rs:13:9
   |
13 | /         polonius_loop!(|s| -> Option<(&'polonius Cx, Self::Item)> {
14 | |             if let Some((cx, item)) = s.next() {
15 | |                 if f(cx, &item) {
16 | |                     polonius_return!(Some((cx, item)));
...  |
20 | |             }
21 | |         });
   | |          ^
   | |          |
   | |__________unreachable call
   |            any code following this expression is unreachable
   |
   = note: `#[warn(unreachable_code)]` on by default
   = note: this warning originates in the macro `$crate::polonius_break_dependent` which comes from the expansion of the macro `polonius_loop` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `iter` (lib) generated 1 warning
danielhenrymantilla added a commit that referenced this issue Aug 24, 2022
This is causing the `unreachable_code_lint` to fire, causing `#9`
@danielhenrymantilla danielhenrymantilla self-assigned this Aug 24, 2022
@danielhenrymantilla danielhenrymantilla added the bug Something isn't working label Aug 24, 2022
danielhenrymantilla added a commit that referenced this issue Aug 24, 2022
This is causing the `unreachable_code_lint` to fire, causing `#9`
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

Successfully merging a pull request may close this issue.

2 participants