Skip to content

#53840: Consolidate pattern check errors #54269

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/librustc_mir/hair/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::slice;

use syntax::ast;
use syntax::ptr::P;
use syntax_pos::{Span, DUMMY_SP};
use syntax_pos::{Span, DUMMY_SP, MultiSpan};

struct OuterVisitor<'a, 'tcx: 'a> { tcx: TyCtxt<'a, 'tcx, 'tcx> }

Expand Down Expand Up @@ -527,8 +527,8 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
}
})
}

let check_move = |p: &Pat, sub: Option<&Pat>| {
let span_vec = &mut Vec::new();
let check_move = |p: &Pat, sub: Option<&Pat>, span_vec: &mut Vec<Span>| {
// check legality of moving out of the enum

// x @ Foo(..) is legal, but x @ Foo(y) isn't.
Expand All @@ -546,16 +546,8 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
crate attributes to enable");
}
err.emit();
} else if let Some(by_ref_span) = by_ref_span {
struct_span_err!(
cx.tcx.sess,
p.span,
E0009,
"cannot bind by-move and by-ref in the same pattern",
)
.span_label(p.span, "by-move pattern here")
.span_label(by_ref_span, "both by-ref and by-move used")
.emit();
} else if let Some(_by_ref_span) = by_ref_span {
span_vec.push(p.span);
}
};

Expand All @@ -567,7 +559,7 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
ty::BindByValue(..) => {
let pat_ty = cx.tables.node_id_to_type(p.hir_id);
if pat_ty.moves_by_default(cx.tcx, cx.param_env, pat.span) {
check_move(p, sub.as_ref().map(|p| &**p));
check_move(p, sub.as_ref().map(|p| &**p), span_vec);
}
}
_ => {}
Expand All @@ -579,6 +571,20 @@ fn check_legality_of_move_bindings(cx: &MatchVisitor,
true
});
}
if !span_vec.is_empty(){
let span = MultiSpan::from_spans(span_vec.clone());
let mut err = struct_span_err!(
cx.tcx.sess,
span,
E0009,
"cannot bind by-move and by-ref in the same pattern",
);
err.span_label(by_ref_span.unwrap(), "both by-ref and by-move used");
for span in span_vec.iter(){
err.span_label(*span, "by-move pattern here");
}
err.emit();
}
}

/// Ensures that a pattern guard doesn't borrow by mutable reference or
Expand Down
27 changes: 27 additions & 0 deletions src/test/ui/issue-53840.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
enum E {
Foo(String, String, String),
}

struct Bar {
a: String,
b: String,
}

fn main() {
let bar = Bar { a: "1".to_string(), b: "2".to_string() };
match E::Foo("".into(), "".into(), "".into()) {
E::Foo(a, b, ref c) => {}
}
match bar {
Bar {a, ref b} => {}
}
}
20 changes: 20 additions & 0 deletions src/test/ui/issue-53840.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error[E0009]: cannot bind by-move and by-ref in the same pattern
--> $DIR/issue-53840.rs:22:16
|
LL | E::Foo(a, b, ref c) => {}
| ^ ^ ----- both by-ref and by-move used
| | |
| | by-move pattern here
| by-move pattern here

error[E0009]: cannot bind by-move and by-ref in the same pattern
--> $DIR/issue-53840.rs:25:14
|
LL | Bar {a, ref b} => {}
| ^ ----- both by-ref and by-move used
| |
| by-move pattern here

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0009`.