Skip to content

Confusion with double negation and booleans #36856

Closed

Description

In certain circumstances, true != !false is considered true.

Steps to reproduce

  1. Create a new project with cargo new negation --bin
  2. Change the file src/main.rs so that the contents is the following:
fn g() -> bool {
    false
}

fn main() {
    let a = !g();
    if a != !g() {
        println!("wrong");
    } else {
        println!("correct");
    }
}

3.Run with cargo run

Expected and observed behavior

When run with cargo run, the compiled program prints wrong.
I expect correct to be printed because after the assignment,
a should still equal the value it was assigned to (note that g() always
returns the same value).

Note that cargo run --release results in printing correct, so even if it's me who is being confused, the output should still be the same in debug mode and in release mode.

Meta

When I do a normal rustc main.rs and witch cargo run --release the resulting
program behaves as I expect.
The unexpected behaviour only occurs when using cargo run (or running target/debug/negation).

rustc --version --verbose:

rustc 1.12.0 (3191fbae9 2016-09-23)
binary: rustc
commit-hash: 3191fbae9da539442351f883bdabcad0d72efcb6
commit-date: 2016-09-23
host: x86_64-unknown-linux-gnu
release: 1.12.0

cargo --version --verbose

cargo 0.13.0-nightly (109cb7c 2016-08-19)

Edit: added short description.

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

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant 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.Performance 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