Skip to content

Commit b7c0c2c

Browse files
committed
update test suite with new error spans
1 parent cf98ecb commit b7c0c2c

File tree

8 files changed

+92
-88
lines changed

8 files changed

+92
-88
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
525525
// Sometimes macros mess up the spans, so do not normalize the
526526
// arg span to equal the error span, because that's less useful
527527
// than pointing out the arg expr in the wrong context.
528-
if normalized_span.source_equal(error_span) {
529-
span
530-
} else {
531-
normalized_span
532-
}
528+
if normalized_span.source_equal(error_span) { span } else { normalized_span }
533529
};
534530

535531
// Precompute the provided types and spans, since that's all we typically need for below
@@ -782,8 +778,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
782778
// can be collated pretty easily if needed.
783779

784780
// Next special case: if there is only one "Incompatible" error, just emit that
785-
if let [Error::Invalid(provided_idx, expected_idx, Compatibility::Incompatible(Some(err)))] =
786-
&errors[..]
781+
if let [
782+
Error::Invalid(provided_idx, expected_idx, Compatibility::Incompatible(Some(err))),
783+
] = &errors[..]
787784
{
788785
let (formal_ty, expected_ty) = formal_and_expected_inputs[*expected_idx];
789786
let (provided_ty, provided_arg_span) = provided_arg_tys[*provided_idx];
@@ -1525,21 +1522,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
15251522
// Our block must be a `assign desugar local; assignment`
15261523
if let Some(hir::Node::Block(hir::Block {
15271524
stmts:
1528-
[hir::Stmt {
1529-
kind:
1530-
hir::StmtKind::Local(hir::Local {
1531-
source: hir::LocalSource::AssignDesugar(_),
1532-
..
1533-
}),
1534-
..
1535-
}, hir::Stmt {
1536-
kind:
1537-
hir::StmtKind::Expr(hir::Expr {
1538-
kind: hir::ExprKind::Assign(..),
1539-
..
1540-
}),
1541-
..
1542-
}],
1525+
[
1526+
hir::Stmt {
1527+
kind:
1528+
hir::StmtKind::Local(hir::Local {
1529+
source:
1530+
hir::LocalSource::AssignDesugar(_),
1531+
..
1532+
}),
1533+
..
1534+
},
1535+
hir::Stmt {
1536+
kind:
1537+
hir::StmtKind::Expr(hir::Expr {
1538+
kind: hir::ExprKind::Assign(..),
1539+
..
1540+
}),
1541+
..
1542+
},
1543+
],
15431544
..
15441545
})) = self.tcx.hir().find(blk.hir_id)
15451546
{
@@ -2061,7 +2062,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20612062
self.tcx.predicates_of(obligation.impl_def_id);
20622063
let Some(impl_predicate_index) = obligation.impl_def_predicate_index else {
20632064
// We don't have the index, so we can only guess.
2064-
// TODO: We could conservatively assume that all generics are relevant and go from there instead...
20652065
return Err(expr);
20662066
};
20672067

@@ -2075,8 +2075,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20752075
// We want to find which of the generics in the `impl_generics` are relevant to
20762076
// the broken obligation predicate.
20772077
// A generic is relevant if it is mentioned in the "original" predicate. If we can't narrow it down, treat them all as relevant.
2078-
// BUG: Oops, I am looking at the wrong thing here. We should find the original predicate, NOT the self type.
2079-
// This requires adding the impl_predicate_index to the obligation when it is created. TODO: do that.
20802078
let relevant_impl_generics = impl_generics.params.iter();
20812079

20822080
let relevant_impl_generics: Vec<&ty::GenericParamDef> = match relevant_broken_predicate {
@@ -2087,7 +2085,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20872085
})
20882086
.collect(),
20892087
_ => {
2090-
relevant_impl_generics.collect() // Treat all generics as potentially relevant
2088+
// Because we are missing information about which might be relevant, we assume that they all are.
2089+
relevant_impl_generics.collect()
20912090
}
20922091
};
20932092

@@ -2096,13 +2095,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20962095
Tuple,
20972096
}
20982097

2099-
// We can only handle Adt types for now.
2100-
// TODO: We could support blanket impls here as well.
2101-
// TODO: We could support tuple impls here as well.
2102-
// TODO: We could potentially support array/vec/slice impls here as well.
2103-
// TODO: We could support ref impls here as well.
21042098
// Note that there is no point in supporting "primitive" types like char/i32,
21052099
// since we cannot refine such a span any more anyway.
2100+
// Right now, only ADTs (struct/enum (variant) constructors) and tuples are supported by this refinement.
21062101
let (impl_self_ty, impl_self_ty_args) = match impl_self_ty.kind() {
21072102
ty::Adt(impl_self_ty_path, impl_self_ty_args) => (
21082103
PointableType::Adt(*impl_self_ty_path),

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2865,11 +2865,7 @@ impl<'o, 'tcx> TraitObligationStackList<'o, 'tcx> {
28652865
}
28662866

28672867
fn depth(&self) -> usize {
2868-
if let Some(head) = self.head {
2869-
head.depth
2870-
} else {
2871-
0
2872-
}
2868+
if let Some(head) = self.head { head.depth } else { 0 }
28732869
}
28742870
}
28752871

src/test/ui/derives/deriving-copyclone.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `B<C>: Copy` is not satisfied
2-
--> $DIR/deriving-copyclone.rs:31:13
2+
--> $DIR/deriving-copyclone.rs:31:26
33
|
44
LL | is_copy(B { a: 1, b: C });
5-
| ------- ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `B<C>`
5+
| ------- ^ the trait `Copy` is not implemented for `B<C>`
66
| |
77
| required by a bound introduced by this call
88
|
@@ -19,14 +19,14 @@ LL | fn is_copy<T: Copy>(_: T) {}
1919
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
2020
help: consider borrowing here
2121
|
22-
LL | is_copy(&B { a: 1, b: C });
23-
| +
22+
LL | is_copy(B { a: 1, b: &C });
23+
| +
2424

2525
error[E0277]: the trait bound `B<C>: Clone` is not satisfied
26-
--> $DIR/deriving-copyclone.rs:32:14
26+
--> $DIR/deriving-copyclone.rs:32:27
2727
|
2828
LL | is_clone(B { a: 1, b: C });
29-
| -------- ^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `B<C>`
29+
| -------- ^ the trait `Clone` is not implemented for `B<C>`
3030
| |
3131
| required by a bound introduced by this call
3232
|
@@ -43,14 +43,14 @@ LL | fn is_clone<T: Clone>(_: T) {}
4343
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
4444
help: consider borrowing here
4545
|
46-
LL | is_clone(&B { a: 1, b: C });
47-
| +
46+
LL | is_clone(B { a: 1, b: &C });
47+
| +
4848

4949
error[E0277]: the trait bound `B<D>: Copy` is not satisfied
50-
--> $DIR/deriving-copyclone.rs:35:13
50+
--> $DIR/deriving-copyclone.rs:35:26
5151
|
5252
LL | is_copy(B { a: 1, b: D });
53-
| ------- ^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `B<D>`
53+
| ------- ^ the trait `Copy` is not implemented for `B<D>`
5454
| |
5555
| required by a bound introduced by this call
5656
|
@@ -67,8 +67,8 @@ LL | fn is_copy<T: Copy>(_: T) {}
6767
= note: this error originates in the derive macro `Copy` (in Nightly builds, run with -Z macro-backtrace for more info)
6868
help: consider borrowing here
6969
|
70-
LL | is_copy(&B { a: 1, b: D });
71-
| +
70+
LL | is_copy(B { a: 1, b: &D });
71+
| +
7272

7373
error: aborting due to 3 previous errors
7474

src/test/ui/higher-rank-trait-bounds/issue-62203-hrtb-ice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ trait Ty<'a> {
3737
fn main() {
3838
let v = Unit2.m(
3939
L {
40-
//~^ ERROR to be a closure that returns `Unit3`, but it returns `Unit4`
41-
//~| ERROR type mismatch
40+
//~^ ERROR type mismatch
4241
f: |x| {
42+
//~^ ERROR to be a closure that returns `Unit3`, but it returns `Unit4`
4343
drop(x);
4444
Unit4
4545
},

src/test/ui/higher-rank-trait-bounds/issue-62203-hrtb-ice.stderr

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error[E0271]: type mismatch resolving `for<'r> <L<[closure@$DIR/issue-62203-hrtb-ice.rs:42:16: 42:19]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
1+
error[E0271]: type mismatch resolving `for<'r> <L<[closure@$DIR/issue-62203-hrtb-ice.rs:41:16: 41:19]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
22
--> $DIR/issue-62203-hrtb-ice.rs:39:9
33
|
44
LL | let v = Unit2.m(
55
| - required by a bound introduced by this call
66
LL | / L {
77
LL | |
8-
LL | |
98
LL | | f: |x| {
9+
LL | |
1010
... |
1111
LL | | },
1212
LL | | },
13-
| |_________^ type mismatch resolving `for<'r> <L<[closure@$DIR/issue-62203-hrtb-ice.rs:42:16: 42:19]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
13+
| |_________^ type mismatch resolving `for<'r> <L<[closure@$DIR/issue-62203-hrtb-ice.rs:41:16: 41:19]> as T0<'r, (&'r u8,)>>::O == <_ as Ty<'r>>::V`
1414
|
1515
note: expected this to be `<_ as Ty<'_>>::V`
1616
--> $DIR/issue-62203-hrtb-ice.rs:21:14
@@ -30,21 +30,21 @@ LL | where
3030
LL | F: for<'r> T0<'r, (<Self as Ty<'r>>::V,), O = <B as Ty<'r>>::V>,
3131
| ^^^^^^^^^^^^^^^^^^^^ required by this bound in `T1::m`
3232

33-
error[E0271]: expected `[[email protected]:42:16]` to be a closure that returns `Unit3`, but it returns `Unit4`
34-
--> $DIR/issue-62203-hrtb-ice.rs:39:9
33+
error[E0271]: expected `[[email protected]:41:16]` to be a closure that returns `Unit3`, but it returns `Unit4`
34+
--> $DIR/issue-62203-hrtb-ice.rs:41:16
3535
|
3636
LL | let v = Unit2.m(
3737
| - required by a bound introduced by this call
38-
LL | / L {
38+
...
39+
LL | f: |x| {
40+
| ________________^
3941
LL | |
40-
LL | |
41-
LL | | f: |x| {
42-
... |
42+
LL | | drop(x);
43+
LL | | Unit4
4344
LL | | },
44-
LL | | },
45-
| |_________^ expected struct `Unit3`, found struct `Unit4`
45+
| |_____________^ expected struct `Unit3`, found struct `Unit4`
4646
|
47-
note: required for `L<[closure@$DIR/issue-62203-hrtb-ice.rs:42:16: 42:19]>` to implement `for<'r> T0<'r, (&'r u8,)>`
47+
note: required for `L<[closure@$DIR/issue-62203-hrtb-ice.rs:41:16: 41:19]>` to implement `for<'r> T0<'r, (&'r u8,)>`
4848
--> $DIR/issue-62203-hrtb-ice.rs:17:16
4949
|
5050
LL | impl<'a, A, T> T0<'a, A> for L<T>

src/test/ui/tuple/blame-trait-error.rs renamed to src/test/ui/traits/bad-trait-error-span-in-call.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// This test examines the error spans reported when a generic `impl` fails.
2+
// For example, if a function wants an `Option<T>` where `T: Copy` but you pass `Some(vec![1, 2])`,
3+
// then we want to point at the `vec![1, 2]` and not the `Some( ... )` expression.
4+
15
trait T1 {}
26
trait T2 {}
37
trait T3 {}
@@ -48,21 +52,30 @@ impl<A: T3, B: T3> T2 for (A, B) {}
4852
fn want<V: T1>(_x: V) {}
4953

5054
fn example<Q>(q: Q) {
55+
// In each of the following examples, we expect the error span to point at the 'q' variable,
56+
// since the missing constraint is `Q: T3`.
57+
58+
// Verifies for struct:
5159
want(Wrapper { value: Burrito { spicy: false, filling: q } });
5260
//~^ ERROR the trait bound `Q: T3` is not satisfied [E0277]
5361

62+
// Verifies for enum with named fields in variant:
5463
want(Wrapper { value: BurritoKinds::SmallBurrito { spicy: true, small_filling: q } });
5564
//~^ ERROR the trait bound `Q: T3` is not satisfied [E0277]
5665

66+
// Verifies for tuple struct:
5767
want(Wrapper { value: Taco(false, q) });
5868
//~^ ERROR the trait bound `Q: T3` is not satisfied [E0277]
5969

70+
// Verifies for tuple enum variant:
6071
want(Wrapper { value: TacoKinds::OneTaco(false, q) });
6172
//~^ ERROR the trait bound `Q: T3` is not satisfied [E0277]
6273

74+
// Verifies for generic type with multiple parameters:
6375
want(Wrapper { value: GenericBurrito { spiciness: NotSpicy, filling: q } });
6476
//~^ ERROR the trait bound `Q: T3` is not satisfied [E0277]
6577

78+
// Verifies for tuple:
6679
want(Wrapper { value: (3, q) });
6780
//~^ ERROR the trait bound `Q: T3` is not satisfied [E0277]
6881
}

0 commit comments

Comments
 (0)