Skip to content

Division by NonZero* checks for zero #76960

New issue

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

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

Already on GitHub? # to your account

Closed
bugadani opened this issue Sep 20, 2020 · 4 comments
Closed

Division by NonZero* checks for zero #76960

bugadani opened this issue Sep 20, 2020 · 4 comments

Comments

@bugadani
Copy link
Contributor

bugadani commented Sep 20, 2020

https://rust.godbolt.org/z/eTPzE6

Technically, NonZero* types are not meant to be used for optimizing arithmetic, but people (like myself) may or may not know that or may assume otherwise.

It is possible to create a NonZero* object with 0 inner value, but since that is a misuse of the type and deliberate violation of the safety guarantees, I think it would make sense to teach the compiler that get() can't return 0.

Edit: unfortunately, since NonZero* methods are const, it's not as simple as adding an assume.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 20, 2020

since NonZero* methods are const, it's not as simple as adding an assume.

we can make assume const, it's a no-op in const eval anyway (though we use it to assert that the assumption holds in miri)

@tesuji
Copy link
Contributor

tesuji commented Sep 20, 2020

I'm preparing a PR for that.

@bugadani
Copy link
Contributor Author

I can add the assume after that's done.

@bugadani
Copy link
Contributor Author

Duplicate of #49572

RalfJung added a commit to RalfJung/rust that referenced this issue Sep 25, 2020
…-obk

Unstably allow assume intrinsic in const contexts

Not sure much about this usage because there are concerns
about [blocking  optimization][1] and [slowing down LLVM][2] when using `assme` intrinsic
in inline functions.
But since Oli suggested in rust-lang#76960 (comment),
here we are.

[1]: rust-lang#54995 (comment)
[2]: rust-lang#49572 (comment)
RalfJung added a commit to RalfJung/rust that referenced this issue Sep 25, 2020
…-obk

Unstably allow assume intrinsic in const contexts

Not sure much about this usage because there are concerns
about [blocking  optimization][1] and [slowing down LLVM][2] when using `assme` intrinsic
in inline functions.
But since Oli suggested in rust-lang#76960 (comment),
here we are.

[1]: rust-lang#54995 (comment)
[2]: rust-lang#49572 (comment)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants