Skip to content

segfault with small Rust program #10325

Closed
@chetmurthy

Description

@chetmurthy

I've got a small Rust program, trying to implement a binary search tree. With Rust 0.8, the compiler segfaults. WIth GIT revision 8ea2123, I get a nicer error-message, THEN a segfault. Looks like some sort of infinite stack-recursion.

I'm including the program.

extern mod extra ;
use std::rand;
use std::rand::Rng;
use std::vec ;
use std::str ;
use extra::sort ;

struct bstnode<K, V> {
    k : K,
    v : V,
    l : Option< bstnode<K, V> >,
    r : Option< bstnode<K, V> >
}

impl<K : Clone + Eq + TotalOrd , V : Clone> bstnode<K, V> {
    fn new(k: &K, v: &V) -> bstnode<K,V> {
        bstnode { k: (*k).clone(), v: (*v).clone(), l: None, r: None }
    }
    fn dumbinsert(node: &mut bstnode<K, V>, k : &K, v : &V) {
        if (node.k.cmp(k) == Equal) {
            node.v = v.clone() ;
        }
        else if (k.cmp(&(node.k)) == Less) {
            match node.l {
                None => { node.l = Some(bstnode::new(k, v)) ; }
                Some(ref mut l) => bstnode::dumbinsert(l, k, v)
            }
        }
        else {
            match node.r {
                None => { node.r = Some(bstnode::new(k, v)) ; }
                Some(ref mut r) => bstnode::dumbinsert(r, k, v)
            }
        }
    }
}

fn main() {
    let k = ~"foo";
    let v = ~"bar";
    let mut t = bstnode::new(&k, &v) ;
    let k2 = ~"foo2" ; let v2 = ~"bar2" ;
    bstnode::dumbinsert(&mut t, &k2, &v2) ;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions