Skip to content

Commit c572f14

Browse files
authored
Rollup merge of rust-lang#82626 - lcnr:encode_with_shorthandb, r=estebank
update array missing `IntoIterator` msg fixes rust-lang#82602 r? ``@estebank`` do you know whether we can use the expr span in `rustc_on_unimplemented`? The label isn't too great rn
2 parents caa1c0e + 5ac917d commit c572f14

File tree

9 files changed

+81
-72
lines changed

9 files changed

+81
-72
lines changed

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

+51-47
Original file line numberDiff line numberDiff line change
@@ -163,61 +163,65 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
163163
flags.push((sym::from_desugaring, None));
164164
flags.push((sym::from_desugaring, Some(format!("{:?}", k))));
165165
}
166-
let generics = self.tcx.generics_of(def_id);
167-
let self_ty = trait_ref.self_ty();
168-
// This is also included through the generics list as `Self`,
169-
// but the parser won't allow you to use it
170-
flags.push((sym::_Self, Some(self_ty.to_string())));
171-
if let Some(def) = self_ty.ty_adt_def() {
172-
// We also want to be able to select self's original
173-
// signature with no type arguments resolved
174-
flags.push((sym::_Self, Some(self.tcx.type_of(def.did).to_string())));
175-
}
176166

177-
for param in generics.params.iter() {
178-
let value = match param.kind {
179-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
180-
trait_ref.substs[param.index as usize].to_string()
181-
}
182-
GenericParamDefKind::Lifetime => continue,
183-
};
184-
let name = param.name;
185-
flags.push((name, Some(value)));
186-
}
167+
// Add all types without trimmed paths.
168+
ty::print::with_no_trimmed_paths(|| {
169+
let generics = self.tcx.generics_of(def_id);
170+
let self_ty = trait_ref.self_ty();
171+
// This is also included through the generics list as `Self`,
172+
// but the parser won't allow you to use it
173+
flags.push((sym::_Self, Some(self_ty.to_string())));
174+
if let Some(def) = self_ty.ty_adt_def() {
175+
// We also want to be able to select self's original
176+
// signature with no type arguments resolved
177+
flags.push((sym::_Self, Some(self.tcx.type_of(def.did).to_string())));
178+
}
187179

188-
if let Some(true) = self_ty.ty_adt_def().map(|def| def.did.is_local()) {
189-
flags.push((sym::crate_local, None));
190-
}
180+
for param in generics.params.iter() {
181+
let value = match param.kind {
182+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
183+
trait_ref.substs[param.index as usize].to_string()
184+
}
185+
GenericParamDefKind::Lifetime => continue,
186+
};
187+
let name = param.name;
188+
flags.push((name, Some(value)));
189+
}
191190

192-
// Allow targeting all integers using `{integral}`, even if the exact type was resolved
193-
if self_ty.is_integral() {
194-
flags.push((sym::_Self, Some("{integral}".to_owned())));
195-
}
191+
if let Some(true) = self_ty.ty_adt_def().map(|def| def.did.is_local()) {
192+
flags.push((sym::crate_local, None));
193+
}
196194

197-
if let ty::Array(aty, len) = self_ty.kind() {
198-
flags.push((sym::_Self, Some("[]".to_owned())));
199-
flags.push((sym::_Self, Some(format!("[{}]", aty))));
200-
if let Some(def) = aty.ty_adt_def() {
201-
// We also want to be able to select the array's type's original
202-
// signature with no type arguments resolved
203-
let type_string = self.tcx.type_of(def.did).to_string();
204-
flags.push((sym::_Self, Some(format!("[{}]", type_string))));
195+
// Allow targeting all integers using `{integral}`, even if the exact type was resolved
196+
if self_ty.is_integral() {
197+
flags.push((sym::_Self, Some("{integral}".to_owned())));
198+
}
205199

206-
let len = len.val.try_to_value().and_then(|v| v.try_to_machine_usize(self.tcx));
207-
let string = match len {
208-
Some(n) => format!("[{}; {}]", type_string, n),
209-
None => format!("[{}; _]", type_string),
210-
};
211-
flags.push((sym::_Self, Some(string)));
200+
if let ty::Array(aty, len) = self_ty.kind() {
201+
flags.push((sym::_Self, Some("[]".to_owned())));
202+
flags.push((sym::_Self, Some(format!("[{}]", aty))));
203+
if let Some(def) = aty.ty_adt_def() {
204+
// We also want to be able to select the array's type's original
205+
// signature with no type arguments resolved
206+
let type_string = self.tcx.type_of(def.did).to_string();
207+
flags.push((sym::_Self, Some(format!("[{}]", type_string))));
208+
209+
let len = len.val.try_to_value().and_then(|v| v.try_to_machine_usize(self.tcx));
210+
let string = match len {
211+
Some(n) => format!("[{}; {}]", type_string, n),
212+
None => format!("[{}; _]", type_string),
213+
};
214+
flags.push((sym::_Self, Some(string)));
215+
}
212216
}
213-
}
214-
if let ty::Dynamic(traits, _) = self_ty.kind() {
215-
for t in traits.iter() {
216-
if let ty::ExistentialPredicate::Trait(trait_ref) = t.skip_binder() {
217-
flags.push((sym::_Self, Some(self.tcx.def_path_str(trait_ref.def_id))))
217+
if let ty::Dynamic(traits, _) = self_ty.kind() {
218+
for t in traits.iter() {
219+
if let ty::ExistentialPredicate::Trait(trait_ref) = t.skip_binder() {
220+
flags.push((sym::_Self, Some(self.tcx.def_path_str(trait_ref.def_id))))
221+
}
218222
}
219223
}
220-
}
224+
});
221225

222226
if let Ok(Some(command)) =
223227
OnUnimplementedDirective::of_item(self.tcx, trait_ref.def_id, def_id)

library/core/src/iter/traits/iterator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ fn _assert_is_object_safe(_: &dyn Iterator<Item = ()>) {}
8181
),
8282
on(
8383
_Self = "[]",
84-
label = "borrow the array with `&` or call `.iter()` on it to iterate over it",
85-
note = "arrays are not iterators, but slices like the following are: `&[1, 2, 3]`"
84+
label = "arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)`",
85+
note = "see <https://github.com/rust-lang/rust/pull/65819> for more details"
8686
),
8787
on(
8888
_Self = "{integral}",

src/test/ui/iterators/array-of-ranges.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,43 @@ error[E0277]: `[RangeInclusive<{integer}>; 1]` is not an iterator
1313
--> $DIR/array-of-ranges.rs:4:14
1414
|
1515
LL | for _ in [0..=1] {}
16-
| ^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
16+
| ^^^^^^^ if you meant to iterate between two values, remove the square brackets
1717
|
1818
= help: the trait `Iterator` is not implemented for `[RangeInclusive<{integer}>; 1]`
19-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
19+
= note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end`
2020
= note: required because of the requirements on the impl of `IntoIterator` for `[RangeInclusive<{integer}>; 1]`
2121
= note: required by `into_iter`
2222

2323
error[E0277]: `[RangeFrom<{integer}>; 1]` is not an iterator
2424
--> $DIR/array-of-ranges.rs:6:14
2525
|
2626
LL | for _ in [0..] {}
27-
| ^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
27+
| ^^^^^ if you meant to iterate from a value onwards, remove the square brackets
2828
|
2929
= help: the trait `Iterator` is not implemented for `[RangeFrom<{integer}>; 1]`
30-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
30+
= note: `[start..]` is an array of one `RangeFrom`; you might have meant to have a `RangeFrom` without the brackets: `start..`, keeping in mind that iterating over an unbounded iterator will run forever unless you `break` or `return` from within the loop
3131
= note: required because of the requirements on the impl of `IntoIterator` for `[RangeFrom<{integer}>; 1]`
3232
= note: required by `into_iter`
3333

3434
error[E0277]: `[RangeTo<{integer}>; 1]` is not an iterator
3535
--> $DIR/array-of-ranges.rs:8:14
3636
|
3737
LL | for _ in [..1] {}
38-
| ^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
38+
| ^^^^^ if you meant to iterate until a value, remove the square brackets and add a starting value
3939
|
4040
= help: the trait `Iterator` is not implemented for `[RangeTo<{integer}>; 1]`
41-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
41+
= note: `[..end]` is an array of one `RangeTo`; you might have meant to have a bounded `Range` without the brackets: `0..end`
4242
= note: required because of the requirements on the impl of `IntoIterator` for `[RangeTo<{integer}>; 1]`
4343
= note: required by `into_iter`
4444

4545
error[E0277]: `[RangeToInclusive<{integer}>; 1]` is not an iterator
4646
--> $DIR/array-of-ranges.rs:10:14
4747
|
4848
LL | for _ in [..=1] {}
49-
| ^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
49+
| ^^^^^^ if you meant to iterate until a value (including it), remove the square brackets and add a starting value
5050
|
5151
= help: the trait `Iterator` is not implemented for `[RangeToInclusive<{integer}>; 1]`
52-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
52+
= note: `[..=end]` is an array of one `RangeToInclusive`; you might have meant to have a bounded `RangeInclusive` without the brackets: `0..=end`
5353
= note: required because of the requirements on the impl of `IntoIterator` for `[RangeToInclusive<{integer}>; 1]`
5454
= note: required by `into_iter`
5555

@@ -79,21 +79,21 @@ error[E0277]: `[std::ops::Range<{integer}>; 2]` is not an iterator
7979
--> $DIR/array-of-ranges.rs:19:14
8080
|
8181
LL | for _ in [0..1, 2..3] {}
82-
| ^^^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
82+
| ^^^^^^^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)`
8383
|
8484
= help: the trait `Iterator` is not implemented for `[std::ops::Range<{integer}>; 2]`
85-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
85+
= note: see <https://github.com/rust-lang/rust/pull/65819> for more details
8686
= note: required because of the requirements on the impl of `IntoIterator` for `[std::ops::Range<{integer}>; 2]`
8787
= note: required by `into_iter`
8888

8989
error[E0277]: `[RangeInclusive<{integer}>; 1]` is not an iterator
9090
--> $DIR/array-of-ranges.rs:21:14
9191
|
9292
LL | for _ in [0..=1] {}
93-
| ^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
93+
| ^^^^^^^ if you meant to iterate between two values, remove the square brackets
9494
|
9595
= help: the trait `Iterator` is not implemented for `[RangeInclusive<{integer}>; 1]`
96-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
96+
= note: `[start..=end]` is an array of one `RangeInclusive`; you might have meant to have a `RangeInclusive` without the brackets: `start..=end`
9797
= note: required because of the requirements on the impl of `IntoIterator` for `[RangeInclusive<{integer}>; 1]`
9898
= note: required by `into_iter`
9999

src/test/ui/iterators/array.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@ error[E0277]: `[{integer}; 2]` is not an iterator
22
--> $DIR/array.rs:2:14
33
|
44
LL | for _ in [1, 2] {}
5-
| ^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
5+
| ^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)`
66
|
77
= help: the trait `Iterator` is not implemented for `[{integer}; 2]`
8-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
8+
= note: see <https://github.com/rust-lang/rust/pull/65819> for more details
99
= note: required because of the requirements on the impl of `IntoIterator` for `[{integer}; 2]`
1010
= note: required by `into_iter`
1111

1212
error[E0277]: `[{integer}; 2]` is not an iterator
1313
--> $DIR/array.rs:5:14
1414
|
1515
LL | for _ in x {}
16-
| ^ borrow the array with `&` or call `.iter()` on it to iterate over it
16+
| ^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)`
1717
|
1818
= help: the trait `Iterator` is not implemented for `[{integer}; 2]`
19-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
19+
= note: see <https://github.com/rust-lang/rust/pull/65819> for more details
2020
= note: required because of the requirements on the impl of `IntoIterator` for `[{integer}; 2]`
2121
= note: required by `into_iter`
2222

2323
error[E0277]: `[{float}; 2]` is not an iterator
2424
--> $DIR/array.rs:7:14
2525
|
2626
LL | for _ in [1.0, 2.0] {}
27-
| ^^^^^^^^^^ borrow the array with `&` or call `.iter()` on it to iterate over it
27+
| ^^^^^^^^^^ arrays do not yet implement `IntoIterator`; try using `std::array::IntoIter::new(arr)`
2828
|
2929
= help: the trait `Iterator` is not implemented for `[{float}; 2]`
30-
= note: arrays are not iterators, but slices like the following are: `&[1, 2, 3]`
30+
= note: see <https://github.com/rust-lang/rust/pull/65819> for more details
3131
= note: required because of the requirements on the impl of `IntoIterator` for `[{float}; 2]`
3232
= note: required by `into_iter`
3333

src/test/ui/iterators/ranges.stderr

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ error[E0277]: `RangeTo<{integer}>` is not an iterator
22
--> $DIR/ranges.rs:2:14
33
|
44
LL | for _ in ..10 {}
5-
| ^^^^ `RangeTo<{integer}>` is not an iterator
5+
| ^^^^ if you meant to iterate until a value, add a starting value
66
|
77
= help: the trait `Iterator` is not implemented for `RangeTo<{integer}>`
8+
= note: `..end` is a `RangeTo`, which cannot be iterated on; you might have meant to have a bounded `Range`: `0..end`
89
= note: required because of the requirements on the impl of `IntoIterator` for `RangeTo<{integer}>`
910
= note: required by `into_iter`
1011

1112
error[E0277]: `RangeToInclusive<{integer}>` is not an iterator
1213
--> $DIR/ranges.rs:4:14
1314
|
1415
LL | for _ in ..=10 {}
15-
| ^^^^^ `RangeToInclusive<{integer}>` is not an iterator
16+
| ^^^^^ if you meant to iterate until a value (including it), add a starting value
1617
|
1718
= help: the trait `Iterator` is not implemented for `RangeToInclusive<{integer}>`
19+
= note: `..=end` is a `RangeToInclusive`, which cannot be iterated on; you might have meant to have a bounded `RangeInclusive`: `0..=end`
1820
= note: required because of the requirements on the impl of `IntoIterator` for `RangeToInclusive<{integer}>`
1921
= note: required by `into_iter`
2022

src/test/ui/iterators/string.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0277]: `String` is not an iterator
22
--> $DIR/string.rs:2:14
33
|
44
LL | for _ in "".to_owned() {}
5-
| ^^^^^^^^^^^^^ `String` is not an iterator
5+
| ^^^^^^^^^^^^^ `String` is not an iterator; try calling `.chars()` or `.bytes()`
66
|
77
= help: the trait `Iterator` is not implemented for `String`
88
= note: required because of the requirements on the impl of `IntoIterator` for `String`

src/test/ui/suggestions/expected-boxed-future-isnt-pinned.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ error[E0277]: `dyn Future<Output = i32> + Send` cannot be unpinned
4646
LL | Pin::new(x)
4747
| ^^^^^^^^ the trait `Unpin` is not implemented for `dyn Future<Output = i32> + Send`
4848
|
49+
= note: consider using `Box::pin`
4950
= note: required by `Pin::<P>::new`
5051

5152
error[E0277]: `dyn Future<Output = i32> + Send` cannot be unpinned
@@ -54,6 +55,7 @@ error[E0277]: `dyn Future<Output = i32> + Send` cannot be unpinned
5455
LL | Pin::new(Box::new(x))
5556
| ^^^^^^^^ the trait `Unpin` is not implemented for `dyn Future<Output = i32> + Send`
5657
|
58+
= note: consider using `Box::pin`
5759
= note: required by `Pin::<P>::new`
5860

5961
error[E0308]: mismatched types

src/test/ui/suggestions/into-str.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LL | fn foo<'a, T>(_t: T) where T: Into<&'a str> {}
77
LL | foo(String::new());
88
| ^^^ the trait `From<String>` is not implemented for `&str`
99
|
10+
= note: to coerce a `String` into a `&str`, use `&*` as a prefix
1011
= note: required because of the requirements on the impl of `Into<&str>` for `String`
1112

1213
error: aborting due to previous error

src/test/ui/suggestions/path-display.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ error[E0277]: `Path` doesn't implement `std::fmt::Display`
22
--> $DIR/path-display.rs:5:20
33
|
44
LL | println!("{}", path);
5-
| ^^^^ `Path` cannot be formatted with the default formatter
5+
| ^^^^ `Path` cannot be formatted with the default formatter; call `.display()` on it
66
|
77
= help: the trait `std::fmt::Display` is not implemented for `Path`
8-
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
8+
= note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data
99
= note: required because of the requirements on the impl of `std::fmt::Display` for `&Path`
1010
= note: required by `std::fmt::Display::fmt`
1111
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)