Skip to content

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

Open

Description

@RalfJung

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

Activity

  1. 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[/+] on Jan 22, 2021
  2. oli-obk commented on Jan 22, 2021

    @oli-obk
    Contributor

    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?

  3. RalfJung commented on Jan 22, 2021

    @RalfJung
    MemberAuthor

    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.^^

  4. oli-obk commented on Jan 22, 2021

    @oli-obk
    Contributor

    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.

  5. RalfJung commented on Jan 22, 2021

    @RalfJung
    MemberAuthor

    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.

  6. oli-obk commented on Jan 22, 2021

    @oli-obk
    Contributor

    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.

  7. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on Jan 23, 2021
  8. added
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jan 23, 2021
  9. added and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Jan 27, 2021
  10. apiraino commented on Jan 27, 2021

    @apiraino
    Contributor

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

  11. emilyalbini commented on May 4, 2021

    @emilyalbini
    Member

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

  12. RalfJung commented on May 4, 2021

    @RalfJung
    MemberAuthor

    @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.

  13. emilyalbini commented on May 4, 2021

    @emilyalbini
    Member

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

  14. added this to the 1.51.0 milestone on May 4, 2021
  15. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    and removed on May 4, 2021
  16. emilyalbini commented on May 4, 2021

    @emilyalbini
    Member

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

  17. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    and removed on Dec 1, 2024
  18. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    and removed
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-bugCategory: This is a bug.P-mediumMedium priorityregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions