Skip to content
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

no compilation error on impossible bitshift if not used #101763

Open
NULLx76 opened this issue Sep 13, 2022 · 4 comments
Open

no compilation error on impossible bitshift if not used #101763

NULLx76 opened this issue Sep 13, 2022 · 4 comments
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@NULLx76
Copy link
Contributor

NULLx76 commented Sep 13, 2022

When creating a library that contains an impossible bitshift, e.g. 1u32 << 32

I expected to see this happen: the library doesn't compile.

Instead, this happened: the library compiles as long as the code is never used in the library but does fail to compile when used in another crate.

Other ways to fail compilation like compile_error! or let a: u8 = -5 do stop the library from compiling even when the code is not in use.

Meta

rustc version: rustc 1.63.0 (4b91a6ea7 2022-08-08)

minimal repro: https://github.com/jonay2000/compile-error-bug

@NULLx76 NULLx76 added the C-bug Category: This is a bug. label Sep 13, 2022
@jdonszelmann
Copy link
Contributor

We just tried some more examples. For explict integer overflow the same problem occurs:

255u8 + 1

will not fail to compile as long as it's not used anywhere. That means you could upload a library containing a public function with this in it, and you could publish the library while the code wouldn't compile for anyone trying to use it.

@jdonszelmann
Copy link
Contributor

note that these expressions (255u8 + 1 or 1u32 << 32) already fail to compile when they are imported in another crate. They do not need to be used in the other crate.

While in the same crate, they compile when not used but don't compile when used (not eliminated as dead code)

@jdonszelmann
Copy link
Contributor

jdonszelmann commented Sep 13, 2022

So I have taken a look through the compiler. I'm a bit new to this, so I may be wrong (though I already feel I learned quite a bit!)

I don't quite get why this bug occurs. It seems to me that this is the order of rust's passes:
Dead code elimination is part of rustc_passes:

image

However, having traced through the path of function calls that leads to the exact error we see, it seems that it is generated during the mir_build pass.

image

That makes it look to me as if dead code elimination is not the problem here. That was actually my hypothesis. That means, either I'm wrong and I don't know the rust compiler well enough yet (not at all unlikely) or something more interesting is going in here (:eyes:)

@jdonszelmann
Copy link
Contributor

Could this be related? #70660

@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed needs-triage-legacy labels Apr 17, 2024
@fmease fmease added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. and removed A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants