Skip to content

Commit e415e2f

Browse files
author
Lukas Markeffsky
committed
fix overlapping spans for explicit_outlives_requirements in macros
also delete trailing comma if necessary
1 parent 31443c6 commit e415e2f

6 files changed

+135
-12
lines changed

compiler/rustc_lint/src/builtin.rs

+30-7
Original file line numberDiff line numberDiff line change
@@ -2173,13 +2173,31 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21732173
dropped_predicate_count += 1;
21742174
}
21752175

2176-
if drop_predicate && !in_where_clause {
2177-
lint_spans.push(predicate_span);
2178-
} else if drop_predicate && i + 1 < num_predicates {
2179-
// If all the bounds on a predicate were inferable and there are
2180-
// further predicates, we want to eat the trailing comma.
2181-
let next_predicate_span = hir_generics.predicates[i + 1].span();
2182-
where_lint_spans.push(predicate_span.to(next_predicate_span.shrink_to_lo()));
2176+
if drop_predicate {
2177+
if !in_where_clause {
2178+
lint_spans.push(predicate_span);
2179+
} else if predicate_span.from_expansion() {
2180+
// Don't try to extend the span if it comes from a macro expansion.
2181+
where_lint_spans.push(predicate_span);
2182+
} else if i + 1 < num_predicates {
2183+
// If all the bounds on a predicate were inferable and there are
2184+
// further predicates, we want to eat the trailing comma.
2185+
let next_predicate_span = hir_generics.predicates[i + 1].span();
2186+
if next_predicate_span.from_expansion() {
2187+
where_lint_spans.push(predicate_span);
2188+
} else {
2189+
where_lint_spans
2190+
.push(predicate_span.to(next_predicate_span.shrink_to_lo()));
2191+
}
2192+
} else {
2193+
// Eat the optional trailing comma after the last predicate.
2194+
let where_span = hir_generics.where_clause_span;
2195+
if where_span.from_expansion() {
2196+
where_lint_spans.push(predicate_span);
2197+
} else {
2198+
where_lint_spans.push(predicate_span.to(where_span.shrink_to_hi()));
2199+
}
2200+
}
21832201
} else {
21842202
where_lint_spans.extend(self.consolidate_outlives_bound_spans(
21852203
predicate_span.shrink_to_lo(),
@@ -2223,6 +2241,11 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22232241
Applicability::MaybeIncorrect
22242242
};
22252243

2244+
// Due to macros, there might be several predicates with the same span
2245+
// and we only want to suggest removing them once.
2246+
lint_spans.sort_unstable();
2247+
lint_spans.dedup();
2248+
22262249
cx.emit_spanned_lint(
22272250
EXPLICIT_OUTLIVES_REQUIREMENTS,
22282251
lint_spans.clone(),

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

+20
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,24 @@ mod unions {
365365
}
366366
}
367367

368+
// https://github.com/rust-lang/rust/issues/106870
369+
mod multiple_predicates_with_same_span {
370+
macro_rules! m {
371+
($($name:ident)+) => {
372+
struct Inline<'a, $($name: 'a,)+>(&'a ($($name,)+));
373+
//~^ ERROR: outlives requirements can be inferred
374+
struct FullWhere<'a, $($name,)+>(&'a ($($name,)+)) where $($name: 'a,)+;
375+
//~^ ERROR: outlives requirements can be inferred
376+
struct PartialWhere<'a, $($name,)+>(&'a ($($name,)+)) where (): Sized, $($name: 'a,)+;
377+
//~^ ERROR: outlives requirements can be inferred
378+
struct Interleaved<'a, $($name,)+>(&'a ($($name,)+))
379+
where
380+
(): Sized,
381+
$($name: 'a, $name: 'a, )+ //~ ERROR: outlives requirements can be inferred
382+
$($name: 'a, $name: 'a, )+;
383+
}
384+
}
385+
m!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
386+
}
387+
368388
fn main() {}

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

+57-1
Original file line numberDiff line numberDiff line change
@@ -819,5 +819,61 @@ LL - union BeeWhereAyTeeYooWhereOutlivesAyIsDebugBee<'a, 'b, T, U> where U:
819819
LL + union BeeWhereAyTeeYooWhereOutlivesAyIsDebugBee<'a, 'b, T, U> where U: Debug, {
820820
|
821821

822-
error: aborting due to 68 previous errors
822+
error: outlives requirements can be inferred
823+
--> $DIR/edition-lint-infer-outlives-multispan.rs:372:38
824+
|
825+
LL | struct Inline<'a, $($name: 'a,)+>(&'a ($($name,)+));
826+
| ^^^^ help: remove these bounds
827+
...
828+
LL | m!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
829+
| --------------------------------------------------------- in this macro invocation
830+
|
831+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
832+
833+
error: outlives requirements can be inferred
834+
--> $DIR/edition-lint-infer-outlives-multispan.rs:374:64
835+
|
836+
LL | struct FullWhere<'a, $($name,)+>(&'a ($($name,)+)) where $($name: 'a,)+;
837+
| ^^^^^^^^^^^^^^^^^^ help: remove these bounds
838+
...
839+
LL | m!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
840+
| --------------------------------------------------------- in this macro invocation
841+
|
842+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
843+
844+
error: outlives requirements can be inferred
845+
--> $DIR/edition-lint-infer-outlives-multispan.rs:376:86
846+
|
847+
LL | struct PartialWhere<'a, $($name,)+>(&'a ($($name,)+)) where (): Sized, $($name: 'a,)+;
848+
| ^^^^^^^^^ help: remove these bounds
849+
...
850+
LL | m!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
851+
| --------------------------------------------------------- in this macro invocation
852+
|
853+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
854+
855+
error: outlives requirements can be inferred
856+
--> $DIR/edition-lint-infer-outlives-multispan.rs:381:19
857+
|
858+
LL | $($name: 'a, $name: 'a, )+
859+
| ^^^^^^^^^ ^^^^^^^^^
860+
LL | $($name: 'a, $name: 'a, )+;
861+
| ^^^^^^^^^ ^^^^^^^^^
862+
...
863+
LL | m!(T0 T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12 T13 T14 T15);
864+
| ---------------------------------------------------------
865+
| |
866+
| in this macro invocation
867+
| in this macro invocation
868+
| in this macro invocation
869+
| in this macro invocation
870+
|
871+
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
872+
help: remove these bounds
873+
|
874+
LL ~ $(, , )+
875+
LL ~ $(, , )+;
876+
|
877+
878+
error: aborting due to 72 previous errors
823879

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

+9
Original file line numberDiff line numberDiff line change
@@ -791,5 +791,14 @@ struct StaticRef<T: 'static> {
791791
field: &'static T
792792
}
793793

794+
struct TrailingCommaInWhereClause<'a, T, U>
795+
where
796+
T: 'a,
797+
798+
//~^ ERROR outlives requirements can be inferred
799+
{
800+
tee: T,
801+
yoo: &'a U
802+
}
794803

795804
fn main() {}

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

+9
Original file line numberDiff line numberDiff line change
@@ -791,5 +791,14 @@ struct StaticRef<T: 'static> {
791791
field: &'static T
792792
}
793793

794+
struct TrailingCommaInWhereClause<'a, T, U>
795+
where
796+
T: 'a,
797+
U: 'a,
798+
//~^ ERROR outlives requirements can be inferred
799+
{
800+
tee: T,
801+
yoo: &'a U
802+
}
794803

795804
fn main() {}

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
error: outlives requirements can be inferred
2-
--> $DIR/edition-lint-infer-outlives.rs:26:31
2+
--> $DIR/edition-lint-infer-outlives.rs:797:5
33
|
4-
LL | struct TeeOutlivesAy<'a, T: 'a> {
5-
| ^^^^ help: remove this bound
4+
LL | U: 'a,
5+
| ^^^^^^ help: remove this bound
66
|
77
note: the lint level is defined here
88
--> $DIR/edition-lint-infer-outlives.rs:4:9
99
|
1010
LL | #![deny(explicit_outlives_requirements)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13+
error: outlives requirements can be inferred
14+
--> $DIR/edition-lint-infer-outlives.rs:26:31
15+
|
16+
LL | struct TeeOutlivesAy<'a, T: 'a> {
17+
| ^^^^ help: remove this bound
18+
1319
error: outlives requirements can be inferred
1420
--> $DIR/edition-lint-infer-outlives.rs:31:40
1521
|
@@ -916,5 +922,5 @@ error: outlives requirements can be inferred
916922
LL | union BeeWhereOutlivesAyTeeWhereDebug<'a, 'b, T> where 'b: 'a, T: Debug {
917923
| ^^^^^^^^ help: remove this bound
918924

919-
error: aborting due to 152 previous errors
925+
error: aborting due to 153 previous errors
920926

0 commit comments

Comments
 (0)