Auto merge of #115893 - RalfJung:match-require-partial-eq, r=oli-obk
lint towards rejecting consts in patterns that do not implement PartialEq I think we definitely don't want to allow such consts, so even while the general plan around structural matching is up in the air, we can start the process of getting non-PartialEq matches out of the ecosystem.
This commit is contained in:
commit
1f2bacf677
8 changed files with 139 additions and 11 deletions
|
@ -2311,6 +2311,57 @@ declare_lint! {
|
|||
};
|
||||
}
|
||||
|
||||
declare_lint! {
|
||||
/// The `const_patterns_without_partial_eq` lint detects constants that are used in patterns,
|
||||
/// whose type does not implement `PartialEq`.
|
||||
///
|
||||
/// ### Example
|
||||
///
|
||||
/// ```rust,compile_fail
|
||||
/// #![deny(const_patterns_without_partial_eq)]
|
||||
///
|
||||
/// trait EnumSetType {
|
||||
/// type Repr;
|
||||
/// }
|
||||
///
|
||||
/// enum Enum8 { }
|
||||
/// impl EnumSetType for Enum8 {
|
||||
/// type Repr = u8;
|
||||
/// }
|
||||
///
|
||||
/// #[derive(PartialEq, Eq)]
|
||||
/// struct EnumSet<T: EnumSetType> {
|
||||
/// __enumset_underlying: T::Repr,
|
||||
/// }
|
||||
///
|
||||
/// const CONST_SET: EnumSet<Enum8> = EnumSet { __enumset_underlying: 3 };
|
||||
///
|
||||
/// fn main() {
|
||||
/// match CONST_SET {
|
||||
/// CONST_SET => { /* ok */ }
|
||||
/// _ => panic!("match fell through?"),
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// {{produces}}
|
||||
///
|
||||
/// ### Explanation
|
||||
///
|
||||
/// Previous versions of Rust accepted constants in patterns, even if those constants' types
|
||||
/// did not have `PartialEq` implemented. The compiler falls back to comparing the value
|
||||
/// field-by-field. In the future we'd like to ensure that pattern matching always
|
||||
/// follows `PartialEq` semantics, so that trait bound will become a requirement for
|
||||
/// matching on constants.
|
||||
pub CONST_PATTERNS_WITHOUT_PARTIAL_EQ,
|
||||
Warn,
|
||||
"constant in pattern does not implement `PartialEq`",
|
||||
@future_incompatible = FutureIncompatibleInfo {
|
||||
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
|
||||
reference: "issue #116122 <https://github.com/rust-lang/rust/issues/116122>",
|
||||
};
|
||||
}
|
||||
|
||||
declare_lint! {
|
||||
/// The `ambiguous_associated_items` lint detects ambiguity between
|
||||
/// [associated items] and [enum variants].
|
||||
|
@ -3357,6 +3408,7 @@ declare_lint_pass! {
|
|||
CONFLICTING_REPR_HINTS,
|
||||
CONST_EVALUATABLE_UNCHECKED,
|
||||
CONST_ITEM_MUTATION,
|
||||
CONST_PATTERNS_WITHOUT_PARTIAL_EQ,
|
||||
DEAD_CODE,
|
||||
DEPRECATED,
|
||||
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue