Skip to content

Use a more accurate span on assoc types WF checks #87819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,13 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let trait_item = tcx.hir().expect_trait_item(hir_id);

let method_sig = match trait_item.kind {
hir::TraitItemKind::Fn(ref sig, _) => Some(sig),
_ => None,
let (method_sig, span) = match trait_item.kind {
hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span),
hir::TraitItemKind::Type(_bounds, Some(ty)) => (None, ty.span),
_ => (None, trait_item.span),
};
check_object_unsafe_self_trait_by_name(tcx, &trait_item);
check_associated_item(tcx, trait_item.hir_id(), trait_item.span, method_sig);
check_associated_item(tcx, trait_item.hir_id(), span, method_sig);
}

fn could_be_self(trait_def_id: LocalDefId, ty: &hir::Ty<'_>) -> bool {
Expand Down Expand Up @@ -268,12 +269,13 @@ pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let impl_item = tcx.hir().expect_impl_item(hir_id);

let method_sig = match impl_item.kind {
hir::ImplItemKind::Fn(ref sig, _) => Some(sig),
_ => None,
let (method_sig, span) = match impl_item.kind {
hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span),
hir::ImplItemKind::TyAlias(ty) => (None, ty.span),
_ => (None, impl_item.span),
};

check_associated_item(tcx, impl_item.hir_id(), impl_item.span, method_sig);
check_associated_item(tcx, impl_item.hir_id(), span, method_sig);
}

fn check_param_wf(tcx: TyCtxt<'_>, param: &hir::GenericParam<'_>) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/associated-types/defaults-cyclic-fail-1.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B == _`
--> $DIR/defaults-cyclic-fail-1.rs:26:5
--> $DIR/defaults-cyclic-fail-1.rs:26:14
|
LL | type A = Box<Self::B>;
| ^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^

error[E0275]: overflow evaluating the requirement `<usize as Tr>::A == _`
--> $DIR/defaults-cyclic-fail-1.rs:32:5
--> $DIR/defaults-cyclic-fail-1.rs:32:14
|
LL | type B = &'static Self::A;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/associated-types/defaults-cyclic-fail-2.stderr
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B == _`
--> $DIR/defaults-cyclic-fail-2.rs:27:5
--> $DIR/defaults-cyclic-fail-2.rs:27:14
|
LL | type A = Box<Self::B>;
| ^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^

error[E0275]: overflow evaluating the requirement `<usize as Tr>::A == _`
--> $DIR/defaults-cyclic-fail-2.rs:33:5
--> $DIR/defaults-cyclic-fail-2.rs:33:14
|
LL | type B = &'static Self::A;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0275]: overflow evaluating the requirement `<T as Foo>::Item: Sized`
--> $DIR/projection-bound-cycle-generic.rs:44:5
--> $DIR/projection-bound-cycle-generic.rs:44:18
|
LL | struct OnlySized<T> where T: Sized { f: T }
| - required by this bound in `OnlySized`
...
LL | type Assoc = OnlySized<<T as Foo>::Item>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0275]: overflow evaluating the requirement `<T as Foo>::Item: Sized`
--> $DIR/projection-bound-cycle.rs:46:5
--> $DIR/projection-bound-cycle.rs:46:18
|
LL | struct OnlySized<T> where T: Sized { f: T }
| - required by this bound in `OnlySized`
...
LL | type Assoc = OnlySized<<T as Foo>::Item>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-21946.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0275]: overflow evaluating the requirement `<FooStruct as Foo>::A == _`
--> $DIR/issue-21946.rs:8:5
--> $DIR/issue-21946.rs:8:14
|
LL | type A = <FooStruct as Foo>::A;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-23122-1.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0275]: overflow evaluating the requirement `<GetNext<T> as Next>::Next == _`
--> $DIR/issue-23122-1.rs:10:5
--> $DIR/issue-23122-1.rs:10:17
|
LL | type Next = <GetNext<T> as Next>::Next;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-23122-2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0275]: overflow evaluating the requirement `<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<T as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next: Sized`
--> $DIR/issue-23122-2.rs:9:5
--> $DIR/issue-23122-2.rs:9:17
|
LL | type Next = <GetNext<T::Next> as Next>::Next;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a `#![recursion_limit="256"]` attribute to your crate (`issue_23122_2`)
note: required because of the requirements on the impl of `Next` for `GetNext<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<T as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next as Next>::Next>`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0491]: in type `&'a Foo<'b>`, reference has a longer lifetime than the data it references
--> $DIR/regions-outlives-nominal-type-region-rev.rs:17:9
--> $DIR/regions-outlives-nominal-type-region-rev.rs:17:20
|
LL | type Out = &'a Foo<'b>;
| ^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^
|
note: the pointer is valid for the lifetime `'a` as defined on the impl at 16:10
--> $DIR/regions-outlives-nominal-type-region-rev.rs:16:10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0491]: in type `&'a Foo<'b>`, reference has a longer lifetime than the data it references
--> $DIR/regions-outlives-nominal-type-region.rs:17:9
--> $DIR/regions-outlives-nominal-type-region.rs:17:20
|
LL | type Out = &'a Foo<'b>;
| ^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^
|
note: the pointer is valid for the lifetime `'a` as defined on the impl at 16:10
--> $DIR/regions-outlives-nominal-type-region.rs:16:10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0491]: in type `&'a Foo<&'b i32>`, reference has a longer lifetime than the data it references
--> $DIR/regions-outlives-nominal-type-type-rev.rs:17:9
--> $DIR/regions-outlives-nominal-type-type-rev.rs:17:20
|
LL | type Out = &'a Foo<&'b i32>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
note: the pointer is valid for the lifetime `'a` as defined on the impl at 16:10
--> $DIR/regions-outlives-nominal-type-type-rev.rs:16:10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0491]: in type `&'a Foo<&'b i32>`, reference has a longer lifetime than the data it references
--> $DIR/regions-outlives-nominal-type-type.rs:17:9
--> $DIR/regions-outlives-nominal-type-type.rs:17:20
|
LL | type Out = &'a Foo<&'b i32>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
note: the pointer is valid for the lifetime `'a` as defined on the impl at 16:10
--> $DIR/regions-outlives-nominal-type-type.rs:16:10
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/rfc-2093-infer-outlives/regions-struct-not-wf.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-struct-not-wf.rs:13:5
--> $DIR/regions-struct-not-wf.rs:13:16
|
LL | impl<'a, T> Trait<'a, T> for usize {
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | type Out = &'a T;
| ^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
| ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at

error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regions-struct-not-wf.rs:21:5
--> $DIR/regions-struct-not-wf.rs:21:16
|
LL | impl<'a, T> Trait<'a, T> for u32 {
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | type Out = RefOk<'a, T>;
| ^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
| ^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
|
note: ...that is required by this bound
--> $DIR/regions-struct-not-wf.rs:16:20
Expand All @@ -21,10 +21,10 @@ LL | struct RefOk<'a, T:'a> {
| ^^

error[E0491]: in type `&'a &'b T`, reference has a longer lifetime than the data it references
--> $DIR/regions-struct-not-wf.rs:25:5
--> $DIR/regions-struct-not-wf.rs:25:16
|
LL | type Out = &'a &'b T;
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^
|
note: the pointer is valid for the lifetime `'a` as defined on the impl at 24:6
--> $DIR/regions-struct-not-wf.rs:24:6
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/specialization/issue-51892.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: unconstrained generic constant
--> $DIR/issue-51892.rs:15:5
--> $DIR/issue-51892.rs:15:17
|
LL | type Type = [u8; std::mem::size_of::<<T as Trait>::Type>()];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: try adding a `where` bound using this expression: `where [(); std::mem::size_of::<<T as Trait>::Type>()]:`

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/wf/hir-wf-check-erase-regions.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: `&T` is not an iterator
--> $DIR/hir-wf-check-erase-regions.rs:7:5
--> $DIR/hir-wf-check-erase-regions.rs:7:21
|
LL | type IntoIter = std::iter::Flatten<std::slice::Iter<'a, T>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&T` is not an iterator
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&T` is not an iterator
|
::: $SRC_DIR/core/src/iter/adapters/flatten.rs:LL:COL
|
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/wf/wf-impl-associated-type-region.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/wf-impl-associated-type-region.rs:10:5
--> $DIR/wf-impl-associated-type-region.rs:10:16
|
LL | impl<'a, T> Foo<'a> for T {
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | type Bar = &'a T;
| ^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
| ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at

error: aborting due to previous error

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/wf/wf-outlives-ty-in-fn-or-trait.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/wf-outlives-ty-in-fn-or-trait.rs:9:5
--> $DIR/wf-outlives-ty-in-fn-or-trait.rs:9:16
|
LL | impl<'a, T> Trait<'a, T> for usize {
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | type Out = &'a fn(T);
| ^^^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a fn(T)` does not outlive the data it points at
| ^^^^^^^^^ ...so that the reference type `&'a fn(T)` does not outlive the data it points at

error[E0309]: the parameter type `T` may not live long enough
--> $DIR/wf-outlives-ty-in-fn-or-trait.rs:19:5
--> $DIR/wf-outlives-ty-in-fn-or-trait.rs:19:16
|
LL | impl<'a, T> Trait<'a, T> for u32 {
| - help: consider adding an explicit lifetime bound...: `T: 'a`
LL | type Out = &'a dyn Baz<T>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a (dyn Baz<T> + 'a)` does not outlive the data it points at
| ^^^^^^^^^^^^^^ ...so that the reference type `&'a (dyn Baz<T> + 'a)` does not outlive the data it points at

error: aborting due to 2 previous errors

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/wf/wf-trait-associated-type-region.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0309]: the associated type `<Self as SomeTrait<'a>>::Type1` may not live long enough
--> $DIR/wf-trait-associated-type-region.rs:9:5
--> $DIR/wf-trait-associated-type-region.rs:9:18
|
LL | type Type2 = &'a Self::Type1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `<Self as SomeTrait<'a>>::Type1: 'a`...
= note: ...so that the reference type `&'a <Self as SomeTrait<'a>>::Type1` does not outlive the data it points at
Expand Down