Skip to content

Commit 36ff82b

Browse files
authored
Rollup merge of rust-lang#120423 - RalfJung:indirect-structural-match, r=petrochenkov
update indirect structural match lints to match RFC and to show up for dependencies This is a large step towards implementing rust-lang/rfcs#3535. We currently have five lints related to "the structural match situation": - nontrivial_structural_match - indirect_structural_match - pointer_structural_match - const_patterns_without_partial_eq - illegal_floating_point_literal_pattern This PR concerns the first 3 of them. (The 4th already is set up to show for dependencies, and the 5th is removed by rust-lang#116284.) nontrivial_structural_match is being removed as per the RFC; the other two are enabled to show up in dependencies. Fixes rust-lang#73448 by removing the affected analysis.
2 parents 93fb121 + efbfb04 commit 36ff82b

30 files changed

+587
-368
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-28
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use std::mem;
2020
use std::ops::{ControlFlow, Deref};
2121

2222
use super::ops::{self, NonConstOp, Status};
23-
use super::qualifs::{self, CustomEq, HasMutInterior, NeedsDrop, NeedsNonConstDrop};
23+
use super::qualifs::{self, HasMutInterior, NeedsDrop, NeedsNonConstDrop};
2424
use super::resolver::FlowSensitiveAnalysis;
2525
use super::{ConstCx, Qualif};
2626
use crate::const_eval::is_unstable_const_fn;
@@ -149,37 +149,10 @@ impl<'mir, 'tcx> Qualifs<'mir, 'tcx> {
149149

150150
let return_loc = ccx.body.terminator_loc(return_block);
151151

152-
let custom_eq = match ccx.const_kind() {
153-
// We don't care whether a `const fn` returns a value that is not structurally
154-
// matchable. Functions calls are opaque and always use type-based qualification, so
155-
// this value should never be used.
156-
hir::ConstContext::ConstFn => true,
157-
158-
// If we know that all values of the return type are structurally matchable, there's no
159-
// need to run dataflow.
160-
// Opaque types do not participate in const generics or pattern matching, so we can safely count them out.
161-
_ if ccx.body.return_ty().has_opaque_types()
162-
|| !CustomEq::in_any_value_of_ty(ccx, ccx.body.return_ty()) =>
163-
{
164-
false
165-
}
166-
167-
hir::ConstContext::Const { .. } | hir::ConstContext::Static(_) => {
168-
let mut cursor = FlowSensitiveAnalysis::new(CustomEq, ccx)
169-
.into_engine(ccx.tcx, ccx.body)
170-
.iterate_to_fixpoint()
171-
.into_results_cursor(ccx.body);
172-
173-
cursor.seek_after_primary_effect(return_loc);
174-
cursor.get().contains(RETURN_PLACE)
175-
}
176-
};
177-
178152
ConstQualifs {
179153
needs_drop: self.needs_drop(ccx, RETURN_PLACE, return_loc),
180154
needs_non_const_drop: self.needs_non_const_drop(ccx, RETURN_PLACE, return_loc),
181155
has_mut_interior: self.has_mut_interior(ccx, RETURN_PLACE, return_loc),
182-
custom_eq,
183156
tainted_by_errors,
184157
}
185158
}

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+1-31
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_middle::mir::*;
1010
use rustc_middle::traits::BuiltinImplSource;
1111
use rustc_middle::ty::{self, AdtDef, GenericArgsRef, Ty};
1212
use rustc_trait_selection::traits::{
13-
self, ImplSource, Obligation, ObligationCause, ObligationCtxt, SelectionContext,
13+
ImplSource, Obligation, ObligationCause, ObligationCtxt, SelectionContext,
1414
};
1515

1616
use super::ConstCx;
@@ -24,7 +24,6 @@ pub fn in_any_value_of_ty<'tcx>(
2424
has_mut_interior: HasMutInterior::in_any_value_of_ty(cx, ty),
2525
needs_drop: NeedsDrop::in_any_value_of_ty(cx, ty),
2626
needs_non_const_drop: NeedsNonConstDrop::in_any_value_of_ty(cx, ty),
27-
custom_eq: CustomEq::in_any_value_of_ty(cx, ty),
2827
tainted_by_errors,
2928
}
3029
}
@@ -213,35 +212,6 @@ impl Qualif for NeedsNonConstDrop {
213212
}
214213
}
215214

216-
/// A constant that cannot be used as part of a pattern in a `match` expression.
217-
pub struct CustomEq;
218-
219-
impl Qualif for CustomEq {
220-
const ANALYSIS_NAME: &'static str = "flow_custom_eq";
221-
222-
fn in_qualifs(qualifs: &ConstQualifs) -> bool {
223-
qualifs.custom_eq
224-
}
225-
226-
fn in_any_value_of_ty<'tcx>(cx: &ConstCx<'_, 'tcx>, ty: Ty<'tcx>) -> bool {
227-
// If *any* component of a composite data type does not implement `Structural{Partial,}Eq`,
228-
// we know that at least some values of that type are not structural-match. I say "some"
229-
// because that component may be part of an enum variant (e.g.,
230-
// `Option::<NonStructuralMatchTy>::Some`), in which case some values of this type may be
231-
// structural-match (`Option::None`).
232-
traits::search_for_structural_match_violation(cx.body.span, cx.tcx, ty).is_some()
233-
}
234-
235-
fn in_adt_inherently<'tcx>(
236-
cx: &ConstCx<'_, 'tcx>,
237-
def: AdtDef<'tcx>,
238-
args: GenericArgsRef<'tcx>,
239-
) -> bool {
240-
let ty = Ty::new_adt(cx.tcx, def, args);
241-
!ty.is_structural_eq_shallow(cx.tcx)
242-
}
243-
}
244-
245215
// FIXME: Use `mir::visit::Visitor` for the `in_*` functions if/when it supports early return.
246216

247217
/// Returns `true` if this `Rvalue` contains qualif `Q`.

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ fn register_builtins(store: &mut LintStore) {
516516
"converted into hard error, see PR #118649 \
517517
<https://github.com/rust-lang/rust/pull/118649> for more information",
518518
);
519+
store.register_removed(
520+
"nontrivial_structural_match",
521+
"no longer needed, see RFC #3535 \
522+
<https://rust-lang.github.io/rfcs/3535-constants-in-patterns.html> for more information",
523+
);
519524
}
520525

521526
fn register_internals(store: &mut LintStore) {

compiler/rustc_lint_defs/src/builtin.rs

+7-44
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
//! These are the built-in lints that are emitted direct in the main
44
//! compiler code, rather than using their own custom pass. Those
55
//! lints are all available in `rustc_lint::builtin`.
6+
//!
7+
//! When removing a lint, make sure to also add a call to `register_removed` in
8+
//! compiler/rustc_lint/src/lib.rs.
69
710
use crate::{declare_lint, declare_lint_pass, FutureIncompatibilityReason};
811
use rustc_span::edition::Edition;
@@ -67,7 +70,6 @@ declare_lint_pass! {
6770
MUST_NOT_SUSPEND,
6871
NAMED_ARGUMENTS_USED_POSITIONALLY,
6972
NON_EXHAUSTIVE_OMITTED_PATTERNS,
70-
NONTRIVIAL_STRUCTURAL_MATCH,
7173
ORDER_DEPENDENT_TRAIT_OBJECTS,
7274
OVERLAPPING_RANGE_ENDPOINTS,
7375
PATTERNS_IN_FNS_WITHOUT_BODY,
@@ -2330,8 +2332,8 @@ declare_lint! {
23302332
Warn,
23312333
"constant used in pattern contains value of non-structural-match type in a field or a variant",
23322334
@future_incompatible = FutureIncompatibleInfo {
2333-
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
2334-
reference: "issue #62411 <https://github.com/rust-lang/rust/issues/62411>",
2335+
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2336+
reference: "issue #120362 <https://github.com/rust-lang/rust/issues/120362>",
23352337
};
23362338
}
23372339

@@ -2386,47 +2388,8 @@ declare_lint! {
23862388
Warn,
23872389
"pointers are not structural-match",
23882390
@future_incompatible = FutureIncompatibleInfo {
2389-
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
2390-
reference: "issue #62411 <https://github.com/rust-lang/rust/issues/70861>",
2391-
};
2392-
}
2393-
2394-
declare_lint! {
2395-
/// The `nontrivial_structural_match` lint detects constants that are used in patterns,
2396-
/// whose type is not structural-match and whose initializer body actually uses values
2397-
/// that are not structural-match. So `Option<NotStructuralMatch>` is ok if the constant
2398-
/// is just `None`.
2399-
///
2400-
/// ### Example
2401-
///
2402-
/// ```rust,compile_fail
2403-
/// #![deny(nontrivial_structural_match)]
2404-
///
2405-
/// #[derive(Copy, Clone, Debug)]
2406-
/// struct NoDerive(u32);
2407-
/// impl PartialEq for NoDerive { fn eq(&self, _: &Self) -> bool { false } }
2408-
/// impl Eq for NoDerive { }
2409-
/// fn main() {
2410-
/// const INDEX: Option<NoDerive> = [None, Some(NoDerive(10))][0];
2411-
/// match None { Some(_) => panic!("whoops"), INDEX => dbg!(INDEX), };
2412-
/// }
2413-
/// ```
2414-
///
2415-
/// {{produces}}
2416-
///
2417-
/// ### Explanation
2418-
///
2419-
/// Previous versions of Rust accepted constants in patterns, even if those constants' types
2420-
/// did not have `PartialEq` derived. Thus the compiler falls back to runtime execution of
2421-
/// `PartialEq`, which can report that two constants are not equal even if they are
2422-
/// bit-equivalent.
2423-
pub NONTRIVIAL_STRUCTURAL_MATCH,
2424-
Warn,
2425-
"constant used in pattern of non-structural-match type and the constant's initializer \
2426-
expression contains values of non-structural-match types",
2427-
@future_incompatible = FutureIncompatibleInfo {
2428-
reason: FutureIncompatibilityReason::FutureReleaseErrorDontReportInDeps,
2429-
reference: "issue #73448 <https://github.com/rust-lang/rust/issues/73448>",
2391+
reason: FutureIncompatibilityReason::FutureReleaseErrorReportInDeps,
2392+
reference: "issue #120362 <https://github.com/rust-lang/rust/issues/120362>",
24302393
};
24312394
}
24322395

compiler/rustc_middle/src/mir/query.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,14 @@ pub struct BorrowCheckResult<'tcx> {
189189

190190
/// The result of the `mir_const_qualif` query.
191191
///
192-
/// Each field (except `error_occurred`) corresponds to an implementer of the `Qualif` trait in
192+
/// Each field (except `tainted_by_errors`) corresponds to an implementer of the `Qualif` trait in
193193
/// `rustc_const_eval/src/transform/check_consts/qualifs.rs`. See that file for more information on each
194194
/// `Qualif`.
195195
#[derive(Clone, Copy, Debug, Default, TyEncodable, TyDecodable, HashStable)]
196196
pub struct ConstQualifs {
197197
pub has_mut_interior: bool,
198198
pub needs_drop: bool,
199199
pub needs_non_const_drop: bool,
200-
pub custom_eq: bool,
201200
pub tainted_by_errors: Option<ErrorGuaranteed>,
202201
}
203202

0 commit comments

Comments
 (0)