Skip to content

Commit cacc00f

Browse files
committed
On implicit Sized bound on fn argument, point at type instead of pattern
1 parent cfad637 commit cacc00f

21 files changed

+100
-64
lines changed

compiler/rustc_infer/src/traits/mod.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,40 @@ impl<'tcx> FulfillmentError<'tcx> {
206206
) -> FulfillmentError<'tcx> {
207207
FulfillmentError { obligation, code, root_obligation }
208208
}
209-
pub fn span(&self) -> Span {
209+
210+
pub fn span(&self, tcx: TyCtxt<'tcx>) -> Span {
210211
let mut span = self.obligation.cause.span;
212+
if let ObligationCauseCode::SizedArgumentType(Some(hir_id)) = self.obligation.cause.code()
213+
&& let Some(ty_span) = {
214+
match tcx.hir_node(*hir_id) {
215+
hir::Node::Ty(hir::Ty { span, .. }) => Some(*span),
216+
hir::Node::Param(hir::Param { ty_span, .. }) => {
217+
// We use `contains` because the type might be surrounded by parentheses,
218+
// which makes `ty_span` and `t.span` disagree with each other, but one
219+
// fully contains the other: `foo: (dyn Foo + Bar)`
220+
// ^-------------^
221+
// ||
222+
// |t.span
223+
// param._ty_span
224+
// Otherwise we'd use `ty_span` directly.
225+
// If we don't do this a case like `fn foo(_: (dyn A + B))` would emit two
226+
// unsized errors one for `(dyn A + B)` and one for `dyn A + B`, because the
227+
// dedup logic would consdider them different.
228+
if let Some(decl) = tcx.parent_hir_node(*hir_id).fn_decl()
229+
&& let Some(t) = decl.inputs.iter().find(|t| ty_span.contains(t.span))
230+
{
231+
Some(t.span)
232+
} else {
233+
Some(*ty_span)
234+
}
235+
}
236+
_ => None,
237+
}
238+
}
239+
{
240+
// On implicit `Sized` on arguments, we want to point at the type and not the pattern.
241+
span = ty_span;
242+
}
211243
// We want to ignore desugarings here: spans are equivalent even
212244
// if one is the result of a desugaring and the other is not.
213245
let expn_data = span.ctxt().outer_expn_data();

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
124124
});
125125

126126
for (index, error) in errors.iter().enumerate() {
127-
error_map.entry(error.span()).or_default().push(ErrorDescriptor {
127+
error_map.entry(error.span(self.tcx)).or_default().push(ErrorDescriptor {
128128
predicate: error.obligation.predicate,
129129
index: Some(index),
130130
});
@@ -166,11 +166,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
166166
for from_expansion in [false, true] {
167167
for (error, suppressed) in iter::zip(&errors, &is_suppressed) {
168168
if !suppressed && error.obligation.cause.span.from_expansion() == from_expansion {
169+
info!(?error.obligation);
170+
info!(?error.obligation.cause);
169171
let guar = self.report_fulfillment_error(error);
170172
reported = Some(guar);
171173
self.reported_trait_errors
172174
.borrow_mut()
173-
.entry(error.span())
175+
.entry(error.span(self.tcx))
174176
.or_insert_with(|| (vec![], guar))
175177
.0
176178
.push(error.obligation.predicate);
@@ -1488,7 +1490,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
14881490
error.obligation.clone(),
14891491
&error.root_obligation,
14901492
selection_error,
1491-
error.span(),
1493+
error.span(self.tcx),
14921494
),
14931495
FulfillmentErrorCode::ProjectionError(ref e) => {
14941496
self.report_projection_error(&error.obligation, e)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ LL | pub trait Foo: NotFoo {
7979
| ^^^^^^^^^^^^^^^^^^^^^
8080

8181
error[E0277]: the size for values of type `(dyn ThriftService<(), AssocType = _> + 'static)` cannot be known at compilation time
82-
--> $DIR/issue-59324.rs:23:20
82+
--> $DIR/issue-59324.rs:23:29
8383
|
8484
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
85-
| ^^^^^^^ doesn't have a size known at compile-time
85+
| ^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
8686
|
8787
= help: the trait `Sized` is not implemented for `(dyn ThriftService<(), AssocType = _> + 'static)`
8888
= help: unsized fn params are gated as an unstable feature

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
2-
--> $DIR/feature-gate-unsized_fn_params.rs:17:8
2+
--> $DIR/feature-gate-unsized_fn_params.rs:17:11
33
|
44
LL | fn foo(x: dyn Foo) {
5-
| ^ doesn't have a size known at compile-time
5+
| ^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
88
= help: unsized fn params are gated as an unstable feature
@@ -16,10 +16,10 @@ LL | fn foo(x: &dyn Foo) {
1616
| +
1717

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

3535
error[E0277]: the size for values of type `[()]` cannot be known at compilation time
36-
--> $DIR/feature-gate-unsized_fn_params.rs:25:8
36+
--> $DIR/feature-gate-unsized_fn_params.rs:25:11
3737
|
3838
LL | fn qux(_: [()]) {}
39-
| ^ doesn't have a size known at compile-time
39+
| ^^^^ doesn't have a size known at compile-time
4040
|
4141
= help: the trait `Sized` is not implemented for `[()]`
4242
= help: unsized fn params are gated as an unstable feature

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `(dyn FnOnce() + 'static)` cannot be known at compilation time
2-
--> $DIR/feature-gate-unsized_locals.rs:1:6
2+
--> $DIR/feature-gate-unsized_locals.rs:1:9
33
|
44
LL | fn f(f: dyn FnOnce()) {}
5-
| ^ doesn't have a size known at compile-time
5+
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn FnOnce() + 'static)`
88
= help: unsized fn params are gated as an unstable feature

tests/ui/issues/issue-38954.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `str` cannot be known at compilation time
2-
--> $DIR/issue-38954.rs:1:10
2+
--> $DIR/issue-38954.rs:1:18
33
|
44
LL | fn _test(ref _p: str) {}
5-
| ^^^^^^ doesn't have a size known at compile-time
5+
| ^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `str`
88
= help: unsized fn params are gated as an unstable feature

tests/ui/issues/issue-41229-ref-str.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `str` cannot be known at compilation time
2-
--> $DIR/issue-41229-ref-str.rs:1:16
2+
--> $DIR/issue-41229-ref-str.rs:1:23
33
|
44
LL | pub fn example(ref s: str) {}
5-
| ^^^^^ doesn't have a size known at compile-time
5+
| ^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `str`
88
= help: unsized fn params are gated as an unstable feature

tests/ui/issues/issue-42312.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `<Self as Deref>::Target` cannot be known at compilation time
2-
--> $DIR/issue-42312.rs:4:12
2+
--> $DIR/issue-42312.rs:4:15
33
|
44
LL | fn baz(_: Self::Target) where Self: Deref {}
5-
| ^ doesn't have a size known at compile-time
5+
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `<Self as Deref>::Target`
88
= help: unsized fn params are gated as an unstable feature
@@ -16,10 +16,10 @@ LL | fn baz(_: &Self::Target) where Self: Deref {}
1616
| +
1717

1818
error[E0277]: the size for values of type `(dyn ToString + 'static)` cannot be known at compilation time
19-
--> $DIR/issue-42312.rs:8:10
19+
--> $DIR/issue-42312.rs:8:13
2020
|
2121
LL | pub fn f(_: dyn ToString) {}
22-
| ^ doesn't have a size known at compile-time
22+
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
2323
|
2424
= help: the trait `Sized` is not implemented for `(dyn ToString + 'static)`
2525
= help: unsized fn params are gated as an unstable feature

tests/ui/issues/issue-5883.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `(dyn A + 'static)` cannot be known at compilation time
2-
--> $DIR/issue-5883.rs:8:5
2+
--> $DIR/issue-5883.rs:8:8
33
|
44
LL | r: dyn A + 'static
5-
| ^ doesn't have a size known at compile-time
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

tests/ui/object-safety/avoid-ice-on-warning-2.new.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ LL | f()
1919
| call expression requires function
2020

2121
error[E0277]: the size for values of type `(dyn Copy + 'static)` cannot be known at compilation time
22-
--> $DIR/avoid-ice-on-warning-2.rs:4:10
22+
--> $DIR/avoid-ice-on-warning-2.rs:4:13
2323
|
2424
LL | fn id<F>(f: Copy) -> usize {
25-
| ^ doesn't have a size known at compile-time
25+
| ^^^^ doesn't have a size known at compile-time
2626
|
2727
= help: the trait `Sized` is not implemented for `(dyn Copy + 'static)`
2828
= help: unsized fn params are gated as an unstable feature

tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ LL | f()
4747
| call expression requires function
4848

4949
error[E0277]: the size for values of type `(dyn Copy + 'static)` cannot be known at compilation time
50-
--> $DIR/avoid-ice-on-warning-2.rs:4:10
50+
--> $DIR/avoid-ice-on-warning-2.rs:4:13
5151
|
5252
LL | fn id<F>(f: Copy) -> usize {
53-
| ^ doesn't have a size known at compile-time
53+
| ^^^^ doesn't have a size known at compile-time
5454
|
5555
= help: the trait `Sized` is not implemented for `(dyn Copy + 'static)`
5656
= help: unsized fn params are gated as an unstable feature

tests/ui/resolve/issue-3907-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ LL | fn bar();
1111
| ^^^ the trait cannot be made into an object because associated function `bar` has no `self` parameter
1212

1313
error[E0277]: the size for values of type `(dyn issue_3907::Foo + 'static)` cannot be known at compilation time
14-
--> $DIR/issue-3907-2.rs:11:8
14+
--> $DIR/issue-3907-2.rs:11:12
1515
|
1616
LL | fn bar(_x: Foo) {}
17-
| ^^ doesn't have a size known at compile-time
17+
| ^^^ doesn't have a size known at compile-time
1818
|
1919
= help: the trait `Sized` is not implemented for `(dyn issue_3907::Foo + 'static)`
2020
= help: unsized fn params are gated as an unstable feature

tests/ui/resolve/issue-5035-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `(dyn I + 'static)` cannot be known at compilation time
2-
--> $DIR/issue-5035-2.rs:4:8
2+
--> $DIR/issue-5035-2.rs:4:12
33
|
44
LL | fn foo(_x: K) {}
5-
| ^^ doesn't have a size known at compile-time
5+
| ^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn I + 'static)`
88
= help: unsized fn params are gated as an unstable feature

tests/ui/suggestions/object-unsafe-trait-references-self.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ LL | trait Other: Sized {}
3232
| this trait cannot be made into an object...
3333

3434
error[E0277]: the size for values of type `Self` cannot be known at compilation time
35-
--> $DIR/object-unsafe-trait-references-self.rs:2:19
35+
--> $DIR/object-unsafe-trait-references-self.rs:2:22
3636
|
3737
LL | fn baz(&self, _: Self) {}
38-
| ^ doesn't have a size known at compile-time
38+
| ^^^^ doesn't have a size known at compile-time
3939
|
4040
= help: unsized fn params are gated as an unstable feature
4141
help: consider further restricting `Self`

tests/ui/suggestions/unsized-function-parameter.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `str` cannot be known at compilation time
2-
--> $DIR/unsized-function-parameter.rs:5:9
2+
--> $DIR/unsized-function-parameter.rs:5:14
33
|
44
LL | fn foo1(bar: str) {}
5-
| ^^^ doesn't have a size known at compile-time
5+
| ^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `str`
88
= help: unsized fn params are gated as an unstable feature
@@ -12,10 +12,10 @@ LL | fn foo1(bar: &str) {}
1212
| +
1313

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

2727
error[E0277]: the size for values of type `str` cannot be known at compilation time
28-
--> $DIR/unsized-function-parameter.rs:17:9
28+
--> $DIR/unsized-function-parameter.rs:17:12
2929
|
3030
LL | fn foo3(_: str) {}
31-
| ^ doesn't have a size known at compile-time
31+
| ^^^ doesn't have a size known at compile-time
3232
|
3333
= help: the trait `Sized` is not implemented for `str`
3434
= help: unsized fn params are gated as an unstable feature

tests/ui/trait-bounds/apit-unsized.stderr

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
error[E0277]: the size for values of type `impl Iterator<Item = i32> + ?Sized` cannot be known at compilation time
2-
--> $DIR/apit-unsized.rs:1:8
2+
--> $DIR/apit-unsized.rs:1:11
33
|
44
LL | fn foo(_: impl Iterator<Item = i32> + ?Sized) {}
5-
| ^ ---------------------------------- this type parameter needs to be `Sized`
6-
| |
7-
| doesn't have a size known at compile-time
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| doesn't have a size known at compile-time
8+
| this type parameter needs to be `Sized`
89
|
910
= help: unsized fn params are gated as an unstable feature
1011
help: consider removing the `?Sized` bound to make the type parameter `Sized`
@@ -18,12 +19,13 @@ LL | fn foo(_: &impl Iterator<Item = i32> + ?Sized) {}
1819
| +
1920

2021
error[E0277]: the size for values of type `impl ?Sized` cannot be known at compilation time
21-
--> $DIR/apit-unsized.rs:2:8
22+
--> $DIR/apit-unsized.rs:2:11
2223
|
2324
LL | fn bar(_: impl ?Sized) {}
24-
| ^ ----------- this type parameter needs to be `Sized`
25-
| |
26-
| doesn't have a size known at compile-time
25+
| ^^^^^^^^^^^
26+
| |
27+
| doesn't have a size known at compile-time
28+
| this type parameter needs to be `Sized`
2729
|
2830
= help: unsized fn params are gated as an unstable feature
2931
help: consider replacing `?Sized` with `Sized`

tests/ui/traits/alias/dont-elaborate-non-self.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `(dyn Fn() -> Fut + 'static)` cannot be known at compilation time
2-
--> $DIR/dont-elaborate-non-self.rs:7:11
2+
--> $DIR/dont-elaborate-non-self.rs:7:14
33
|
44
LL | fn f<Fut>(a: dyn F<Fut>) {}
5-
| ^ doesn't have a size known at compile-time
5+
| ^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Fn() -> Fut + 'static)`
88
= help: unsized fn params are gated as an unstable feature

tests/ui/traits/bound/not-on-bare-trait-2021.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `(dyn Foo + Send + 'static)` cannot be known at compilation time
2-
--> $DIR/not-on-bare-trait-2021.rs:8:8
2+
--> $DIR/not-on-bare-trait-2021.rs:8:12
33
|
44
LL | fn foo(_x: Foo + Send) {
5-
| ^^ doesn't have a size known at compile-time
5+
| ^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Foo + Send + 'static)`
88
= help: unsized fn params are gated as an unstable feature
@@ -16,10 +16,10 @@ LL | fn foo(_x: &(dyn Foo + Send)) {
1616
| +++++ +
1717

1818
error[E0277]: the size for values of type `(dyn Foo + 'static)` cannot be known at compilation time
19-
--> $DIR/not-on-bare-trait-2021.rs:12:8
19+
--> $DIR/not-on-bare-trait-2021.rs:12:11
2020
|
2121
LL | fn bar(x: Foo) -> Foo {
22-
| ^ doesn't have a size known at compile-time
22+
| ^^^ doesn't have a size known at compile-time
2323
|
2424
= help: the trait `Sized` is not implemented for `(dyn Foo + 'static)`
2525
= help: unsized fn params are gated as an unstable feature

tests/ui/traits/bound/not-on-bare-trait.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ LL | fn foo(_x: dyn Foo + Send) {
1313
| +++
1414

1515
error[E0277]: the size for values of type `(dyn Foo + Send + 'static)` cannot be known at compilation time
16-
--> $DIR/not-on-bare-trait.rs:7:8
16+
--> $DIR/not-on-bare-trait.rs:7:12
1717
|
1818
LL | fn foo(_x: Foo + Send) {
19-
| ^^ doesn't have a size known at compile-time
19+
| ^^^^^^^^^^ doesn't have a size known at compile-time
2020
|
2121
= help: the trait `Sized` is not implemented for `(dyn Foo + Send + 'static)`
2222
= help: unsized fn params are gated as an unstable feature
@@ -30,10 +30,10 @@ LL | fn foo(_x: &(dyn Foo + Send)) {
3030
| +++++ +
3131

3232
error[E0277]: the size for values of type `(dyn Foo + Send + 'static)` cannot be known at compilation time
33-
--> $DIR/not-on-bare-trait.rs:12:8
33+
--> $DIR/not-on-bare-trait.rs:12:13
3434
|
3535
LL | fn bar(_x: (dyn Foo + Send)) {
36-
| ^^ doesn't have a size known at compile-time
36+
| ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
3737
|
3838
= help: the trait `Sized` is not implemented for `(dyn Foo + Send + 'static)`
3939
= help: unsized fn params are gated as an unstable feature

tests/ui/unsized/unsized-fn-arg.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the size for values of type `T` cannot be known at compilation time
2-
--> $DIR/unsized-fn-arg.rs:5:17
2+
--> $DIR/unsized-fn-arg.rs:5:20
33
|
44
LL | fn f<T: ?Sized>(t: T) {}
5-
| - ^ doesn't have a size known at compile-time
5+
| - ^ doesn't have a size known at compile-time
66
| |
77
| this type parameter needs to be `Sized`
88
|

0 commit comments

Comments
 (0)