Skip to content

Commit 77766d5

Browse files
authored
Rollup merge of rust-lang#106223 - estebank:suggest-let-ty-borrow, r=compiler-errors
On unsized locals with explicit types suggest `&` Fix rust-lang#72742.
2 parents 499055c + 083eb93 commit 77766d5

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,15 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
25142514
ObligationCauseCode::VariableType(hir_id) => {
25152515
let parent_node = self.tcx.hir().get_parent_node(hir_id);
25162516
match self.tcx.hir().find(parent_node) {
2517+
Some(Node::Local(hir::Local { ty: Some(ty), .. })) => {
2518+
err.span_suggestion_verbose(
2519+
ty.span.shrink_to_lo(),
2520+
"consider borrowing here",
2521+
"&",
2522+
Applicability::MachineApplicable,
2523+
);
2524+
err.note("all local variables must have a statically known size");
2525+
}
25172526
Some(Node::Local(hir::Local {
25182527
init: Some(hir::Expr { kind: hir::ExprKind::Index(_, _), span, .. }),
25192528
..
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let x: [u8] = vec!(1, 2, 3)[..]; //~ ERROR E0277
3+
let x: &[u8] = vec!(1, 2, 3)[..]; //~ ERROR E0308
4+
let x: [u8] = &vec!(1, 2, 3)[..]; //~ ERROR E0308
5+
//~^ ERROR E0277
6+
let x: &[u8] = &vec!(1, 2, 3)[..];
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2+
--> $DIR/suggest-borrow.rs:2:9
3+
|
4+
LL | let x: [u8] = vec!(1, 2, 3)[..];
5+
| ^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[u8]`
8+
= note: all local variables must have a statically known size
9+
= help: unsized locals are gated as an unstable feature
10+
help: consider borrowing here
11+
|
12+
LL | let x: &[u8] = vec!(1, 2, 3)[..];
13+
| +
14+
15+
error[E0308]: mismatched types
16+
--> $DIR/suggest-borrow.rs:3:20
17+
|
18+
LL | let x: &[u8] = vec!(1, 2, 3)[..];
19+
| ----- ^^^^^^^^^^^^^^^^^
20+
| | |
21+
| | expected `&[u8]`, found slice `[{integer}]`
22+
| | help: consider borrowing here: `&vec!(1, 2, 3)[..]`
23+
| expected due to this
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/suggest-borrow.rs:4:19
27+
|
28+
LL | let x: [u8] = &vec!(1, 2, 3)[..];
29+
| ---- ^^^^^^^^^^^^^^^^^^ expected slice `[u8]`, found `&[{integer}]`
30+
| |
31+
| expected due to this
32+
|
33+
help: consider removing the borrow
34+
|
35+
LL - let x: [u8] = &vec!(1, 2, 3)[..];
36+
LL + let x: [u8] = vec!(1, 2, 3)[..];
37+
|
38+
help: alternatively, consider changing the type annotation
39+
|
40+
LL | let x: &[u8] = &vec!(1, 2, 3)[..];
41+
| +
42+
43+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
44+
--> $DIR/suggest-borrow.rs:4:9
45+
|
46+
LL | let x: [u8] = &vec!(1, 2, 3)[..];
47+
| ^ doesn't have a size known at compile-time
48+
|
49+
= help: the trait `Sized` is not implemented for `[u8]`
50+
= note: all local variables must have a statically known size
51+
= help: unsized locals are gated as an unstable feature
52+
help: consider borrowing here
53+
|
54+
LL | let x: &[u8] = &vec!(1, 2, 3)[..];
55+
| +
56+
57+
error: aborting due to 4 previous errors
58+
59+
Some errors have detailed explanations: E0277, E0308.
60+
For more information about an error, try `rustc --explain E0277`.

src/test/ui/unsized-locals/unsized-locals-using-unsized-fn-params.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ LL | let _foo: [u8] = *foo;
2727
= help: the trait `Sized` is not implemented for `[u8]`
2828
= note: all local variables must have a statically known size
2929
= help: unsized locals are gated as an unstable feature
30+
help: consider borrowing here
31+
|
32+
LL | let _foo: &[u8] = *foo;
33+
| +
3034

3135
error: aborting due to 3 previous errors
3236

src/test/ui/unsized/unsized6.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ help: consider removing the `?Sized` bound to make the type parameter `Sized`
1414
LL - fn f1<W: ?Sized, X: ?Sized, Y: ?Sized, Z: ?Sized>(x: &X) {
1515
LL + fn f1<W: ?Sized, X: ?Sized, Y, Z: ?Sized>(x: &X) {
1616
|
17+
help: consider borrowing here
18+
|
19+
LL | let y: &Y;
20+
| +
1721

1822
error[E0277]: the size for values of type `X` cannot be known at compilation time
1923
--> $DIR/unsized6.rs:7:12
@@ -62,6 +66,10 @@ help: consider removing the `?Sized` bound to make the type parameter `Sized`
6266
LL - fn f2<X: ?Sized, Y: ?Sized>(x: &X) {
6367
LL + fn f2<X, Y: ?Sized>(x: &X) {
6468
|
69+
help: consider borrowing here
70+
|
71+
LL | let y: &X;
72+
| +
6573

6674
error[E0277]: the size for values of type `Y` cannot be known at compilation time
6775
--> $DIR/unsized6.rs:17:12
@@ -94,6 +102,10 @@ help: consider removing the `?Sized` bound to make the type parameter `Sized`
94102
LL - fn f3<X: ?Sized>(x1: Box<X>, x2: Box<X>, x3: Box<X>) {
95103
LL + fn f3<X>(x1: Box<X>, x2: Box<X>, x3: Box<X>) {
96104
|
105+
help: consider borrowing here
106+
|
107+
LL | let y: &X = *x1;
108+
| +
97109

98110
error[E0277]: the size for values of type `X` cannot be known at compilation time
99111
--> $DIR/unsized6.rs:24:9
@@ -144,6 +156,10 @@ help: consider removing the `?Sized` bound to make the type parameter `Sized`
144156
LL - fn f4<X: ?Sized + T>(x1: Box<X>, x2: Box<X>, x3: Box<X>) {
145157
LL + fn f4<X: T>(x1: Box<X>, x2: Box<X>, x3: Box<X>) {
146158
|
159+
help: consider borrowing here
160+
|
161+
LL | let y: &X = *x1;
162+
| +
147163

148164
error[E0277]: the size for values of type `X` cannot be known at compilation time
149165
--> $DIR/unsized6.rs:32:9

0 commit comments

Comments
 (0)