Skip to content

Bug: stream() causes random behavior / internal compiler error #6878

Closed
@honzasp

Description

@honzasp

Updated bug report

fn sum_halves1() -> u64 {
    let mut buf: u64 = 0;
    for i in range(0u64, 50_000_000) {
        buf = buf + (i / 2);
    }
    buf
}

#[cfg(variant1)]
fn sum_halves2() -> u64 {
    use std::comm::{stream};

    let (_port,_chan): (Port<u64>,Chan<u64>) = stream();

    let mut buf: u64 = 0;
    for j in range(0u64, 50_000_000) {
        buf = buf + (j / 2);
    }

    println(fmt!("buf  %s", buf.to_str()));
    println(fmt!("buf  %s", buf.to_str()));
    println(fmt!("buf  %s", buf.to_str()));

    buf
}

#[cfg(variant2)]
fn sum_halves2() -> u64 {
    use std::comm::{stream};

    let (_port,_chan) = stream();

    let mut buf: u64 = 0;
    for j in range(0u64, 50_000_000) {
        buf = buf + (j / 2);
    }

    println(fmt!("buf  %s", buf.to_str()));
    println(fmt!("buf  %s", buf.to_str()));
    println(fmt!("buf  %s", buf.to_str()));

    buf
}

#[cfg(variant3)]
fn sum_halves2() -> u64 {
    use std::comm::{stream};

    let mut buf: u64 = 0;
    for j in range(0u64, 50_000_000) {
        buf = buf + (j / 2);
    }

    println(fmt!("buf  %s", buf.to_str()));
    println(fmt!("buf  %s", buf.to_str()));
    println(fmt!("buf  %s", buf.to_str()));

    buf
}

fn main() {
    do 1.times {
        println(fmt!("res1 %s", sum_halves1().to_str()));
        let res = sum_halves2();
        println(fmt!("res2 %s", res.to_str()));
        println(fmt!("res2 %s", res.to_str()));
    }
}

Invoke via rustc --cfg variant1 /tmp/sum.rs && /tmp/sum for (or variant2 or variant3).

Original bug report

Hi,

I found a strange behavior of the compiler, which seems to modify a variable by random. The inputs and outputs are here: https://gist.github.com/honzasp/5689535.

The program sum_halvesA.rs computes a correct value in sum_halves1() and prints it, then it computes the same value in sum_halves2(), but this value is mysterously modified somewhere between the caller and callee.

A small change (removing stream() or some println) makes it work as expected.

If I remove the type signature for (port,chan), as in sum_halvesB.rs, rustc fails with internal compiler error.

Original reported output

~/prog/benchrust $ rustc sum.rs -o sum_rust -O && ./sum_rust
sum.rs:12:7: 12:11 warning: unused variable: `port` [-W unused_variable]
sum.rs:12 let (port,chan): (Port<u64>,Chan<u64>) = stream();
^~~~
sum.rs:12:12: 12:16 warning: unused variable: `chan` [-W unused_variable]
sum.rs:12 let (port,chan): (Port<u64>,Chan<u64>) = stream();
^~~~
res1 624999975000000
buf 624999975000000
buf 624999975000000
buf 624999975000000
res2 593020068061681600
res2 593020068061681600

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions