Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0b4f423

Browse files
committedAug 1, 2024
Auto merge of rust-lang#128519 - tgross35:rollup-cq66zmj, r=tgross35
Rollup of 5 pull requests Successful merges: - rust-lang#123994 (Use Default visibility for rustc-generated C symbol declarations) - rust-lang#126818 (Better handle suggestions for the already present code and fix some suggestions) - rust-lang#127624 (Migrate and rename `issue-47551`, `issue-35164` and `issue-69368` `run-make` tests to rmake) - rust-lang#128361 (Migrate `link-cfg` and `rustdoc-default-output` `run-make` tests to rmake) - rust-lang#128436 (Update sysinfo version to 0.31.2) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8e86c95 + c5289b4 commit 0b4f423

File tree

41 files changed

+380
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+380
-197
lines changed
 

‎compiler/rustc_errors/src/emitter.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,10 @@ impl HumanEmitter {
17671767
debug!(?suggestions);
17681768

17691769
if suggestions.is_empty() {
1770-
// Suggestions coming from macros can have malformed spans. This is a heavy handed
1770+
// Here we check if there are suggestions that have actual code changes. We sometimes
1771+
// suggest the same code that is already there, instead of changing how we produce the
1772+
// suggestions and filtering there, we just don't emit the suggestion.
1773+
// Suggestions coming from macros can also have malformed spans. This is a heavy handed
17711774
// approach to avoid ICEs by ignoring the suggestion outright.
17721775
return Ok(());
17731776
}
@@ -2046,7 +2049,9 @@ impl HumanEmitter {
20462049
assert!(underline_start >= 0 && underline_end >= 0);
20472050
let padding: usize = max_line_num_len + 3;
20482051
for p in underline_start..underline_end {
2049-
if let DisplaySuggestion::Underline = show_code_change {
2052+
if let DisplaySuggestion::Underline = show_code_change
2053+
&& is_different(sm, &part.snippet, part.span)
2054+
{
20502055
// If this is a replacement, underline with `~`, if this is an addition
20512056
// underline with `+`.
20522057
buffer.putc(
@@ -2824,6 +2829,18 @@ impl Style {
28242829
}
28252830
}
28262831

2832+
/// Whether the original and suggested code are the same.
2833+
pub fn is_different(sm: &SourceMap, suggested: &str, sp: Span) -> bool {
2834+
let found = match sm.span_to_snippet(sp) {
2835+
Ok(snippet) => snippet,
2836+
Err(e) => {
2837+
warn!(error = ?e, "Invalid span {:?}", sp);
2838+
return true;
2839+
}
2840+
};
2841+
found != suggested
2842+
}
2843+
28272844
/// Whether the original and suggested code are visually similar enough to warrant extra wording.
28282845
pub fn is_case_difference(sm: &SourceMap, suggested: &str, sp: Span) -> bool {
28292846
// FIXME: this should probably be extended to also account for `FO0` → `FOO` and unicode.

‎compiler/rustc_errors/src/lib.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub use diagnostic_impls::{
5050
IndicateAnonymousLifetime, SingleLabelManySpans,
5151
};
5252
pub use emitter::ColorConfig;
53-
use emitter::{is_case_difference, DynEmitter, Emitter};
53+
use emitter::{is_case_difference, is_different, DynEmitter, Emitter};
5454
use registry::Registry;
5555
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
5656
use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
@@ -357,10 +357,16 @@ impl CodeSuggestion {
357357
_ => 1,
358358
})
359359
.sum();
360-
line_highlight.push(SubstitutionHighlight {
361-
start: (cur_lo.col.0 as isize + acc) as usize,
362-
end: (cur_lo.col.0 as isize + acc + len) as usize,
363-
});
360+
if !is_different(sm, &part.snippet, part.span) {
361+
// Account for cases where we are suggesting the same code that's already
362+
// there. This shouldn't happen often, but in some cases for multipart
363+
// suggestions it's much easier to handle it here than in the origin.
364+
} else {
365+
line_highlight.push(SubstitutionHighlight {
366+
start: (cur_lo.col.0 as isize + acc) as usize,
367+
end: (cur_lo.col.0 as isize + acc + len) as usize,
368+
});
369+
}
364370
buf.push_str(&part.snippet);
365371
let cur_hi = sm.lookup_char_pos(part.span.hi());
366372
// Account for the difference between the width of the current code and the
@@ -392,7 +398,11 @@ impl CodeSuggestion {
392398
while buf.ends_with('\n') {
393399
buf.pop();
394400
}
395-
Some((buf, substitution.parts, highlights, only_capitalization))
401+
if highlights.iter().all(|parts| parts.is_empty()) {
402+
None
403+
} else {
404+
Some((buf, substitution.parts, highlights, only_capitalization))
405+
}
396406
})
397407
.collect()
398408
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.