Skip to content

Commit 5b7786c

Browse files
committed
make non-PartialEq-typed consts as patterns a hard error
1 parent e9f9594 commit 5b7786c

File tree

8 files changed

+27
-112
lines changed

8 files changed

+27
-112
lines changed

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ fn register_builtins(store: &mut LintStore) {
527527
"no longer needed, see #93367 \
528528
<https://github.com/rust-lang/rust/issues/93367> for more information",
529529
);
530+
store.register_removed(
531+
"const_patterns_without_partial_eq",
532+
"converted into hard error, see RFC #3535 \
533+
<https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information",
534+
);
530535
}
531536

532537
fn register_internals(store: &mut LintStore) {

compiler/rustc_lint_defs/src/builtin.rs

-52
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ declare_lint_pass! {
3232
CONFLICTING_REPR_HINTS,
3333
CONST_EVALUATABLE_UNCHECKED,
3434
CONST_ITEM_MUTATION,
35-
CONST_PATTERNS_WITHOUT_PARTIAL_EQ,
3635
DEAD_CODE,
3736
DEPRECATED,
3837
DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
@@ -2342,57 +2341,6 @@ declare_lint! {
23422341
};
23432342
}
23442343

2345-
declare_lint! {
2346-
/// The `const_patterns_without_partial_eq` lint detects constants that are used in patterns,
2347-
/// whose type does not implement `PartialEq`.
2348-
///
2349-
/// ### Example
2350-
///
2351-
/// ```rust,compile_fail
2352-
/// #![deny(const_patterns_without_partial_eq)]
2353-
///
2354-
/// trait EnumSetType {
2355-
/// type Repr;
2356-
/// }
2357-
///
2358-
/// enum Enum8 { }
2359-
/// impl EnumSetType for Enum8 {
2360-
/// type Repr = u8;
2361-
/// }
2362-
///
2363-
/// #[derive(PartialEq, Eq)]
2364-
/// struct EnumSet<T: EnumSetType> {
2365-
/// __enumset_underlying: T::Repr,
2366-
/// }
2367-
///
2368-
/// const CONST_SET: EnumSet<Enum8> = EnumSet { __enumset_underlying: 3 };
2369-
///
2370-
/// fn main() {
2371-
/// match CONST_SET {
2372-
/// CONST_SET => { /* ok */ }
2373-
/// _ => panic!("match fell through?"),
2374-
/// }
2375-
/// }
2376-
/// ```
2377-
///
2378-
/// {{produces}}
2379-
///
2380-
/// ### Explanation
2381-
///
2382-
/// Previous versions of Rust accepted constants in patterns, even if those constants' types
2383-
/// did not have `PartialEq` implemented. The compiler falls back to comparing the value
2384-
/// field-by-field. In the future we'd like to ensure that pattern matching always
2385-
/// follows `PartialEq` semantics, so that trait bound will become a requirement for
2386-
/// matching on constants.
2387-
pub CONST_PATTERNS_WITHOUT_PARTIAL_EQ,
2388-
Warn,
2389-
"constant in pattern does not implement `PartialEq`",
2390-
@future_incompatible = FutureIncompatibleInfo {
2391-
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2392-
reference: "issue #116122 <https://github.com/rust-lang/rust/issues/116122>",
2393-
};
2394-
}
2395-
23962344
declare_lint! {
23972345
/// The `ambiguous_associated_items` lint detects ambiguity between
23982346
/// [associated items] and [enum variants].

compiler/rustc_mir_build/src/errors.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,14 @@ pub struct TypeNotStructural<'tcx> {
767767
pub non_sm_ty: Ty<'tcx>,
768768
}
769769

770+
#[derive(Diagnostic)]
771+
#[diag(mir_build_non_partial_eq_match)]
772+
pub struct TypeNotPartialEq<'tcx> {
773+
#[primary_span]
774+
pub span: Span,
775+
pub non_peq_ty: Ty<'tcx>,
776+
}
777+
770778
#[derive(Diagnostic)]
771779
#[diag(mir_build_invalid_pattern)]
772780
pub struct InvalidPattern<'tcx> {
@@ -822,12 +830,6 @@ pub struct NontrivialStructuralMatch<'tcx> {
822830
pub non_sm_ty: Ty<'tcx>,
823831
}
824832

825-
#[derive(LintDiagnostic)]
826-
#[diag(mir_build_non_partial_eq_match)]
827-
pub struct NonPartialEqMatch<'tcx> {
828-
pub non_peq_ty: Ty<'tcx>,
829-
}
830-
831833
#[derive(Diagnostic)]
832834
#[diag(mir_build_pattern_not_covered, code = E0005)]
833835
pub(crate) struct PatternNotCovered<'s, 'tcx> {

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::cell::Cell;
1616

1717
use super::PatCtxt;
1818
use crate::errors::{
19-
IndirectStructuralMatch, InvalidPattern, NaNPattern, NonPartialEqMatch, PointerPattern,
19+
IndirectStructuralMatch, InvalidPattern, NaNPattern, PointerPattern, TypeNotPartialEq,
2020
TypeNotStructural, UnionPattern, UnsizedPattern,
2121
};
2222

@@ -208,15 +208,12 @@ impl<'tcx> ConstToPat<'tcx> {
208208
);
209209
}
210210

211-
// Always check for `PartialEq`, even if we emitted other lints. (But not if there were
212-
// any errors.) This ensures it shows up in cargo's future-compat reports as well.
211+
// Always check for `PartialEq` if we had no other errors yet.
213212
if !self.type_has_partial_eq_impl(cv.ty()) {
214-
self.tcx().emit_node_span_lint(
215-
lint::builtin::CONST_PATTERNS_WITHOUT_PARTIAL_EQ,
216-
self.id,
217-
self.span,
218-
NonPartialEqMatch { non_peq_ty: cv.ty() },
219-
);
213+
let err = TypeNotPartialEq { span: self.span, non_peq_ty: cv.ty() };
214+
let e = self.tcx().dcx().emit_err(err);
215+
let kind = PatKind::Error(e);
216+
return Box::new(Pat { span: self.span, ty: cv.ty(), kind });
220217
}
221218
}
222219

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#![deny(indirect_structural_match)]
2-
3-
//@ check-pass
4-
51
#[derive(PartialEq, Eq)]
62
enum O<T> {
73
Some(*const T), // Can also use PhantomData<T>
@@ -15,8 +11,7 @@ const C: &[O<B>] = &[O::None];
1511
fn main() {
1612
let x = O::None;
1713
match &[x][..] {
18-
C => (), //~WARN: the type must implement `PartialEq`
19-
//~| previously accepted
14+
C => (), //~ERROR: the type must implement `PartialEq`
2015
_ => (),
2116
}
2217
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
warning: to use a constant of type `&[O<B>]` in a pattern, the type must implement `PartialEq`
2-
--> $DIR/issue-65466.rs:18:9
1+
error: to use a constant of type `&[O<B>]` in a pattern, the type must implement `PartialEq`
2+
--> $DIR/issue-65466.rs:14:9
33
|
44
LL | C => (),
55
| ^
6-
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8-
= note: for more information, see issue #116122 <https://github.com/rust-lang/rust/issues/116122>
9-
= note: `#[warn(const_patterns_without_partial_eq)]` on by default
10-
11-
warning: 1 warning emitted
126

13-
Future incompatibility report: Future breakage diagnostic:
14-
warning: to use a constant of type `&[O<B>]` in a pattern, the type must implement `PartialEq`
15-
--> $DIR/issue-65466.rs:18:9
16-
|
17-
LL | C => (),
18-
| ^
19-
|
20-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
21-
= note: for more information, see issue #116122 <https://github.com/rust-lang/rust/issues/116122>
22-
= note: `#[warn(const_patterns_without_partial_eq)]` on by default
7+
error: aborting due to 1 previous error
238

tests/ui/match/issue-72896-non-partial-eq-const.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//@ run-pass
21
trait EnumSetType {
32
type Repr;
43
}
@@ -17,8 +16,7 @@ const CONST_SET: EnumSet<Enum8> = EnumSet { __enumset_underlying: 3 };
1716

1817
fn main() {
1918
match CONST_SET {
20-
CONST_SET => { /* ok */ } //~WARN: must implement `PartialEq`
21-
//~| previously accepted
19+
CONST_SET => { /* ok */ } //~ERROR: must implement `PartialEq`
2220
_ => panic!("match fell through?"),
2321
}
2422
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
1-
warning: to use a constant of type `EnumSet<Enum8>` in a pattern, the type must implement `PartialEq`
2-
--> $DIR/issue-72896-non-partial-eq-const.rs:20:9
1+
error: to use a constant of type `EnumSet<Enum8>` in a pattern, the type must implement `PartialEq`
2+
--> $DIR/issue-72896-non-partial-eq-const.rs:19:9
33
|
44
LL | CONST_SET => { /* ok */ }
55
| ^^^^^^^^^
6-
|
7-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8-
= note: for more information, see issue #116122 <https://github.com/rust-lang/rust/issues/116122>
9-
= note: `#[warn(const_patterns_without_partial_eq)]` on by default
10-
11-
warning: 1 warning emitted
126

13-
Future incompatibility report: Future breakage diagnostic:
14-
warning: to use a constant of type `EnumSet<Enum8>` in a pattern, the type must implement `PartialEq`
15-
--> $DIR/issue-72896-non-partial-eq-const.rs:20:9
16-
|
17-
LL | CONST_SET => { /* ok */ }
18-
| ^^^^^^^^^
19-
|
20-
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
21-
= note: for more information, see issue #116122 <https://github.com/rust-lang/rust/issues/116122>
22-
= note: `#[warn(const_patterns_without_partial_eq)]` on by default
7+
error: aborting due to 1 previous error
238

0 commit comments

Comments
 (0)