Skip to content

Rollup of 4 pull requests #134795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 26, 2024
86 changes: 79 additions & 7 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2216,6 +2216,11 @@ impl HumanEmitter {
show_code_change
{
for part in parts {
let snippet = if let Ok(snippet) = sm.span_to_snippet(part.span) {
snippet
} else {
String::new()
};
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
let span_end_pos = sm.lookup_char_pos(part.span.hi()).col_display;

Expand Down Expand Up @@ -2263,13 +2268,80 @@ impl HumanEmitter {
}
if let DisplaySuggestion::Diff = show_code_change {
// Colorize removal with red in diff format.
buffer.set_style_range(
row_num - 2,
(padding as isize + span_start_pos as isize) as usize,
(padding as isize + span_end_pos as isize) as usize,
Style::Removal,
true,
);

// Below, there's some tricky buffer indexing going on. `row_num` at this
// point corresponds to:
//
// |
// LL | CODE
// | ++++ <- `row_num`
//
// in the buffer. When we have a diff format output, we end up with
//
// |
// LL - OLDER <- row_num - 2
// LL + NEWER
// | <- row_num
//
// The `row_num - 2` is to select the buffer line that has the "old version
// of the diff" at that point. When the removal is a single line, `i` is
// `0`, `newlines` is `1` so `(newlines - i - 1)` ends up being `0`, so row
// points at `LL - OLDER`. When the removal corresponds to multiple lines,
// we end up with `newlines > 1` and `i` being `0..newlines - 1`.
//
// |
// LL - OLDER <- row_num - 2 - (newlines - last_i - 1)
// LL - CODE
// LL - BEING
// LL - REMOVED <- row_num - 2 - (newlines - first_i - 1)
// LL + NEWER
// | <- row_num

let newlines = snippet.lines().count();
if newlines > 0 && row_num > newlines {
// Account for removals where the part being removed spans multiple
// lines.
// FIXME: We check the number of rows because in some cases, like in
// `tests/ui/lint/invalid-nan-comparison-suggestion.rs`, the rendered
// suggestion will only show the first line of code being replaced. The
// proper way of doing this would be to change the suggestion rendering
// logic to show the whole prior snippet, but the current output is not
// too bad to begin with, so we side-step that issue here.
for (i, line) in snippet.lines().enumerate() {
let line = normalize_whitespace(line);
let row = row_num - 2 - (newlines - i - 1);
// On the first line, we highlight between the start of the part
// span, and the end of that line.
// On the last line, we highlight between the start of the line, and
// the column of the part span end.
// On all others, we highlight the whole line.
let start = if i == 0 {
(padding as isize + span_start_pos as isize) as usize
} else {
padding
};
let end = if i == 0 {
(padding as isize
+ span_start_pos as isize
+ line.len() as isize)
as usize
} else if i == newlines - 1 {
(padding as isize + span_end_pos as isize) as usize
} else {
(padding as isize + line.len() as isize) as usize
};
buffer.set_style_range(row, start, end, Style::Removal, true);
}
} else {
// The removed code fits all in one line.
buffer.set_style_range(
row_num - 2,
(padding as isize + span_start_pos as isize) as usize,
(padding as isize + span_end_pos as isize) as usize,
Style::Removal,
true,
);
}
}

// length of the code after substitution
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ fn clean_ty_generics<'tcx>(

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
} = interface::run_compiler(config, |compiler| {
let krate = rustc_interface::passes::parse(&compiler.sess);

let collector = rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
let collector = rustc_interface::create_and_enter_global_ctxt(compiler, krate, |tcx| {
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
let opts = scrape_test_config(crate_name, crate_attrs, args_path);
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/doctest/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ fn handle_attr(mod_attr_pending: &mut String, source_info: &mut SourceInfo, edit
// If it's complete, then we can clear the pending content.
mod_attr_pending.clear();
} else {
mod_attr_pending.push_str("\n");
mod_attr_pending.push('\n');
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ impl DocFolder for CacheBuilder<'_, '_> {
let impl_item = Impl { impl_item: item };
let impl_did = impl_item.def_id();
let trait_did = impl_item.trait_did();
if trait_did.map_or(true, |d| self.cache.traits.contains_key(&d)) {
if trait_did.is_none_or(|d| self.cache.traits.contains_key(&d)) {
for did in dids {
if self.impl_ids.entry(did).or_default().insert(impl_did) {
self.cache.impls.entry(did).or_default().push(impl_item.clone());
Expand Down
7 changes: 3 additions & 4 deletions src/librustdoc/html/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ impl fmt::Display for EscapeBodyTextWithWbr<'_> {
continue;
}
let is_uppercase = || s.chars().any(|c| c.is_uppercase());
let next_is_uppercase =
|| pk.map_or(true, |(_, t)| t.chars().any(|c| c.is_uppercase()));
let next_is_underscore = || pk.map_or(true, |(_, t)| t.contains('_'));
let next_is_colon = || pk.map_or(true, |(_, t)| t.contains(':'));
let next_is_uppercase = || pk.is_none_or(|(_, t)| t.chars().any(|c| c.is_uppercase()));
let next_is_underscore = || pk.is_none_or(|(_, t)| t.contains('_'));
let next_is_colon = || pk.is_none_or(|(_, t)| t.contains(':'));
// Check for CamelCase.
//
// `i - last > 3` avoids turning FmRadio into Fm<wbr>Radio, which is technically
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ impl<'a, I: Iterator<Item = SpannedEvent<'a>>> Iterator for SpannedLinkReplacer<
type Item = SpannedEvent<'a>;

fn next(&mut self) -> Option<Self::Item> {
let Some((mut event, range)) = self.iter.next() else { return None };
let (mut event, range) = self.iter.next()?;
self.inner.handle_event(&mut event);
// Yield the modified event
Some((event, range))
Expand Down Expand Up @@ -2039,7 +2039,7 @@ impl IdMap {
let candidate = candidate.to_string();
if is_default_id(&candidate) {
let id = format!("{}-{}", candidate, 1);
self.map.insert(candidate.into(), 2);
self.map.insert(candidate, 2);
id
} else {
candidate
Expand All @@ -2052,7 +2052,7 @@ impl IdMap {
}
};

self.map.insert(id.clone().into(), 1);
self.map.insert(id.clone(), 1);
id
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
&shared.layout,
&page,
"",
scrape_examples_help(&shared),
scrape_examples_help(shared),
&shared.style_files,
);
shared.fs.write(scrape_examples_help_file, v)?;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,7 @@ fn render_impl(
.opt_doc_value()
.map(|dox| {
Markdown {
content: &*dox,
content: &dox,
links: &i.impl_item.links(cx),
ids: &mut cx.id_map.borrow_mut(),
error_codes: cx.shared.codes,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
ty: &'a clean::Type,
) -> impl fmt::Display + Captures<'a> + 'b + Captures<'cx> {
display_fn(move |f| {
let v = ty.print(&self.cx);
let v = ty.print(self.cx);
write!(f, "{v}")
})
}
Expand Down
11 changes: 5 additions & 6 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ fn from_clean_item(item: clean::Item, renderer: &JsonRenderer<'_>) -> ItemEnum {
StructFieldItem(f) => ItemEnum::StructField(f.into_json(renderer)),
EnumItem(e) => ItemEnum::Enum(e.into_json(renderer)),
VariantItem(v) => ItemEnum::Variant(v.into_json(renderer)),
FunctionItem(f) => ItemEnum::Function(from_function(f, true, header.unwrap(), renderer)),
FunctionItem(f) => ItemEnum::Function(from_function(*f, true, header.unwrap(), renderer)),
ForeignFunctionItem(f, _) => {
ItemEnum::Function(from_function(f, false, header.unwrap(), renderer))
ItemEnum::Function(from_function(*f, false, header.unwrap(), renderer))
}
TraitItem(t) => ItemEnum::Trait((*t).into_json(renderer)),
TraitAliasItem(t) => ItemEnum::TraitAlias(t.into_json(renderer)),
MethodItem(m, _) => ItemEnum::Function(from_function(m, true, header.unwrap(), renderer)),
MethodItem(m, _) => ItemEnum::Function(from_function(*m, true, header.unwrap(), renderer)),
RequiredMethodItem(m) => {
ItemEnum::Function(from_function(m, false, header.unwrap(), renderer))
ItemEnum::Function(from_function(*m, false, header.unwrap(), renderer))
}
ImplItem(i) => ItemEnum::Impl((*i).into_json(renderer)),
StaticItem(s) => ItemEnum::Static(convert_static(s, rustc_hir::Safety::Safe, renderer)),
Expand Down Expand Up @@ -730,12 +730,11 @@ impl FromClean<clean::Impl> for Impl {
}

pub(crate) fn from_function(
function: Box<clean::Function>,
clean::Function { decl, generics }: clean::Function,
has_body: bool,
header: rustc_hir::FnHeader,
renderer: &JsonRenderer<'_>,
) -> Function {
let clean::Function { decl, generics } = *function;
Function {
sig: decl.into_json(renderer),
generics: generics.into_json(renderer),
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ fn main_args(
sess.dcx().fatal("Compilation failed, aborting rustdoc");
}

rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
rustc_interface::create_and_enter_global_ctxt(compiler, krate, |tcx| {
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
core::run_global_ctxt(tcx, show_coverage, render_options, output_format)
});
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1977,7 +1977,7 @@ fn resolution_failure(
}

if !path_str.contains("::") {
if disambiguator.map_or(true, |d| d.ns() == MacroNS)
if disambiguator.is_none_or(|d| d.ns() == MacroNS)
&& collector
.cx
.tcx
Expand Down
1 change: 0 additions & 1 deletion src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
run-make/branch-protection-check-IBT/Makefile
run-make/cat-and-grep-sanity-check/Makefile
run-make/extern-fn-reachable/Makefile
run-make/incr-add-rust-src-component/Makefile
run-make/jobserver-error/Makefile
run-make/libs-through-symlinks/Makefile
run-make/split-debuginfo/Makefile
Expand Down
45 changes: 0 additions & 45 deletions tests/run-make/incr-add-rust-src-component/Makefile

This file was deleted.

Loading
Loading