Rollup merge of #122835 - compiler-errors:deref-pure, r=Nadrieril
Require `DerefMut` and `DerefPure` on `deref!()` patterns when appropriate Waiting on the deref pattern syntax pr to merge r? nadrieril
This commit is contained in:
commit
0029a11d7d
14 changed files with 124 additions and 5 deletions
|
@ -199,6 +199,7 @@ language_item_table! {
|
|||
|
||||
Deref, sym::deref, deref_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DerefMut, sym::deref_mut, deref_mut_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DerefPure, sym::deref_pure, deref_pure_trait, Target::Trait, GenericRequirement::Exact(0);
|
||||
DerefTarget, sym::deref_target, deref_target, Target::AssocTy, GenericRequirement::None;
|
||||
Receiver, sym::receiver, receiver_trait, Target::Trait, GenericRequirement::None;
|
||||
|
||||
|
|
|
@ -2002,8 +2002,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
pat_info: PatInfo<'tcx, '_>,
|
||||
) -> Ty<'tcx> {
|
||||
let tcx = self.tcx;
|
||||
// FIXME(deref_patterns): use `DerefPure` for soundness
|
||||
// FIXME(deref_patterns): use `DerefMut` when required
|
||||
// Register a `DerefPure` bound, which is required by all `deref!()` pats.
|
||||
self.register_bound(
|
||||
expected,
|
||||
tcx.require_lang_item(hir::LangItem::DerefPure, Some(span)),
|
||||
self.misc(span),
|
||||
);
|
||||
// <expected as Deref>::Target
|
||||
let ty = Ty::new_projection(
|
||||
tcx,
|
||||
|
@ -2013,6 +2017,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
let ty = self.normalize(span, ty);
|
||||
let ty = self.try_structurally_resolve_type(span, ty);
|
||||
self.check_pat(inner, ty, pat_info);
|
||||
|
||||
// Check if the pattern has any `ref mut` bindings, which would require
|
||||
// `DerefMut` to be emitted in MIR building instead of just `Deref`.
|
||||
// We do this *after* checking the inner pattern, since we want to make
|
||||
// sure to apply any match-ergonomics adjustments.
|
||||
if self.typeck_results.borrow().pat_has_ref_mut_binding(inner) {
|
||||
self.register_bound(
|
||||
expected,
|
||||
tcx.require_lang_item(hir::LangItem::DerefMut, Some(span)),
|
||||
self.misc(span),
|
||||
);
|
||||
}
|
||||
|
||||
expected
|
||||
}
|
||||
|
||||
|
|
|
@ -430,6 +430,31 @@ impl<'tcx> TypeckResults<'tcx> {
|
|||
LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.pat_adjustments }
|
||||
}
|
||||
|
||||
/// Does the pattern recursively contain a `ref mut` binding in it?
|
||||
///
|
||||
/// This is used to determined whether a `deref` pattern should emit a `Deref`
|
||||
/// or `DerefMut` call for its pattern scrutinee.
|
||||
///
|
||||
/// This is computed from the typeck results since we want to make
|
||||
/// sure to apply any match-ergonomics adjustments, which we cannot
|
||||
/// determine from the HIR alone.
|
||||
pub fn pat_has_ref_mut_binding(&self, pat: &'tcx hir::Pat<'tcx>) -> bool {
|
||||
let mut has_ref_mut = false;
|
||||
pat.walk(|pat| {
|
||||
if let hir::PatKind::Binding(_, id, _, _) = pat.kind
|
||||
&& let Some(ty::BindByReference(ty::Mutability::Mut)) =
|
||||
self.pat_binding_modes().get(id)
|
||||
{
|
||||
has_ref_mut = true;
|
||||
// No need to continue recursing
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
has_ref_mut
|
||||
}
|
||||
|
||||
/// For a given closure, returns the iterator of `ty::CapturedPlace`s that are captured
|
||||
/// by the closure.
|
||||
pub fn closure_min_captures_flattened(
|
||||
|
|
|
@ -674,6 +674,7 @@ symbols! {
|
|||
deref_mut,
|
||||
deref_mut_method,
|
||||
deref_patterns,
|
||||
deref_pure,
|
||||
deref_target,
|
||||
derive,
|
||||
derive_const,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue