Skip to content

casting in a static breaks type inference #17756

Closed
@porglezomp

Description

@porglezomp
Contributor

Writing the following works perfectly:

static count : uint = 2;
fn main() {
    let larger : [uint, ..count*2];
}

While the following:

static count : uint = 2 as uint;
fn main() {
    let larger : [uint, ..count*2];
}

Fails with error: expected constant expr for vector length: can't do this op on a uint and int. This looks like a problem with the type inference, and using 2u fixes this example, but breaks the one that previously worked.
When the array is being initialized rather than having the type set:

let larger = [0u, ..count*2];

The error is a much more cryptic error: expected constant integer for repeat count, found variable, and which one fails can again be swapped by multiplying by 2u instead of 2.

Activity

mahkoh

mahkoh commented on Oct 4, 2014

@mahkoh
Contributor
arielb1

arielb1 commented on Jun 29, 2015

@arielb1
Contributor

This works on 1.1+:

const count : usize = 2 as usize;
fn main() {
    let larger : [usize; count*2];
}
added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
and removed on Jun 29, 2015
frewsxcv

frewsxcv commented on Jun 30, 2015

@frewsxcv
Member

Regression test added in #26668

added a commit that references this issue on Jun 30, 2015

Auto merge of #26668 - frewsxcv:regression-test-17756, r=alexcrichton

e6596d0
added a commit that references this issue on Aug 1, 2024

Auto merge of rust-lang#17756 - Wilfred:fix_adoc_json, r=Veykril

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

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @frewsxcv@huonw@porglezomp@arielb1@mahkoh

        Issue actions

          casting in a static breaks type inference · Issue #17756 · rust-lang/rust