Skip to content

Matching on floating-point literal values is totally allowed and shouldn't be #41255

Closed
@carols10cents

Description

@carols10cents

Per this comment on the tracking issue for disallowing the use of constants set to floating point numbers (which errors as expected today), @nikomatsakis says the intention was to disallow floating point literal values in matches, but uh... this is totally accepted by Rust 1.16.0:

fn main() {
    let x = 13.4;

    match x {
        1.0 => println!("one"),
        22.4 => println!("two"),
        3.67 => println!("three"),
        13.4 => println!("thirteen point four"),
        _ => println!("anything"),
    }
}

This prints "thirteen point four". I expected to get a compiler error like the one I get if I change a floating point value in the match to a constant:

const FOO: f64 = 3.67;

fn main() {
    let x = 13.4;

    match x {
        1.0 => println!("one"),
        22.4 => println!("two"),
        FOO => println!("three"),
        13.4 => println!("thirteen point four"),
        _ => println!("anything"),
    }
}

results in:

error: floating point constants cannot be used in patterns, #[deny(illegal_floating_point_constant_pattern)] on by default
 --> src/main.rs:9:9
  |
9 |         FOO => println!("three"),
  |         ^^^
  |
  = 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 #36890 <https://github.com/rust-lang/rust/issues/36890>

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions