Skip to content

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

Closed

Description

@drbawb

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
*/

Activity

  1. drbawb commented on Apr 2, 2013

    @drbawb
    Author

    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]

  2. catamorphism commented on May 24, 2013

    @catamorphism
    Contributor

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

  3. emberian commented on Jul 16, 2013

    @emberian
    Contributor

    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.

  4. emberian commented on Jul 16, 2013

    @emberian
    Contributor

    (Removing nomination)

  5. added a commit that references this issue on Jun 4, 2020
  6. added a commit that references this issue on Aug 21, 2026
    8638bcb
  7. added 2 commits that reference this issue on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenA-trait-systemE-easyE-needs-test

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions