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

"arithmetic_overflow" and "unconditional_panic" lints do not fire on const fn that is only const-used in bin crate #81265

Open
RalfJung opened this issue Jan 22, 2021 · 10 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. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another.
Milestone

Comments

@RalfJung
Copy link
Member

RalfJung commented Jan 22, 2021

I tried the following code:

const fn overflow() -> u32 {
    0 - 1
}

pub const X: u32 = overflow();

fn main() {}

On stable, in release mode, the emits a lint (err-by-default) about the arithmetic overflow. On nightly, it does not.

This probably is related to #78407. However, the part I do not understand is that I can only reproduce the problem with a binary crate, not with a library crate. Cc @oli-obk

@RalfJung RalfJung changed the title "arithmetic_overflow" and "unconditional_panic" lints do not fire on const fn in bin crate that is only const-used "arithmetic_overflow" and "unconditional_panic" lints do not fire on const fn that is only const-used in bin crate Jan 22, 2021
@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2021

That is odd indeed. I think it is a bug. Apparently the library crate will build the optimized_mir for overflow even though overflow is not public.

Is it a problem that we do not emit these lints on const fn that are only used in constants?

@RalfJung
Copy link
Member Author

Is it a problem that we do not emit these lints on const fn that are only used in constants?

Yeah I think so. We should properly emit lints for all fn, no matter if they are const or not and no matter if they are used or not and no matter if this is a bin crate or a lib crate.

Emiting lints as part of an optimizations makes this hard. To me that just shows it's a mistake to emit lints as part of an optimization.^^

@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2021

Note that there are a bunch of lints that we do not emit in dead code. I feel like this is a similar situation. Either the lint is triggered on dead code in the const fn, or the interpretation of the const fn as part of the interpretation of a const item will report the same error anyway.

@RalfJung
Copy link
Member Author

Note that there are a bunch of lints that we do not emit in dead code.

If this is a dead code argument, then I assume we certainly should still emit the lint in a lib crate if the fn is pub?

or the interpretation of the const fn as part of the interpretation of a const item will report the same error anyway

No error will be reported in release mode, since overflow checks are disabled. So the fact that there is an overflow is entirely lost when no lint is emitted.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 22, 2021

I assume we certainly should still emit the lint in a lib crate if the fn is pub?

Yes, and we would, because libraries generate optimized_mir for all their pub functions

No error will be reported in release mode, since overflow checks are disabled. So the fact that there is an overflow is entirely lost when no lint is emitted.

ugh, right.

So... if we want these lints unconditionally, we can run const prop just in analysis mode in the place where we also run borrowck and similar guaranteed MIR passes. in the optimization pipeline we'd then run const prop in mutating mode and possibly even not report any diagnostics from there.

@camelid camelid added A-const-fn A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels Jan 23, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jan 23, 2021
@camelid camelid added the C-bug Category: This is a bug. label Jan 23, 2021
@apiraino apiraino added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jan 27, 2021
@apiraino
Copy link
Contributor

Assigning P-medium as discussed as part of the Prioritization Working Group procedure and removing I-prioritize.

@pietroalbini
Copy link
Member

I'm a bit confused about this being labeled a regression, it seems to correctly emit an error on stable, beta and nightly.

@RalfJung
Copy link
Member Author

RalfJung commented May 4, 2021

@pietroalbini try doing a release build. Then it does not error on nightly, but AFAIK it used to error in the past (stable at the time the report was made). I will edit the OP to mention the "release" bit.

@pietroalbini
Copy link
Member

Ugh. Then this does not only affect nightly, this affects at least stable 1.51.0.

@pietroalbini pietroalbini added this to the 1.51.0 milestone May 4, 2021
@pietroalbini pietroalbini added regression-from-stable-to-beta Performance or correctness regression from stable to beta. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels May 4, 2021
@pietroalbini
Copy link
Member

Yeah the PR you mention could've caused it landed in 1.51.0. Updated the labels accordingly.

@RalfJung RalfJung added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) and removed A-const-fn labels Dec 1, 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. P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another.
Projects
None yet
Development

No branches or pull requests

7 participants