Skip to content

Remove UB statement #94013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

fee1-dead
Copy link
Member

@fee1-dead fee1-dead commented Feb 15, 2022

No description provided.

In other words, makes the language 100 times safer 🚀 🚀
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Feb 15, 2022
@rust-highfive
Copy link
Contributor

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 15, 2022
@fee1-dead fee1-dead added T-lang Relevant to the language team, which will review and decide on the PR/issue. and removed T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Feb 15, 2022
@bjorn3
Copy link
Member

bjorn3 commented Feb 15, 2022

At MIR level division by zero is UB. Explicit checks for 0 are inserted are inserted to allow it to not be UB for the surface language.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1d680261de85ff6b6e19b15d570d636c

// WARNING: This output format is intended for human consumers only
// and is subject to change without notice. Knock yourself out.
fn foo(_1: u8, _2: u8) -> u8 {
    debug a => _1;                       // in scope 0 at [src/lib.rs:1:8: 1:9](https://play.rust-lang.org/#)
    debug b => _2;                       // in scope 0 at [src/lib.rs:1:15: 1:16](https://play.rust-lang.org/#)
    let mut _0: u8;                      // return place in scope 0 at [src/lib.rs:1:25: 1:27](https://play.rust-lang.org/#)
    let mut _3: u8;                      // in scope 0 at [src/lib.rs:2:5: 2:6](https://play.rust-lang.org/#)
    let mut _4: u8;                      // in scope 0 at [src/lib.rs:2:9: 2:10](https://play.rust-lang.org/#)
    let mut _5: bool;                    // in scope 0 at [src/lib.rs:2:5: 2:10](https://play.rust-lang.org/#)

    bb0: {
        _3 = _1;                         // scope 0 at [src/lib.rs:2:5: 2:6](https://play.rust-lang.org/#)
        _4 = _2;                         // scope 0 at [src/lib.rs:2:9: 2:10](https://play.rust-lang.org/#)
        _5 = Eq(_4, const 0_u8);         // scope 0 at [src/lib.rs:2:5: 2:10](https://play.rust-lang.org/#)
        assert(!move _5, "attempt to divide `{}` by zero", _3) -> bb1; // scope 0 at [src/lib.rs:2:5: 2:10](https://play.rust-lang.org/#)
    }

    bb1: {
        _0 = Div(move _3, move _4);      // scope 0 at [src/lib.rs:2:5: 2:10](https://play.rust-lang.org/#)
        return;                          // scope 0 at [src/lib.rs:3:2: 3:2](https://play.rust-lang.org/#)
    }
}

@fee1-dead fee1-dead closed this Feb 15, 2022
@fee1-dead fee1-dead deleted the unub branch February 15, 2022 11:12
@fee1-dead fee1-dead restored the unub branch February 15, 2022 11:16
@fee1-dead fee1-dead changed the title Remove outdated UB claims in compiler Clarify confusing UB statement Feb 15, 2022
@fee1-dead fee1-dead changed the title Clarify confusing UB statement Remove UB statement Feb 15, 2022
@fee1-dead
Copy link
Member Author

fee1-dead commented Feb 15, 2022

@bjorn3: Thanks for the explanation! I have #94017 up which clarifies the UB part a bit more so that it is easier to understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants