Skip to content

Commit 22b3e1f

Browse files
committed
Move label for Sized obligation introduced by type param or assoc type to its own note
1 parent 61c2b7a commit 22b3e1f

30 files changed

+218
-185
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
202202
&obligation.cause.code,
203203
&mut vec![],
204204
&mut Default::default(),
205+
None,
205206
);
206207

207208
err.emit();
@@ -232,6 +233,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
232233
) {
233234
let tcx = self.tcx;
234235
let span = obligation.cause.span;
236+
let mut obligation_note = None;
235237

236238
let mut err = match *error {
237239
SelectionError::Unimplemented => {
@@ -321,7 +323,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
321323
ident,
322324
..
323325
})) => {
324-
err.note(
326+
obligation_note = Some(
325327
"associated types introduce an implicit `Sized` \
326328
obligation",
327329
);
@@ -349,7 +351,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
349351
kind: hir::ImplItemKind::TyAlias(_),
350352
..
351353
})) => {
352-
err.note(
354+
obligation_note = Some(
353355
"associated types on `impl` blocks for types, have an \
354356
implicit mandatory `Sized` obligation; associated \
355357
types from `trait`s can be relaxed to `?Sized`",
@@ -358,7 +360,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
358360
_ => {
359361
// This is (likely?) a type parameter. The suggestion is handled
360362
// in `rustc_middle/src/ty/diagnostics.rs`.
361-
err.note(
363+
obligation_note = Some(
362364
"type parameters introduce an implicit `Sized` \
363365
obligation",
364366
);
@@ -423,7 +425,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
423425
points_at_arg,
424426
have_alt_message,
425427
) {
426-
self.note_obligation_cause(&mut err, obligation);
428+
self.note_obligation_cause(&mut err, obligation, obligation_note);
427429
err.emit();
428430
return;
429431
}
@@ -852,7 +854,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
852854
}
853855
};
854856

855-
self.note_obligation_cause(&mut err, obligation);
857+
self.note_obligation_cause(&mut err, obligation, obligation_note);
856858
self.point_at_returns_when_relevant(&mut err, &obligation);
857859

858860
err.emit();
@@ -1135,6 +1137,7 @@ trait InferCtxtPrivExt<'tcx> {
11351137
&self,
11361138
err: &mut DiagnosticBuilder<'tcx>,
11371139
obligation: &PredicateObligation<'tcx>,
1140+
note: Option<&str>,
11381141
);
11391142

11401143
fn suggest_unsized_bound_if_applicable(
@@ -1355,7 +1358,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
13551358
_ => None,
13561359
};
13571360
self.note_type_err(&mut diag, &obligation.cause, secondary_span, values, err, true);
1358-
self.note_obligation_cause(&mut diag, obligation);
1361+
self.note_obligation_cause(&mut diag, obligation, None);
13591362
diag.emit();
13601363
}
13611364
});
@@ -1764,7 +1767,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
17641767
err
17651768
}
17661769
};
1767-
self.note_obligation_cause(&mut err, obligation);
1770+
self.note_obligation_cause(&mut err, obligation, None);
17681771
err.emit();
17691772
}
17701773

@@ -1828,6 +1831,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
18281831
&self,
18291832
err: &mut DiagnosticBuilder<'tcx>,
18301833
obligation: &PredicateObligation<'tcx>,
1834+
note: Option<&str>,
18311835
) {
18321836
// First, attempt to add note to this error with an async-await-specific
18331837
// message, and fall back to regular note otherwise.
@@ -1838,6 +1842,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
18381842
&obligation.cause.code,
18391843
&mut vec![],
18401844
&mut Default::default(),
1845+
note,
18411846
);
18421847
self.suggest_unsized_bound_if_applicable(err, obligation);
18431848
}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ pub trait InferCtxtExt<'tcx> {
162162
cause_code: &ObligationCauseCode<'tcx>,
163163
obligated_types: &mut Vec<&ty::TyS<'tcx>>,
164164
seen_requirements: &mut FxHashSet<DefId>,
165+
note: Option<&str>,
165166
) where
166167
T: fmt::Display;
167168

@@ -1872,6 +1873,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18721873
next_code.unwrap(),
18731874
&mut Vec::new(),
18741875
&mut Default::default(),
1876+
None,
18751877
);
18761878
}
18771879

@@ -1882,6 +1884,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
18821884
cause_code: &ObligationCauseCode<'tcx>,
18831885
obligated_types: &mut Vec<&ty::TyS<'tcx>>,
18841886
seen_requirements: &mut FxHashSet<DefId>,
1887+
note: Option<&str>,
18851888
) where
18861889
T: fmt::Display,
18871890
{
@@ -1946,7 +1949,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19461949
}
19471950
}
19481951
if span != DUMMY_SP {
1949-
err.span_label(span, &msg);
1952+
if let Some(note) = note {
1953+
let mut sp: MultiSpan = span.into();
1954+
sp.push_span_label(span, msg);
1955+
err.span_note(sp, note);
1956+
} else {
1957+
err.span_label(span, &msg);
1958+
}
19501959
} else {
19511960
err.note(&msg);
19521961
}
@@ -2148,6 +2157,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
21482157
&data.parent_code,
21492158
obligated_types,
21502159
seen_requirements,
2160+
note,
21512161
)
21522162
});
21532163
}
@@ -2218,6 +2228,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
22182228
&data.parent_code,
22192229
obligated_types,
22202230
seen_requirements,
2231+
note,
22212232
)
22222233
});
22232234
}
@@ -2232,6 +2243,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
22322243
&data.parent_code,
22332244
obligated_types,
22342245
seen_requirements,
2246+
note,
22352247
)
22362248
});
22372249
}

src/test/ui/associated-types/defaults-wf.stderr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ error[E0277]: the size for values of type `[u8]` cannot be known at compilation
33
|
44
LL | type Ty = Vec<[u8]>;
55
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6-
|
7-
::: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
86
|
9-
LL | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
10-
| - required by this bound in `Vec`
11-
|
12-
= note: type parameters introduce an implicit `Sized` obligation
137
= help: the trait `Sized` is not implemented for `[u8]`
8+
note: type parameters introduce an implicit `Sized` obligation
9+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
10+
|
11+
LL | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
12+
| ^ required by this bound in `Vec`
1413

1514
error: aborting due to previous error
1615

src/test/ui/associated-types/trait-with-supertraits-needing-sized-self.stderr

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ error[E0277]: the size for values of type `Self` cannot be known at compilation
33
|
44
LL | trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> {}
55
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6-
|
7-
::: $SRC_DIR/core/src/ops/arith.rs:LL:COL
86
|
9-
LL | pub trait Add<Rhs = Self> {
10-
| --- required by this bound in `Add`
7+
note: type parameters introduce an implicit `Sized` obligation
8+
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
119
|
12-
= note: type parameters introduce an implicit `Sized` obligation
10+
LL | pub trait Add<Rhs = Self> {
11+
| ^^^ required by this bound in `Add`
1312
help: consider further restricting `Self`
1413
|
1514
LL | trait ArithmeticOps: Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + Div<Output=Self> + Sized {}

src/test/ui/dst/dst-sized-trait-param.stderr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
error[E0277]: the size for values of type `[isize]` cannot be known at compilation time
22
--> $DIR/dst-sized-trait-param.rs:7:6
33
|
4-
LL | trait Foo<T> : Sized { fn take(self, x: &T) { } } // Note: T is sized
5-
| - required by this bound in `Foo`
6-
LL |
74
LL | impl Foo<[isize]> for usize { }
85
| ^^^^^^^^^^^^ doesn't have a size known at compile-time
96
|
10-
= note: type parameters introduce an implicit `Sized` obligation
117
= help: the trait `Sized` is not implemented for `[isize]`
8+
note: type parameters introduce an implicit `Sized` obligation
9+
--> $DIR/dst-sized-trait-param.rs:5:11
10+
|
11+
LL | trait Foo<T> : Sized { fn take(self, x: &T) { } } // Note: T is sized
12+
| ^ required by this bound in `Foo`
1213
help: consider relaxing the implicit `Sized` restriction
1314
|
1415
LL | trait Foo<T: ?Sized> : Sized { fn take(self, x: &T) { } } // Note: T is sized

src/test/ui/extern/extern-types-unsized.stderr

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
error[E0277]: the size for values of type `A` cannot be known at compilation time
22
--> $DIR/extern-types-unsized.rs:22:20
33
|
4-
LL | fn assert_sized<T>() {}
5-
| - required by this bound in `assert_sized`
6-
...
74
LL | assert_sized::<A>();
85
| ^ doesn't have a size known at compile-time
96
|
10-
= note: type parameters introduce an implicit `Sized` obligation
117
= help: the trait `Sized` is not implemented for `A`
8+
note: type parameters introduce an implicit `Sized` obligation
9+
--> $DIR/extern-types-unsized.rs:19:17
10+
|
11+
LL | fn assert_sized<T>() {}
12+
| ^ required by this bound in `assert_sized`
1213
help: consider relaxing the implicit `Sized` restriction
1314
|
1415
LL | fn assert_sized<T: ?Sized>() {}
@@ -17,19 +18,20 @@ LL | fn assert_sized<T: ?Sized>() {}
1718
error[E0277]: the size for values of type `A` cannot be known at compilation time
1819
--> $DIR/extern-types-unsized.rs:25:5
1920
|
20-
LL | fn assert_sized<T>() {}
21-
| - required by this bound in `assert_sized`
22-
...
2321
LL | assert_sized::<Foo>();
2422
| ^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
2523
|
26-
= note: type parameters introduce an implicit `Sized` obligation
2724
= help: within `Foo`, the trait `Sized` is not implemented for `A`
2825
note: required because it appears within the type `Foo`
2926
--> $DIR/extern-types-unsized.rs:9:8
3027
|
3128
LL | struct Foo {
3229
| ^^^
30+
note: type parameters introduce an implicit `Sized` obligation
31+
--> $DIR/extern-types-unsized.rs:19:17
32+
|
33+
LL | fn assert_sized<T>() {}
34+
| ^ required by this bound in `assert_sized`
3335
help: consider relaxing the implicit `Sized` restriction
3436
|
3537
LL | fn assert_sized<T: ?Sized>() {}
@@ -38,19 +40,20 @@ LL | fn assert_sized<T: ?Sized>() {}
3840
error[E0277]: the size for values of type `A` cannot be known at compilation time
3941
--> $DIR/extern-types-unsized.rs:28:5
4042
|
41-
LL | fn assert_sized<T>() {}
42-
| - required by this bound in `assert_sized`
43-
...
4443
LL | assert_sized::<Bar<A>>();
4544
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
4645
|
47-
= note: type parameters introduce an implicit `Sized` obligation
4846
= help: within `Bar<A>`, the trait `Sized` is not implemented for `A`
4947
note: required because it appears within the type `Bar<A>`
5048
--> $DIR/extern-types-unsized.rs:14:8
5149
|
5250
LL | struct Bar<T: ?Sized> {
5351
| ^^^
52+
note: type parameters introduce an implicit `Sized` obligation
53+
--> $DIR/extern-types-unsized.rs:19:17
54+
|
55+
LL | fn assert_sized<T>() {}
56+
| ^ required by this bound in `assert_sized`
5457
help: consider relaxing the implicit `Sized` restriction
5558
|
5659
LL | fn assert_sized<T: ?Sized>() {}
@@ -59,13 +62,9 @@ LL | fn assert_sized<T: ?Sized>() {}
5962
error[E0277]: the size for values of type `A` cannot be known at compilation time
6063
--> $DIR/extern-types-unsized.rs:31:5
6164
|
62-
LL | fn assert_sized<T>() {}
63-
| - required by this bound in `assert_sized`
64-
...
6565
LL | assert_sized::<Bar<Bar<A>>>();
6666
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6767
|
68-
= note: type parameters introduce an implicit `Sized` obligation
6968
= help: within `Bar<Bar<A>>`, the trait `Sized` is not implemented for `A`
7069
note: required because it appears within the type `Bar<A>`
7170
--> $DIR/extern-types-unsized.rs:14:8
@@ -77,6 +76,11 @@ note: required because it appears within the type `Bar<Bar<A>>`
7776
|
7877
LL | struct Bar<T: ?Sized> {
7978
| ^^^
79+
note: type parameters introduce an implicit `Sized` obligation
80+
--> $DIR/extern-types-unsized.rs:19:17
81+
|
82+
LL | fn assert_sized<T>() {}
83+
| ^ required by this bound in `assert_sized`
8084
help: consider relaxing the implicit `Sized` restriction
8185
|
8286
LL | fn assert_sized<T: ?Sized>() {}

src/test/ui/generics/issue-61631-default-type-param-can-reference-self-in-trait.stderr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
error[E0277]: the size for values of type `[()]` cannot be known at compilation time
22
--> $DIR/issue-61631-default-type-param-can-reference-self-in-trait.rs:19:6
33
|
4-
LL | trait Tsized<P: Sized = [Self]> {}
5-
| - required by this bound in `Tsized`
6-
LL |
74
LL | impl Tsized for () {}
85
| ^^^^^^ doesn't have a size known at compile-time
96
|
10-
= note: type parameters introduce an implicit `Sized` obligation
117
= help: the trait `Sized` is not implemented for `[()]`
8+
note: type parameters introduce an implicit `Sized` obligation
9+
--> $DIR/issue-61631-default-type-param-can-reference-self-in-trait.rs:17:14
10+
|
11+
LL | trait Tsized<P: Sized = [Self]> {}
12+
| ^ required by this bound in `Tsized`
1213

1314
error: aborting due to previous error
1415

src/test/ui/issues/issue-10412.stderr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ LL | impl<'self> Serializable<str> for &'self str {
4949
error[E0277]: the size for values of type `str` cannot be known at compilation time
5050
--> $DIR/issue-10412.rs:6:13
5151
|
52-
LL | trait Serializable<'self, T> {
53-
| - required by this bound in `Serializable`
54-
...
5552
LL | impl<'self> Serializable<str> for &'self str {
5653
| ^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
5754
|
58-
= note: type parameters introduce an implicit `Sized` obligation
5955
= help: the trait `Sized` is not implemented for `str`
56+
note: type parameters introduce an implicit `Sized` obligation
57+
--> $DIR/issue-10412.rs:1:27
58+
|
59+
LL | trait Serializable<'self, T> {
60+
| ^ required by this bound in `Serializable`
6061
help: consider relaxing the implicit `Sized` restriction
6162
|
6263
LL | trait Serializable<'self, T: ?Sized> {

src/test/ui/issues/issue-18919.stderr

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ error[E0277]: the size for values of type `dyn for<'r> Fn(&'r isize) -> isize` c
33
|
44
LL | fn ho_func(f: Option<FuncType>) {
55
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6-
...
7-
LL | enum Option<T> {
8-
| - required by this bound in `Option`
96
|
10-
= note: type parameters introduce an implicit `Sized` obligation
117
= help: the trait `Sized` is not implemented for `dyn for<'r> Fn(&'r isize) -> isize`
8+
note: type parameters introduce an implicit `Sized` obligation
9+
--> $DIR/issue-18919.rs:7:13
10+
|
11+
LL | enum Option<T> {
12+
| ^ required by this bound in `Option`
1213
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
1314
--> $DIR/issue-18919.rs:7:13
1415
|

src/test/ui/issues/issue-20433.stderr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ error[E0277]: the size for values of type `[i32]` cannot be known at compilation
33
|
44
LL | fn iceman(c: Vec<[i32]>) {}
55
| ^^^^^^^^^^ doesn't have a size known at compile-time
6-
|
7-
::: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
86
|
9-
LL | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
10-
| - required by this bound in `Vec`
11-
|
12-
= note: type parameters introduce an implicit `Sized` obligation
137
= help: the trait `Sized` is not implemented for `[i32]`
8+
note: type parameters introduce an implicit `Sized` obligation
9+
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
10+
|
11+
LL | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
12+
| ^ required by this bound in `Vec`
1413

1514
error: aborting due to previous error
1615

0 commit comments

Comments
 (0)