Skip to content

Commit d419a5f

Browse files
committed
Fix pointing at arg when cause is outside of call
1 parent d0126e8 commit d419a5f

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

src/librustc_typeck/check/mod.rs

+37-28
Original file line numberDiff line numberDiff line change
@@ -3880,36 +3880,45 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38803880
call_sp: Span,
38813881
args: &'tcx [hir::Expr],
38823882
) {
3883-
if !call_sp.desugaring_kind().is_some() {
3884-
// We *do not* do this for desugared call spans to keep good diagnostics when involving
3885-
// the `?` operator.
3886-
for error in errors {
3887-
if let ty::Predicate::Trait(predicate) = error.obligation.predicate {
3888-
// Collect the argument position for all arguments that could have caused this
3889-
// `FulfillmentError`.
3890-
let mut referenced_in = final_arg_types.iter()
3891-
.map(|(i, checked_ty, _)| (i, checked_ty))
3892-
.chain(final_arg_types.iter().map(|(i, _, coerced_ty)| (i, coerced_ty)))
3893-
.flat_map(|(i, ty)| {
3894-
let ty = self.resolve_vars_if_possible(ty);
3895-
// We walk the argument type because the argument's type could have
3896-
// been `Option<T>`, but the `FulfillmentError` references `T`.
3897-
ty.walk()
3898-
.filter(|&ty| ty == predicate.skip_binder().self_ty())
3899-
.map(move |_| *i)
3900-
})
3901-
.collect::<Vec<_>>();
3883+
// We *do not* do this for desugared call spans to keep good diagnostics when involving
3884+
// the `?` operator.
3885+
if call_sp.desugaring_kind().is_some() {
3886+
return
3887+
}
3888+
3889+
for error in errors {
3890+
// Only if the cause is somewhere inside the expression we want try to point at arg.
3891+
// Otherwise, it means that the cause is somewhere else and we should not change
3892+
// anything because we can break the correct span.
3893+
if !call_sp.contains(error.obligation.cause.span) {
3894+
continue
3895+
}
3896+
3897+
if let ty::Predicate::Trait(predicate) = error.obligation.predicate {
3898+
// Collect the argument position for all arguments that could have caused this
3899+
// `FulfillmentError`.
3900+
let mut referenced_in = final_arg_types.iter()
3901+
.map(|(i, checked_ty, _)| (i, checked_ty))
3902+
.chain(final_arg_types.iter().map(|(i, _, coerced_ty)| (i, coerced_ty)))
3903+
.flat_map(|(i, ty)| {
3904+
let ty = self.resolve_vars_if_possible(ty);
3905+
// We walk the argument type because the argument's type could have
3906+
// been `Option<T>`, but the `FulfillmentError` references `T`.
3907+
ty.walk()
3908+
.filter(|&ty| ty == predicate.skip_binder().self_ty())
3909+
.map(move |_| *i)
3910+
})
3911+
.collect::<Vec<_>>();
39023912

3903-
// Both checked and coerced types could have matched, thus we need to remove
3904-
// duplicates.
3905-
referenced_in.dedup();
3913+
// Both checked and coerced types could have matched, thus we need to remove
3914+
// duplicates.
3915+
referenced_in.dedup();
39063916

3907-
if let (Some(ref_in), None) = (referenced_in.pop(), referenced_in.pop()) {
3908-
// We make sure that only *one* argument matches the obligation failure
3909-
// and we assign the obligation's span to its expression's.
3910-
error.obligation.cause.span = args[ref_in].span;
3911-
error.points_at_arg_span = true;
3912-
}
3917+
if let (Some(ref_in), None) = (referenced_in.pop(), referenced_in.pop()) {
3918+
// We make sure that only *one* argument matches the obligation failure
3919+
// and we assign the obligation's span to its expression's.
3920+
error.obligation.cause.span = args[ref_in].span;
3921+
error.points_at_arg_span = true;
39133922
}
39143923
}
39153924
}

src/test/ui/issues/issue-66923.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
let v = vec![1_f64, 2.2_f64];
3+
let mut fft: Vec<Vec<f64>> = vec![];
4+
5+
let x1: &[f64] = &v;
6+
let x2: Vec<f64> = x1.into_iter().collect();
7+
//~^ ERROR a collection of type
8+
fft.push(x2);
9+
10+
let x3 = x1.into_iter().collect::<Vec<f64>>();
11+
//~^ ERROR a collection of type
12+
fft.push(x3);
13+
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0277]: a collection of type `std::vec::Vec<f64>` cannot be built from an iterator over elements of type `&f64`
2+
--> $DIR/issue-66923.rs:6:39
3+
|
4+
LL | let x2: Vec<f64> = x1.into_iter().collect();
5+
| ^^^^^^^ a collection of type `std::vec::Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`
6+
|
7+
= help: the trait `std::iter::FromIterator<&f64>` is not implemented for `std::vec::Vec<f64>`
8+
9+
error[E0277]: a collection of type `std::vec::Vec<f64>` cannot be built from an iterator over elements of type `&f64`
10+
--> $DIR/issue-66923.rs:10:29
11+
|
12+
LL | let x3 = x1.into_iter().collect::<Vec<f64>>();
13+
| ^^^^^^^ a collection of type `std::vec::Vec<f64>` cannot be built from `std::iter::Iterator<Item=&f64>`
14+
|
15+
= help: the trait `std::iter::FromIterator<&f64>` is not implemented for `std::vec::Vec<f64>`
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)