Skip to content

Commit

Permalink
Document FallbackToConstRef and make sure we don't accidentally use it
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Sep 24, 2020
1 parent 9550ca6 commit fc9f294
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Expand Up @@ -63,8 +63,23 @@ struct ConstToPat<'a, 'tcx> {
include_lint_checks: bool,
}

#[derive(Debug)]
struct FallbackToConstRef;
mod fallback_to_const_ref {
#[derive(Debug)]
/// This error type signals that we encountered a non-struct-eq situation behind a reference.
/// We bubble this up in order to get back to the reference destructuring and make that emit
/// a const pattern instead of a deref pattern. This allows us to simply call `PartialEq::eq`
/// on such patterns (since that function takes a reference) and not have to jump through any
/// hoops to get a reference to the value.
pub(super) struct FallbackToConstRef(());

pub(super) fn fallback_to_const_ref<'a, 'tcx>(
c2p: &super::ConstToPat<'a, 'tcx>,
) -> FallbackToConstRef {
assert!(c2p.behind_reference.get());
FallbackToConstRef(())
}
}
use fallback_to_const_ref::{fallback_to_const_ref, FallbackToConstRef};

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

0 comments on commit fc9f294

Please sign in to comment.