Skip to content

Commit 7f20b6f

Browse files
Rollup merge of rust-lang#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 8efc394 + da2acec commit 7f20b6f

24 files changed

+62
-35
lines changed

compiler/rustc_typeck/src/check/gather_locals.rs

Lines changed: 6 additions & 7 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 9 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 6 additions & 0 deletions
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

0 commit comments

Comments
 (0)