Skip to content

Miscompile when comparing function pointers #54696

Closed
@dtolnay

Description

@dtolnay

From rustc 1.4.0 through 1.25.0, the following code compiles and prints true. This is the way I would expect it to work.

fn main() {
    println!("{}", main as fn() == main as fn());
}

On 1.26.0 and 1.27.0, compilation emits a warning and we get a segfault at runtime.

warning: constant evaluation error
 --> src/main.rs:2:20
  |
2 |     println!("{}", main as fn() == main as fn());
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "Pointer arithmetic or comparison" needs an rfc before being allowed inside constants
  |
  = note: #[warn(const_err)] on by default
Segmentation fault (core dumped)

On 1.28.0 the code fails to compile.

error: this expression will panic at runtime
 --> src/main.rs:2:20
  |
2 |     println!("{}", main as fn() == main as fn());
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "Pointer arithmetic or comparison" needs an rfc before being allowed inside constants
  |
  = note: #[deny(const_err)] on by default

On 1.29.0 through rustc 1.30.0-nightly (bb0896a 2018-09-29) the code compiles but is not correct.

Illegal instruction (core dumped)

Mentioning @oli-obk because your name is on this line so you may know what's up:

ConstEvalError::NeedsRfc("Pointer arithmetic or comparison".to_string()).into(),

Metadata

Metadata

Assignees

No one assigned

    Labels

    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions