diff options
| author | dianne <diannes.gm@gmail.com> | 2025-07-04 21:53:03 -0700 |
|---|---|---|
| committer | dianne <diannes.gm@gmail.com> | 2025-07-04 23:47:31 -0700 |
| commit | bb643159786c789c0b64f819d36a0979c63f9791 (patch) | |
| tree | 5425926c3529f74b57a549e749ca345e68eef482 | |
| parent | 50061f3b11f51d7a6e3acd8ce793a1f17f99b597 (diff) | |
| download | rust-bb643159786c789c0b64f819d36a0979c63f9791.tar.gz rust-bb643159786c789c0b64f819d36a0979c63f9791.zip | |
only check for mixed deref/normal constructors when needed
| -rw-r--r-- | compiler/rustc_mir_build/src/builder/scope.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_mir_build/src/thir/pattern/check_match.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_pattern_analysis/src/lib.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_pattern_analysis/src/rustc.rs | 16 | ||||
| -rw-r--r-- | compiler/rustc_pattern_analysis/src/usefulness.rs | 4 |
5 files changed, 26 insertions, 1 deletions
diff --git a/compiler/rustc_mir_build/src/builder/scope.rs b/compiler/rustc_mir_build/src/builder/scope.rs index 405d47c7c79..7fd3050012f 100644 --- a/compiler/rustc_mir_build/src/builder/scope.rs +++ b/compiler/rustc_mir_build/src/builder/scope.rs @@ -927,6 +927,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { scrut_span: rustc_span::Span::default(), refutable: true, known_valid_scrutinee: true, + internal_state: Default::default(), }; let valtree = match self.eval_unevaluated_mir_constant_to_valtree(constant) { diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index b7b160c738d..a49bfc1b8f4 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -406,6 +406,7 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> { scrut_span, refutable, known_valid_scrutinee, + internal_state: Default::default(), } } diff --git a/compiler/rustc_pattern_analysis/src/lib.rs b/compiler/rustc_pattern_analysis/src/lib.rs index 129fd38725c..66df35f9ee4 100644 --- a/compiler/rustc_pattern_analysis/src/lib.rs +++ b/compiler/rustc_pattern_analysis/src/lib.rs @@ -109,6 +109,11 @@ pub trait PatCx: Sized + fmt::Debug { ) { } + /// Check if we may need to perform additional deref-pattern-specific validation. + fn match_may_contain_deref_pats(&self) -> bool { + true + } + /// The current implementation of deref patterns requires that they can't match on the same /// place as a normal constructor. Since this isn't caught by type-checking, we check it in the /// `PatCx` before running the analysis. This reports an error if the check fails. diff --git a/compiler/rustc_pattern_analysis/src/rustc.rs b/compiler/rustc_pattern_analysis/src/rustc.rs index e9795126db6..ee72b676b38 100644 --- a/compiler/rustc_pattern_analysis/src/rustc.rs +++ b/compiler/rustc_pattern_analysis/src/rustc.rs @@ -1,3 +1,4 @@ +use std::cell::Cell; use std::fmt; use std::iter::once; @@ -99,6 +100,16 @@ pub struct RustcPatCtxt<'p, 'tcx: 'p> { /// Whether the data at the scrutinee is known to be valid. This is false if the scrutinee comes /// from a union field, a pointer deref, or a reference deref (pending opsem decisions). pub known_valid_scrutinee: bool, + pub internal_state: RustcPatCtxtState, +} + +/// Private fields of [`RustcPatCtxt`], separated out to permit record initialization syntax. +#[derive(Clone, Default)] +pub struct RustcPatCtxtState { + /// Has a deref pattern been lowered? This is initialized to `false` and is updated by + /// [`RustcPatCtxt::lower_pat`] in order to avoid performing deref-pattern-specific validation + /// for everything containing patterns. + has_lowered_deref_pat: Cell<bool>, } impl<'p, 'tcx: 'p> fmt::Debug for RustcPatCtxt<'p, 'tcx> { @@ -474,6 +485,7 @@ impl<'p, 'tcx: 'p> RustcPatCtxt<'p, 'tcx> { fields = vec![self.lower_pat(subpattern).at_index(0)]; arity = 1; ctor = DerefPattern(cx.reveal_opaque_ty(subpattern.ty)); + self.internal_state.has_lowered_deref_pat.set(true); } PatKind::Leaf { subpatterns } | PatKind::Variant { subpatterns, .. } => { match ty.kind() { @@ -1028,6 +1040,10 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> { } } + fn match_may_contain_deref_pats(&self) -> bool { + self.internal_state.has_lowered_deref_pat.get() + } + fn report_mixed_deref_pat_ctors( &self, deref_pat: &crate::pat::DeconstructedPat<Self>, diff --git a/compiler/rustc_pattern_analysis/src/usefulness.rs b/compiler/rustc_pattern_analysis/src/usefulness.rs index fb94b4afebb..b1c646e9884 100644 --- a/compiler/rustc_pattern_analysis/src/usefulness.rs +++ b/compiler/rustc_pattern_analysis/src/usefulness.rs @@ -1837,7 +1837,9 @@ pub fn compute_match_usefulness<'p, Cx: PatCx>( complexity_limit: usize, ) -> Result<UsefulnessReport<'p, Cx>, Cx::Error> { // The analysis doesn't support deref patterns mixed with normal constructors; error if present. - checks::detect_mixed_deref_pat_ctors(tycx, arms)?; + if tycx.match_may_contain_deref_pats() { + checks::detect_mixed_deref_pat_ctors(tycx, arms)?; + } let mut cx = UsefulnessCtxt { tycx, |
