Skip to content
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

Rollup of 7 pull requests #134991

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
1e10e50
Remove allowing static_mut_refs lint
mu001999 Oct 14, 2024
403c8c2
E0277: suggest dereferencing function arguments in more cases
dianne Nov 21, 2024
08644b7
Avoid use of LFS64 symbols on Emscripten
kleisauke Dec 9, 2024
dbe9724
std::fs::DirEntry.metadata(): prefer use of lstat() on Emscripten
kleisauke Dec 9, 2024
62c3c9a
add suggestion for wrongly ordered format parameters
DavisRayM Dec 29, 2024
b560f5a
Update books
rustbot Dec 30, 2024
b994124
Explain how to mutate a HashMap/BTreeMap with more nuance
compiler-errors Dec 31, 2024
6a3474e
Use if-let in structured suggestion instead of Option::map
compiler-errors Dec 31, 2024
f28e13b
Fix span for IndexMut method call on HashMap/BTreeMap
compiler-errors Dec 31, 2024
2b2ea9e
Provide structured suggestion for `impl Default` of type where all fi…
estebank Dec 31, 2024
40b2cd9
Update reference, edition-guide
ehuss Dec 31, 2024
6f3cf8d
Rollup merge of #131439 - mu001999-contrib:cleanup/static-mut, r=este…
Zalathar Jan 1, 2025
7f0a180
Rollup merge of #133292 - dianne:e0277-suggest-deref, r=estebank
Zalathar Jan 1, 2025
45d8d5f
Rollup merge of #134080 - kleisauke:avoid-lfs64-emscripten, r=Noratrieb
Zalathar Jan 1, 2025
38bc2be
Rollup merge of #134877 - DavisRayM:129966-format-string-help-message…
Zalathar Jan 1, 2025
a0a9657
Rollup merge of #134926 - rustbot:docs-update, r=ehuss
Zalathar Jan 1, 2025
225e43a
Rollup merge of #134945 - compiler-errors:map-mutate-nits, r=estebank
Zalathar Jan 1, 2025
e349ce4
Rollup merge of #134979 - estebank:default-lint-sugg, r=compiler-errors
Zalathar Jan 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
// ---------- place
self.err.multipart_suggestions(
format!(
"to modify a `{}`, use `.get_mut()`, `.insert()` or the entry API",
"use `.insert()` to insert a value into a `{}`, `.get_mut()` \
to modify it, or the entry API for more flexibility",
self.ty,
),
vec![
Expand All @@ -592,16 +593,17 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
(rv.span.shrink_to_hi(), ")".to_string()),
],
vec![
// val.get_mut(index).map(|v| { *v = rv; });
// if let Some(v) = val.get_mut(index) { *v = rv; }
(val.span.shrink_to_lo(), "if let Some(val) = ".to_string()),
(
val.span.shrink_to_hi().with_hi(index.span.lo()),
".get_mut(".to_string(),
),
(
index.span.shrink_to_hi().with_hi(place.span.hi()),
").map(|val| { *val".to_string(),
") { *val".to_string(),
),
(rv.span.shrink_to_hi(), "; })".to_string()),
(rv.span.shrink_to_hi(), "; }".to_string()),
],
vec![
// let x = val.entry(index).or_insert(rv);
Expand All @@ -622,21 +624,22 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
self.suggested = true;
} else if let hir::ExprKind::MethodCall(_path, receiver, _, sp) = expr.kind
&& let hir::ExprKind::Index(val, index, _) = receiver.kind
&& expr.span == self.assign_span
&& receiver.span == self.assign_span
{
// val[index].path(args..);
self.err.multipart_suggestion(
format!("to modify a `{}` use `.get_mut()`", self.ty),
vec![
(val.span.shrink_to_lo(), "if let Some(val) = ".to_string()),
(
val.span.shrink_to_hi().with_hi(index.span.lo()),
".get_mut(".to_string(),
),
(
index.span.shrink_to_hi().with_hi(receiver.span.hi()),
").map(|val| val".to_string(),
") { val".to_string(),
),
(sp.shrink_to_hi(), ")".to_string()),
(sp.shrink_to_hi(), "; }".to_string()),
],
Applicability::MachineApplicable,
);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ builtin_macros_format_redundant_args = redundant {$n ->

builtin_macros_format_remove_raw_ident = remove the `r#`

builtin_macros_format_reorder_format_parameter = did you mean `{$replacement}`?

builtin_macros_format_requires_string = requires at least a format string argument

builtin_macros_format_string_invalid = invalid format string: {$desc}
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_builtin_macros/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@ pub(crate) enum InvalidFormatStringSuggestion {
#[primary_span]
span: Span,
},
#[suggestion(
builtin_macros_format_reorder_format_parameter,
code = "{replacement}",
style = "verbose",
applicability = "machine-applicable"
)]
ReorderFormatParameter {
#[primary_span]
span: Span,
replacement: String,
},
}

#[derive(Diagnostic)]
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ fn make_format_args(
e.sugg_ = Some(errors::InvalidFormatStringSuggestion::RemoveRawIdent { span })
}
}
parse::Suggestion::ReorderFormatParameter(span, replacement) => {
let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
e.sugg_ = Some(errors::InvalidFormatStringSuggestion::ReorderFormatParameter {
span,
replacement,
});
}
}
let guar = ecx.dcx().emit_err(e);
return ExpandResult::Ready(Err(guar));
Expand Down
20 changes: 11 additions & 9 deletions compiler/rustc_driver_impl/src/signal_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! Primarily used to extract a backtrace from stack overflow

use std::alloc::{Layout, alloc};
use std::{fmt, mem, ptr};
use std::{fmt, mem, ptr, slice};

use rustc_interface::util::{DEFAULT_STACK_SIZE, STACK_SIZE};

Expand Down Expand Up @@ -35,20 +35,22 @@ macro raw_errln($tokens:tt) {
}

/// Signal handler installed for SIGSEGV
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
#[allow(static_mut_refs)]
extern "C" fn print_stack_trace(_: libc::c_int) {
///
/// # Safety
///
/// Caller must ensure that this function is not re-entered.
unsafe extern "C" fn print_stack_trace(_: libc::c_int) {
const MAX_FRAMES: usize = 256;
// Reserve data segment so we don't have to malloc in a signal handler, which might fail
// in incredibly undesirable and unexpected ways due to e.g. the allocator deadlocking
static mut STACK_TRACE: [*mut libc::c_void; MAX_FRAMES] = [ptr::null_mut(); MAX_FRAMES];
let stack = unsafe {
// Reserve data segment so we don't have to malloc in a signal handler, which might fail
// in incredibly undesirable and unexpected ways due to e.g. the allocator deadlocking
static mut STACK_TRACE: [*mut libc::c_void; MAX_FRAMES] = [ptr::null_mut(); MAX_FRAMES];
// Collect return addresses
let depth = libc::backtrace(STACK_TRACE.as_mut_ptr(), MAX_FRAMES as i32);
let depth = libc::backtrace(&raw mut STACK_TRACE as _, MAX_FRAMES as i32);
if depth == 0 {
return;
}
&STACK_TRACE.as_slice()[0..(depth as _)]
slice::from_raw_parts(&raw const STACK_TRACE as _, depth as _)
};

// Just a stack trace is cryptic. Explain what we're doing.
Expand Down
34 changes: 26 additions & 8 deletions compiler/rustc_lint/src/default_could_be_derived.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::Diag;
use rustc_errors::{Applicability, Diag};
use rustc_hir as hir;
use rustc_middle::ty;
use rustc_middle::ty::TyCtxt;
use rustc_session::{declare_lint, impl_lint_pass};
use rustc_span::Symbol;
use rustc_span::def_id::DefId;
use rustc_span::symbol::sym;

use crate::{LateContext, LateLintPass};
Expand Down Expand Up @@ -149,13 +151,16 @@ impl<'tcx> LateLintPass<'tcx> for DefaultCouldBeDerived {
let hir_id = cx.tcx.local_def_id_to_hir_id(local);
let hir::Node::Item(item) = cx.tcx.hir_node(hir_id) else { return };
cx.tcx.node_span_lint(DEFAULT_OVERRIDES_DEFAULT_FIELDS, hir_id, item.span, |diag| {
mk_lint(diag, orig_fields, fields);
mk_lint(cx.tcx, diag, type_def_id, parent, orig_fields, fields);
});
}
}

fn mk_lint(
tcx: TyCtxt<'_>,
diag: &mut Diag<'_, ()>,
type_def_id: DefId,
impl_def_id: DefId,
orig_fields: FxHashMap<Symbol, &hir::FieldDef<'_>>,
fields: &[hir::ExprField<'_>],
) {
Expand All @@ -175,11 +180,24 @@ fn mk_lint(
}
}

diag.help(if removed_all_fields {
"to avoid divergence in behavior between `Struct { .. }` and \
`<Struct as Default>::default()`, derive the `Default`"
if removed_all_fields {
let msg = "to avoid divergence in behavior between `Struct { .. }` and \
`<Struct as Default>::default()`, derive the `Default`";
if let Some(hir::Node::Item(impl_)) = tcx.hir().get_if_local(impl_def_id) {
diag.multipart_suggestion_verbose(
msg,
vec![
(tcx.def_span(type_def_id).shrink_to_lo(), "#[derive(Default)] ".to_string()),
(impl_.span, String::new()),
],
Applicability::MachineApplicable,
);
} else {
diag.help(msg);
}
} else {
"use the default values in the `impl` with `Struct { mandatory_field, .. }` to avoid them \
diverging over time"
});
let msg = "use the default values in the `impl` with `Struct { mandatory_field, .. }` to \
avoid them diverging over time";
diag.help(msg);
}
}
35 changes: 35 additions & 0 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ pub enum Suggestion {
/// Remove `r#` from identifier:
/// `format!("{r#foo}")` -> `format!("{foo}")`
RemoveRawIdent(InnerSpan),
/// Reorder format parameter:
/// `format!("{foo:?#}")` -> `format!("{foo:#?}")`
/// `format!("{foo:?x}")` -> `format!("{foo:x?}")`
/// `format!("{foo:?X}")` -> `format!("{foo:X?}")`
ReorderFormatParameter(InnerSpan, string::String),
}

/// The parser structure for interpreting the input format string. This is
Expand Down Expand Up @@ -731,6 +736,12 @@ impl<'a> Parser<'a> {
}
} else if self.consume('?') {
spec.ty = "?";
if let Some(&(_, maybe)) = self.cur.peek() {
match maybe {
'#' | 'x' | 'X' => self.suggest_format_parameter(maybe),
_ => (),
}
}
} else {
spec.ty = self.word();
if !spec.ty.is_empty() {
Expand Down Expand Up @@ -932,6 +943,30 @@ impl<'a> Parser<'a> {
}
}
}

fn suggest_format_parameter(&mut self, c: char) {
let replacement = match c {
'#' => "#?",
'x' => "x?",
'X' => "X?",
_ => return,
};
let Some(pos) = self.consume_pos(c) else {
return;
};

let span = self.span(pos - 1, pos + 1);
let pos = self.to_span_index(pos);

self.errors.insert(0, ParseError {
description: format!("expected `}}`, found `{c}`"),
note: None,
label: "expected `'}'`".into(),
span: pos.to(pos),
secondary_label: None,
suggestion: Suggestion::ReorderFormatParameter(span, format!("{replacement}")),
})
}
}

/// Finds the indices of all characters that have been processed and differ between the actual
Expand Down
Loading
Loading