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: compiler panics when defining default trait impl #6946

Closed
cairolover opened this issue Dec 28, 2024 · 0 comments · Fixed by #7027
Closed

bug: compiler panics when defining default trait impl #6946

cairolover opened this issue Dec 28, 2024 · 0 comments · Fixed by #7027
Labels
bug Something isn't working

Comments

@cairolover
Copy link
Contributor

cairolover commented Dec 28, 2024

Bug Report

Cairo version:
2.9.2

Current behavior:
While working on iterators I wanted to implement advance_by which is basically a loop of next. As such the implementation is the same for all types, and I was writing the default trait impl as:

/// An iterator over a collection of values.
pub trait Iterator<T> {
    /// The type of the elements being iterated over.
    type Item;

    /// Advance the iterator and return the next value.
    fn next(ref self: T) -> Option<Self::Item>;

    fn advance_by(ref self: T, n: usize) -> Result<(), NonZero<usize>> {
        let mut res = Result::Ok(());
        for i in 0..n {
            if Self::next(ref self).is_none() {
                res = Result::Err((n - i).try_into().unwrap());
                break;
            }
        };
        res
    }
}

However, this breaks compilation with:

thread 'scarb compile tmp-lk338q382tp00' panicked at /Users/runner/.cargo/git/checkouts/cairo-f086c7e6d4098a68/809dfef/crates/cairo-lang-semantic/src/substitution.rs:493:21:
assertion `left == right` failed
  left: TraitId(0)
 right: TraitId(7)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at scarb/src/ops/compile.rs:197:14:
Compiler thread has panicked.: Any { .. }

Expected behavior:
This should compile successfully or fail compilation but without a panic

Steps to reproduce:
Create an isolated cairo file with:

/// An iterator over a collection of values.
pub trait Iterator2<T> {
    /// The type of the elements being iterated over.
    type Item;

    /// Advance the iterator and return the next value.
    fn next(ref self: T) -> Option<Self::Item>;
    
    fn advance_by(ref self: T, n: usize) -> Result<(), NonZero<usize>> {
        let mut res = Result::Ok(());
        for i in 0..n {
            if Self::next(ref self).is_none() {
                res = Result::Err((n - i).try_into().unwrap());
                break;
            }
        };
        res
    }
}

without advance_by - runs fine. with, panics

@cairolover cairolover added the bug Something isn't working label Dec 28, 2024
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.

1 participant