Skip to content

Commit 1d9ec37

Browse files
committed
Emit single explanatory note on unsized locals and fn params instead of two
On stable, unify wording of unsized locals and unsized fn params so the deduplication machinery can trigger. On nightly, mention the nightly feature to enable unsized locals or unsized fn params.
1 parent dc1e10d commit 1d9ec37

File tree

52 files changed

+158
-183
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+158
-183
lines changed

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

+71-59
Original file line numberDiff line numberDiff line change
@@ -3137,11 +3137,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
31373137
Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }) => {
31383138
err.span_suggestion_verbose(
31393139
ty.span.shrink_to_lo(),
3140-
"consider borrowing here",
3140+
"borrowed types have a statically known size",
31413141
"&",
31423142
Applicability::MachineApplicable,
31433143
);
3144-
err.note("all local variables must have a statically known size");
31453144
}
31463145
Node::LetStmt(hir::LetStmt {
31473146
init: Some(hir::Expr { kind: hir::ExprKind::Index(..), span, .. }),
@@ -3152,11 +3151,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
31523151
// order to use have a slice instead.
31533152
err.span_suggestion_verbose(
31543153
span.shrink_to_lo(),
3155-
"consider borrowing here",
3154+
"borrowed values have a statically known size",
31563155
"&",
31573156
Applicability::MachineApplicable,
31583157
);
3159-
err.note("all local variables must have a statically known size");
31603158
}
31613159
Node::Param(param) => {
31623160
err.span_suggestion_verbose(
@@ -3168,11 +3166,20 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
31683166
);
31693167
}
31703168
_ => {
3171-
err.note("all local variables must have a statically known size");
3169+
if !tcx.sess.opts.unstable_features.is_nightly_build()
3170+
&& !tcx.features().unsized_locals
3171+
{
3172+
err.note("all bindings must have a statically known size");
3173+
}
31723174
}
31733175
}
3174-
if !tcx.features().unsized_locals {
3175-
err.help("unsized locals are gated as an unstable feature");
3176+
if tcx.sess.opts.unstable_features.is_nightly_build()
3177+
&& !tcx.features().unsized_locals
3178+
{
3179+
err.help(
3180+
"unsized locals are gated as unstable feature \
3181+
`#[feature(unsized_locals)]`",
3182+
);
31763183
}
31773184
}
31783185
ObligationCauseCode::SizedArgumentType(hir_id) => {
@@ -3197,64 +3204,69 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
31973204
{
31983205
ty = Some(t);
31993206
}
3200-
if let Some(ty) = ty {
3201-
match ty.kind {
3202-
hir::TyKind::TraitObject(traits, _, _) => {
3203-
let (span, kw) = match traits {
3204-
[first, ..] if first.span.lo() == ty.span.lo() => {
3205-
// Missing `dyn` in front of trait object.
3206-
(ty.span.shrink_to_lo(), "dyn ")
3207-
}
3208-
[first, ..] => (ty.span.until(first.span), ""),
3209-
[] => span_bug!(ty.span, "trait object with no traits: {ty:?}"),
3210-
};
3211-
let needs_parens = traits.len() != 1;
3212-
err.span_suggestion_verbose(
3213-
span,
3214-
"you can use `impl Trait` as the argument type",
3215-
"impl ",
3216-
Applicability::MaybeIncorrect,
3217-
);
3218-
let sugg = if !needs_parens {
3219-
vec![(span.shrink_to_lo(), format!("&{kw}"))]
3220-
} else {
3221-
vec![
3222-
(span.shrink_to_lo(), format!("&({kw}")),
3223-
(ty.span.shrink_to_hi(), ")".to_string()),
3224-
]
3225-
};
3226-
err.multipart_suggestion_verbose(
3227-
borrowed_msg,
3228-
sugg,
3229-
Applicability::MachineApplicable,
3230-
);
3231-
}
3232-
hir::TyKind::Slice(_ty) => {
3233-
err.span_suggestion_verbose(
3234-
ty.span.shrink_to_lo(),
3235-
"function arguments must have a statically known size, borrowed \
3236-
slices always have a known size",
3237-
"&",
3238-
Applicability::MachineApplicable,
3239-
);
3240-
}
3241-
hir::TyKind::Path(_) => {
3242-
err.span_suggestion_verbose(
3243-
ty.span.shrink_to_lo(),
3244-
borrowed_msg,
3245-
"&",
3246-
Applicability::MachineApplicable,
3247-
);
3207+
match ty.map(|ty| (ty.kind, ty.span)) {
3208+
Some((hir::TyKind::TraitObject(traits, _, _), sp)) => {
3209+
let (span, kw) = match traits {
3210+
[first, ..] if first.span.lo() == sp.lo() => {
3211+
// Missing `dyn` in front of trait object.
3212+
(sp.shrink_to_lo(), "dyn ")
3213+
}
3214+
[first, ..] => (sp.until(first.span), ""),
3215+
[] => span_bug!(sp, "trait object with no traits: {ty:?}"),
3216+
};
3217+
let needs_parens = traits.len() != 1;
3218+
err.span_suggestion_verbose(
3219+
span,
3220+
"you can use `impl Trait` as the argument type",
3221+
"impl ",
3222+
Applicability::MaybeIncorrect,
3223+
);
3224+
let sugg = if !needs_parens {
3225+
vec![(span.shrink_to_lo(), format!("&{kw}"))]
3226+
} else {
3227+
vec![
3228+
(span.shrink_to_lo(), format!("&({kw}")),
3229+
(sp.shrink_to_hi(), ")".to_string()),
3230+
]
3231+
};
3232+
err.multipart_suggestion_verbose(
3233+
borrowed_msg,
3234+
sugg,
3235+
Applicability::MachineApplicable,
3236+
);
3237+
}
3238+
Some((hir::TyKind::Slice(_ty), span)) => {
3239+
err.span_suggestion_verbose(
3240+
span.shrink_to_lo(),
3241+
"function arguments must have a statically known size, borrowed \
3242+
slices always have a known size",
3243+
"&",
3244+
Applicability::MachineApplicable,
3245+
);
3246+
}
3247+
Some((hir::TyKind::Path(_), span)) => {
3248+
err.span_suggestion_verbose(
3249+
span.shrink_to_lo(),
3250+
borrowed_msg,
3251+
"&",
3252+
Applicability::MachineApplicable,
3253+
);
3254+
}
3255+
_ => {
3256+
if !tcx.sess.opts.unstable_features.is_nightly_build()
3257+
&& !tcx.features().unsized_fn_params
3258+
{
3259+
err.note("all bindings must have a statically known size");
32483260
}
3249-
_ => {}
32503261
}
3251-
} else {
3252-
err.note("all function arguments must have a statically known size");
32533262
}
32543263
if tcx.sess.opts.unstable_features.is_nightly_build()
32553264
&& !tcx.features().unsized_fn_params
32563265
{
3257-
err.help("unsized fn params are gated as an unstable feature");
3266+
err.help(
3267+
"unsized fn params are gated as unstable feature \
3268+
`#[feature(unsized_fn_params)]`",
3269+
);
32583270
}
32593271
}
32603272
ObligationCauseCode::SizedReturnType | ObligationCauseCode::SizedCallReturnType(_) => {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: [u8]| x }]> {
55
| ^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `std::marker::Sized` is not implemented for `[u8]`
8-
= help: unsized fn params are gated as an unstable feature
8+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
99
help: function arguments must have a statically known size, borrowed slices always have a known size
1010
|
1111
LL | fn bug<T>() -> impl Iterator<Item = [(); { |x: &[u8]| x }]> {

tests/ui/associated-types/associated-types-unsized.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ LL | let x = t.get();
55
| ^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `<T as Get>::Value`
8-
= note: all local variables must have a statically known size
9-
= help: unsized locals are gated as an unstable feature
8+
= help: unsized locals are gated as unstable feature `#[feature(unsized_locals)]`
109
help: consider further restricting the associated type
1110
|
1211
LL | fn foo<T:Get>(t: T) where <T as Get>::Value: Sized {

tests/ui/associated-types/issue-20005.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
2424
LL | self
2525
| ^^^^ doesn't have a size known at compile-time
2626
|
27-
= help: unsized fn params are gated as an unstable feature
27+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
2828
help: consider further restricting `Self`
2929
|
3030
LL | ) -> <Dst as From<Self>>::Result where Dst: From<Self>, Self: Sized {
@@ -60,8 +60,7 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
6060
LL | From::from(self)
6161
| ^^^^ doesn't have a size known at compile-time
6262
|
63-
= note: all function arguments must have a statically known size
64-
= help: unsized fn params are gated as an unstable feature
63+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
6564
help: consider further restricting `Self`
6665
|
6766
LL | ) -> <Dst as From<Self>>::Result where Dst: From<Self>, Self: Sized {

tests/ui/associated-types/issue-59324.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
8585
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
8686
|
8787
= help: the trait `Sized` is not implemented for `(dyn ThriftService<(), AssocType = _> + 'static)`
88-
= help: unsized fn params are gated as an unstable feature
88+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
8989
help: you can use `impl Trait` as the argument type
9090
|
9191
LL | fn with_factory<H>(factory: impl ThriftService<()>) {}

tests/ui/async-await/issue-72590-type-error-sized.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ note: required because it appears within the type `Foo`
2222
|
2323
LL | struct Foo {
2424
| ^^^
25-
= help: unsized fn params are gated as an unstable feature
25+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
2626
help: function arguments must have a statically known size, borrowed types always have a known size
2727
|
2828
LL | async fn frob(&self) {}

tests/ui/closures/cannot-call-unsized-via-ptr-2.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ LL | let f = if true { |_a| {} } else { |_b| {} };
55
| ^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[u8]`
8-
= note: all function arguments must have a statically known size
98

109
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
1110
--> $DIR/cannot-call-unsized-via-ptr-2.rs:5:41
@@ -14,7 +13,6 @@ LL | let f = if true { |_a| {} } else { |_b| {} };
1413
| ^^ doesn't have a size known at compile-time
1514
|
1615
= help: the trait `Sized` is not implemented for `[u8]`
17-
= note: all function arguments must have a statically known size
1816

1917
error: aborting due to 2 previous errors
2018

tests/ui/closures/cannot-call-unsized-via-ptr.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ LL | let f: fn([u8]) = |_result| {};
55
| ^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[u8]`
8-
= note: all function arguments must have a statically known size
98

109
error: aborting due to 1 previous error
1110

tests/ui/closures/issue-111932.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ LL | foos.for_each(|foo| {
55
| ^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
8-
= note: all function arguments must have a statically known size
9-
= help: unsized fn params are gated as an unstable feature
8+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
109

1110
error: aborting due to 1 previous error
1211

tests/ui/closures/issue-78720.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
4343
LL | fn map2<F>(self, f: F) -> Map2<F> {}
4444
| ^^^^ doesn't have a size known at compile-time
4545
|
46-
= help: unsized fn params are gated as an unstable feature
46+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
4747
help: consider further restricting `Self`
4848
|
4949
LL | fn map2<F>(self, f: F) -> Map2<F> where Self: Sized {}

tests/ui/error-codes/E0038.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ LL | let y = x.foo();
3535
| ^^^ doesn't have a size known at compile-time
3636
|
3737
= help: the trait `Sized` is not implemented for `dyn Trait`
38-
= note: all local variables must have a statically known size
39-
= help: unsized locals are gated as an unstable feature
38+
= help: unsized locals are gated as unstable feature `#[feature(unsized_locals)]`
4039

4140
error: aborting due to 3 previous errors
4241

tests/ui/error-codes/E0277.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn f(p: Path) { }
77
= help: within `Path`, the trait `Sized` is not implemented for `[u8]`, which is required by `Path: Sized`
88
note: required because it appears within the type `Path`
99
--> $SRC_DIR/std/src/path.rs:LL:COL
10-
= help: unsized fn params are gated as an unstable feature
10+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
1111
help: function arguments must have a statically known size, borrowed types always have a known size
1212
|
1313
LL | fn f(p: &Path) { }

tests/ui/feature-gates/feature-gate-unsized_fn_params.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn foo(x: dyn Foo) {
55
| ^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
8-
= help: unsized fn params are gated as an unstable feature
8+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
99
help: you can use `impl Trait` as the argument type
1010
|
1111
LL | fn foo(x: impl Foo) {
@@ -22,7 +22,7 @@ LL | fn bar(x: Foo) {
2222
| ^^^ doesn't have a size known at compile-time
2323
|
2424
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
25-
= help: unsized fn params are gated as an unstable feature
25+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
2626
help: you can use `impl Trait` as the argument type
2727
|
2828
LL | fn bar(x: impl Foo) {
@@ -39,7 +39,7 @@ LL | fn qux(_: [()]) {}
3939
| ^^^^ doesn't have a size known at compile-time
4040
|
4141
= help: the trait `Sized` is not implemented for `[()]`
42-
= help: unsized fn params are gated as an unstable feature
42+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
4343
help: function arguments must have a statically known size, borrowed slices always have a known size
4444
|
4545
LL | fn qux(_: &[()]) {}
@@ -52,8 +52,7 @@ LL | foo(*x);
5252
| ^^ doesn't have a size known at compile-time
5353
|
5454
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
55-
= note: all function arguments must have a statically known size
56-
= help: unsized fn params are gated as an unstable feature
55+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
5756

5857
error: aborting due to 4 previous errors
5958

tests/ui/feature-gates/feature-gate-unsized_locals.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn f(f: dyn FnOnce()) {}
55
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn FnOnce() + 'static)`
8-
= help: unsized fn params are gated as an unstable feature
8+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
99
help: you can use `impl Trait` as the argument type
1010
|
1111
LL | fn f(f: impl FnOnce()) {}

tests/ui/generic-associated-types/issue-79636-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
2020
LL | fn bind<B, F>(self, f: F) -> Self::Wrapped<B> {
2121
| ^^^^ doesn't have a size known at compile-time
2222
|
23-
= help: unsized fn params are gated as an unstable feature
23+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
2424
help: consider further restricting `Self`
2525
|
2626
LL | fn bind<B, F>(self, f: F) -> Self::Wrapped<B> where Self: Sized {

tests/ui/impl-trait/dyn-impl-type-mismatch.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ LL | let a: dyn Trait = if true {
55
| ^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `dyn Trait`
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
8+
= help: unsized locals are gated as unstable feature `#[feature(unsized_locals)]`
9+
help: borrowed types have a statically known size
1110
|
1211
LL | let a: &dyn Trait = if true {
1312
| +

tests/ui/inference/ice-ifer-var-leaked-out-of-rollback-122098.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
4141
LL | fn for_each(mut self, mut f: Box<dyn FnMut(Self::Item<'_>) + 'static>) {}
4242
| ^^^^ doesn't have a size known at compile-time
4343
|
44-
= help: unsized fn params are gated as an unstable feature
44+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
4545
help: consider further restricting `Self`
4646
|
4747
LL | fn for_each(mut self, mut f: Box<dyn FnMut(Self::Item<'_>) + 'static>) where Self: Sized {}

tests/ui/issues/issue-15756.stderr

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ LL | &mut something
55
| ^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[T]`
8-
= note: all local variables must have a statically known size
9-
= help: unsized locals are gated as an unstable feature
8+
= help: unsized locals are gated as unstable feature `#[feature(unsized_locals)]`
109

1110
error: aborting due to 1 previous error
1211

tests/ui/issues/issue-27078.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
44
LL | fn foo(self) -> &'static i32 {
55
| ^^^^ doesn't have a size known at compile-time
66
|
7-
= help: unsized fn params are gated as an unstable feature
7+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
88
help: consider further restricting `Self`
99
|
1010
LL | fn foo(self) -> &'static i32 where Self: Sized {

tests/ui/issues/issue-38954.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn _test(ref _p: str) {}
55
| ^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `str`
8-
= help: unsized fn params are gated as an unstable feature
8+
= help: unsized fn params are gated as unstable feature `#[feature(unsized_fn_params)]`
99
help: function arguments must have a statically known size, borrowed types always have a known size
1010
|
1111
LL | fn _test(ref _p: &str) {}

0 commit comments

Comments
 (0)