Rollup merge of #112612 - sginnett:issue-105150, r=compiler-errors

Fix explicit-outlives-requirements lint span

Fixes #105150 which caused the span reported by the explicit-outlives-requirements lint to be incorrect when
1) the lint should suggest the entire where clause to be removed and
2) there are inline bounds present that are not inferable outlives requirements

In particular, this would cause rustfix to leave a dangling empty where clause.
This commit is contained in:
Matthias Krüger 2023-06-14 18:10:31 +02:00 committed by GitHub
commit 41d5aeccec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 8 deletions

View File

@ -2124,12 +2124,16 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
}
let ty_generics = cx.tcx.generics_of(def_id);
let num_where_predicates = hir_generics
.predicates
.iter()
.filter(|predicate| predicate.in_where_clause())
.count();
let mut bound_count = 0;
let mut lint_spans = Vec::new();
let mut where_lint_spans = Vec::new();
let mut dropped_predicate_count = 0;
let num_predicates = hir_generics.predicates.len();
let mut dropped_where_predicate_count = 0;
for (i, where_predicate) in hir_generics.predicates.iter().enumerate() {
let (relevant_lifetimes, bounds, predicate_span, in_where_clause) =
match where_predicate {
@ -2186,8 +2190,8 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
bound_count += bound_spans.len();
let drop_predicate = bound_spans.len() == bounds.len();
if drop_predicate {
dropped_predicate_count += 1;
if drop_predicate && in_where_clause {
dropped_where_predicate_count += 1;
}
if drop_predicate {
@ -2196,7 +2200,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
} else if predicate_span.from_expansion() {
// Don't try to extend the span if it comes from a macro expansion.
where_lint_spans.push(predicate_span);
} else if i + 1 < num_predicates {
} else if i + 1 < num_where_predicates {
// If all the bounds on a predicate were inferable and there are
// further predicates, we want to eat the trailing comma.
let next_predicate_span = hir_generics.predicates[i + 1].span();
@ -2224,9 +2228,10 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
}
}
// If all predicates are inferable, drop the entire clause
// If all predicates in where clause are inferable, drop the entire clause
// (including the `where`)
if hir_generics.has_where_clause_predicates && dropped_predicate_count == num_predicates
if hir_generics.has_where_clause_predicates
&& dropped_where_predicate_count == num_where_predicates
{
let where_span = hir_generics.where_clause_span;
// Extend the where clause back to the closing `>` of the

View File

@ -801,4 +801,10 @@ where
yoo: &'a U
}
// https://github.com/rust-lang/rust/issues/105150
struct InferredWhereBoundWithInlineBound<'a, T: ?Sized>
{
data: &'a T,
}
fn main() {}

View File

@ -801,4 +801,12 @@ struct TrailingCommaInWhereClause<'a, T, U>
yoo: &'a U
}
// https://github.com/rust-lang/rust/issues/105150
struct InferredWhereBoundWithInlineBound<'a, T: ?Sized>
//~^ ERROR outlives requirements can be inferred
where T: 'a,
{
data: &'a T,
}
fn main() {}

View File

@ -10,6 +10,15 @@ note: the lint level is defined here
LL | #![deny(explicit_outlives_requirements)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: outlives requirements can be inferred
--> $DIR/edition-lint-infer-outlives.rs:805:56
|
LL | struct InferredWhereBoundWithInlineBound<'a, T: ?Sized>
| ________________________________________________________^
LL | |
LL | | where T: 'a,
| |________________^ help: remove this bound
error: outlives requirements can be inferred
--> $DIR/edition-lint-infer-outlives.rs:26:31
|
@ -922,5 +931,5 @@ error: outlives requirements can be inferred
LL | union BeeWhereOutlivesAyTeeWhereDebug<'a, 'b, T> where 'b: 'a, T: Debug {
| ^^^^^^^^ help: remove this bound
error: aborting due to 153 previous errors
error: aborting due to 154 previous errors