Skip to content

Commit 1d30a2e

Browse files
authored
Rollup merge of #102922 - kper:bugfix/102902-filtering-json, r=oli-obk
Filtering spans when emitting json According to the issue #102902, we shouldn't emit spans which have an empty span and no suggested replacement.
2 parents a03ca01 + 9e2c2a5 commit 1d30a2e

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ impl Diagnostic {
567567
style: SuggestionStyle,
568568
) -> &mut Self {
569569
assert!(!suggestion.is_empty());
570+
debug_assert!(
571+
!(suggestion.iter().any(|(sp, text)| sp.is_empty() && text.is_empty())),
572+
"Span must not be empty and have no suggestion"
573+
);
574+
570575
self.push_suggestion(CodeSuggestion {
571576
substitutions: vec![Substitution {
572577
parts: suggestion
@@ -644,6 +649,10 @@ impl Diagnostic {
644649
applicability: Applicability,
645650
style: SuggestionStyle,
646651
) -> &mut Self {
652+
debug_assert!(
653+
!(sp.is_empty() && suggestion.to_string().is_empty()),
654+
"Span must not be empty and have no suggestion"
655+
);
647656
self.push_suggestion(CodeSuggestion {
648657
substitutions: vec![Substitution {
649658
parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
@@ -684,6 +693,12 @@ impl Diagnostic {
684693
) -> &mut Self {
685694
let mut suggestions: Vec<_> = suggestions.collect();
686695
suggestions.sort();
696+
697+
debug_assert!(
698+
!(sp.is_empty() && suggestions.iter().any(|suggestion| suggestion.is_empty())),
699+
"Span must not be empty and have no suggestion"
700+
);
701+
687702
let substitutions = suggestions
688703
.into_iter()
689704
.map(|snippet| Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] })
@@ -705,8 +720,18 @@ impl Diagnostic {
705720
suggestions: impl Iterator<Item = Vec<(Span, String)>>,
706721
applicability: Applicability,
707722
) -> &mut Self {
723+
let suggestions: Vec<_> = suggestions.collect();
724+
debug_assert!(
725+
!(suggestions
726+
.iter()
727+
.flat_map(|suggs| suggs)
728+
.any(|(sp, suggestion)| sp.is_empty() && suggestion.is_empty())),
729+
"Span must not be empty and have no suggestion"
730+
);
731+
708732
self.push_suggestion(CodeSuggestion {
709733
substitutions: suggestions
734+
.into_iter()
710735
.map(|sugg| Substitution {
711736
parts: sugg
712737
.into_iter()

compiler/rustc_expand/src/base.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_span::edition::Edition;
2222
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
2323
use rustc_span::source_map::SourceMap;
2424
use rustc_span::symbol::{kw, sym, Ident, Symbol};
25-
use rustc_span::{FileName, Span, DUMMY_SP};
25+
use rustc_span::{BytePos, FileName, Span, DUMMY_SP};
2626
use smallvec::{smallvec, SmallVec};
2727

2828
use std::default::Default;
@@ -1228,8 +1228,9 @@ pub fn expr_to_spanned_string<'a>(
12281228
ast::LitKind::Str(s, style) => return Ok((s, style, expr.span)),
12291229
ast::LitKind::ByteStr(_) => {
12301230
let mut err = cx.struct_span_err(l.span, err_msg);
1231+
let span = expr.span.shrink_to_lo();
12311232
err.span_suggestion(
1232-
expr.span.shrink_to_lo(),
1233+
span.with_hi(span.lo() + BytePos(1)),
12331234
"consider removing the leading `b`",
12341235
"",
12351236
Applicability::MaybeIncorrect,

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,24 +3051,27 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
30513051
.map_or(false, |s| s.trim_end().ends_with('<'));
30523052

30533053
let is_global = poly_trait_ref.trait_ref.path.is_global();
3054-
let sugg = Vec::from_iter([
3055-
(
3056-
self_ty.span.shrink_to_lo(),
3057-
format!(
3058-
"{}dyn {}",
3059-
if needs_bracket { "<" } else { "" },
3060-
if is_global { "(" } else { "" },
3061-
),
3054+
3055+
let mut sugg = Vec::from_iter([(
3056+
self_ty.span.shrink_to_lo(),
3057+
format!(
3058+
"{}dyn {}",
3059+
if needs_bracket { "<" } else { "" },
3060+
if is_global { "(" } else { "" },
30623061
),
3063-
(
3062+
)]);
3063+
3064+
if is_global || needs_bracket {
3065+
sugg.push((
30643066
self_ty.span.shrink_to_hi(),
30653067
format!(
30663068
"{}{}",
30673069
if is_global { ")" } else { "" },
30683070
if needs_bracket { ">" } else { "" },
30693071
),
3070-
),
3071-
]);
3072+
));
3073+
}
3074+
30723075
if self_ty.span.edition() >= Edition::Edition2021 {
30733076
let msg = "trait objects must include the `dyn` keyword";
30743077
let label = "add `dyn` keyword before this trait";

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,9 +1374,17 @@ impl<'a> Parser<'a> {
13741374
kind: IncDecRecovery,
13751375
(pre_span, post_span): (Span, Span),
13761376
) -> MultiSugg {
1377+
let mut patches = Vec::new();
1378+
1379+
if !pre_span.is_empty() {
1380+
patches.push((pre_span, String::new()));
1381+
}
1382+
1383+
patches.push((post_span, format!(" {}= 1", kind.op.chr())));
1384+
13771385
MultiSugg {
13781386
msg: format!("use `{}= 1` instead", kind.op.chr()),
1379-
patches: vec![(pre_span, String::new()), (post_span, format!(" {}= 1", kind.op.chr()))],
1387+
patches,
13801388
applicability: Applicability::MachineApplicable,
13811389
}
13821390
}

0 commit comments

Comments
 (0)