Skip to content

function pointers as match patterns have optimization-dependent behavior #70861

@pnkfelix

Description

@pnkfelix

I tried this code, supplied by @eddyb play:

#![feature(test)] use std::hint::black_box; fn foo() {} const FOO: fn() = foo; fn bar() {} const BAR: fn() = bar; #[inline(never)] fn print(s: &str) { println!("{}", s); } fn main() { match black_box(FOO) { FOO => print("FOO"), BAR => print("BAR"), _ => unreachable!(), } match black_box(FOO) { BAR => print("BAR"), FOO => print("FOO"), _ => unreachable!(), } match black_box(BAR) { FOO => print("FOO"), BAR => print("BAR"), _ => unreachable!(), } match black_box(BAR) { BAR => print("BAR"), FOO => print("FOO"), _ => unreachable!(), } }

I expected to see this happen: print out of

FOO FOO BAR BAR 

(and in debug builds, that is what happens).

Instead, this happened: in release builds, it prints:

FOO BAR FOO BAR 

Meta

This was on a playpen running 1.44.0-nightly, 2020-04-05.

This all arose from a conversation with @eddyb regarding structural match; you can read it on the zulip archive

cc #31434

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.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