Skip to content

Compiler incorrectly assumes int will never be one #69841

Description

@AnttiParaoanu

The following code tries to parse a decimal integer followed by a newline. For some reason the compiler assumes that the parsed number will never be one. opt-level has to be 2, 3, s or z

fn main() {
    let buffer = [49u8, 10];
    let mut a : u64 = 0;
    'read: loop {
        for c in &buffer {
            match c {
                48..=57 => {
                    a*= 10;
                    a+= *c as u64 - 48;
                }
                10 => {
                    break 'read;
                }
                _ => {
                    unsafe { std::hint::unreachable_unchecked() };
                }
            }
        }
    }
    if a == 1 {
        println!("What did you expect?");
    }
}

I expected to see this happen: When running the program it prints "What did you expect?"

Instead, this happened: The program doesn't print anything, just exits

Meta

rustc --version --verbose:

rustc 1.43.0-nightly (823ff8cf1 2020-03-07)
binary: rustc
commit-hash: 823ff8cf1397a5772b1f6954b60576202bf91836
commit-date: 2020-03-07
host: x86_64-unknown-linux-gnu
release: 1.43.0-nightly
LLVM version: 9.0

On godbolt the issue seems to go back to version 1.38

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions