Skip to content

Commit 119ea42

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 2c7160b commit 119ea42

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1125,13 +1125,13 @@ fn check_region_bounds_on_impl_item<'tcx>(
11251125
.expect("expected impl item to have generics or else we can't compare them")
11261126
.span;
11271127

1128-
let mut generics_span = None;
1128+
let mut generics_span = tcx.def_span(trait_m.def_id);
11291129
let mut bounds_span = vec![];
11301130
let mut where_span = None;
11311131
if let Some(trait_node) = tcx.hir_get_if_local(trait_m.def_id)
11321132
&& let Some(trait_generics) = trait_node.generics()
11331133
{
1134-
generics_span = Some(trait_generics.span);
1134+
generics_span = trait_generics.span;
11351135
// FIXME: we could potentially look at the impl's bounds to not point at bounds that
11361136
// *are* present in the impl.
11371137
for p in trait_generics.predicates {

compiler/rustc_hir_analysis/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub(crate) struct LifetimesOrBoundsMismatchOnTrait {
198198
#[label]
199199
pub span: Span,
200200
#[label(hir_analysis_generics_label)]
201-
pub generics_span: Option<Span>,
201+
pub generics_span: Span,
202202
#[label(hir_analysis_where_label)]
203203
pub where_span: Option<Span>,
204204
#[label(hir_analysis_bounds_label)]

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

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

1518
error: aborting due to 2 previous errors
1619

0 commit comments

Comments
 (0)