Skip to content

Commit fc9f294

Browse files
committed
Document FallbackToConstRef and make sure we don't accidentally use it
1 parent 9550ca6 commit fc9f294

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,23 @@ struct ConstToPat<'a, 'tcx> {
6363
include_lint_checks: bool,
6464
}
6565

66-
#[derive(Debug)]
67-
struct FallbackToConstRef;
66+
mod fallback_to_const_ref {
67+
#[derive(Debug)]
68+
/// This error type signals that we encountered a non-struct-eq situation behind a reference.
69+
/// We bubble this up in order to get back to the reference destructuring and make that emit
70+
/// a const pattern instead of a deref pattern. This allows us to simply call `PartialEq::eq`
71+
/// on such patterns (since that function takes a reference) and not have to jump through any
72+
/// hoops to get a reference to the value.
73+
pub(super) struct FallbackToConstRef(());
74+
75+
pub(super) fn fallback_to_const_ref<'a, 'tcx>(
76+
c2p: &super::ConstToPat<'a, 'tcx>,
77+
) -> FallbackToConstRef {
78+
assert!(c2p.behind_reference.get());
79+
FallbackToConstRef(())
80+
}
81+
}
82+
use fallback_to_const_ref::{fallback_to_const_ref, FallbackToConstRef};
6883

6984
impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
7085
fn new(
@@ -314,7 +329,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
314329
// Since we are behind a reference, we can just bubble the error up so we get a
315330
// constant at reference type, making it easy to let the fallback call
316331
// `PartialEq::eq` on it.
317-
return Err(FallbackToConstRef);
332+
return Err(fallback_to_const_ref(self));
318333
}
319334
ty::Adt(adt_def, _) if !self.type_marked_structural(cv.ty) => {
320335
debug!("adt_def {:?} has !type_marked_structural for cv.ty: {:?}", adt_def, cv.ty);
@@ -447,7 +462,7 @@ impl<'a, 'tcx> ConstToPat<'a, 'tcx> {
447462
// very hard to invoke `PartialEq::eq` on it as a fallback.
448463
let val = match self.recur(tcx.deref_const(self.param_env.and(cv)), false) {
449464
Ok(subpattern) => PatKind::Deref { subpattern },
450-
Err(FallbackToConstRef) => PatKind::Constant { value: cv },
465+
Err(_) => PatKind::Constant { value: cv },
451466
};
452467
self.behind_reference.set(old);
453468
val

0 commit comments

Comments
 (0)