Skip to content

Match with functions: first arm always triggered #44333

Closed
@MageSlayer

Description

@MageSlayer

Hi all

Following code produces always the same output in console and emits

warning: unreachable pattern
  --> src/main.rs:31:5
   |
31 |     plus => println!("plus"),
   |     ^^^^ this is an unreachable pattern
   |
   = note: #[warn(unreachable_patterns)] on by default
note: this pattern matches any value

... in compilation.

type 
  TFunc = fn (i64,i64)->i64;
  
fn plus(a:i64, b:i64)->i64 {
  a+b
}

fn minus(a:i64, b:i64)->i64 {
  a-b
}

fn other(a:i64, b:i64)->i64 {
  0
}

fn test(c:i64) -> TFunc {
  match c {
    0 => plus,
    1 => minus,
    _ => other
  }
}

extern crate rand;
use rand::Rng;
fn main() {
  let mut rng = rand::thread_rng();
  for i in 0..10 {
  match test( rng.gen() ) {
    minus => println!("minus"),
    plus => println!("plus"),
    _ => println!("other"),
  }
  }
}

Tested in Rust playground in nightly.
Any comments?

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-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