mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 16:24:46 +00:00
refactor with extract_binding_mode
This commit is contained in:
parent
eed311f719
commit
5a8baa2876
@ -648,6 +648,13 @@ impl<'tcx> TypeckTables<'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_binding_mode(&self, s: &Session, id: HirId, sp: Span) -> Option<BindingMode> {
|
||||
self.pat_binding_modes().get(id).copied().or_else(|| {
|
||||
s.delay_span_bug(sp, "missing binding mode");
|
||||
None
|
||||
})
|
||||
}
|
||||
|
||||
pub fn pat_binding_modes(&self) -> LocalTableInContext<'_, BindingMode> {
|
||||
LocalTableInContext {
|
||||
local_id_root: self.local_id_root,
|
||||
|
@ -816,15 +816,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
|
||||
if let Some(Node::Binding(pat)) = tcx_hir.find(var_id) {
|
||||
if let hir::PatKind::Binding(_, _, ident, _) = pat.kind {
|
||||
name = ident.name;
|
||||
|
||||
if let Some(&bm) = hir_tables.pat_binding_modes().get(pat.hir_id) {
|
||||
if bm == ty::BindByValue(hir::Mutability::Mut) {
|
||||
match hir_tables.extract_binding_mode(tcx.sess, pat.hir_id, pat.span) {
|
||||
Some(ty::BindByValue(hir::Mutability::Mut)) => {
|
||||
mutability = Mutability::Mut;
|
||||
} else {
|
||||
mutability = Mutability::Not;
|
||||
}
|
||||
} else {
|
||||
tcx.sess.delay_span_bug(pat.span, "missing binding mode");
|
||||
Some(_) => mutability = Mutability::Not,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -271,11 +271,9 @@ fn const_not_var(err: &mut DiagnosticBuilder<'_>, tcx: TyCtxt<'_>, pat: &Pat, pa
|
||||
fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
|
||||
pat.walk(|p| {
|
||||
if let hir::PatKind::Binding(_, _, ident, None) = p.kind {
|
||||
if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
|
||||
if bm != ty::BindByValue(hir::Mutability::Not) {
|
||||
// Nothing to check.
|
||||
return true;
|
||||
}
|
||||
if let Some(ty::BindByValue(hir::Mutability::Not)) =
|
||||
cx.tables.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
|
||||
{
|
||||
let pat_ty = cx.tables.pat_ty(p);
|
||||
if let ty::Adt(edef, _) = pat_ty.kind {
|
||||
if edef.is_enum()
|
||||
@ -303,8 +301,6 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
|
||||
err.emit();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cx.tcx.sess.delay_span_bug(p.span, "missing binding mode");
|
||||
}
|
||||
}
|
||||
true
|
||||
@ -581,15 +577,14 @@ fn maybe_point_at_variant(ty: Ty<'_>, patterns: &[super::Pat<'_>]) -> Vec<Span>
|
||||
|
||||
// Check the legality of legality of by-move bindings.
|
||||
fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: bool, pat: &Pat) {
|
||||
let sess = cx.tcx.sess;
|
||||
let tables = cx.tables;
|
||||
|
||||
// Find all by-ref spans.
|
||||
let mut by_ref_spans = Vec::new();
|
||||
pat.each_binding(|_, hir_id, span, _| {
|
||||
if let Some(&bm) = cx.tables.pat_binding_modes().get(hir_id) {
|
||||
if let ty::BindByReference(..) = bm {
|
||||
by_ref_spans.push(span);
|
||||
}
|
||||
} else {
|
||||
cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
|
||||
if let Some(ty::BindByReference(_)) = tables.extract_binding_mode(sess, hir_id, span) {
|
||||
by_ref_spans.push(span);
|
||||
}
|
||||
});
|
||||
|
||||
@ -600,7 +595,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
|
||||
//
|
||||
// `x @ Foo(..)` is legal, but `x @ Foo(y)` isn't.
|
||||
if sub.map_or(false, |p| p.contains_bindings()) {
|
||||
struct_span_err!(cx.tcx.sess, p.span, E0007, "cannot bind by-move with sub-bindings")
|
||||
struct_span_err!(sess, p.span, E0007, "cannot bind by-move with sub-bindings")
|
||||
.span_label(p.span, "binds an already bound by-move value by moving it")
|
||||
.emit();
|
||||
} else if !has_guard && !by_ref_spans.is_empty() {
|
||||
@ -609,15 +604,11 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
|
||||
};
|
||||
pat.walk(|p| {
|
||||
if let hir::PatKind::Binding(.., sub) = &p.kind {
|
||||
if let Some(&bm) = cx.tables.pat_binding_modes().get(p.hir_id) {
|
||||
if let ty::BindByValue(..) = bm {
|
||||
let pat_ty = cx.tables.node_type(p.hir_id);
|
||||
if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) {
|
||||
check_move(p, sub.as_deref());
|
||||
}
|
||||
if let Some(ty::BindByValue(_)) = tables.extract_binding_mode(sess, p.hir_id, p.span) {
|
||||
let pat_ty = tables.node_type(p.hir_id);
|
||||
if !pat_ty.is_copy_modulo_regions(cx.tcx, cx.param_env, pat.span) {
|
||||
check_move(p, sub.as_deref());
|
||||
}
|
||||
} else {
|
||||
cx.tcx.sess.delay_span_bug(pat.span, "missing binding mode");
|
||||
}
|
||||
}
|
||||
true
|
||||
@ -626,7 +617,7 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
|
||||
// Found some bad by-move spans, error!
|
||||
if !by_move_spans.is_empty() {
|
||||
let mut err = struct_span_err!(
|
||||
cx.tcx.sess,
|
||||
sess,
|
||||
MultiSpan::from_spans(by_move_spans.clone()),
|
||||
E0009,
|
||||
"cannot bind by-move and by-ref in the same pattern",
|
||||
@ -642,14 +633,12 @@ fn check_legality_of_move_bindings(cx: &mut MatchVisitor<'_, '_>, has_guard: boo
|
||||
}
|
||||
|
||||
fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
|
||||
let tab = cx.tables;
|
||||
let sess = cx.tcx.sess;
|
||||
// Get the mutability of `p` if it's by-ref.
|
||||
let extract_binding_mut = |hir_id, span| match cx.tables.pat_binding_modes().get(hir_id) {
|
||||
None => {
|
||||
cx.tcx.sess.delay_span_bug(span, "missing binding mode");
|
||||
None
|
||||
}
|
||||
Some(ty::BindByValue(..)) => None,
|
||||
Some(ty::BindByReference(m)) => Some(*m),
|
||||
let extract_binding_mut = |hir_id, span| match tab.extract_binding_mode(sess, hir_id, span)? {
|
||||
ty::BindByValue(_) => None,
|
||||
ty::BindByReference(m) => Some(m),
|
||||
};
|
||||
pat.walk(|pat| {
|
||||
// Extract `sub` in `binding @ sub`.
|
||||
@ -671,8 +660,8 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
|
||||
sub.each_binding(|_, hir_id, span, _| {
|
||||
if let Some(mut_inner) = extract_binding_mut(hir_id, span) {
|
||||
match (mut_outer, mut_inner) {
|
||||
(Mutability::Immutable, Mutability::Immutable) => {}
|
||||
(Mutability::Mutable, Mutability::Mutable) => conflicts_mut_mut.push(span),
|
||||
(Mutability::Not, Mutability::Not) => {}
|
||||
(Mutability::Mut, Mutability::Mut) => conflicts_mut_mut.push(span),
|
||||
_ => conflicts_mut_ref.push(span),
|
||||
}
|
||||
}
|
||||
@ -683,7 +672,7 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
|
||||
if !conflicts_mut_mut.is_empty() {
|
||||
// Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`.
|
||||
let msg = &format!("cannot borrow `{}` as mutable more than once at a time", name);
|
||||
let mut err = cx.tcx.sess.struct_span_err(pat.span, msg);
|
||||
let mut err = sess.struct_span_err(pat.span, msg);
|
||||
err.span_label(binding_span, "first mutable borrow occurs here");
|
||||
for sp in conflicts_mut_mut {
|
||||
err.span_label(sp, "another mutable borrow occurs here");
|
||||
@ -695,14 +684,14 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_>, pat: &Pat) {
|
||||
} else if !conflicts_mut_ref.is_empty() {
|
||||
// Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse.
|
||||
let (primary, also) = match mut_outer {
|
||||
Mutability::Mutable => ("mutable", "immutable"),
|
||||
Mutability::Immutable => ("immutable", "mutable"),
|
||||
Mutability::Mut => ("mutable", "immutable"),
|
||||
Mutability::Not => ("immutable", "mutable"),
|
||||
};
|
||||
let msg = &format!(
|
||||
"cannot borrow `{}` as {} because it is also borrowed as {}",
|
||||
name, primary, also,
|
||||
);
|
||||
let mut err = cx.tcx.sess.struct_span_err(pat.span, msg);
|
||||
let mut err = sess.struct_span_err(pat.span, msg);
|
||||
err.span_label(binding_span, &format!("{} borrow occurs here", primary));
|
||||
for sp in conflicts_mut_ref {
|
||||
err.span_label(sp, &format!("{} borrow occurs here", also));
|
||||
|
@ -1007,20 +1007,13 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
|
||||
fn link_pattern(&self, discr_cmt: mc::Place<'tcx>, root_pat: &hir::Pat) {
|
||||
debug!("link_pattern(discr_cmt={:?}, root_pat={:?})", discr_cmt, root_pat);
|
||||
ignore_err!(self.with_mc(|mc| {
|
||||
mc.cat_pattern(discr_cmt, root_pat, |sub_cmt, sub_pat| {
|
||||
mc.cat_pattern(discr_cmt, root_pat, |sub_cmt, hir::Pat { kind, span, hir_id }| {
|
||||
// `ref x` pattern
|
||||
if let PatKind::Binding(..) = sub_pat.kind {
|
||||
if let Some(&bm) = mc.tables.pat_binding_modes().get(sub_pat.hir_id) {
|
||||
if let ty::BindByReference(mutbl) = bm {
|
||||
self.link_region_from_node_type(
|
||||
sub_pat.span,
|
||||
sub_pat.hir_id,
|
||||
mutbl,
|
||||
&sub_cmt,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
self.tcx.sess.delay_span_bug(sub_pat.span, "missing binding mode");
|
||||
if let PatKind::Binding(..) = kind {
|
||||
if let Some(ty::BindByReference(mutbl)) =
|
||||
mc.tables.extract_binding_mode(self.tcx.sess, *hir_id, *span)
|
||||
{
|
||||
self.link_region_from_node_type(*span, *hir_id, mutbl, &sub_cmt);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -284,10 +284,9 @@ impl<'cx, 'tcx> Visitor<'tcx> for WritebackCx<'cx, 'tcx> {
|
||||
fn visit_pat(&mut self, p: &'tcx hir::Pat) {
|
||||
match p.kind {
|
||||
hir::PatKind::Binding(..) => {
|
||||
if let Some(&bm) = self.fcx.tables.borrow().pat_binding_modes().get(p.hir_id) {
|
||||
let tables = self.fcx.tables.borrow();
|
||||
if let Some(bm) = tables.extract_binding_mode(self.tcx().sess, p.hir_id, p.span) {
|
||||
self.tables.pat_binding_modes_mut().insert(p.hir_id, bm);
|
||||
} else {
|
||||
self.tcx().sess.delay_span_bug(p.span, "missing binding mode");
|
||||
}
|
||||
}
|
||||
hir::PatKind::Struct(_, ref fields, _) => {
|
||||
|
@ -534,7 +534,7 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
||||
return_if_err!(mc.cat_pattern(discr_place.clone(), pat, |place, pat| {
|
||||
if let PatKind::Binding(_, canonical_id, ..) = pat.kind {
|
||||
debug!("walk_pat: binding place={:?} pat={:?}", place, pat,);
|
||||
if let Some(&bm) = mc.tables.pat_binding_modes().get(pat.hir_id) {
|
||||
if let Some(bm) = mc.tables.extract_binding_mode(tcx.sess, pat.hir_id, pat.span) {
|
||||
debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat.hir_id, bm);
|
||||
|
||||
// pat_ty: the type of the binding being produced.
|
||||
@ -560,8 +560,6 @@ impl<'a, 'tcx> ExprUseVisitor<'a, 'tcx> {
|
||||
delegate.consume(place, mode);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
tcx.sess.delay_span_bug(pat.span, "missing binding mode");
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
Loading…
Reference in New Issue
Block a user