Skip to content

Commit 72531b7

Browse files
committed
Fix explicit-outlives-requirements lint span
1 parent fa8762b commit 72531b7

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,12 +2124,16 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21242124
}
21252125

21262126
let ty_generics = cx.tcx.generics_of(def_id);
2127+
let num_where_predicates = hir_generics
2128+
.predicates
2129+
.iter()
2130+
.filter(|predicate| predicate.in_where_clause())
2131+
.count();
21272132

21282133
let mut bound_count = 0;
21292134
let mut lint_spans = Vec::new();
21302135
let mut where_lint_spans = Vec::new();
2131-
let mut dropped_predicate_count = 0;
2132-
let num_predicates = hir_generics.predicates.len();
2136+
let mut dropped_where_predicate_count = 0;
21332137
for (i, where_predicate) in hir_generics.predicates.iter().enumerate() {
21342138
let (relevant_lifetimes, bounds, predicate_span, in_where_clause) =
21352139
match where_predicate {
@@ -2186,8 +2190,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21862190
bound_count += bound_spans.len();
21872191

21882192
let drop_predicate = bound_spans.len() == bounds.len();
2189-
if drop_predicate {
2190-
dropped_predicate_count += 1;
2193+
if drop_predicate && in_where_clause {
2194+
dropped_where_predicate_count += 1;
21912195
}
21922196

21932197
if drop_predicate {
@@ -2196,7 +2200,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21962200
} else if predicate_span.from_expansion() {
21972201
// Don't try to extend the span if it comes from a macro expansion.
21982202
where_lint_spans.push(predicate_span);
2199-
} else if i + 1 < num_predicates {
2203+
} else if i + 1 < num_where_predicates {
22002204
// If all the bounds on a predicate were inferable and there are
22012205
// further predicates, we want to eat the trailing comma.
22022206
let next_predicate_span = hir_generics.predicates[i + 1].span();
@@ -2224,9 +2228,10 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22242228
}
22252229
}
22262230

2227-
// If all predicates are inferable, drop the entire clause
2231+
// If all predicates in where clause are inferable, drop the entire clause
22282232
// (including the `where`)
2229-
if hir_generics.has_where_clause_predicates && dropped_predicate_count == num_predicates
2233+
if hir_generics.has_where_clause_predicates
2234+
&& dropped_where_predicate_count == num_where_predicates
22302235
{
22312236
let where_span = hir_generics.where_clause_span;
22322237
// Extend the where clause back to the closing `>` of the

tests/ui/rust-2018/edition-lint-infer-outlives.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,4 +801,10 @@ where
801801
yoo: &'a U
802802
}
803803

804+
// https://github.com/rust-lang/rust/issues/105150
805+
struct InferredWhereBoundWithInlineBound<'a, T: ?Sized>
806+
{
807+
data: &'a T,
808+
}
809+
804810
fn main() {}

tests/ui/rust-2018/edition-lint-infer-outlives.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,4 +801,12 @@ where
801801
yoo: &'a U
802802
}
803803

804+
// https://github.com/rust-lang/rust/issues/105150
805+
struct InferredWhereBoundWithInlineBound<'a, T: ?Sized>
806+
//~^ ERROR outlives requirements can be inferred
807+
where T: 'a,
808+
{
809+
data: &'a T,
810+
}
811+
804812
fn main() {}

tests/ui/rust-2018/edition-lint-infer-outlives.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ note: the lint level is defined here
1010
LL | #![deny(explicit_outlives_requirements)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13+
error: outlives requirements can be inferred
14+
--> $DIR/edition-lint-infer-outlives.rs:805:56
15+
|
16+
LL | struct InferredWhereBoundWithInlineBound<'a, T: ?Sized>
17+
| ________________________________________________________^
18+
LL | |
19+
LL | | where T: 'a,
20+
| |________________^ help: remove this bound
21+
1322
error: outlives requirements can be inferred
1423
--> $DIR/edition-lint-infer-outlives.rs:26:31
1524
|
@@ -922,5 +931,5 @@ error: outlives requirements can be inferred
922931
LL | union BeeWhereOutlivesAyTeeWhereDebug<'a, 'b, T> where 'b: 'a, T: Debug {
923932
| ^^^^^^^^ help: remove this bound
924933

925-
error: aborting due to 153 previous errors
934+
error: aborting due to 154 previous errors
926935

0 commit comments

Comments
 (0)