Skip to content

Derives can conflict with constants #49679

Closed
@Manishearth

Description

@Manishearth
// pub const __self_0: u8 = 1;
pub const __cmp: u8 = 1;

#[derive(PartialOrd, PartialEq)]
pub enum Foo {
    A(u8), B(u8)
}

fn main() {
}

(playpen)

With __cmp uncommented we get errors like:

error[E0308]: mismatched types
 --> src/main.rs:7:7
  |
7 |     A(u8), B(u8)
  |       ^^^ expected enum `std::option::Option`, found u8
  |
  = note: expected type `std::option::Option<std::cmp::Ordering>`
             found type `u8`

error[E0308]: match arms have incompatible types
 --> src/main.rs:7:7
  |
7 |     A(u8), B(u8)
  |       ^^^
  |       |
  |       expected enum `std::option::Option`, found u8
  |       match arm with an incompatible type
  |
  = note: expected type `std::option::Option<std::cmp::Ordering>`
             found type `u8`

With __self_0 uncommented we get errors like:

error[E0530]: match bindings cannot shadow constants
 --> src/main.rs:7:7
  |
2 | pub const __self_0: u8 = 1;
  | --------------------------- a constant `__self_0` is defined here
...
7 |     A(u8), B(u8)
  |       ^^^ cannot be named the same as a constant

This is basically because they generate matches on variables named __cmp and __self_# and __arg_#_#, and rustc doesn't like it when you conflict those with constants.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-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