Skip to content

Passing enum by-value loses range metadata #13926

Closed
@huonw

Description

@huonw

Test case update for Rust 1.0:

pub enum Foo {
    A, B, C, D
}

static X: [isize; 4] = [10, 1, 4, 3];

pub fn foo(y: Foo) -> isize {
    X[y as usize]
}

Original report below:


pub enum Foo {
    A, B, C, D
}

static X: [int, .. 4] = [10, 1, 4, 3];
pub fn foo(y: Foo) -> int {
    X[y as uint]
}

becomes

; Function Attrs: uwtable
define i64 @_ZN3foo20h19859df9e3d3b0bcsaa4v0.0E(i8) unnamed_addr #0 {
entry-block:
  %1 = zext i8 %0 to i64
  %2 = icmp ugt i8 %0, 3
  br i1 %2, label %cond, label %next, !prof !0

next:                                             ; preds = %entry-block
  %3 = getelementptr inbounds [4 x i64]* @_ZN1X20h61a81bb0de98e867iaa4v0.0E, i64 0, i64 %1
  %4 = load i64* %3, align 8
  ret i64 %4

cond:                                             ; preds = %entry-block
  tail call void @_ZN2rt6unwind17fail_bounds_check20h0f41a1608cc59c5e1199v0.11.preE(i8* getelementptr inbounds ([14 x i8]* @str879, i64 0, i64 0), i64 7, i64 %1, i64 4)
  unreachable
}

even though it's impossible for a Foo to be larger than 3, so the bounds checking can never fail. (Doing a similar thing with e.g. [int, .. 256] and a value of type u8 does eliminate the bounds checks.)

(I guess this is an LLVM bug, but filing it here just in case we're emiting range info incorrectly, or some-such.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-enhancementCategory: An issue proposing an enhancement or a PR with one.I-heavyIssue: Problems and improvements with respect to binary size of generated code.I-slowIssue: Problems and improvements with respect to performance of generated code.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions