Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Derives can conflict with constants #49679

Closed
Manishearth opened this issue Apr 5, 2018 · 3 comments
Closed

Derives can conflict with constants #49679

Manishearth opened this issue Apr 5, 2018 · 3 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Manishearth
Copy link
Member

// 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.

@Manishearth Manishearth added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. labels Apr 5, 2018
@Manishearth
Copy link
Member Author

I first noticed the __cmp one -- that comes from a match that does:

match cmp(...) {
   Equal => ...
   __cmp => __cmp
}

This could be rewritten to do __cmp @ _ => __cmp but that hits the shadow error. It can also be rewritten to bind the result of cmp() to a temporary, which should work.

However there's no way at all of fixing the __self stuff.

The reason we have the match bindings shadow error is #33118 (comment), which we hopefully can fix? cc @petrochenkov

@eddyb
Copy link
Member

eddyb commented Apr 5, 2018

Note the difference between unhygienic old macros and hygienic new macros.

@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 20, 2020
@jonas-schievink
Copy link
Contributor

This appears to be fixed (the binding names are now different, but those still don't cause conflicts, presumably because they're gensym'd, or whatever our equivalent to that is)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants