@@ -30,10 +30,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30
30
self . check_pat_arg ( pat, expected, false ) ;
31
31
}
32
32
33
+ /// The `is_arg` argument indicates whether this pattern is the
34
+ /// *outermost* pattern in an argument (e.g., in `fn foo(&x:
35
+ /// &u32)`, it is true for the `&x` pattern but not `x`). This is
36
+ /// used to tailor error reporting.
33
37
pub fn check_pat_arg ( & self , pat : & ' gcx hir:: Pat , expected : Ty < ' tcx > , is_arg : bool ) {
34
38
let tcx = self . tcx ;
35
39
36
- debug ! ( "check_pat(pat={:?},expected={:?})" , pat, expected) ;
40
+ debug ! ( "check_pat(pat={:?},expected={:?},is_arg={} )" , pat, expected, is_arg ) ;
37
41
38
42
let ty = match pat. node {
39
43
PatKind :: Wild => {
@@ -206,6 +210,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
206
210
// can, to avoid creating needless variables. This
207
211
// also helps with the bad interactions of the given
208
212
// hack detailed in (*) below.
213
+ debug ! ( "check_pat_arg: expected={:?}" , expected) ;
209
214
let ( rptr_ty, inner_ty) = match expected. sty {
210
215
ty:: TyRef ( _, mt) if mt. mutbl == mutbl => {
211
216
( expected, mt. ty )
@@ -216,15 +221,21 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
216
221
let mt = ty:: TypeAndMut { ty : inner_ty, mutbl : mutbl } ;
217
222
let region = self . next_region_var ( infer:: PatternRegion ( pat. span ) ) ;
218
223
let rptr_ty = tcx. mk_ref ( region, mt) ;
224
+ debug ! ( "check_pat_arg: demanding {:?} = {:?}" , expected, rptr_ty) ;
219
225
let err = self . demand_eqtype_diag ( pat. span , expected, rptr_ty) ;
226
+
227
+ // Look for a case like `fn foo(&foo: u32)` and suggest
228
+ // `fn foo(foo: &u32)`
220
229
if let Some ( mut err) = err {
221
230
if is_arg {
222
- if let Ok ( snippet) = self . sess ( ) . codemap ( )
223
- . span_to_snippet ( pat. span )
224
- {
225
- err. help ( & format ! ( "did you mean `{}: &{}`?" ,
226
- & snippet[ 1 ..] ,
227
- expected) ) ;
231
+ if let PatKind :: Binding ( ..) = inner. node {
232
+ if let Ok ( snippet) = self . sess ( ) . codemap ( )
233
+ . span_to_snippet ( pat. span )
234
+ {
235
+ err. help ( & format ! ( "did you mean `{}: &{}`?" ,
236
+ & snippet[ 1 ..] ,
237
+ expected) ) ;
238
+ }
228
239
}
229
240
}
230
241
err. emit ( ) ;
0 commit comments