Do not suggest incorrect syntax on pattern borrow error

This commit is contained in:
Esteban Küber 2019-04-29 18:12:57 -07:00
parent a55c2eb325
commit 0e7e9382fa
3 changed files with 19 additions and 9 deletions

View File

@ -378,12 +378,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
// `fn foo(foo: &u32)`
if let Some(mut err) = err {
if let PatKind::Binding(..) = inner.node {
if let Ok(snippet) = tcx.sess.source_map()
.span_to_snippet(pat.span)
{
err.help(&format!("did you mean `{}: &{}`?",
&snippet[1..],
expected));
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
let parent = tcx.hir().get_by_hir_id(parent_id);
match parent {
hir::Node::Item(_) |
hir::Node::ForeignItem(_) |
hir::Node::TraitItem(_) |
hir::Node::ImplItem(_) => { // this pat is an argument
if let Ok(snippet) = tcx.sess.source_map()
.span_to_snippet(pat.span)
{ // FIXME: turn into structured suggestion, will need
// a span that also includes the the type.
err.help(&format!(
"did you mean `{}: &{}`?",
&snippet[1..],
expected,
));
}
}
_ => {} // don't provide the suggestion from above #55175
}
}
err.emit();

View File

@ -24,7 +24,6 @@ LL | let &&x = &1isize as &T;
|
= note: expected type `dyn T`
found type `&_`
= help: did you mean `x: &dyn T`?
error[E0308]: mismatched types
--> $DIR/destructure-trait-ref.rs:36:11
@ -34,7 +33,6 @@ LL | let &&&x = &(&1isize as &T);
|
= note: expected type `dyn T`
found type `&_`
= help: did you mean `x: &dyn T`?
error[E0308]: mismatched types
--> $DIR/destructure-trait-ref.rs:41:13

View File

@ -16,7 +16,6 @@ LL | fn agh(&&bar: &u32) {
|
= note: expected type `u32`
found type `&_`
= help: did you mean `bar: &u32`?
error[E0308]: mismatched types
--> $DIR/issue-38371.rs:21:8