Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3307,6 +3307,19 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType => {
err.note("the return type of a function must have a statically known size");
if let Some(node_body_id) = tcx.hir_node_by_def_id(body_id).body_id()
&& let body = tcx.hir_body(node_body_id)
&& let hir::Node::Expr(closure_expr) = tcx.hir_node_by_def_id(body_id)
&& let hir::ExprKind::Closure(closure) = closure_expr.kind
&& let hir::FnRetTy::DefaultReturn(_) = closure.fn_decl.output
{
err.span_suggestion_verbose(
body.value.span.shrink_to_lo(),
"consider borrowing the value",
"&",
Applicability::MaybeIncorrect,
);
}
}
ObligationCauseCode::SizedYieldType => {
err.note("the yield type of a coroutine must have a statically known size");
Expand Down
4 changes: 4 additions & 0 deletions src/tools/clippy/tests/ui/crashes/ice-6251.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: [u8]| x }]> {
|
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
= note: the return type of a function must have a statically known size
help: consider borrowing the value
|
LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: [u8]| &x }]> {
| +

error[E0308]: mismatched types
--> tests/ui/crashes/ice-6251.rs:4:44
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/closures/unsized-return-suggest-ref-issue-152064.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![allow(unused_variables, for_loops_over_fallibles)]
//@ run-rustfix

fn main() {
// Basic case from the issue: str slice
let o = Some("Hello, world!");
for s in o.map(|s| &s[3..8]) {}
//~^ ERROR the size for values of type `str` cannot be known at compilation time
//~| ERROR the size for values of type `str` cannot be known at compilation time
//~| ERROR the size for values of type `str` cannot be known at compilation time
//~| ERROR `Option<str>` is not an iterator

// Byte slice case
let arr = Some(b"Hello, world!");
for s in arr.map(|s| &s[3..8]) {}
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
//~| ERROR the size for values of type `[u8]` cannot be known at compilation time
//~| ERROR the size for values of type `[u8]` cannot be known at compilation time
//~| ERROR `Option<[u8]>` is not an iterator
}
20 changes: 20 additions & 0 deletions tests/ui/closures/unsized-return-suggest-ref-issue-152064.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![allow(unused_variables, for_loops_over_fallibles)]
//@ run-rustfix

fn main() {
// Basic case from the issue: str slice
let o = Some("Hello, world!");
for s in o.map(|s| s[3..8]) {}
//~^ ERROR the size for values of type `str` cannot be known at compilation time
//~| ERROR the size for values of type `str` cannot be known at compilation time
//~| ERROR the size for values of type `str` cannot be known at compilation time
//~| ERROR `Option<str>` is not an iterator

// Byte slice case
let arr = Some(b"Hello, world!");
for s in arr.map(|s| s[3..8]) {}
//~^ ERROR the size for values of type `[u8]` cannot be known at compilation time
//~| ERROR the size for values of type `[u8]` cannot be known at compilation time
//~| ERROR the size for values of type `[u8]` cannot be known at compilation time
//~| ERROR `Option<[u8]>` is not an iterator
}
105 changes: 105 additions & 0 deletions tests/ui/closures/unsized-return-suggest-ref-issue-152064.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/unsized-return-suggest-ref-issue-152064.rs:7:16
|
LL | for s in o.map(|s| s[3..8]) {}
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Option::<T>::map`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/unsized-return-suggest-ref-issue-152064.rs:7:24
|
LL | for s in o.map(|s| s[3..8]) {}
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
= note: the return type of a function must have a statically known size
help: consider borrowing the value
|
LL | for s in o.map(|s| &s[3..8]) {}
| +

error[E0277]: the size for values of type `str` cannot be known at compilation time
--> $DIR/unsized-return-suggest-ref-issue-152064.rs:7:14
|
LL | for s in o.map(|s| s[3..8]) {}
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `str`
note: required by an implicit `Sized` bound in `Option`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0277]: `Option<str>` is not an iterator
--> $DIR/unsized-return-suggest-ref-issue-152064.rs:7:14
|
LL | for s in o.map(|s| s[3..8]) {}
| ^^^^^^^^^^^^^^^^^^ `Option<str>` is not an iterator
|
= help: the trait `IntoIterator` is not implemented for `Option<str>`
help: the following other types implement trait `IntoIterator`
--> $SRC_DIR/core/src/option.rs:LL:COL
|
= note: `Option<T>`
::: $SRC_DIR/core/src/option.rs:LL:COL
|
= note: `&Option<T>`
::: $SRC_DIR/core/src/option.rs:LL:COL
|
= note: `&mut Option<T>`

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/unsized-return-suggest-ref-issue-152064.rs:15:18
|
LL | for s in arr.map(|s| s[3..8]) {}
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
note: required by an implicit `Sized` bound in `Option::<T>::map`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/unsized-return-suggest-ref-issue-152064.rs:15:26
|
LL | for s in arr.map(|s| s[3..8]) {}
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
= note: the return type of a function must have a statically known size
help: consider borrowing the value
|
LL | for s in arr.map(|s| &s[3..8]) {}
| +

error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> $DIR/unsized-return-suggest-ref-issue-152064.rs:15:14
|
LL | for s in arr.map(|s| s[3..8]) {}
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `[u8]`
note: required by an implicit `Sized` bound in `Option`
--> $SRC_DIR/core/src/option.rs:LL:COL

error[E0277]: `Option<[u8]>` is not an iterator
--> $DIR/unsized-return-suggest-ref-issue-152064.rs:15:14
|
LL | for s in arr.map(|s| s[3..8]) {}
| ^^^^^^^^^^^^^^^^^^^^ `Option<[u8]>` is not an iterator
|
= help: the trait `IntoIterator` is not implemented for `Option<[u8]>`
help: the following other types implement trait `IntoIterator`
--> $SRC_DIR/core/src/option.rs:LL:COL
|
= note: `Option<T>`
::: $SRC_DIR/core/src/option.rs:LL:COL
|
= note: `&Option<T>`
::: $SRC_DIR/core/src/option.rs:LL:COL
|
= note: `&mut Option<T>`

error: aborting due to 8 previous errors

For more information about this error, try `rustc --explain E0277`.
Loading