Skip to content

Commit 4b394b2

Browse files
committed
Always point at trait assoc item when generics don't match
Previously we only showed the trait's assoc item if the trait was local, because we were looking for a small span only for the generics, which we don't have for foreign traits. We now use `def_span` for the item, so we at least provide some context, even if its span is too wide. ``` error[E0195]: lifetime parameters or bounds on type `IntoIter` do not match the trait declaration --> tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.rs:7:18 | 7 | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>; | ^^^^ lifetimes do not match type in trait | ::: /home/gh-estebank/rust/library/core/src/iter/traits/collect.rs:292:5 | 292 | type IntoIter: Iterator<Item = Self::Item>; | ------------------------------------------ lifetimes in impl do not match this type in trait ```
1 parent 6fcc9f5 commit 4b394b2

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,14 +1103,14 @@ fn check_region_bounds_on_impl_item<'tcx>(
11031103
.expect("expected impl item to have generics or else we can't compare them")
11041104
.span;
11051105

1106-
let mut generics_span = None;
1106+
let mut generics_span = tcx.def_span(trait_m.def_id);
11071107
let mut bounds_span = vec![];
11081108
let mut where_span = None;
11091109

11101110
if let Some(trait_node) = tcx.hir_get_if_local(trait_m.def_id)
11111111
&& let Some(trait_generics) = trait_node.generics()
11121112
{
1113-
generics_span = Some(trait_generics.span);
1113+
generics_span = trait_generics.span;
11141114
// FIXME: we could potentially look at the impl's bounds to not point at bounds that
11151115
// *are* present in the impl.
11161116
for p in trait_generics.predicates {

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub(crate) struct LifetimesOrBoundsMismatchOnTrait {
190190
#[label]
191191
pub span: Span,
192192
#[label(hir_analysis_generics_label)]
193-
pub generics_span: Option<Span>,
193+
pub generics_span: Span,
194194
#[label(hir_analysis_where_label)]
195195
pub where_span: Option<Span>,
196196
#[label(hir_analysis_bounds_label)]

tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ error[E0195]: lifetime parameters or bounds on associated type `IntoIter` do not
1111
|
1212
LL | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>;
1313
| ^^^^ lifetimes do not match associated type in trait
14+
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
15+
|
16+
= note: lifetimes in impl do not match this associated type in trait
1417

1518
error: aborting due to 2 previous errors
1619

0 commit comments

Comments
 (0)