Skip to content

Commit f26b748

Browse files
committed
Suggest &[_] instead of &[_; N] on inference error
1 parent 1ae1a13 commit f26b748

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,21 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
366366
None
367367
};
368368
printer.name_resolver = Some(Box::new(&getter));
369-
let _ = if let ty::FnDef(..) = ty.kind() {
369+
let _ = match ty.kind() {
370370
// We don't want the regular output for `fn`s because it includes its path in
371371
// invalid pseudo-syntax, we want the `fn`-pointer output instead.
372-
ty.fn_sig(self.tcx).print(printer)
373-
} else {
374-
ty.print(printer)
372+
ty::FnDef(..) => ty.fn_sig(self.tcx).print(printer),
373+
ty::Ref(_, ref_ty, mutability) => {
374+
match ref_ty.kind() {
375+
ty::Array(ty, _) => {
376+
// Do not suggest `&[_; 0]`, instead suggest `&[_]`.
377+
let _ = ty.print(printer);
378+
return format!("&{}[{}]", mutability.prefix_str(), s);
379+
}
380+
_ => ty.print(printer),
381+
}
382+
}
383+
_ => ty.print(printer),
375384
};
376385
s
377386
};

src/test/ui/issues/issue-7813.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0282]: type annotations needed for `&[_; 0]`
1+
error[E0282]: type annotations needed for `&[_]`
22
--> $DIR/issue-7813.rs:2:13
33
|
44
LL | let v = &[];
55
| ^^^ cannot infer type
66
|
7-
help: consider giving this binding the explicit type `&[_; 0]`, with the type parameters specified
7+
help: consider giving this binding the explicit type `&[_]`, with the type parameters specified
88
|
9-
LL | let v: &[_; 0] = &[];
10-
| ^^^^^^^^^
9+
LL | let v: &[_] = &[];
10+
| ^^^^^^
1111

1212
error: aborting due to previous error
1313

0 commit comments

Comments
 (0)