Skip to content

Commit

Permalink
On implicit Sized bound on fn argument, point at type instead of pa…
Browse files Browse the repository at this point in the history
…ttern

Instead of

```
error[E0277]: the size for values of type `(dyn ThriftService<(), AssocType = _> + 'static)` cannot be known at compilation time
  --> $DIR/issue-59324.rs:23:20
   |
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
   |                    ^^^^^^^ doesn't have a size known at compile-time
```

output

```
error[E0277]: the size for values of type `(dyn ThriftService<(), AssocType = _> + 'static)` cannot be known at compilation time
  --> $DIR/issue-59324.rs:23:29
   |
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
   |                             ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
```
  • Loading branch information
estebank committed Sep 27, 2024
1 parent 648d024 commit c7d171d
Show file tree
Hide file tree
Showing 31 changed files with 110 additions and 104 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub(super) fn check_fn<'a, 'tcx>(
if !params_can_be_unsized {
fcx.require_type_is_sized(
param_ty,
param.pat.span,
param.ty_span,
// ty.span == binding_span iff this is a closure parameter with no type ascription,
// or if it's an implicit `self` parameter
ObligationCauseCode::SizedArgumentType(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/gather_locals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
if !self.fcx.tcx.features().unsized_fn_params {
self.fcx.require_type_is_sized(
var_ty,
p.span,
ty_span,
// ty_span == ident.span iff this is a closure parameter with no type
// ascription, or if it's an implicit `self` parameter
ObligationCauseCode::SizedArgumentType(
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/tests/ui/crashes/ice-6251.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> tests/ui/crashes/ice-6251.rs:4:45
--> tests/ui/crashes/ice-6251.rs:4:48
|
LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: [u8]| x }]> {
| ^ doesn't have a size known at compile-time
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/issue-59324.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ LL | pub trait Foo: NotFoo {
| ^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the size for values of type `(dyn ThriftService<(), AssocType = _> + 'static)` cannot be known at compilation time
--> $DIR/issue-59324.rs:23:20
--> $DIR/issue-59324.rs:23:29
|
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
| ^^^^^^^ doesn't have a size known at compile-time
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn ThriftService<(), AssocType = _> + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/coherence/occurs-check/opaques.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ LL | impl<T> Trait<T> for defining_scope::Alias<T> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation

error[E0282]: type annotations needed
--> $DIR/opaques.rs:13:20
--> $DIR/opaques.rs:13:23
|
LL | pub fn cast<T>(x: Container<Alias<T>, T>) -> Container<T, T> {
| ^ cannot infer type
| ^^^^^^^^^^^^^^^^^^^^^^ cannot infer type

error: aborting due to 2 previous errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/did_you_mean/replace-impl-infer-ty-from-trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ LL | fn bar(i: i32, t: usize, s: &()) -> (usize, i32) {
| ~~~ ~~~~~ ~~~ ~~~~~~~~~~~~

error[E0282]: type annotations needed
--> $DIR/replace-impl-infer-ty-from-trait.rs:9:12
--> $DIR/replace-impl-infer-ty-from-trait.rs:9:15
|
LL | fn bar(i: _, t: _, s: _) -> _ {
| ^ cannot infer type
| ^ cannot infer type

error: aborting due to 2 previous errors

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/error-codes/E0277.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/E0277.rs:11:6
--> $DIR/E0277.rs:11:9
|
LL | fn f(p: Path) { }
| ^ doesn't have a size known at compile-time
| ^^^^ doesn't have a size known at compile-time
|
= help: within `Path`, the trait `Sized` is not implemented for `[u8]`, which is required by `Path: Sized`
note: required because it appears within the type `Path`
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/feature-gates/feature-gate-unsized_fn_params.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
--> $DIR/feature-gate-unsized_fn_params.rs:17:8
--> $DIR/feature-gate-unsized_fn_params.rs:17:11
|
LL | fn foo(x: dyn Foo) {
| ^ doesn't have a size known at compile-time
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand All @@ -16,10 +16,10 @@ LL | fn foo(x: &dyn Foo) {
| +

error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
--> $DIR/feature-gate-unsized_fn_params.rs:21:8
--> $DIR/feature-gate-unsized_fn_params.rs:21:11
|
LL | fn bar(x: Foo) {
| ^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand All @@ -33,10 +33,10 @@ LL | fn bar(x: &dyn Foo) {
| ++++

error[E0277]: the size for values of type `[()]` cannot be known at compilation time
--> $DIR/feature-gate-unsized_fn_params.rs:25:8
--> $DIR/feature-gate-unsized_fn_params.rs:25:11
|
LL | fn qux(_: [()]) {}
| ^ doesn't have a size known at compile-time
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[()]`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/feature-gates/feature-gate-unsized_locals.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `(dyn FnOnce() + 'static)` cannot be known at compilation time
--> $DIR/feature-gate-unsized_locals.rs:1:6
--> $DIR/feature-gate-unsized_locals.rs:1:9
|
LL | fn f(f: dyn FnOnce()) {}
| ^ doesn't have a size known at compile-time
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn FnOnce() + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ LL | struct Query<'q> {}
= help: consider removing `'q`, referring to it in a field, or using a marker such as `PhantomData`

error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> $DIR/ice-ifer-var-leaked-out-of-rollback-122098.rs:7:17
--> $DIR/ice-ifer-var-leaked-out-of-rollback-122098.rs:7:21
|
LL | fn for_each(mut self, mut f: Box<dyn FnMut(Self::Item<'_>) + 'static>) {}
| ^^^^^^^^ doesn't have a size known at compile-time
| ^^^^ doesn't have a size known at compile-time
|
= help: unsized fn params are gated as an unstable feature
help: consider further restricting `Self`
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-38954.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/issue-38954.rs:1:10
--> $DIR/issue-38954.rs:1:18
|
LL | fn _test(ref _p: str) {}
| ^^^^^^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-41229-ref-str.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/issue-41229-ref-str.rs:1:16
--> $DIR/issue-41229-ref-str.rs:1:23
|
LL | pub fn example(ref s: str) {}
| ^^^^^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= help: unsized fn params are gated as an unstable feature
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/issues/issue-42312.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `<Self as Deref>::Target` cannot be known at compilation time
--> $DIR/issue-42312.rs:4:12
--> $DIR/issue-42312.rs:4:15
|
LL | fn baz(_: Self::Target) where Self: Deref {}
| ^ doesn't have a size known at compile-time
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `<Self as Deref>::Target`
= help: unsized fn params are gated as an unstable feature
Expand All @@ -16,10 +16,10 @@ LL | fn baz(_: &Self::Target) where Self: Deref {}
| +

error[E0277]: the size for values of type `(dyn ToString + 'static)` cannot be known at compilation time
--> $DIR/issue-42312.rs:8:10
--> $DIR/issue-42312.rs:8:13
|
LL | pub fn f(_: dyn ToString) {}
| ^ doesn't have a size known at compile-time
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn ToString + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-5883.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
--> $DIR/issue-5883.rs:8:5
--> $DIR/issue-5883.rs:8:8
|
LL | r: dyn A + 'static
| ^ doesn't have a size known at compile-time
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn A + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-66706.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ LL | [0; match [|f @ &ref _| () ] {} ]
| while parsing this `match` expression

error[E0282]: type annotations needed
--> $DIR/issue-66706.rs:2:11
--> $DIR/issue-66706.rs:2:14
|
LL | [0; [|_: _ &_| ()].len()]
| ^ cannot infer type
| ^ cannot infer type

error[E0282]: type annotations needed
--> $DIR/issue-66706.rs:13:11
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/object-safety/avoid-ice-on-warning-2.new.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ LL | f()
| call expression requires function

error[E0277]: the size for values of type `(dyn Copy + 'static)` cannot be known at compilation time
--> $DIR/avoid-ice-on-warning-2.rs:4:10
--> $DIR/avoid-ice-on-warning-2.rs:4:13
|
LL | fn id<F>(f: Copy) -> usize {
| ^ doesn't have a size known at compile-time
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Copy + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ LL | f()
| call expression requires function

error[E0277]: the size for values of type `(dyn Copy + 'static)` cannot be known at compilation time
--> $DIR/avoid-ice-on-warning-2.rs:4:10
--> $DIR/avoid-ice-on-warning-2.rs:4:13
|
LL | fn id<F>(f: Copy) -> usize {
| ^ doesn't have a size known at compile-time
| ^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Copy + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/resolve/issue-3907-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ LL | fn bar();
| ^^^ the trait cannot be made into an object because associated function `bar` has no `self` parameter

error[E0277]: the size for values of type `(dyn issue_3907::Foo + 'static)` cannot be known at compilation time
--> $DIR/issue-3907-2.rs:11:8
--> $DIR/issue-3907-2.rs:11:12
|
LL | fn bar(_x: Foo) {}
| ^^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn issue_3907::Foo + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/resolve/issue-5035-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `(dyn I + 'static)` cannot be known at compilation time
--> $DIR/issue-5035-2.rs:4:8
--> $DIR/issue-5035-2.rs:4:12
|
LL | fn foo(_x: K) {}
| ^^ doesn't have a size known at compile-time
| ^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn I + 'static)`
= help: unsized fn params are gated as an unstable feature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ LL | trait Other: Sized {}
| this trait cannot be made into an object...

error[E0277]: the size for values of type `Self` cannot be known at compilation time
--> $DIR/object-unsafe-trait-references-self.rs:2:19
--> $DIR/object-unsafe-trait-references-self.rs:2:22
|
LL | fn baz(&self, _: Self) {}
| ^ doesn't have a size known at compile-time
| ^^^^ doesn't have a size known at compile-time
|
= help: unsized fn params are gated as an unstable feature
help: consider further restricting `Self`
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/suggestions/path-by-value.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/path-by-value.rs:3:6
--> $DIR/path-by-value.rs:3:9
|
LL | fn f(p: Path) { }
| ^ doesn't have a size known at compile-time
| ^^^^ doesn't have a size known at compile-time
|
= help: within `Path`, the trait `Sized` is not implemented for `[u8]`, which is required by `Path: Sized`
note: required because it appears within the type `Path`
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/suggestions/unsized-function-parameter.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/unsized-function-parameter.rs:5:9
--> $DIR/unsized-function-parameter.rs:5:14
|
LL | fn foo1(bar: str) {}
| ^^^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= help: unsized fn params are gated as an unstable feature
Expand All @@ -12,10 +12,10 @@ LL | fn foo1(bar: &str) {}
| +

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/unsized-function-parameter.rs:11:9
--> $DIR/unsized-function-parameter.rs:11:15
|
LL | fn foo2(_bar: str) {}
| ^^^^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= help: unsized fn params are gated as an unstable feature
Expand All @@ -25,10 +25,10 @@ LL | fn foo2(_bar: &str) {}
| +

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/unsized-function-parameter.rs:17:9
--> $DIR/unsized-function-parameter.rs:17:12
|
LL | fn foo3(_: str) {}
| ^ doesn't have a size known at compile-time
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= help: unsized fn params are gated as an unstable feature
Expand Down
18 changes: 10 additions & 8 deletions tests/ui/trait-bounds/apit-unsized.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
error[E0277]: the size for values of type `impl Iterator<Item = i32> + ?Sized` cannot be known at compilation time
--> $DIR/apit-unsized.rs:1:8
--> $DIR/apit-unsized.rs:1:11
|
LL | fn foo(_: impl Iterator<Item = i32> + ?Sized) {}
| ^ ---------------------------------- this type parameter needs to be `Sized`
| |
| doesn't have a size known at compile-time
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| doesn't have a size known at compile-time
| this type parameter needs to be `Sized`
|
= help: unsized fn params are gated as an unstable feature
help: consider removing the `?Sized` bound to make the type parameter `Sized`
Expand All @@ -18,12 +19,13 @@ LL | fn foo(_: &impl Iterator<Item = i32> + ?Sized) {}
| +

error[E0277]: the size for values of type `impl ?Sized` cannot be known at compilation time
--> $DIR/apit-unsized.rs:2:8
--> $DIR/apit-unsized.rs:2:11
|
LL | fn bar(_: impl ?Sized) {}
| ^ ----------- this type parameter needs to be `Sized`
| |
| doesn't have a size known at compile-time
| ^^^^^^^^^^^
| |
| doesn't have a size known at compile-time
| this type parameter needs to be `Sized`
|
= help: unsized fn params are gated as an unstable feature
help: consider replacing `?Sized` with `Sized`
Expand Down
Loading

0 comments on commit c7d171d

Please sign in to comment.