Skip to content

Commit eaa940a

Browse files
authored
Rollup merge of #112325 - notriddle:notriddle/issue-111932, r=compiler-errors
diagnostics: do not suggest type name tweaks on type-inferred closure args Fixes #111932
2 parents 0d24c6a + 467bc9f commit eaa940a

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

compiler/rustc_hir_typeck/src/check.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,19 @@ pub(super) fn check_fn<'a, 'tcx>(
9696
// for simple cases like `fn foo(x: Trait)`,
9797
// where we would error once on the parameter as a whole, and once on the binding `x`.
9898
if param.pat.simple_ident().is_none() && !params_can_be_unsized {
99-
fcx.require_type_is_sized(param_ty, param.pat.span, traits::SizedArgumentType(ty_span));
99+
fcx.require_type_is_sized(
100+
param_ty,
101+
param.pat.span,
102+
// ty_span == binding_span iff this is a closure parameter with no type ascription,
103+
// or if it's an implicit `self` parameter
104+
traits::SizedArgumentType(
105+
if ty_span == Some(param.span) && tcx.is_closure(fn_def_id.into()) {
106+
None
107+
} else {
108+
ty_span
109+
},
110+
),
111+
);
100112
}
101113

102114
fcx.write_ty(param.hir_id, param_ty);

compiler/rustc_hir_typeck/src/gather_locals.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,17 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
129129
self.fcx.require_type_is_sized(
130130
var_ty,
131131
p.span,
132-
traits::SizedArgumentType(Some(ty_span)),
132+
// ty_span == ident.span iff this is a closure parameter with no type
133+
// ascription, or if it's an implicit `self` parameter
134+
traits::SizedArgumentType(
135+
if ty_span == ident.span
136+
&& self.fcx.tcx.is_closure(self.fcx.body_id.into())
137+
{
138+
None
139+
} else {
140+
Some(ty_span)
141+
},
142+
),
133143
);
134144
}
135145
} else {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -2807,8 +2807,8 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
28072807
err.help("unsized locals are gated as an unstable feature");
28082808
}
28092809
}
2810-
ObligationCauseCode::SizedArgumentType(sp) => {
2811-
if let Some(span) = sp {
2810+
ObligationCauseCode::SizedArgumentType(ty_span) => {
2811+
if let Some(span) = ty_span {
28122812
if let ty::PredicateKind::Clause(clause) = predicate.kind().skip_binder()
28132813
&& let ty::Clause::Trait(trait_pred) = clause
28142814
&& let ty::Dynamic(..) = trait_pred.self_ty().kind()

tests/ui/closures/issue-111932.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
trait Foo: std::fmt::Debug {}
2+
3+
fn print_foos(foos: impl Iterator<Item = dyn Foo>) {
4+
foos.for_each(|foo| { //~ ERROR [E0277]
5+
println!("{:?}", foo); //~ ERROR [E0277]
6+
});
7+
}
8+
9+
fn main() {}

tests/ui/closures/issue-111932.stderr

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
2+
--> $DIR/issue-111932.rs:4:20
3+
|
4+
LL | foos.for_each(|foo| {
5+
| ^^^ doesn't have a size known at compile-time
6+
|
7+
= 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
10+
11+
error[E0277]: the size for values of type `dyn Foo` cannot be known at compilation time
12+
--> $DIR/issue-111932.rs:5:26
13+
|
14+
LL | println!("{:?}", foo);
15+
| ---- ^^^ doesn't have a size known at compile-time
16+
| |
17+
| required by a bound introduced by this call
18+
|
19+
= help: the trait `Sized` is not implemented for `dyn Foo`
20+
note: required by a bound in `core::fmt::rt::Argument::<'a>::new_debug`
21+
--> $SRC_DIR/core/src/fmt/rt.rs:LL:COL
22+
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
24+
error: aborting due to 2 previous errors
25+
26+
For more information about this error, try `rustc --explain E0277`.

tests/ui/unsized-locals/issue-67981.stderr

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

1310
error: aborting due to previous error
1411

0 commit comments

Comments
 (0)