Skip to content
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

Fix explicit-outlives-requirements lint span #112612

Merged
merged 1 commit into from
Jun 14, 2023
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
19 changes: 12 additions & 7 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2124,12 +2124,16 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
}

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 {
Expand Down Expand Up @@ -2186,8 +2190,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
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 {
Expand All @@ -2196,7 +2200,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
} 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();
Expand Down Expand Up @@ -2224,9 +2228,10 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
}
}

// 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
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/rust-2018/edition-lint-infer-outlives.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
8 changes: 8 additions & 0 deletions tests/ui/rust-2018/edition-lint-infer-outlives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,4 +801,12 @@ where
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() {}
11 changes: 10 additions & 1 deletion tests/ui/rust-2018/edition-lint-infer-outlives.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
|
Expand Down Expand Up @@ -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