Skip to content

Segmentation Fault while iterating over a vector of "~[~T as Trait]" #5666

New issue

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

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

Already on GitHub? # to your account

Closed
drbawb opened this issue Apr 1, 2013 · 4 comments
Closed

Segmentation Fault while iterating over a vector of "~[~T as Trait]" #5666

drbawb opened this issue Apr 1, 2013 · 4 comments
Labels
A-codegen Area: Code generation A-trait-system Area: Trait system E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@drbawb
Copy link

drbawb commented Apr 1, 2013

Tested in rust 0.6 on Arch (Linux - 3.8.4, x86_64)
Modified testcase exhibits similar behavior on Mac OS X 10.7

I can call #bark() on any index of the vector and it does not seg-fault.
When using the #each() iterator however, only the first element is printed, the next access results in a segmentation fault.

I've been able to reproduce this on two separate systems, one running rust 0.6 (6dd20c8) on a fairly vanilla Linux kernel, the other running (release-0.5) on Mac OS X.

I've written a small test-case that exhibits this behavior on my systems.

Note

Please let me know if I can gather any additional information that might aid in resolving this issue.

struct Dog {
    name : ~str
}

trait Barks {
        fn bark(&self) -> ~str;
}

// impl Dog : Barks { //uncomment for Rust 0.5
impl Barks for Dog {
  fn bark(&self) -> ~str {
    return ~"bark (for ~Dog)";
  }
}


fn main() {
        let snoopy = ~Dog{name: ~"snoopy"};
        let bubbles = ~Dog{name: ~"bubbles"};
        let barker = [snoopy as ~Barks, bubbles as ~Barks];

    for barker.each |pup| {
                println(fmt!("%s", pup.bark()));
    }
}

/* Expected:
* woof! (I'm snoopy)
* woof! (I'm bubbles)
*/

/* Actual Ouptut:
* woof! (I'm snoopy)
* SEGMENTATION FAULT
*/
@drbawb
Copy link
Author

drbawb commented Apr 2, 2013

I've been fiddling around with this example some more, here's what I've dug up.

If I instead store the vector as ~[@T as @Trait], the code works as expected.

Printing [~T as ~Trait].len() on a vector of n where n is greater than 1 yields n +1, as opposed to the actual length.

Printing the length on a vector [@T as @Trait] or [&T as &Trait] yields the actual length.

Attempting to access an out of bounds index still panics as expected; so I believe the segmentation fault is a combination of using the iterator and the storage of [~Trait]

@catamorphism
Copy link
Contributor

Reproduced with 64963d6. Nominating for milestone 5, production-ready

@emberian
Copy link
Member

Cannot reproduce with:

struct Dog {
    name : ~str
}

trait Barks {
    fn bark(&self) -> ~str;
}

impl Barks for Dog {
    fn bark(&self) -> ~str {
        return fmt!("woof! (I'm %s)", self.name);
    }
}


fn main() {
    let snoopy = ~Dog{name: ~"snoopy"};
    let bubbles = ~Dog{name: ~"bubbles"};
    let barker = [snoopy as ~Barks, bubbles as ~Barks];

    for barker.iter().advance |pup| {
        println(fmt!("%s", pup.bark()));
    }
}

Probably fixed by @luqmana's ~Trait fixes.

@emberian emberian reopened this Jul 16, 2013
@emberian
Copy link
Member

(Removing nomination)

bors added a commit that referenced this issue Aug 15, 2013
Closes #3907
Closes #5493
Closes #4464
Closes #4759
Closes #5666
Closes #5884
Closes #5926
Closes #6318
Closes #6557
Closes #6898
Closes #6919
Closes #7222
tesuji pushed a commit to tesuji/rustc that referenced this issue Jun 4, 2020
Rollup of 3 pull requests

Successful merges:

 - rust-lang#5637 (new lint: vec_resize_to_zero)
 - rust-lang#5656 (len_zero: skip ranges if feature `range_is_empty` is not enabled)
 - rust-lang#5663 (add testcase that no longer ICEs)

Failed merges:

r? @ghost

changelog: rollup
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-codegen Area: Code generation A-trait-system Area: Trait system E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants