Skip to content

Commit 615542a

Browse files
committed
Point at closure definitions
1 parent 9a7345b commit 615542a

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

src/librustc_typeck/check/method/suggest.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -538,28 +538,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
538538
let def_span =
539539
|def_id| self.tcx.sess.source_map().def_span(self.tcx.def_span(def_id));
540540
let mut bound_spans = vec![];
541-
let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str| {
541+
let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
542+
let msg = format!(
543+
"doesn't satisfy {}",
544+
if obligation.len() > 50 { quiet } else { obligation }
545+
);
542546
match &self_ty.kind {
543-
ty::Adt(def, _) => {
544-
// Point at the type that couldn't satisfy the bound.
545-
bound_spans.push((
546-
def_span(def.did),
547-
format!("doesn't satisfy {}", obligation),
548-
));
549-
}
547+
// Point at the type that couldn't satisfy the bound.
548+
ty::Adt(def, _) => bound_spans.push((def_span(def.did), msg)),
549+
// Point at the trait object that couldn't satisfy the bound.
550550
ty::Dynamic(preds, _) => {
551-
// Point at the trait object that couldn't satisfy the bound.
552551
for pred in *preds.skip_binder() {
553552
match pred {
554-
ty::ExistentialPredicate::Trait(tr) => bound_spans.push((
555-
def_span(tr.def_id),
556-
format!("doesn't satisfy {}", obligation),
557-
)),
553+
ty::ExistentialPredicate::Trait(tr) => {
554+
bound_spans.push((def_span(tr.def_id), msg.clone()))
555+
}
558556
ty::ExistentialPredicate::Projection(_)
559557
| ty::ExistentialPredicate::AutoTrait(_) => {}
560558
}
561559
}
562560
}
561+
// Point at the closure that couldn't satisfy the bound.
562+
ty::Closure(def_id, _) => bound_spans
563+
.push((def_span(*def_id), format!("doesn't satisfy {}", quiet))),
563564
_ => {}
564565
}
565566
};
@@ -573,17 +574,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
573574
.tcx
574575
.associated_item(pred.skip_binder().projection_ty.item_def_id);
575576
let ty = pred.skip_binder().ty;
576-
let obligation =
577-
format!("`{}::{} = {}`", trait_ref, assoc.ident, ty);
578-
bound_span_label(trait_ref.self_ty(), &obligation);
577+
let msg = format!("`{}::{} = {}`", trait_ref, assoc.ident, ty);
578+
let quiet = format!(
579+
"`<_ as {}>::{} = {}`",
580+
trait_ref.print_only_trait_path(),
581+
assoc.ident,
582+
ty
583+
);
584+
bound_span_label(trait_ref.self_ty(), &msg, &quiet);
579585
Some(obligation)
580586
}
581587
ty::Predicate::Trait(poly_trait_ref, _) => {
582588
let p = poly_trait_ref.skip_binder().trait_ref;
583589
let self_ty = p.self_ty();
584590
let path = p.print_only_trait_path();
585591
let obligation = format!("`{}: {}`", self_ty, path);
586-
bound_span_label(self_ty, &obligation);
592+
let quiet = format!("`_: {}`", path);
593+
bound_span_label(self_ty, &obligation, &quiet);
587594
Some(obligation)
588595
}
589596
_ => None,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ LL | .collect();
1616
::: $SRC_DIR/libcore/iter/adapters/mod.rs:LL:COL
1717
|
1818
LL | pub struct Cloned<I> {
19-
| -------------------- doesn't satisfy `std::iter::Cloned<std::iter::TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:10:39: 13:6 found_e:_]>>: std::iter::Iterator`
19+
| -------------------- doesn't satisfy `_: std::iter::Iterator`
2020
...
2121
LL | pub struct TakeWhile<I, P> {
22-
| -------------------------- doesn't satisfy `<std::iter::TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:10:39: 13:6 found_e:_]> as std::iter::Iterator>::Item = &_`
22+
| -------------------------- doesn't satisfy `<_ as std::iter::Iterator>::Item = &_`
2323
|
2424
= note: the method `collect` exists but the following trait bounds were not satisfied:
2525
`<std::iter::TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:10:39: 13:6 found_e:_]> as std::iter::Iterator>::Item = &_` which is required by `std::iter::Cloned<std::iter::TakeWhile<&mut std::vec::IntoIter<u8>, [closure@$DIR/issue-31173.rs:10:39: 13:6 found_e:_]>>: std::iter::Iterator`

src/test/ui/mismatched_types/issue-36053-2.stderr

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ error[E0599]: no method named `count` found for struct `std::iter::Filter<std::i
22
--> $DIR/issue-36053-2.rs:11:55
33
|
44
LL | once::<&str>("str").fuse().filter(|a: &str| true).count();
5-
| ^^^^^ method not found in `std::iter::Filter<std::iter::Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:11:39: 11:53]>`
5+
| -------------- ^^^^^ method not found in `std::iter::Filter<std::iter::Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:11:39: 11:53]>`
6+
| |
7+
| doesn't satisfy `<_ as std::ops::FnOnce<(&&str,)>>::Output = bool`
8+
| doesn't satisfy `_: std::ops::FnMut<(&&str,)>`
69
|
710
::: $SRC_DIR/libcore/iter/adapters/mod.rs:LL:COL
811
|
912
LL | pub struct Filter<I, P> {
10-
| ----------------------- doesn't satisfy `std::iter::Filter<std::iter::Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:11:39: 11:53]>: std::iter::Iterator`
13+
| ----------------------- doesn't satisfy `_: std::iter::Iterator`
1114
|
1215
= note: the method `count` exists but the following trait bounds were not satisfied:
1316
`<[closure@$DIR/issue-36053-2.rs:11:39: 11:53] as std::ops::FnOnce<(&&str,)>>::Output = bool` which is required by `std::iter::Filter<std::iter::Fuse<std::iter::Once<&str>>, [closure@$DIR/issue-36053-2.rs:11:39: 11:53]>: std::iter::Iterator`

src/test/ui/suggestions/mut-borrow-needed-by-trait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ LL | writeln!(fp, "hello world").unwrap();
3434
::: $SRC_DIR/libstd/io/buffered.rs:LL:COL
3535
|
3636
LL | pub struct BufWriter<W: Write> {
37-
| ------------------------------ doesn't satisfy `std::io::BufWriter<&dyn std::io::Write>: std::io::Write`
37+
| ------------------------------ doesn't satisfy `_: std::io::Write`
3838
|
3939
= note: the method `write_fmt` exists but the following trait bounds were not satisfied:
4040
`&dyn std::io::Write: std::io::Write` which is required by `std::io::BufWriter<&dyn std::io::Write>: std::io::Write`

0 commit comments

Comments
 (0)