Skip to content

Commit 0e7e938

Browse files
committed
Do not suggest incorrect syntax on pattern borrow error
1 parent a55c2eb commit 0e7e938

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
378378
// `fn foo(foo: &u32)`
379379
if let Some(mut err) = err {
380380
if let PatKind::Binding(..) = inner.node {
381-
if let Ok(snippet) = tcx.sess.source_map()
382-
.span_to_snippet(pat.span)
383-
{
384-
err.help(&format!("did you mean `{}: &{}`?",
385-
&snippet[1..],
386-
expected));
381+
let parent_id = tcx.hir().get_parent_node_by_hir_id(pat.hir_id);
382+
let parent = tcx.hir().get_by_hir_id(parent_id);
383+
match parent {
384+
hir::Node::Item(_) |
385+
hir::Node::ForeignItem(_) |
386+
hir::Node::TraitItem(_) |
387+
hir::Node::ImplItem(_) => { // this pat is an argument
388+
if let Ok(snippet) = tcx.sess.source_map()
389+
.span_to_snippet(pat.span)
390+
{ // FIXME: turn into structured suggestion, will need
391+
// a span that also includes the the type.
392+
err.help(&format!(
393+
"did you mean `{}: &{}`?",
394+
&snippet[1..],
395+
expected,
396+
));
397+
}
398+
}
399+
_ => {} // don't provide the suggestion from above #55175
387400
}
388401
}
389402
err.emit();

src/test/ui/destructure-trait-ref.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ LL | let &&x = &1isize as &T;
2424
|
2525
= note: expected type `dyn T`
2626
found type `&_`
27-
= help: did you mean `x: &dyn T`?
2827

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

3937
error[E0308]: mismatched types
4038
--> $DIR/destructure-trait-ref.rs:41:13

src/test/ui/mismatched_types/issue-38371.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ LL | fn agh(&&bar: &u32) {
1616
|
1717
= note: expected type `u32`
1818
found type `&_`
19-
= help: did you mean `bar: &u32`?
2019

2120
error[E0308]: mismatched types
2221
--> $DIR/issue-38371.rs:21:8

0 commit comments

Comments
 (0)