Skip to content

Commit 9598369

Browse files
authored
Rollup merge of rust-lang#106916 - lukas-code:overlapping-substs, r=estebank
Remove overlapping parts of multipart suggestions This PR adds a debug assertion that the parts of a single substitution cannot overlap, fixes a overlapping substitution from the testsuite, and fixes rust-lang#106870. Note that a single suggestion can still have multiple overlapping substitutions / possible edits, we just don't suggest overlapping replacements in a single edit anymore. I've also included a fix for an unrelated bug where rustfix for `explicit_outlives_requirements` would produce multiple trailing commas for a where clause.
2 parents a7daf9a + d18768a commit 9598369

12 files changed

+184
-47
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,18 +630,27 @@ impl Diagnostic {
630630
style: SuggestionStyle,
631631
) -> &mut Self {
632632
assert!(!suggestion.is_empty());
633-
debug_assert!(
634-
!(suggestion.iter().any(|(sp, text)| sp.is_empty() && text.is_empty())),
635-
"Span must not be empty and have no suggestion"
633+
634+
let mut parts = suggestion
635+
.into_iter()
636+
.map(|(span, snippet)| SubstitutionPart { snippet, span })
637+
.collect::<Vec<_>>();
638+
639+
parts.sort_unstable_by_key(|part| part.span);
640+
641+
debug_assert_eq!(
642+
None,
643+
parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
644+
"Span must not be empty and have no suggestion",
645+
);
646+
debug_assert_eq!(
647+
None,
648+
parts.array_windows().find(|[a, b]| a.span.overlaps(b.span)),
649+
"suggestion must not have overlapping parts",
636650
);
637651

638652
self.push_suggestion(CodeSuggestion {
639-
substitutions: vec![Substitution {
640-
parts: suggestion
641-
.into_iter()
642-
.map(|(span, snippet)| SubstitutionPart { snippet, span })
643-
.collect(),
644-
}],
653+
substitutions: vec![Substitution { parts }],
645654
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
646655
style,
647656
applicability,

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
//! This module contains the code for creating and emitting diagnostics.
44
55
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
6+
#![feature(array_windows)]
67
#![feature(drain_filter)]
78
#![feature(if_let_guard)]
89
#![feature(is_terminal)]

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::mbe::transcribe::transcribe;
1010

1111
use rustc_ast as ast;
1212
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind, TokenKind::*};
13-
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
13+
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
1414
use rustc_ast::{NodeId, DUMMY_NODE_ID};
1515
use rustc_ast_pretty::pprust;
1616
use rustc_attr::{self as attr, TransparencyError};
@@ -212,7 +212,6 @@ fn expand_macro<'cx>(
212212
};
213213
let arm_span = rhses[i].span();
214214

215-
let rhs_spans = rhs.tts.iter().map(|t| t.span()).collect::<Vec<_>>();
216215
// rhs has holes ( `$id` and `$(...)` that need filled)
217216
let mut tts = match transcribe(cx, &named_matches, &rhs, rhs_span, transparency) {
218217
Ok(tts) => tts,
@@ -224,12 +223,25 @@ fn expand_macro<'cx>(
224223

225224
// Replace all the tokens for the corresponding positions in the macro, to maintain
226225
// proper positions in error reporting, while maintaining the macro_backtrace.
227-
if rhs_spans.len() == tts.len() {
226+
if tts.len() == rhs.tts.len() {
228227
tts = tts.map_enumerated(|i, tt| {
229228
let mut tt = tt.clone();
230-
let mut sp = rhs_spans[i];
231-
sp = sp.with_ctxt(tt.span().ctxt());
232-
tt.set_span(sp);
229+
let rhs_tt = &rhs.tts[i];
230+
let ctxt = tt.span().ctxt();
231+
match (&mut tt, rhs_tt) {
232+
// preserve the delim spans if able
233+
(
234+
TokenTree::Delimited(target_sp, ..),
235+
mbe::TokenTree::Delimited(source_sp, ..),
236+
) => {
237+
target_sp.open = source_sp.open.with_ctxt(ctxt);
238+
target_sp.close = source_sp.close.with_ctxt(ctxt);
239+
}
240+
_ => {
241+
let sp = rhs_tt.span().with_ctxt(ctxt);
242+
tt.set_span(sp);
243+
}
244+
}
233245
tt
234246
});
235247
}

compiler/rustc_lint/src/builtin.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,8 +2109,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21092109
let ty_generics = cx.tcx.generics_of(def_id);
21102110

21112111
let mut bound_count = 0;
2112-
let mut lint_spans = Vec::new();
2113-
let mut where_lint_spans = Vec::new();
2112+
let mut lint_spans = FxHashSet::default();
2113+
let mut where_lint_spans = FxHashSet::default();
21142114
let mut dropped_predicate_count = 0;
21152115
let num_predicates = hir_generics.predicates.len();
21162116
for (i, where_predicate) in hir_generics.predicates.iter().enumerate() {
@@ -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.insert(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.insert(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.insert(predicate_span);
2188+
} else {
2189+
where_lint_spans
2190+
.insert(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.insert(predicate_span);
2197+
} else {
2198+
where_lint_spans.insert(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(),
@@ -2206,7 +2224,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22062224

22072225
// Due to macro expansions, the `full_where_span` might not actually contain all predicates.
22082226
if where_lint_spans.iter().all(|&sp| full_where_span.contains(sp)) {
2209-
lint_spans.push(full_where_span);
2227+
lint_spans.insert(full_where_span);
22102228
} else {
22112229
lint_spans.extend(where_lint_spans);
22122230
}
@@ -2223,6 +2241,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22232241
Applicability::MaybeIncorrect
22242242
};
22252243

2244+
let lint_spans = lint_spans.into_iter().collect::<Vec<_>>();
2245+
22262246
cx.emit_spanned_lint(
22272247
EXPLICIT_OUTLIVES_REQUIREMENTS,
22282248
lint_spans.clone(),

tests/ui/const-generics/min_const_generics/macro-fail.stderr

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | fn make_marker() -> impl Marker<gimme_a_const!(marker)> {
88
| in this macro invocation
99
...
1010
LL | ($rusty: ident) => {{ let $rusty = 3; *&$rusty }}
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type
11+
| ^ expected type
1212
|
1313
= note: this error originates in the macro `gimme_a_const` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

@@ -22,26 +22,21 @@ LL | Example::<gimme_a_const!(marker)>
2222
| in this macro invocation
2323
...
2424
LL | ($rusty: ident) => {{ let $rusty = 3; *&$rusty }}
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type
25+
| ^ expected type
2626
|
2727
= note: this error originates in the macro `gimme_a_const` (in Nightly builds, run with -Z macro-backtrace for more info)
2828

2929
error: expected type, found `{`
3030
--> $DIR/macro-fail.rs:4:10
3131
|
32-
LL | () => {{
33-
| __________^
34-
LL | |
35-
LL | | const X: usize = 1337;
36-
LL | | X
37-
LL | | }}
38-
| |___^ expected type
32+
LL | () => {{
33+
| ^ expected type
3934
...
40-
LL | let _fail = Example::<external_macro!()>;
41-
| -----------------
42-
| |
43-
| this macro call doesn't expand to a type
44-
| in this macro invocation
35+
LL | let _fail = Example::<external_macro!()>;
36+
| -----------------
37+
| |
38+
| this macro call doesn't expand to a type
39+
| in this macro invocation
4540
|
4641
= note: this error originates in the macro `external_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
4742

tests/ui/imports/import-prefix-macro-1.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: expected one of `::`, `;`, or `as`, found `{`
22
--> $DIR/import-prefix-macro-1.rs:11:27
33
|
44
LL | ($p: path) => (use $p {S, Z});
5-
| ^^^^^^ expected one of `::`, `;`, or `as`
5+
| ^ expected one of `::`, `;`, or `as`
66
...
77
LL | import! { a::b::c }
88
| ------------------- in this macro invocation

tests/ui/parser/issues/issue-44406.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ LL | foo!(true);
2121
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
2222
help: if `bar` is a struct, use braces as delimiters
2323
|
24-
LL | bar { }
25-
| ~
24+
LL | bar { baz: $rest }
25+
| ~ ~
2626
help: if `bar` is a function, use the arguments directly
2727
|
2828
LL - bar(baz: $rest)

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

Lines changed: 20 additions & 0 deletions
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

Lines changed: 57 additions & 1 deletion
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 10 additions & 4 deletions
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)