Skip to content

Type-parameterized 'vec_omit' produces incorrect result #640

Closed
@jruderman

Description

@jruderman

The function

fn vec_omit_T[T] (&T  [] v, uint i) -> T[]   { slice(v, 0u, i) + slice(v, i+1u, len(v)) }

produces an array with bogus values in it. In contrast, an int-specialized function produces correct results.

use std;
import std::ivec;
import std::ivec::slice;
import std::ivec::len;
import std::int;

fn vec_omit      (&int[] v, uint i) -> int[] { slice(v, 0u, i) + slice(v, i+1u, len(v)) }
fn vec_omit_T[T] (&T  [] v, uint i) -> T[]   { slice(v, 0u, i) + slice(v, i+1u, len(v)) }

fn vec_to_str(&int[] v) -> str {
    auto i = 0u;
    auto s = "[";
    while (i < len(v)) {
        s += int::str(v.(i));
        if (i + 1u < len(v)) {
            s += ", "
        }
        i += 1u;
    }
    ret s + "]";
}

fn main() {
    auto a = ~[1, 2, 3, 4, 5];
    log_err vec_to_str(a);

    auto b = vec_omit(a, 2u);
    log_err vec_to_str(b);

    auto c = vec_omit_T(a, 2u);
    log_err vec_to_str(c);
}

The last line of output should be '[1, 2, 4, 5]' but is something like '[262145, 327680, 1, 6291707]'.

Metadata

Metadata

Assignees

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