Skip to content

Commit 349fae3

Browse files
authored
Rollup merge of #84313 - lcnr:sized-err-msg, r=petrochenkov
fix suggestion for unsized function parameters taken from `@fasterthanlime's` article https://fasterthanli.me/articles/whats-in-the-box
2 parents 7612435 + a8193ca commit 349fae3

25 files changed

+64
-37
lines changed

compiler/rustc_typeck/src/check/gather_locals.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
66
use rustc_middle::ty::Ty;
77
use rustc_span::{sym, Span};
88
use rustc_trait_selection::traits;
9-
use std::mem;
109

1110
pub(super) struct GatherLocalsVisitor<'a, 'tcx> {
1211
fcx: &'a FnCtxt<'a, 'tcx>,
1312
parent_id: hir::HirId,
1413
// parameters are special cases of patterns, but we want to handle them as
1514
// *distinct* cases. so track when we are hitting a pattern *within* an fn
1615
// parameter.
17-
outermost_fn_param_pat: bool,
16+
outermost_fn_param_pat: Option<Span>,
1817
}
1918

2019
impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
2120
pub(super) fn new(fcx: &'a FnCtxt<'a, 'tcx>, parent_id: hir::HirId) -> Self {
22-
Self { fcx, parent_id, outermost_fn_param_pat: false }
21+
Self { fcx, parent_id, outermost_fn_param_pat: None }
2322
}
2423

2524
fn assign(&mut self, span: Span, nid: hir::HirId, ty_opt: Option<LocalTy<'tcx>>) -> Ty<'tcx> {
@@ -92,7 +91,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
9291
}
9392

9493
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
95-
let old_outermost_fn_param_pat = mem::replace(&mut self.outermost_fn_param_pat, true);
94+
let old_outermost_fn_param_pat = self.outermost_fn_param_pat.replace(param.ty_span);
9695
intravisit::walk_param(self, param);
9796
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
9897
}
@@ -102,12 +101,12 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
102101
if let PatKind::Binding(_, _, ident, _) = p.kind {
103102
let var_ty = self.assign(p.span, p.hir_id, None);
104103

105-
if self.outermost_fn_param_pat {
104+
if let Some(ty_span) = self.outermost_fn_param_pat {
106105
if !self.fcx.tcx.features().unsized_fn_params {
107106
self.fcx.require_type_is_sized(
108107
var_ty,
109108
p.span,
110-
traits::SizedArgumentType(Some(p.span)),
109+
traits::SizedArgumentType(Some(ty_span)),
111110
);
112111
}
113112
} else {
@@ -123,7 +122,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
123122
var_ty
124123
);
125124
}
126-
let old_outermost_fn_param_pat = mem::replace(&mut self.outermost_fn_param_pat, false);
125+
let old_outermost_fn_param_pat = self.outermost_fn_param_pat.take();
127126
intravisit::walk_pat(self, p);
128127
self.outermost_fn_param_pat = old_outermost_fn_param_pat;
129128
}

src/test/ui/error-codes/E0277.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | fn f(p: Path) { }
99
= help: unsized fn params are gated as an unstable feature
1010
help: function arguments must have a statically known size, borrowed types always have a known size
1111
|
12-
LL | fn f(&p: Path) { }
13-
| ^
12+
LL | fn f(p: &Path) { }
13+
| ^
1414

1515
error[E0277]: the trait bound `i32: Foo` is not satisfied
1616
--> $DIR/E0277.rs:15:15

src/test/ui/feature-gates/feature-gate-unsized_fn_params.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | fn foo(x: dyn Foo) {
88
= help: unsized fn params are gated as an unstable feature
99
help: function arguments must have a statically known size, borrowed types always have a known size
1010
|
11-
LL | fn foo(&x: dyn Foo) {
12-
| ^
11+
LL | fn foo(x: &dyn Foo) {
12+
| ^
1313

1414
error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
1515
--> $DIR/feature-gate-unsized_fn_params.rs:24:5

src/test/ui/feature-gates/feature-gate-unsized_locals.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | fn f(f: dyn FnOnce()) {}
88
= help: unsized fn params are gated as an unstable feature
99
help: function arguments must have a statically known size, borrowed types always have a known size
1010
|
11-
LL | fn f(&f: dyn FnOnce()) {}
12-
| ^
11+
LL | fn f(f: &dyn FnOnce()) {}
12+
| ^
1313

1414
error: aborting due to previous error
1515

src/test/ui/issues/issue-5883.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ struct Struct {
44
r: dyn A + 'static
55
}
66

7-
fn new_struct(r: dyn A + 'static)
8-
-> Struct { //~^ ERROR the size for values of type
9-
//~^ ERROR the size for values of type
7+
fn new_struct(
8+
r: dyn A + 'static //~ ERROR the size for values of type
9+
) -> Struct { //~ ERROR the size for values of type
1010
Struct { r: r }
1111
}
1212

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

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
2-
--> $DIR/issue-5883.rs:7:15
2+
--> $DIR/issue-5883.rs:8:5
33
|
4-
LL | fn new_struct(r: dyn A + 'static)
5-
| ^ doesn't have a size known at compile-time
4+
LL | r: dyn A + 'static
5+
| ^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn A + 'static)`
88
= help: unsized fn params are gated as an unstable feature
99
help: function arguments must have a statically known size, borrowed types always have a known size
1010
|
11-
LL | fn new_struct(&r: dyn A + 'static)
12-
| ^
11+
LL | r: &dyn A + 'static
12+
| ^
1313

1414
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
15-
--> $DIR/issue-5883.rs:8:8
15+
--> $DIR/issue-5883.rs:9:6
1616
|
17-
LL | -> Struct {
18-
| ^^^^^^ doesn't have a size known at compile-time
19-
LL |
17+
LL | ) -> Struct {
18+
| ^^^^^^ doesn't have a size known at compile-time
2019
LL | Struct { r: r }
2120
| --------------- this returned value is of type `Struct`
2221
|

src/test/ui/resolve/issue-5035-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | fn foo(_x: K) {}
88
= help: unsized fn params are gated as an unstable feature
99
help: function arguments must have a statically known size, borrowed types always have a known size
1010
|
11-
LL | fn foo(&_x: K) {}
12-
| ^
11+
LL | fn foo(_x: &K) {}
12+
| ^
1313

1414
error: aborting due to previous error
1515

src/test/ui/suggestions/path-by-value.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ LL | fn f(p: Path) { }
99
= help: unsized fn params are gated as an unstable feature
1010
help: function arguments must have a statically known size, borrowed types always have a known size
1111
|
12-
LL | fn f(&p: Path) { }
13-
| ^
12+
LL | fn f(p: &Path) { }
13+
| ^
1414

1515
error: aborting due to previous error
1616

src/test/ui/traits/bound/not-on-bare-trait.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ LL | fn foo(_x: Foo + Send) {
1616
= help: unsized fn params are gated as an unstable feature
1717
help: function arguments must have a statically known size, borrowed types always have a known size
1818
|
19-
LL | fn foo(&_x: Foo + Send) {
20-
| ^
19+
LL | fn foo(_x: &Foo + Send) {
20+
| ^
2121

2222
error: aborting due to previous error; 1 warning emitted
2323

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
#![crate_type="lib"]
3+
#![allow(unused)]
4+
5+
fn f<T: ?Sized>(t: &T) {}
6+
//~^ ERROR the size for values of type `T` cannot be known at compilation time

src/test/ui/unsized/unsized-fn-arg.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// run-rustfix
2+
#![crate_type="lib"]
3+
#![allow(unused)]
4+
5+
fn f<T: ?Sized>(t: T) {}
6+
//~^ ERROR the size for values of type `T` cannot be known at compilation time
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0277]: the size for values of type `T` cannot be known at compilation time
2+
--> $DIR/unsized-fn-arg.rs:5:17
3+
|
4+
LL | fn f<T: ?Sized>(t: T) {}
5+
| - ^ doesn't have a size known at compile-time
6+
| |
7+
| this type parameter needs to be `std::marker::Sized`
8+
|
9+
= help: unsized fn params are gated as an unstable feature
10+
help: function arguments must have a statically known size, borrowed types always have a known size
11+
|
12+
LL | fn f<T: ?Sized>(t: &T) {}
13+
| ^
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0277`.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/test/ui/unsized6.stderr src/test/ui/unsized/unsized6.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ LL | fn g1<X: ?Sized>(x: X) {}
135135
= help: unsized fn params are gated as an unstable feature
136136
help: function arguments must have a statically known size, borrowed types always have a known size
137137
|
138-
LL | fn g1<X: ?Sized>(&x: X) {}
139-
| ^
138+
LL | fn g1<X: ?Sized>(x: &X) {}
139+
| ^
140140

141141
error[E0277]: the size for values of type `X` cannot be known at compilation time
142142
--> $DIR/unsized6.rs:40:22
@@ -149,8 +149,8 @@ LL | fn g2<X: ?Sized + T>(x: X) {}
149149
= help: unsized fn params are gated as an unstable feature
150150
help: function arguments must have a statically known size, borrowed types always have a known size
151151
|
152-
LL | fn g2<X: ?Sized + T>(&x: X) {}
153-
| ^
152+
LL | fn g2<X: ?Sized + T>(x: &X) {}
153+
| ^
154154

155155
error: aborting due to 13 previous errors
156156

File renamed without changes.
File renamed without changes.

src/tools/clippy/tests/ui/crashes/ice-6251.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: [u8]| x }]> {
1616
= help: unsized fn params are gated as an unstable feature
1717
help: function arguments must have a statically known size, borrowed types always have a known size
1818
|
19-
LL | fn bug<T>() -> impl Iterator<Item = [(); { |&x: [u8]| x }]> {
20-
| ^
19+
LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: &[u8]| x }]> {
20+
| ^
2121

2222
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
2323
--> $DIR/ice-6251.rs:4:54

0 commit comments

Comments
 (0)