Skip to content

Commit c371a94

Browse files
authored
Rollup merge of #134745 - compiler-errors:better-arg-span-in-typeck, r=BoxyUwU
Normalize each signature input/output in `typeck_with_fallback` with its own span Applies the same hack as #106582 but to the args in typeck. Greatly improves normalization error spans from a signature.
2 parents 191fb23 + 5a56600 commit c371a94

16 files changed

+69
-77
lines changed

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,23 @@ fn typeck_with_fallback<'tcx>(
147147
check_abi(tcx, span, fn_sig.abi());
148148

149149
// Compute the function signature from point of view of inside the fn.
150-
let fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig);
151-
let fn_sig = fcx.normalize(body.value.span, fn_sig);
150+
let mut fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig);
151+
152+
// Normalize the input and output types one at a time, using a different
153+
// `WellFormedLoc` for each. We cannot call `normalize_associated_types`
154+
// on the entire `FnSig`, since this would use the same `WellFormedLoc`
155+
// for each type, preventing the HIR wf check from generating
156+
// a nice error message.
157+
let arg_span =
158+
|idx| decl.inputs.get(idx).map_or(decl.output.span(), |arg: &hir::Ty<'_>| arg.span);
159+
160+
fn_sig.inputs_and_output = tcx.mk_type_list_from_iter(
161+
fn_sig
162+
.inputs_and_output
163+
.iter()
164+
.enumerate()
165+
.map(|(idx, ty)| fcx.normalize(arg_span(idx), ty)),
166+
);
152167

153168
check_fn(&mut fcx, fn_sig, None, decl, def_id, body, tcx.features().unsized_fn_params());
154169
} else {

tests/ui/associated-types/associated-types-for-unimpl-trait.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ LL | fn uhoh<U: Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: S
1010
| +++++++++++
1111

1212
error[E0277]: the trait bound `Self: Get` is not satisfied
13-
--> $DIR/associated-types-for-unimpl-trait.rs:11:81
13+
--> $DIR/associated-types-for-unimpl-trait.rs:11:41
1414
|
1515
LL | fn uhoh<U: Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Sized {}
16-
| ^^ the trait `Get` is not implemented for `Self`
16+
| ^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
1717
|
18+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1819
help: consider further restricting `Self`
1920
|
2021
LL | fn uhoh<U: Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Sized, Self: Get {}

tests/ui/associated-types/associated-types-no-suitable-bound.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
1010
| +++++
1111

1212
error[E0277]: the trait bound `T: Get` is not satisfied
13-
--> $DIR/associated-types-no-suitable-bound.rs:11:40
13+
--> $DIR/associated-types-no-suitable-bound.rs:11:21
1414
|
1515
LL | fn uhoh<T>(foo: <T as Get>::Value) {}
16-
| ^^ the trait `Get` is not implemented for `T`
16+
| ^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `T`
1717
|
18+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1819
help: consider restricting type parameter `T` with trait `Get`
1920
|
2021
LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}

tests/ui/associated-types/associated-types-no-suitable-supertrait-2.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Ge
1010
| +++++++++++++++
1111

1212
error[E0277]: the trait bound `Self: Get` is not satisfied
13-
--> $DIR/associated-types-no-suitable-supertrait-2.rs:17:62
13+
--> $DIR/associated-types-no-suitable-supertrait-2.rs:17:40
1414
|
1515
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) {}
16-
| ^^ the trait `Get` is not implemented for `Self`
16+
| ^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
1717
|
18+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1819
help: consider further restricting `Self`
1920
|
2021
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Get {}

tests/ui/associated-types/associated-types-no-suitable-supertrait.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,29 @@ LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Ge
3434
| +++++++++++++++
3535

3636
error[E0277]: the trait bound `Self: Get` is not satisfied
37-
--> $DIR/associated-types-no-suitable-supertrait.rs:17:62
37+
--> $DIR/associated-types-no-suitable-supertrait.rs:17:40
3838
|
3939
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) {}
40-
| ^^ the trait `Get` is not implemented for `Self`
40+
| ^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
4141
|
42+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
4243
help: consider further restricting `Self`
4344
|
4445
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Get {}
4546
| +++++++++++++++
4647

4748
error[E0277]: the trait bound `(T, U): Get` is not satisfied
48-
--> $DIR/associated-types-no-suitable-supertrait.rs:23:64
49+
--> $DIR/associated-types-no-suitable-supertrait.rs:23:40
4950
|
5051
LL | fn uhoh<U:Get>(&self, foo: U, bar: <(T, U) as Get>::Value) {}
51-
| ^^ the trait `Get` is not implemented for `(T, U)`
52+
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `(T, U)`
5253
|
5354
help: this trait has no implementations, consider adding one
5455
--> $DIR/associated-types-no-suitable-supertrait.rs:12:1
5556
|
5657
LL | trait Get {
5758
| ^^^^^^^^^
59+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
5860

5961
error: aborting due to 5 previous errors
6062

tests/ui/associated-types/issue-59324.stderr

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,17 @@ LL | pub trait ThriftService<Bug: NotFoo + Foo>:
6565
| +++++
6666

6767
error[E0277]: the trait bound `(): Foo` is not satisfied
68-
--> $DIR/issue-59324.rs:23:52
68+
--> $DIR/issue-59324.rs:23:29
6969
|
7070
LL | fn with_factory<H>(factory: dyn ThriftService<()>) {}
71-
| ^^ the trait `Foo` is not implemented for `()`
71+
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `()`
7272
|
7373
help: this trait has no implementations, consider adding one
7474
--> $DIR/issue-59324.rs:3:1
7575
|
7676
LL | pub trait Foo: NotFoo {
7777
| ^^^^^^^^^^^^^^^^^^^^^
78+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
7879

7980
error[E0277]: the size for values of type `(dyn ThriftService<(), AssocType = _> + 'static)` cannot be known at compilation time
8081
--> $DIR/issue-59324.rs:23:29

tests/ui/auto-traits/issue-83857-ub.stderr

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,10 @@ LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i
1818
| +++++++++++++++++++++
1919

2020
error[E0277]: `Foo<T, U>` cannot be sent between threads safely
21-
--> $DIR/issue-83857-ub.rs:21:80
21+
--> $DIR/issue-83857-ub.rs:21:35
2222
|
23-
LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) {
24-
| ________________________________________________________________________________^
25-
LL | |
26-
LL | |
27-
LL | | f(foo(v));
28-
LL | |
29-
LL | | }
30-
| |_^ `Foo<T, U>` cannot be sent between threads safely
23+
LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) {
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Foo<T, U>` cannot be sent between threads safely
3125
|
3226
= help: the trait `Send` is not implemented for `Foo<T, U>`
3327
note: required for `Foo<T, U>` to implement `WithAssoc`

tests/ui/error-codes/E0229.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ LL | fn baz<I: Foo>(x: &<I as Foo<A = Bar>>::A) {}
4848
| +++++
4949

5050
error[E0277]: the trait bound `I: Foo` is not satisfied
51-
--> $DIR/E0229.rs:13:39
51+
--> $DIR/E0229.rs:13:14
5252
|
5353
LL | fn baz<I>(x: &<I as Foo<A = Bar>>::A) {}
54-
| ^^ the trait `Foo` is not implemented for `I`
54+
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `I`
5555
|
5656
help: consider restricting type parameter `I` with trait `Foo`
5757
|

tests/ui/issues/issue-18611.stderr

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,17 @@ LL | trait HasState {
1111
| ^^^^^^^^^^^^^^
1212

1313
error[E0277]: the trait bound `isize: HasState` is not satisfied
14-
--> $DIR/issue-18611.rs:1:46
14+
--> $DIR/issue-18611.rs:1:18
1515
|
16-
LL | fn add_state(op: <isize as HasState>::State) {
17-
| ______________________________________________^
18-
... |
19-
LL | | }
20-
| |_^ the trait `HasState` is not implemented for `isize`
16+
LL | fn add_state(op: <isize as HasState>::State) {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HasState` is not implemented for `isize`
2118
|
2219
help: this trait has no implementations, consider adding one
2320
--> $DIR/issue-18611.rs:6:1
2421
|
2522
LL | trait HasState {
2623
| ^^^^^^^^^^^^^^
24+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2725

2826
error: aborting due to 2 previous errors
2927

tests/ui/issues/issue-35570.stderr

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@ LL | trait Trait2<'a> {
1111
| ^^^^^^^^^^^^^^^^
1212

1313
error[E0277]: the trait bound `for<'a> (): Trait2<'a>` is not satisfied
14-
--> $DIR/issue-35570.rs:8:66
14+
--> $DIR/issue-35570.rs:8:16
1515
|
16-
LL | fn _ice(param: Box<dyn for <'a> Trait1<<() as Trait2<'a>>::Ty>>) {
17-
| __________________________________________________________________^
18-
LL | |
19-
LL | |
20-
LL | | let _e: (usize, usize) = unsafe{mem::transmute(param)};
21-
LL | | }
22-
| |_^ the trait `for<'a> Trait2<'a>` is not implemented for `()`
16+
LL | fn _ice(param: Box<dyn for <'a> Trait1<<() as Trait2<'a>>::Ty>>) {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'a> Trait2<'a>` is not implemented for `()`
2318
|
2419
help: this trait has no implementations, consider adding one
2520
--> $DIR/issue-35570.rs:4:1

0 commit comments

Comments
 (0)