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

Restrict constants in patterns #32199

Merged
merged 10 commits into from
Mar 26, 2016

Commits on Mar 25, 2016

  1. Configuration menu
    Copy the full SHA
    5bc2868 View commit details
    Browse the repository at this point in the history
  2. modify #[deriving(Eq)] to emit #[structural_match]

    to careful use of the span from deriving, we
    can permit it in stable code if it derives from
    deriving (not-even-a-pun intended)
    nikomatsakis committed Mar 25, 2016
    Configuration menu
    Copy the full SHA
    99c2a6b View commit details
    Browse the repository at this point in the history
  3. do not overwrite spans as eagerly

    this was required to preserve the span from
    the #[structural_match] attribute -- but honestly
    I am not 100% sure if it makes sense.
    nikomatsakis committed Mar 25, 2016
    Configuration menu
    Copy the full SHA
    05baf64 View commit details
    Browse the repository at this point in the history
  4. issue a future-compat lint for constants of invalid type

    This is a [breaking-change]: according to RFC rust-lang#1445, constants used as
    patterns must be of a type that *derives* `Eq`. If you encounter a
    problem, you are most likely using a constant in an expression where the
    type of the constant is some struct that does not currently implement
    `Eq`. Something like the following:
    
    ```rust
    struct SomeType { ... }
    const SOME_CONST: SomeType = ...;
    
    match foo {
        SOME_CONST => ...
    }
    ```
    
    The easiest and most future compatible fix is to annotate the type in
    question with `#[derive(Eq)]` (note that merely *implementing* `Eq` is
    not enough, it must be *derived*):
    
    ```rust
    struct SomeType { ... }
    const SOME_CONST: SomeType = ...;
    
    match foo {
        SOME_CONST => ...
    }
    ```
    
    Another good option is to rewrite the match arm to use an `if`
    condition (this is also particularly good for floating point types,
    which implement `PartialEq` but not `Eq`):
    
    ```rust
    match foo {
        c if c == SOME_CONST => ...
    }
    ```
    
    Finally, a third alternative is to tag the type with
    `#[structural_match]`; but this is not recommended, as the attribute is
    never expected to be stabilized. Please see RFC rust-lang#1445 for more details.
    nikomatsakis committed Mar 25, 2016
    Configuration menu
    Copy the full SHA
    f69eb8e View commit details
    Browse the repository at this point in the history
  5. suppress duplicate lints

    nikomatsakis committed Mar 25, 2016
    Configuration menu
    Copy the full SHA
    73b4f06 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    56ebf2b View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    7f661ec View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    93e4443 View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    944dc4a View commit details
    Browse the repository at this point in the history
  10. fix error message

    nikomatsakis committed Mar 25, 2016
    Configuration menu
    Copy the full SHA
    2536ae5 View commit details
    Browse the repository at this point in the history