Skip to content

Commit 7dae5c0

Browse files
committed
Auto merge of #51686 - nikomatsakis:issue-51415-borrowck-match-default-bindings-bug, r=eddyb
yet another "old borrowck" bug around match default bindings We were getting the type of the parameter from its pattern, but that didn't include adjustments. I did a `ripgrep` around and this seemed to be the only affected case. The reason this didn't show up as an ICE earlier is that mem-categorization is lenient with respect to weird discrepancies. I am going to add more delay-span-bug calls shortly around that (I'll push onto the PR). This example is an ICE, but I presume that there is a way to make a soundness example out of this -- it basically ignores borrows occuring inside match-default-bindings in a closure, though only if the implicit deref is at the top-level. It happens though that this occurs frequently in iterators, which often give a `&T` parameter. Fixes #51415 Fixes #49534 r? @eddyb
2 parents 4b17d31 + 8289bf2 commit 7dae5c0

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

src/librustc/middle/expr_use_visitor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
313313
debug!("consume_body(body={:?})", body);
314314

315315
for arg in &body.arguments {
316-
let arg_ty = return_if_err!(self.mc.node_ty(arg.pat.hir_id));
316+
let arg_ty = return_if_err!(self.mc.pat_ty_adjusted(&arg.pat));
317+
debug!("consume_body: arg_ty = {:?}", arg_ty);
317318

318319
let fn_body_scope_r =
319320
self.tcx().mk_region(ty::ReScope(region::Scope::Node(body.value.hir_id.local_id)));

src/librustc/middle/mem_categorization.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
517517
/// implicit deref patterns attached (e.g., it is really
518518
/// `&Some(x)`). In that case, we return the "outermost" type
519519
/// (e.g., `&Option<T>).
520-
fn pat_ty(&self, pat: &hir::Pat) -> McResult<Ty<'tcx>> {
520+
pub fn pat_ty_adjusted(&self, pat: &hir::Pat) -> McResult<Ty<'tcx>> {
521521
// Check for implicit `&` types wrapping the pattern; note
522522
// that these are never attached to binding patterns, so
523523
// actually this is somewhat "disjoint" from the code below
@@ -1300,7 +1300,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13001300
};
13011301

13021302
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
1303-
let subpat_ty = self.pat_ty(&subpat)?; // see (*2)
1303+
let subpat_ty = self.pat_ty_adjusted(&subpat)?; // see (*2)
13041304
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string())));
13051305
let subcmt = Rc::new(self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
13061306
self.cat_pattern_(subcmt, &subpat, op)?;
@@ -1323,7 +1323,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13231323
};
13241324

13251325
for fp in field_pats {
1326-
let field_ty = self.pat_ty(&fp.node.pat)?; // see (*2)
1326+
let field_ty = self.pat_ty_adjusted(&fp.node.pat)?; // see (*2)
13271327
let f_index = self.tcx.field_index(fp.node.id, self.tables);
13281328
let cmt_field = Rc::new(self.cat_field(pat, cmt.clone(), f_index,
13291329
fp.node.ident, field_ty));
@@ -1342,7 +1342,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
13421342
ref ty => span_bug!(pat.span, "tuple pattern unexpected type {:?}", ty),
13431343
};
13441344
for (i, subpat) in subpats.iter().enumerate_and_adjust(expected_len, ddpos) {
1345-
let subpat_ty = self.pat_ty(&subpat)?; // see (*2)
1345+
let subpat_ty = self.pat_ty_unadjusted(&subpat)?; // see (*2)
13461346
let interior = InteriorField(FieldIndex(i, Name::intern(&i.to_string())));
13471347
let subcmt = Rc::new(self.cat_imm_interior(pat, cmt.clone(), subpat_ty, interior));
13481348
self.cat_pattern_(subcmt, &subpat, op)?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0507]: cannot move out of borrowed content
2+
--> $DIR/issue-51415.rs:16:47
3+
|
4+
LL | let opt = a.iter().enumerate().find(|(_, &s)| {
5+
| ^ cannot move out of borrowed content
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0507`.

src/test/ui/borrowck/issue-51415.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #51415: match default bindings were failing to
12+
// see the "move out" implied by `&s` below.
13+
14+
fn main() {
15+
let a = vec![String::from("a")];
16+
let opt = a.iter().enumerate().find(|(_, &s)| {
17+
//~^ ERROR cannot move out
18+
*s == String::from("d")
19+
}).map(|(i, _)| i);
20+
println!("{:?}", opt);
21+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0507]: cannot move out of borrowed content
2+
--> $DIR/issue-51415.rs:16:46
3+
|
4+
LL | let opt = a.iter().enumerate().find(|(_, &s)| {
5+
| ^-
6+
| ||
7+
| |hint: to prevent move, use `ref s` or `ref mut s`
8+
| cannot move out of borrowed content
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)