Skip to content

Commit

Permalink
Rollup merge of rust-lang#134745 - compiler-errors:better-arg-span-in…
Browse files Browse the repository at this point in the history
…-typeck, r=BoxyUwU

Normalize each signature input/output in `typeck_with_fallback` with its own span

Applies the same hack as rust-lang#106582 but to the args in typeck. Greatly improves normalization error spans from a signature.
  • Loading branch information
matthiaskrgr authored Jan 7, 2025
2 parents 59c4b4d + 5a56600 commit 2034dc4
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 77 deletions.
19 changes: 17 additions & 2 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,23 @@ fn typeck_with_fallback<'tcx>(
check_abi(tcx, span, fn_sig.abi());

// Compute the function signature from point of view of inside the fn.
let fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig);
let fn_sig = fcx.normalize(body.value.span, fn_sig);
let mut fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig);

// Normalize the input and output types one at a time, using a different
// `WellFormedLoc` for each. We cannot call `normalize_associated_types`
// on the entire `FnSig`, since this would use the same `WellFormedLoc`
// for each type, preventing the HIR wf check from generating
// a nice error message.
let arg_span =
|idx| decl.inputs.get(idx).map_or(decl.output.span(), |arg: &hir::Ty<'_>| arg.span);

fn_sig.inputs_and_output = tcx.mk_type_list_from_iter(
fn_sig
.inputs_and_output
.iter()
.enumerate()
.map(|(idx, ty)| fcx.normalize(arg_span(idx), ty)),
);

check_fn(&mut fcx, fn_sig, None, decl, def_id, body, tcx.features().unsized_fn_params());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ LL | fn uhoh<U: Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: S
| +++++++++++

error[E0277]: the trait bound `Self: Get` is not satisfied
--> $DIR/associated-types-for-unimpl-trait.rs:11:81
--> $DIR/associated-types-for-unimpl-trait.rs:11:41
|
LL | fn uhoh<U: Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Sized {}
| ^^ the trait `Get` is not implemented for `Self`
| ^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider further restricting `Self`
|
LL | fn uhoh<U: Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Sized, Self: Get {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
| +++++

error[E0277]: the trait bound `T: Get` is not satisfied
--> $DIR/associated-types-no-suitable-bound.rs:11:40
--> $DIR/associated-types-no-suitable-bound.rs:11:21
|
LL | fn uhoh<T>(foo: <T as Get>::Value) {}
| ^^ the trait `Get` is not implemented for `T`
| ^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `T`
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider restricting type parameter `T` with trait `Get`
|
LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Ge
| +++++++++++++++

error[E0277]: the trait bound `Self: Get` is not satisfied
--> $DIR/associated-types-no-suitable-supertrait-2.rs:17:62
--> $DIR/associated-types-no-suitable-supertrait-2.rs:17:40
|
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) {}
| ^^ the trait `Get` is not implemented for `Self`
| ^^^^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `Self`
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider further restricting `Self`
|
LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Get {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,29 @@ LL | fn uhoh<U:Get>(&self, foo: U, bar: <Self as Get>::Value) where Self: Ge
| +++++++++++++++

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

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

error: aborting due to 5 previous errors

Expand Down
5 changes: 3 additions & 2 deletions tests/ui/associated-types/issue-59324.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++

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

error[E0277]: the size for values of type `(dyn ThriftService<(), AssocType = _> + 'static)` cannot be known at compilation time
--> $DIR/issue-59324.rs:23:29
Expand Down
12 changes: 3 additions & 9 deletions tests/ui/auto-traits/issue-83857-ub.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@ LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i
| +++++++++++++++++++++

error[E0277]: `Foo<T, U>` cannot be sent between threads safely
--> $DIR/issue-83857-ub.rs:21:80
--> $DIR/issue-83857-ub.rs:21:35
|
LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) {
| ________________________________________________________________________________^
LL | |
LL | |
LL | | f(foo(v));
LL | |
LL | | }
| |_^ `Foo<T, U>` cannot be sent between threads safely
LL | fn generic<T, U>(v: Foo<T, U>, f: fn(<Foo<T, U> as WithAssoc>::Output) -> i32) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Foo<T, U>` cannot be sent between threads safely
|
= help: the trait `Send` is not implemented for `Foo<T, U>`
note: required for `Foo<T, U>` to implement `WithAssoc`
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/error-codes/E0229.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ LL | fn baz<I: Foo>(x: &<I as Foo<A = Bar>>::A) {}
| +++++

error[E0277]: the trait bound `I: Foo` is not satisfied
--> $DIR/E0229.rs:13:39
--> $DIR/E0229.rs:13:14
|
LL | fn baz<I>(x: &<I as Foo<A = Bar>>::A) {}
| ^^ the trait `Foo` is not implemented for `I`
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `I`
|
help: consider restricting type parameter `I` with trait `Foo`
|
Expand Down
10 changes: 4 additions & 6 deletions tests/ui/issues/issue-18611.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ LL | trait HasState {
| ^^^^^^^^^^^^^^

error[E0277]: the trait bound `isize: HasState` is not satisfied
--> $DIR/issue-18611.rs:1:46
--> $DIR/issue-18611.rs:1:18
|
LL | fn add_state(op: <isize as HasState>::State) {
| ______________________________________________^
... |
LL | | }
| |_^ the trait `HasState` is not implemented for `isize`
LL | fn add_state(op: <isize as HasState>::State) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `HasState` is not implemented for `isize`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-18611.rs:6:1
|
LL | trait HasState {
| ^^^^^^^^^^^^^^
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 2 previous errors

Expand Down
11 changes: 3 additions & 8 deletions tests/ui/issues/issue-35570.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@ LL | trait Trait2<'a> {
| ^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `for<'a> (): Trait2<'a>` is not satisfied
--> $DIR/issue-35570.rs:8:66
--> $DIR/issue-35570.rs:8:16
|
LL | fn _ice(param: Box<dyn for <'a> Trait1<<() as Trait2<'a>>::Ty>>) {
| __________________________________________________________________^
LL | |
LL | |
LL | | let _e: (usize, usize) = unsafe{mem::transmute(param)};
LL | | }
| |_^ the trait `for<'a> Trait2<'a>` is not implemented for `()`
LL | fn _ice(param: Box<dyn for <'a> Trait1<<() as Trait2<'a>>::Ty>>) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'a> Trait2<'a>` is not implemented for `()`
|
help: this trait has no implementations, consider adding one
--> $DIR/issue-35570.rs:4:1
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/proc-macro/bad-projection.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,17 @@ LL | trait Project {
| ^^^^^^^^^^^^^

error[E0277]: the trait bound `(): Project` is not satisfied
--> $DIR/bad-projection.rs:14:40
--> $DIR/bad-projection.rs:14:17
|
LL | pub fn uwu() -> <() as Project>::Assoc {}
| ^^ the trait `Project` is not implemented for `()`
| ^^^^^^^^^^^^^^^^^^^^^^ the trait `Project` is not implemented for `()`
|
help: this trait has no implementations, consider adding one
--> $DIR/bad-projection.rs:9:1
|
LL | trait Project {
| ^^^^^^^^^^^^^
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 5 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ trait Trait2<'a, 'b> {
// do not infer that.
fn callee<'x, 'y, T>(t: &'x dyn for<'z> Trait1< <T as Trait2<'y, 'z>>::Foo >)
//~^ ERROR the trait bound `for<'z> T: Trait2<'y, 'z>` is not satisfied
//~| ERROR the trait bound `for<'z> T: Trait2<'y, 'z>` is not satisfied
{
//~^ ERROR the trait bound `for<'z> T: Trait2<'y, 'z>` is not satisfied
}

fn main() { }
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ LL | fn callee<'x, 'y, T: for<'z> Trait2<'y, 'z>>(t: &'x dyn for<'z> Trait1< <T
| ++++++++++++++++++++++++

error[E0277]: the trait bound `for<'z> T: Trait2<'y, 'z>` is not satisfied
--> $DIR/regions-implied-bounds-projection-gap-hr-1.rs:23:1
--> $DIR/regions-implied-bounds-projection-gap-hr-1.rs:21:25
|
LL | / {
LL | |
LL | | }
| |_^ the trait `for<'z> Trait2<'y, 'z>` is not implemented for `T`
LL | fn callee<'x, 'y, T>(t: &'x dyn for<'z> Trait1< <T as Trait2<'y, 'z>>::Foo >)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `for<'z> Trait2<'y, 'z>` is not implemented for `T`
|
help: consider restricting type parameter `T` with trait `Trait2`
|
Expand Down
9 changes: 3 additions & 6 deletions tests/ui/specialization/min_specialization/issue-79224.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@ LL | impl<B: ?Sized + std::clone::Clone> Display for Cow<'_, B> {
| +++++++++++++++++++

error[E0277]: the trait bound `B: Clone` is not satisfied
--> $DIR/issue-79224.rs:30:62
--> $DIR/issue-79224.rs:30:12
|
LL | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
| ______________________________________________________________^
... |
LL | | }
| |_____^ the trait `Clone` is not implemented for `B`
LL | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
| ^^^^^ the trait `Clone` is not implemented for `B`
|
= note: required for `B` to implement `ToOwned`
help: consider further restricting type parameter `B` with trait `Clone`
Expand Down
12 changes: 4 additions & 8 deletions tests/ui/type-alias-impl-trait/generic_underconstrained.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ LL | fn underconstrain<T: Trait>(_: T) -> Underconstrained<T> {
| +++++++

error[E0277]: the trait bound `T: Trait` is not satisfied
--> $DIR/generic_underconstrained.rs:9:51
--> $DIR/generic_underconstrained.rs:9:31
|
LL | fn underconstrain<T>(_: T) -> Underconstrained<T> {
| ___________________________________________________^
LL | |
LL | |
LL | | unimplemented!()
LL | | }
| |_^ the trait `Trait` is not implemented for `T`
LL | fn underconstrain<T>(_: T) -> Underconstrained<T> {
| ^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `T`
|
note: required by a bound on the type alias `Underconstrained`
--> $DIR/generic_underconstrained.rs:6:26
|
LL | type Underconstrained<T: Trait> = impl Send;
| ^^^^^ required by this bound
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider restricting type parameter `T` with trait `Trait`
|
LL | fn underconstrain<T: Trait>(_: T) -> Underconstrained<T> {
Expand Down
24 changes: 8 additions & 16 deletions tests/ui/type-alias-impl-trait/generic_underconstrained2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,34 @@ LL | fn underconstrained2<U, V: std::fmt::Debug>(_: U, _: V) -> Underconstrained
| +++++++++++++++++

error[E0277]: `U` doesn't implement `Debug`
--> $DIR/generic_underconstrained2.rs:8:53
--> $DIR/generic_underconstrained2.rs:8:33
|
LL | fn underconstrained<U>(_: U) -> Underconstrained<U> {
| _____________________________________________________^
LL | |
LL | |
LL | | 5u32
LL | | }
| |_^ `U` cannot be formatted using `{:?}` because it doesn't implement `Debug`
LL | fn underconstrained<U>(_: U) -> Underconstrained<U> {
| ^^^^^^^^^^^^^^^^^^^ `U` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
note: required by a bound on the type alias `Underconstrained`
--> $DIR/generic_underconstrained2.rs:5:26
|
LL | type Underconstrained<T: std::fmt::Debug> = impl Send;
| ^^^^^^^^^^^^^^^ required by this bound
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider restricting type parameter `U` with trait `Debug`
|
LL | fn underconstrained<U: std::fmt::Debug>(_: U) -> Underconstrained<U> {
| +++++++++++++++++

error[E0277]: `V` doesn't implement `Debug`
--> $DIR/generic_underconstrained2.rs:17:64
--> $DIR/generic_underconstrained2.rs:17:43
|
LL | fn underconstrained2<U, V>(_: U, _: V) -> Underconstrained2<V> {
| ________________________________________________________________^
LL | |
LL | |
LL | | 5u32
LL | | }
| |_^ `V` cannot be formatted using `{:?}` because it doesn't implement `Debug`
LL | fn underconstrained2<U, V>(_: U, _: V) -> Underconstrained2<V> {
| ^^^^^^^^^^^^^^^^^^^^ `V` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
note: required by a bound on the type alias `Underconstrained2`
--> $DIR/generic_underconstrained2.rs:14:27
|
LL | type Underconstrained2<T: std::fmt::Debug> = impl Send;
| ^^^^^^^^^^^^^^^ required by this bound
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider restricting type parameter `V` with trait `Debug`
|
LL | fn underconstrained2<U, V: std::fmt::Debug>(_: U, _: V) -> Underconstrained2<V> {
Expand Down

0 comments on commit 2034dc4

Please sign in to comment.