Skip to content

Commit 917bfa7

Browse files
committed
Auto merge of rust-lang#134795 - GuillaumeGomez:rollup-9x8n7pi, r=GuillaumeGomez
Rollup of 4 pull requests Successful merges: - rust-lang#134656 (Migrate `incr-add-rust-src-component` to rmake) - rust-lang#134664 (Account for removal of multiline span in suggestion) - rust-lang#134772 (Improve/cleanup rustdoc code) - rust-lang#134781 (Add more `begin_panic` normalizations to panic backtrace tests) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 19e75f4 + 3d50eba commit 917bfa7

26 files changed

+806
-81
lines changed

compiler/rustc_errors/src/emitter.rs

+79-7
Original file line numberDiff line numberDiff line change
@@ -2216,6 +2216,11 @@ impl HumanEmitter {
22162216
show_code_change
22172217
{
22182218
for part in parts {
2219+
let snippet = if let Ok(snippet) = sm.span_to_snippet(part.span) {
2220+
snippet
2221+
} else {
2222+
String::new()
2223+
};
22192224
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
22202225
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;
22212226

@@ -2263,13 +2268,80 @@ impl HumanEmitter {
22632268
}
22642269
if let DisplaySuggestion::Diff = show_code_change {
22652270
// Colorize removal with red in diff format.
2266-
buffer.set_style_range(
2267-
row_num - 2,
2268-
(padding as isize + span_start_pos as isize) as usize,
2269-
(padding as isize + span_end_pos as isize) as usize,
2270-
Style::Removal,
2271-
true,
2272-
);
2271+
2272+
// Below, there's some tricky buffer indexing going on. `row_num` at this
2273+
// point corresponds to:
2274+
//
2275+
// |
2276+
// LL | CODE
2277+
// | ++++ <- `row_num`
2278+
//
2279+
// in the buffer. When we have a diff format output, we end up with
2280+
//
2281+
// |
2282+
// LL - OLDER <- row_num - 2
2283+
// LL + NEWER
2284+
// | <- row_num
2285+
//
2286+
// The `row_num - 2` is to select the buffer line that has the "old version
2287+
// of the diff" at that point. When the removal is a single line, `i` is
2288+
// `0`, `newlines` is `1` so `(newlines - i - 1)` ends up being `0`, so row
2289+
// points at `LL - OLDER`. When the removal corresponds to multiple lines,
2290+
// we end up with `newlines > 1` and `i` being `0..newlines - 1`.
2291+
//
2292+
// |
2293+
// LL - OLDER <- row_num - 2 - (newlines - last_i - 1)
2294+
// LL - CODE
2295+
// LL - BEING
2296+
// LL - REMOVED <- row_num - 2 - (newlines - first_i - 1)
2297+
// LL + NEWER
2298+
// | <- row_num
2299+
2300+
let newlines = snippet.lines().count();
2301+
if newlines > 0 && row_num > newlines {
2302+
// Account for removals where the part being removed spans multiple
2303+
// lines.
2304+
// FIXME: We check the number of rows because in some cases, like in
2305+
// `tests/ui/lint/invalid-nan-comparison-suggestion.rs`, the rendered
2306+
// suggestion will only show the first line of code being replaced. The
2307+
// proper way of doing this would be to change the suggestion rendering
2308+
// logic to show the whole prior snippet, but the current output is not
2309+
// too bad to begin with, so we side-step that issue here.
2310+
for (i, line) in snippet.lines().enumerate() {
2311+
let line = normalize_whitespace(line);
2312+
let row = row_num - 2 - (newlines - i - 1);
2313+
// On the first line, we highlight between the start of the part
2314+
// span, and the end of that line.
2315+
// On the last line, we highlight between the start of the line, and
2316+
// the column of the part span end.
2317+
// On all others, we highlight the whole line.
2318+
let start = if i == 0 {
2319+
(padding as isize + span_start_pos as isize) as usize
2320+
} else {
2321+
padding
2322+
};
2323+
let end = if i == 0 {
2324+
(padding as isize
2325+
+ span_start_pos as isize
2326+
+ line.len() as isize)
2327+
as usize
2328+
} else if i == newlines - 1 {
2329+
(padding as isize + span_end_pos as isize) as usize
2330+
} else {
2331+
(padding as isize + line.len() as isize) as usize
2332+
};
2333+
buffer.set_style_range(row, start, end, Style::Removal, true);
2334+
}
2335+
} else {
2336+
// The removed code fits all in one line.
2337+
buffer.set_style_range(
2338+
row_num - 2,
2339+
(padding as isize + span_start_pos as isize) as usize,
2340+
(padding as isize + span_end_pos as isize) as usize,
2341+
Style::Removal,
2342+
true,
2343+
);
2344+
}
22732345
}
22742346

22752347
// length of the code after substitution

src/librustdoc/clean/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ fn clean_ty_generics<'tcx>(
894894

895895
// Add back a `Sized` bound if there are no *trait* bounds remaining (incl. `?Sized`).
896896
// Since all potential trait bounds are at the front we can just check the first bound.
897-
if bounds.first().map_or(true, |b| !b.is_trait_bound()) {
897+
if bounds.first().is_none_or(|b| !b.is_trait_bound()) {
898898
bounds.insert(0, GenericBound::sized(cx));
899899
}
900900

@@ -1811,7 +1811,7 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
18111811
}
18121812
TyKind::Slice(ty) => Slice(Box::new(clean_ty(ty, cx))),
18131813
TyKind::Pat(ty, pat) => Type::Pat(Box::new(clean_ty(ty, cx)), format!("{pat:?}").into()),
1814-
TyKind::Array(ty, ref const_arg) => {
1814+
TyKind::Array(ty, const_arg) => {
18151815
// NOTE(min_const_generics): We can't use `const_eval_poly` for constants
18161816
// as we currently do not supply the parent generics to anonymous constants
18171817
// but do allow `ConstKind::Param`.
@@ -2337,7 +2337,7 @@ fn clean_middle_opaque_bounds<'tcx>(
23372337

23382338
// Add back a `Sized` bound if there are no *trait* bounds remaining (incl. `?Sized`).
23392339
// Since all potential trait bounds are at the front we can just check the first bound.
2340-
if bounds.first().map_or(true, |b| !b.is_trait_bound()) {
2340+
if bounds.first().is_none_or(|b| !b.is_trait_bound()) {
23412341
bounds.insert(0, GenericBound::sized(cx));
23422342
}
23432343

src/librustdoc/doctest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
220220
} = interface::run_compiler(config, |compiler| {
221221
let krate = rustc_interface::passes::parse(&compiler.sess);
222222

223-
let collector = rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
223+
let collector = rustc_interface::create_and_enter_global_ctxt(compiler, krate, |tcx| {
224224
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
225225
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
226226
let opts = scrape_test_config(crate_name, crate_attrs, args_path);

src/librustdoc/doctest/make.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ fn handle_attr(mod_attr_pending: &mut String, source_info: &mut SourceInfo, edit
538538
// If it's complete, then we can clear the pending content.
539539
mod_attr_pending.clear();
540540
} else {
541-
mod_attr_pending.push_str("\n");
541+
mod_attr_pending.push('\n');
542542
}
543543
}
544544

src/librustdoc/formats/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ impl DocFolder for CacheBuilder<'_, '_> {
413413
let impl_item = Impl { impl_item: item };
414414
let impl_did = impl_item.def_id();
415415
let trait_did = impl_item.trait_did();
416-
if trait_did.map_or(true, |d| self.cache.traits.contains_key(&d)) {
416+
if trait_did.is_none_or(|d| self.cache.traits.contains_key(&d)) {
417417
for did in dids {
418418
if self.impl_ids.entry(did).or_default().insert(impl_did) {
419419
self.cache.impls.entry(did).or_default().push(impl_item.clone());

src/librustdoc/html/escape.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ impl fmt::Display for EscapeBodyTextWithWbr<'_> {
104104
continue;
105105
}
106106
let is_uppercase = || s.chars().any(|c| c.is_uppercase());
107-
let next_is_uppercase =
108-
|| pk.map_or(true, |(_, t)| t.chars().any(|c| c.is_uppercase()));
109-
let next_is_underscore = || pk.map_or(true, |(_, t)| t.contains('_'));
110-
let next_is_colon = || pk.map_or(true, |(_, t)| t.contains(':'));
107+
let next_is_uppercase = || pk.is_none_or(|(_, t)| t.chars().any(|c| c.is_uppercase()));
108+
let next_is_underscore = || pk.is_none_or(|(_, t)| t.contains('_'));
109+
let next_is_colon = || pk.is_none_or(|(_, t)| t.contains(':'));
111110
// Check for CamelCase.
112111
//
113112
// `i - last > 3` avoids turning FmRadio into Fm<wbr>Radio, which is technically

src/librustdoc/html/markdown.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for SpannedLinkReplacer<
480480
type Item = SpannedEvent<'a>;
481481

482482
fn next(&mut self) -> Option<Self::Item> {
483-
let Some((mut event, range)) = self.iter.next() else { return None };
483+
let (mut event, range) = self.iter.next()?;
484484
self.inner.handle_event(&mut event);
485485
// Yield the modified event
486486
Some((event, range))
@@ -2039,7 +2039,7 @@ impl IdMap {
20392039
let candidate = candidate.to_string();
20402040
if is_default_id(&candidate) {
20412041
let id = format!("{}-{}", candidate, 1);
2042-
self.map.insert(candidate.into(), 2);
2042+
self.map.insert(candidate, 2);
20432043
id
20442044
} else {
20452045
candidate
@@ -2052,7 +2052,7 @@ impl IdMap {
20522052
}
20532053
};
20542054

2055-
self.map.insert(id.clone().into(), 1);
2055+
self.map.insert(id.clone(), 1);
20562056
id
20572057
}
20582058

src/librustdoc/html/render/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
748748
&shared.layout,
749749
&page,
750750
"",
751-
scrape_examples_help(&shared),
751+
scrape_examples_help(shared),
752752
&shared.style_files,
753753
);
754754
shared.fs.write(scrape_examples_help_file, v)?;

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,7 @@ fn render_impl(
19741974
.opt_doc_value()
19751975
.map(|dox| {
19761976
Markdown {
1977-
content: &*dox,
1977+
content: &dox,
19781978
links: &i.impl_item.links(cx),
19791979
ids: &mut cx.id_map.borrow_mut(),
19801980
error_codes: cx.shared.codes,

src/librustdoc/html/render/print_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
14201420
ty: &'a clean::Type,
14211421
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
14221422
display_fn(move |f| {
1423-
let v = ty.print(&self.cx);
1423+
let v = ty.print(self.cx);
14241424
write!(f, "{v}")
14251425
})
14261426
}

src/librustdoc/json/conversions.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,15 @@ fn from_clean_item(item: clean::Item, renderer: &JsonRenderer<'_>) -> ItemEnum {
312312
StructFieldItem(f) => ItemEnum::StructField(f.into_json(renderer)),
313313
EnumItem(e) => ItemEnum::Enum(e.into_json(renderer)),
314314
VariantItem(v) => ItemEnum::Variant(v.into_json(renderer)),
315-
FunctionItem(f) => ItemEnum::Function(from_function(f, true, header.unwrap(), renderer)),
315+
FunctionItem(f) => ItemEnum::Function(from_function(*f, true, header.unwrap(), renderer)),
316316
ForeignFunctionItem(f, _) => {
317-
ItemEnum::Function(from_function(f, false, header.unwrap(), renderer))
317+
ItemEnum::Function(from_function(*f, false, header.unwrap(), renderer))
318318
}
319319
TraitItem(t) => ItemEnum::Trait((*t).into_json(renderer)),
320320
TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_json(renderer)),
321-
MethodItem(m, _) => ItemEnum::Function(from_function(m, true, header.unwrap(), renderer)),
321+
MethodItem(m, _) => ItemEnum::Function(from_function(*m, true, header.unwrap(), renderer)),
322322
RequiredMethodItem(m) => {
323-
ItemEnum::Function(from_function(m, false, header.unwrap(), renderer))
323+
ItemEnum::Function(from_function(*m, false, header.unwrap(), renderer))
324324
}
325325
ImplItem(i) => ItemEnum::Impl((*i).into_json(renderer)),
326326
StaticItem(s) => ItemEnum::Static(convert_static(s, rustc_hir::Safety::Safe, renderer)),
@@ -730,12 +730,11 @@ impl FromClean<clean::Impl> for Impl {
730730
}
731731

732732
pub(crate) fn from_function(
733-
function: Box<clean::Function>,
733+
clean::Function { decl, generics }: clean::Function,
734734
has_body: bool,
735735
header: rustc_hir::FnHeader,
736736
renderer: &JsonRenderer<'_>,
737737
) -> Function {
738-
let clean::Function { decl, generics } = *function;
739738
Function {
740739
sig: decl.into_json(renderer),
741740
generics: generics.into_json(renderer),

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ fn main_args(
869869
sess.dcx().fatal("Compilation failed, aborting rustdoc");
870870
}
871871

872-
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
872+
rustc_interface::create_and_enter_global_ctxt(compiler, krate, |tcx| {
873873
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
874874
core::run_global_ctxt(tcx, show_coverage, render_options, output_format)
875875
});

src/librustdoc/passes/collect_intra_doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ fn resolution_failure(
19771977
}
19781978

19791979
if !path_str.contains("::") {
1980-
if disambiguator.map_or(true, |d| d.ns() == MacroNS)
1980+
if disambiguator.is_none_or(|d| d.ns() == MacroNS)
19811981
&& collector
19821982
.cx
19831983
.tcx

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
run-make/branch-protection-check-IBT/Makefile
22
run-make/cat-and-grep-sanity-check/Makefile
33
run-make/extern-fn-reachable/Makefile
4-
run-make/incr-add-rust-src-component/Makefile
54
run-make/jobserver-error/Makefile
65
run-make/libs-through-symlinks/Makefile
76
run-make/split-debuginfo/Makefile

tests/run-make/incr-add-rust-src-component/Makefile

-45
This file was deleted.

0 commit comments

Comments
 (0)