Skip to content

Commit 179a6a0

Browse files
committed
remove IndirectStructuralMatch lint, emit the usual hard error instead
1 parent 79734f1 commit 179a6a0

31 files changed

+54
-398
lines changed

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ fn register_builtins(store: &mut LintStore) {
539539
"converted into hard error, see RFC #3535 \
540540
<https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information",
541541
);
542+
store.register_removed(
543+
"indirect_structural_match",
544+
"converted into hard error, see RFC #3535 \
545+
<https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information",
546+
);
542547
}
543548

544549
fn register_internals(store: &mut LintStore) {

compiler/rustc_lint_defs/src/builtin.rs

-47
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ declare_lint_pass! {
5050
HIDDEN_GLOB_REEXPORTS,
5151
ILL_FORMED_ATTRIBUTE_INPUT,
5252
INCOMPLETE_INCLUDE,
53-
INDIRECT_STRUCTURAL_MATCH,
5453
INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
5554
INLINE_NO_SANITIZE,
5655
INVALID_DOC_ATTRIBUTES,
@@ -2355,52 +2354,6 @@ declare_lint! {
23552354
"outlives requirements can be inferred"
23562355
}
23572356

2358-
declare_lint! {
2359-
/// The `indirect_structural_match` lint detects a `const` in a pattern
2360-
/// that manually implements [`PartialEq`] and [`Eq`].
2361-
///
2362-
/// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html
2363-
/// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html
2364-
///
2365-
/// ### Example
2366-
///
2367-
/// ```rust,compile_fail
2368-
/// #![deny(indirect_structural_match)]
2369-
///
2370-
/// struct NoDerive(i32);
2371-
/// impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
2372-
/// impl Eq for NoDerive { }
2373-
/// #[derive(PartialEq, Eq)]
2374-
/// struct WrapParam<T>(T);
2375-
/// const WRAP_INDIRECT_PARAM: & &WrapParam<NoDerive> = & &WrapParam(NoDerive(0));
2376-
/// fn main() {
2377-
/// match WRAP_INDIRECT_PARAM {
2378-
/// WRAP_INDIRECT_PARAM => { }
2379-
/// _ => { }
2380-
/// }
2381-
/// }
2382-
/// ```
2383-
///
2384-
/// {{produces}}
2385-
///
2386-
/// ### Explanation
2387-
///
2388-
/// The compiler unintentionally accepted this form in the past. This is a
2389-
/// [future-incompatible] lint to transition this to a hard error in the
2390-
/// future. See [issue #62411] for a complete description of the problem,
2391-
/// and some possible solutions.
2392-
///
2393-
/// [issue #62411]: https://github.com/rust-lang/rust/issues/62411
2394-
/// [future-incompatible]: ../index.md#future-incompatible-lints
2395-
pub INDIRECT_STRUCTURAL_MATCH,
2396-
Warn,
2397-
"constant used in pattern contains value of non-structural-match type in a field or a variant",
2398-
@future_incompatible = FutureIncompatibleInfo {
2399-
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2400-
reference: "issue #120362 <https://github.com/rust-lang/rust/issues/120362>",
2401-
};
2402-
}
2403-
24042357
declare_lint! {
24052358
/// The `deprecated_in_future` lint is internal to rustc and should not be
24062359
/// used by user code.

compiler/rustc_mir_build/messages.ftl

-6
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ mir_build_extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
109109
.note = extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
110110
.label = use of extern static
111111
112-
mir_build_indirect_structural_match =
113-
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`
114-
115112
mir_build_inform_irrefutable = `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
116113
117114
mir_build_initializing_type_with_requires_unsafe =
@@ -257,9 +254,6 @@ mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type
257254
mir_build_non_partial_eq_match =
258255
to use a constant of type `{$non_peq_ty}` in a pattern, the type must implement `PartialEq`
259256
260-
mir_build_nontrivial_structural_match =
261-
to use a constant of type `{$non_sm_ty}` in a pattern, the constant's initializer must be trivial or `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`
262-
263257
mir_build_pattern_not_covered = refutable pattern in {$origin}
264258
.pattern_ty = the matched value is of type `{$pattern_ty}`
265259

compiler/rustc_mir_build/src/errors.rs

-16
Original file line numberDiff line numberDiff line change
@@ -802,22 +802,6 @@ pub struct NonEmptyNeverPattern<'tcx> {
802802
pub ty: Ty<'tcx>,
803803
}
804804

805-
#[derive(LintDiagnostic)]
806-
#[diag(mir_build_indirect_structural_match)]
807-
#[note(mir_build_type_not_structural_tip)]
808-
#[note(mir_build_type_not_structural_more_info)]
809-
pub struct IndirectStructuralMatch<'tcx> {
810-
pub non_sm_ty: Ty<'tcx>,
811-
}
812-
813-
#[derive(LintDiagnostic)]
814-
#[diag(mir_build_nontrivial_structural_match)]
815-
#[note(mir_build_type_not_structural_tip)]
816-
#[note(mir_build_type_not_structural_more_info)]
817-
pub struct NontrivialStructuralMatch<'tcx> {
818-
pub non_sm_ty: Ty<'tcx>,
819-
}
820-
821805
#[derive(Diagnostic)]
822806
#[diag(mir_build_exceeds_mcdc_condition_num_limit)]
823807
pub(crate) struct MCDCExceedsConditionNumLimit {

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

+3-73
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::cell::Cell;
1616

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

2323
impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
@@ -49,15 +49,6 @@ struct ConstToPat<'tcx> {
4949
// value.
5050
saw_const_match_error: Cell<Option<ErrorGuaranteed>>,
5151

52-
// This tracks if we emitted some diagnostic for a given const value, so that
53-
// we will not subsequently issue an irrelevant lint for the same const
54-
// value.
55-
saw_const_match_lint: Cell<bool>,
56-
57-
// For backcompat we need to keep allowing non-structurally-eq types behind references.
58-
// See also all the `cant-hide-behind` tests.
59-
behind_reference: Cell<bool>,
60-
6152
// inference context used for checking `T: Structural` bounds.
6253
infcx: InferCtxt<'tcx>,
6354

@@ -84,8 +75,6 @@ impl<'tcx> ConstToPat<'tcx> {
8475
infcx,
8576
param_env: pat_ctxt.param_env,
8677
saw_const_match_error: Cell::new(None),
87-
saw_const_match_lint: Cell::new(false),
88-
behind_reference: Cell::new(false),
8978
treat_byte_string_as_slice: pat_ctxt
9079
.typeck_results
9180
.treat_byte_string_as_slice
@@ -197,7 +186,7 @@ impl<'tcx> ConstToPat<'tcx> {
197186
// complained about structural match violations there, so we don't
198187
// have to check anything any more.
199188
}
200-
} else if !have_valtree && !self.saw_const_match_lint.get() {
189+
} else if !have_valtree {
201190
// The only way valtree construction can fail without the structural match
202191
// checker finding a violation is if there is a pointer somewhere.
203192
self.tcx().emit_node_span_lint(
@@ -274,36 +263,11 @@ impl<'tcx> ConstToPat<'tcx> {
274263
cv: ValTree<'tcx>,
275264
ty: Ty<'tcx>,
276265
) -> Result<Box<Pat<'tcx>>, FallbackToOpaqueConst> {
277-
let id = self.id;
278266
let span = self.span;
279267
let tcx = self.tcx();
280268
let param_env = self.param_env;
281269

282270
let kind = match ty.kind() {
283-
// If the type is not structurally comparable, just emit the constant directly,
284-
// causing the pattern match code to treat it opaquely.
285-
// FIXME: This code doesn't emit errors itself, the caller emits the errors.
286-
// So instead of specific errors, you just get blanket errors about the whole
287-
// const type. See
288-
// https://github.com/rust-lang/rust/pull/70743#discussion_r404701963 for
289-
// details.
290-
// Backwards compatibility hack because we can't cause hard errors on these
291-
// types, so we compare them via `PartialEq::eq` at runtime.
292-
ty::Adt(..) if !self.type_marked_structural(ty) && self.behind_reference.get() => {
293-
if self.saw_const_match_error.get().is_none() && !self.saw_const_match_lint.get() {
294-
self.saw_const_match_lint.set(true);
295-
tcx.emit_node_span_lint(
296-
lint::builtin::INDIRECT_STRUCTURAL_MATCH,
297-
id,
298-
span,
299-
IndirectStructuralMatch { non_sm_ty: ty },
300-
);
301-
}
302-
// Since we are behind a reference, we can just bubble the error up so we get a
303-
// constant at reference type, making it easy to let the fallback call
304-
// `PartialEq::eq` on it.
305-
return Err(FallbackToOpaqueConst);
306-
}
307271
ty::FnDef(..) => {
308272
let e = tcx.dcx().emit_err(InvalidPattern { span, non_sm_ty: ty });
309273
self.saw_const_match_error.set(Some(e));
@@ -377,38 +341,6 @@ impl<'tcx> ConstToPat<'tcx> {
377341
ty::Str => {
378342
PatKind::Constant { value: mir::Const::Ty(ty::Const::new_value(tcx, cv, ty)) }
379343
}
380-
// Backwards compatibility hack: support references to non-structural types,
381-
// but hard error if we aren't behind a double reference. We could just use
382-
// the fallback code path below, but that would allow *more* of this fishy
383-
// code to compile, as then it only goes through the future incompat lint
384-
// instead of a hard error.
385-
ty::Adt(_, _) if !self.type_marked_structural(*pointee_ty) => {
386-
if self.behind_reference.get() {
387-
if self.saw_const_match_error.get().is_none()
388-
&& !self.saw_const_match_lint.get()
389-
{
390-
self.saw_const_match_lint.set(true);
391-
tcx.emit_node_span_lint(
392-
lint::builtin::INDIRECT_STRUCTURAL_MATCH,
393-
self.id,
394-
span,
395-
IndirectStructuralMatch { non_sm_ty: *pointee_ty },
396-
);
397-
}
398-
return Err(FallbackToOpaqueConst);
399-
} else {
400-
if let Some(e) = self.saw_const_match_error.get() {
401-
// We already errored. Signal that in the pattern, so that follow up errors can be silenced.
402-
PatKind::Error(e)
403-
} else {
404-
let err = TypeNotStructural { span, non_sm_ty: *pointee_ty };
405-
let e = tcx.dcx().emit_err(err);
406-
self.saw_const_match_error.set(Some(e));
407-
// We errored. Signal that in the pattern, so that follow up errors can be silenced.
408-
PatKind::Error(e)
409-
}
410-
}
411-
}
412344
// All other references are converted into deref patterns and then recursively
413345
// convert the dereferenced constant to a pattern that is the sub-pattern of the
414346
// deref pattern.
@@ -419,7 +351,6 @@ impl<'tcx> ConstToPat<'tcx> {
419351
// We errored. Signal that in the pattern, so that follow up errors can be silenced.
420352
PatKind::Error(e)
421353
} else {
422-
let old = self.behind_reference.replace(true);
423354
// `b"foo"` produces a `&[u8; 3]`, but you can't use constants of array type when
424355
// matching against references, you can only use byte string literals.
425356
// The typechecker has a special case for byte string literals, by treating them
@@ -434,7 +365,6 @@ impl<'tcx> ConstToPat<'tcx> {
434365
};
435366
// References have the same valtree representation as their pointee.
436367
let subpattern = self.recur(cv, pointee_ty)?;
437-
self.behind_reference.set(old);
438368
PatKind::Deref { subpattern }
439369
}
440370
}

tests/ui/consts/const_in_pattern/accept_structural.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//@ run-pass
22

33
#![allow(non_local_definitions)]
4-
#![warn(indirect_structural_match)]
54

65
// This test is checking our logic for structural match checking by enumerating
76
// the different kinds of const expressions. This test is collecting cases where

tests/ui/consts/const_in_pattern/cross-crate-fail.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ aux-build:consts.rs
22

3-
#![warn(indirect_structural_match)]
4-
53
extern crate consts;
64

75
struct Defaulted;

tests/ui/consts/const_in_pattern/cross-crate-fail.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq)]`
2-
--> $DIR/cross-crate-fail.rs:13:9
2+
--> $DIR/cross-crate-fail.rs:11:9
33
|
44
LL | consts::SOME => panic!(),
55
| ^^^^^^^^^^^^
@@ -8,7 +8,7 @@ LL | consts::SOME => panic!(),
88
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
99

1010
error: to use a constant of type `CustomEq` in a pattern, `CustomEq` must be annotated with `#[derive(PartialEq)]`
11-
--> $DIR/cross-crate-fail.rs:20:9
11+
--> $DIR/cross-crate-fail.rs:18:9
1212
|
1313
LL | <Defaulted as consts::AssocConst>::SOME => panic!(),
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/consts/const_in_pattern/cross-crate-pass.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//@ run-pass
22
//@ aux-build:consts.rs
33

4-
#![warn(indirect_structural_match)]
5-
64
extern crate consts;
75
use consts::CustomEq;
86

tests/ui/consts/const_in_pattern/custom-eq-branch-pass.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//@ run-pass
22

3-
#![warn(indirect_structural_match)]
4-
53
struct CustomEq;
64

75
impl Eq for CustomEq {}

tests/ui/consts/const_in_pattern/no-eq-branch-fail.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![warn(indirect_structural_match)]
2-
31
struct NoEq;
42

53
enum Foo {

tests/ui/consts/const_in_pattern/no-eq-branch-fail.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: to use a constant of type `Foo` in a pattern, `Foo` must be annotated with `#[derive(PartialEq)]`
2-
--> $DIR/no-eq-branch-fail.rs:21:9
2+
--> $DIR/no-eq-branch-fail.rs:19:9
33
|
44
LL | BAR_BAZ => panic!(),
55
| ^^^^^^^

tests/ui/consts/const_in_pattern/reject_non_structural.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
// See also RFC 1445
1212

1313
#![feature(type_ascription)]
14-
#![warn(indirect_structural_match)]
15-
//~^ NOTE lint level is defined here
1614

1715
#[derive(Copy, Clone, Debug)]
1816
struct NoPartialEq;
@@ -96,9 +94,7 @@ fn main() {
9694

9795
const ADDR_OF: &OND = &Some(NoDerive);
9896
match &Some(NoDerive) { ADDR_OF => dbg!(ADDR_OF), _ => panic!("whoops"), };
99-
//~^ WARN must be annotated with `#[derive(PartialEq)]`
97+
//~^ ERROR must be annotated with `#[derive(PartialEq)]`
10098
//~| NOTE the traits must be derived
10199
//~| NOTE StructuralPartialEq.html for details
102-
//~| WARN previously accepted by the compiler but is being phased out
103-
//~| NOTE for more information, see
104100
}

0 commit comments

Comments
 (0)