From 52e2065ed4dba1a68a5f85e8de212e76dde72e7a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 16 Aug 2022 23:54:54 +0000 Subject: [PATCH] Revert closure mismatch spans --- .../rustc_typeck/src/check/fn_ctxt/checks.rs | 9 ++ .../anonymous-higher-ranked-lifetime.stderr | 110 +++++++----------- .../expect-infer-var-appearing-twice.stderr | 15 +-- src/test/ui/mismatched_types/E0631.stderr | 20 ++-- .../mismatched_types/closure-arg-count.stderr | 26 ++--- .../closure-arg-type-mismatch.stderr | 30 ++--- .../ui/mismatched_types/issue-36053-2.stderr | 10 +- .../unboxed-closures-vtable-mismatch.rs | 1 - .../unboxed-closures-vtable-mismatch.stderr | 6 +- 9 files changed, 94 insertions(+), 133 deletions(-) diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs index f2f327e0812..204bb6a532d 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs @@ -1630,6 +1630,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { { return; } + // Skip over closure arg mismatch, which has a better heuristic + // to determine what span to point at. + if let traits::FulfillmentErrorCode::CodeSelectionError( + traits::SelectionError::OutputTypeParameterMismatch(_, expected, _), + ) = error.code + && let ty::Closure(..) | ty::Generator(..) = expected.skip_binder().self_ty().kind() + { + return; + } let Some(unsubstituted_pred) = self.tcx.predicates_of(def_id).instantiate_identity(self.tcx).predicates.into_iter().nth(idx) else { return; }; diff --git a/src/test/ui/anonymous-higher-ranked-lifetime.stderr b/src/test/ui/anonymous-higher-ranked-lifetime.stderr index d6deefe693f..1a0a5fdf4eb 100644 --- a/src/test/ui/anonymous-higher-ranked-lifetime.stderr +++ b/src/test/ui/anonymous-higher-ranked-lifetime.stderr @@ -1,12 +1,10 @@ error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:2:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:2:5 | LL | f1(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'r, 's> fn(&'r (), &'s ()) -> _` found closure signature `fn((), ()) -> _` @@ -17,14 +15,12 @@ LL | fn f1(_: F) where F: Fn(&(), &()) {} | ^^^^^^^^^^^^ required by this bound in `f1` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:3:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:3:5 | LL | f2(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'a, 'r> fn(&'a (), &'r ()) -> _` found closure signature `fn((), ()) -> _` @@ -35,14 +31,12 @@ LL | fn f2(_: F) where F: for<'a> Fn(&'a (), &()) {} | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f2` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:4:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:4:5 | LL | f3(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'r> fn(&(), &'r ()) -> _` found closure signature `fn((), ()) -> _` @@ -53,14 +47,12 @@ LL | fn f3<'a, F>(_: F) where F: Fn(&'a (), &()) {} | ^^^^^^^^^^^^^^^ required by this bound in `f3` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:5:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:5:5 | LL | f4(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'r, 's> fn(&'s (), &'r ()) -> _` found closure signature `fn((), ()) -> _` @@ -71,14 +63,12 @@ LL | fn f4(_: F) where F: for<'r> Fn(&(), &'r ()) {} | ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f4` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:6:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:6:5 | LL | f5(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'r> fn(&'r (), &'r ()) -> _` found closure signature `fn((), ()) -> _` @@ -89,14 +79,12 @@ LL | fn f5(_: F) where F: for<'r> Fn(&'r (), &'r ()) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f5` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:7:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:7:5 | LL | g1(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'r> fn(&'r (), Box<(dyn for<'r> Fn(&'r ()) + 'static)>) -> _` found closure signature `fn((), ()) -> _` @@ -107,14 +95,12 @@ LL | fn g1(_: F) where F: Fn(&(), Box) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g1` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:8:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:8:5 | LL | g2(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'r> fn(&'r (), for<'r> fn(&'r ())) -> _` found closure signature `fn((), ()) -> _` @@ -125,14 +111,12 @@ LL | fn g2(_: F) where F: Fn(&(), fn(&())) {} | ^^^^^^^^^^^^^^^^ required by this bound in `g2` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:9:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:9:5 | LL | g3(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'s> fn(&'s (), Box<(dyn for<'r> Fn(&'r ()) + 'static)>) -> _` found closure signature `fn((), ()) -> _` @@ -143,14 +127,12 @@ LL | fn g3(_: F) where F: for<'s> Fn(&'s (), Box) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g3` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:10:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:10:5 | LL | g4(|_: (), _: ()| {}); - | -- --------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ -------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'s> fn(&'s (), for<'r> fn(&'r ())) -> _` found closure signature `fn((), ()) -> _` @@ -161,14 +143,12 @@ LL | fn g4(_: F) where F: Fn(&(), for<'r> fn(&'r ())) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g4` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:11:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:11:5 | LL | h1(|_: (), _: (), _: (), _: ()| {}); - | -- ----------------------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ ---------------------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'r, 's> fn(&'r (), Box<(dyn for<'r> Fn(&'r ()) + 'static)>, &'s (), for<'r, 's> fn(&'r (), &'s ())) -> _` found closure signature `fn((), (), (), ()) -> _` @@ -179,14 +159,12 @@ LL | fn h1(_: F) where F: Fn(&(), Box, &(), fn(&(), &())) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h1` error[E0631]: type mismatch in closure arguments - --> $DIR/anonymous-higher-ranked-lifetime.rs:12:8 + --> $DIR/anonymous-higher-ranked-lifetime.rs:12:5 | LL | h2(|_: (), _: (), _: (), _: ()| {}); - | -- ----------------------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^ ---------------------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'t0, 'r> fn(&'r (), Box<(dyn for<'r> Fn(&'r ()) + 'static)>, &'t0 (), for<'r, 's> fn(&'r (), &'s ())) -> _` found closure signature `fn((), (), (), ()) -> _` diff --git a/src/test/ui/closure-expected-type/expect-infer-var-appearing-twice.stderr b/src/test/ui/closure-expected-type/expect-infer-var-appearing-twice.stderr index b013798c824..8dccf929b2b 100644 --- a/src/test/ui/closure-expected-type/expect-infer-var-appearing-twice.stderr +++ b/src/test/ui/closure-expected-type/expect-infer-var-appearing-twice.stderr @@ -1,15 +1,10 @@ error[E0631]: type mismatch in closure arguments - --> $DIR/expect-infer-var-appearing-twice.rs:14:18 + --> $DIR/expect-infer-var-appearing-twice.rs:14:5 | -LL | with_closure(|x: u32, y: i32| { - | ------------ ^--------------- - | | | - | _____|____________found signature defined here - | | | - | | required by a bound introduced by this call -LL | | -LL | | }); - | |_____^ expected due to this +LL | with_closure(|x: u32, y: i32| { + | ^^^^^^^^^^^^ ---------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `fn(_, _) -> _` found closure signature `fn(u32, i32) -> _` diff --git a/src/test/ui/mismatched_types/E0631.stderr b/src/test/ui/mismatched_types/E0631.stderr index 32ec9ecc474..410ea4b0b34 100644 --- a/src/test/ui/mismatched_types/E0631.stderr +++ b/src/test/ui/mismatched_types/E0631.stderr @@ -1,12 +1,10 @@ error[E0631]: type mismatch in closure arguments - --> $DIR/E0631.rs:7:9 + --> $DIR/E0631.rs:7:5 | LL | foo(|_: isize| {}); - | --- ----------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^^ ---------- found signature defined here + | | + | expected due to this | = note: expected closure signature `fn(usize) -> _` found closure signature `fn(isize) -> _` @@ -17,14 +15,12 @@ LL | fn foo(_: F) {} | ^^^^^^^^^ required by this bound in `foo` error[E0631]: type mismatch in closure arguments - --> $DIR/E0631.rs:8:9 + --> $DIR/E0631.rs:8:5 | LL | bar(|_: isize| {}); - | --- ----------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^^ ---------- found signature defined here + | | + | expected due to this | = note: expected closure signature `fn(usize) -> _` found closure signature `fn(isize) -> _` diff --git a/src/test/ui/mismatched_types/closure-arg-count.stderr b/src/test/ui/mismatched_types/closure-arg-count.stderr index c194e1dbc9a..a6ed22781e9 100644 --- a/src/test/ui/mismatched_types/closure-arg-count.stderr +++ b/src/test/ui/mismatched_types/closure-arg-count.stderr @@ -46,14 +46,12 @@ LL | [1, 2, 3].sort_by(|tuple, tuple2| panic!()); | ~~~~~~~~~~~~~~~ error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments - --> $DIR/closure-arg-count.rs:13:7 + --> $DIR/closure-arg-count.rs:13:5 | LL | f(|| panic!()); - | - --^^^^^^^^^ - | | | - | | expected closure that takes 1 argument - | | takes 0 arguments - | required by a bound introduced by this call + | ^ -- takes 0 arguments + | | + | expected closure that takes 1 argument | note: required by a bound in `f` --> $DIR/closure-arg-count.rs:3:9 @@ -66,14 +64,12 @@ LL | f(|_| panic!()); | ~~~ error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments - --> $DIR/closure-arg-count.rs:15:9 + --> $DIR/closure-arg-count.rs:15:5 | LL | f( move || panic!()); - | - ----------^^^^^^^^^ - | | | - | | expected closure that takes 1 argument - | | takes 0 arguments - | required by a bound introduced by this call + | ^ ---------- takes 0 arguments + | | + | expected closure that takes 1 argument | note: required by a bound in `f` --> $DIR/closure-arg-count.rs:3:9 @@ -137,14 +133,12 @@ LL | F: FnMut(Self::Item) -> B, | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `map` error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 3 distinct arguments - --> $DIR/closure-arg-count.rs:27:57 + --> $DIR/closure-arg-count.rs:27:53 | LL | let bar = |i, x, y| i; | --------- takes 3 distinct arguments LL | let _it = vec![1, 2, 3].into_iter().enumerate().map(bar); - | --- ^^^ expected closure that takes a single 2-tuple as argument - | | - | required by a bound introduced by this call + | ^^^ expected closure that takes a single 2-tuple as argument | note: required by a bound in `map` --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL diff --git a/src/test/ui/mismatched_types/closure-arg-type-mismatch.stderr b/src/test/ui/mismatched_types/closure-arg-type-mismatch.stderr index 0df71af3e70..71469bfec2d 100644 --- a/src/test/ui/mismatched_types/closure-arg-type-mismatch.stderr +++ b/src/test/ui/mismatched_types/closure-arg-type-mismatch.stderr @@ -1,12 +1,10 @@ error[E0631]: type mismatch in closure arguments - --> $DIR/closure-arg-type-mismatch.rs:3:18 + --> $DIR/closure-arg-type-mismatch.rs:3:14 | LL | a.iter().map(|_: (u32, u32)| 45); - | --- ---------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^^ --------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `fn(&(u32, u32)) -> _` found closure signature `fn((u32, u32)) -> _` @@ -17,14 +15,12 @@ LL | F: FnMut(Self::Item) -> B, | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `map` error[E0631]: type mismatch in closure arguments - --> $DIR/closure-arg-type-mismatch.rs:4:18 + --> $DIR/closure-arg-type-mismatch.rs:4:14 | LL | a.iter().map(|_: &(u16, u16)| 45); - | --- ----------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^^ ---------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `fn(&(u32, u32)) -> _` found closure signature `for<'r> fn(&'r (u16, u16)) -> _` @@ -35,14 +31,12 @@ LL | F: FnMut(Self::Item) -> B, | ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `map` error[E0631]: type mismatch in closure arguments - --> $DIR/closure-arg-type-mismatch.rs:5:18 + --> $DIR/closure-arg-type-mismatch.rs:5:14 | LL | a.iter().map(|_: (u16, u16)| 45); - | --- ---------------^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^^ --------------- found signature defined here + | | + | expected due to this | = note: expected closure signature `fn(&(u32, u32)) -> _` found closure signature `fn((u16, u16)) -> _` diff --git a/src/test/ui/mismatched_types/issue-36053-2.stderr b/src/test/ui/mismatched_types/issue-36053-2.stderr index 86348b50475..b11ea97d160 100644 --- a/src/test/ui/mismatched_types/issue-36053-2.stderr +++ b/src/test/ui/mismatched_types/issue-36053-2.stderr @@ -1,12 +1,10 @@ error[E0631]: type mismatch in closure arguments - --> $DIR/issue-36053-2.rs:7:39 + --> $DIR/issue-36053-2.rs:7:32 | LL | once::<&str>("str").fuse().filter(|a: &str| true).count(); - | ------ ---------^^^^^ - | | | - | | expected due to this - | | found signature defined here - | required by a bound introduced by this call + | ^^^^^^ --------- found signature defined here + | | + | expected due to this | = note: expected closure signature `for<'r> fn(&'r &str) -> _` found closure signature `for<'r> fn(&'r str) -> _` diff --git a/src/test/ui/mismatched_types/unboxed-closures-vtable-mismatch.rs b/src/test/ui/mismatched_types/unboxed-closures-vtable-mismatch.rs index 8dbe3472ea8..0d64ded215a 100644 --- a/src/test/ui/mismatched_types/unboxed-closures-vtable-mismatch.rs +++ b/src/test/ui/mismatched_types/unboxed-closures-vtable-mismatch.rs @@ -17,6 +17,5 @@ pub fn main() { //~^ ERROR type mismatch //~| NOTE expected due to this //~| NOTE expected closure signature `fn(isize, _) -> _` - //~| NOTE required by a bound introduced by this call println!("{}", z); } diff --git a/src/test/ui/mismatched_types/unboxed-closures-vtable-mismatch.stderr b/src/test/ui/mismatched_types/unboxed-closures-vtable-mismatch.stderr index 54b22006527..0b0d9f10786 100644 --- a/src/test/ui/mismatched_types/unboxed-closures-vtable-mismatch.stderr +++ b/src/test/ui/mismatched_types/unboxed-closures-vtable-mismatch.stderr @@ -1,13 +1,11 @@ error[E0631]: type mismatch in closure arguments - --> $DIR/unboxed-closures-vtable-mismatch.rs:16:24 + --> $DIR/unboxed-closures-vtable-mismatch.rs:16:13 | LL | let f = to_fn_mut(|x: usize, y: isize| -> isize { (x as isize) + y }); | ----------------------------- found signature defined here LL | LL | let z = call_it(3, f); - | ------- ^ expected due to this - | | - | required by a bound introduced by this call + | ^^^^^^^ expected due to this | = note: expected closure signature `fn(isize, _) -> _` found closure signature `fn(usize, _) -> _`