Skip to content

Pattern guard problem replacing FP literal pattern #42663

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
leonardo-m opened this issue Jun 14, 2017 · 2 comments
Closed

Pattern guard problem replacing FP literal pattern #42663

leonardo-m opened this issue Jun 14, 2017 · 2 comments
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. I-needs-decision Issue: In need of a decision. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

This is an Rust language ergonomy problem regarding pattern matching that contains floating point values.

This is a reduction of code that was OK:

#![feature(box_patterns, box_syntax)]
#![allow(illegal_floating_point_literal_pattern)]
#![allow(dead_code)]

enum Exp {
    Const(f64),
    Sum(Box<Exp>, Box<Exp>)
}

fn simplify(e: Exp) -> Exp {
    use Exp::*;
    match e {
        Sum(box Const(0.0), box x) | Sum(box x, box Const(0.0))
            => simplify(x),
        Sum(box Const(l), box Const(r))
            => Const(l + r),
        _ => e,
    }
}

fn main() {}

Those FP patterns will go away, so I've tried to replace them like this:

#![feature(box_patterns, box_syntax)]
#![allow(dead_code)]

enum Exp {
    Const(f64),
    Sum(Box<Exp>, Box<Exp>)
}

fn simplify(e: Exp) -> Exp {
    use Exp::*;
    match e {
        Sum(box Const(z), box x) | Sum(box x, box Const(z))
            if z == 0.0 => simplify(x),
        Sum(box Const(l), box Const(r))
            => Const(l + r),
        _ => e,
    }
}

fn main() {}

But I get errors:

error[E0008]: cannot bind by-move into a pattern guard
  --> ...\test.rs:12:31
   |
12 |         Sum(box Const(z), box x) | Sum(box x, box Const(z))
   |                               ^ moves value into pattern guard

error[E0008]: cannot bind by-move into a pattern guard
  --> ...\test.rs:12:44
   |
12 |         Sum(box Const(z), box x) | Sum(box x, box Const(z))
   |                                            ^ moves value into pattern guard

The pattern guard is "if z == 0.0" it uses just z and a FP contanst, it doesn't touch the x value.

I'd like ergonomy improvements to solve this problem in some nice way at language level (or Prelude level).

See also the short thread:
https://users.rust-lang.org/t/pattern-guard-problem-replacing-fp-literal-pattern/11207

@Mark-Simulacrum Mark-Simulacrum added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added C-question C-bug Category: This is a bug. I-needs-decision Issue: In need of a decision. T-lang Relevant to the language team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. and removed C-question C-bug Category: This is a bug. labels Jul 27, 2017
@leoyvens
Copy link
Contributor

Dupe of #15287.

@lcnr
Copy link
Contributor

lcnr commented May 18, 2021

this now compiles

@lcnr lcnr closed this as completed May 18, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-feature-request Category: A feature request, i.e: not implemented / a PR. I-needs-decision Issue: In need of a decision. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants