Skip to content

Commit a11eb4f

Browse files
committed
Auto merge of rust-lang#106822 - matthiaskrgr:rollup-46bi4pi, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - rust-lang#104645 (Add log-backtrace option to show backtraces along with logging) - rust-lang#106465 (Bump `IMPLIED_BOUNDS_ENTAILMENT` to Deny + ReportNow) - rust-lang#106489 (Fix linker detection for linker (drivers) with a version postfix (e.g. clang-12 instead of clang)) - rust-lang#106585 (When suggesting writing a fully qualified path probe for appropriate types) - rust-lang#106641 (Provide help on closures capturing self causing borrow checker errors) - rust-lang#106678 (Warn when using panic-strategy abort for proc-macro crates) - rust-lang#106701 (Fix `mpsc::SyncSender` spinning behavior) - rust-lang#106793 (Normalize test output more thoroughly) - rust-lang#106797 (riscv: Fix ELF header flags) - rust-lang#106813 (Remove redundant session field) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 0b90256 + f709382 commit a11eb4f

File tree

58 files changed

+1011
-165
lines changed

Some content is hidden

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

58 files changed

+1011
-165
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4273,6 +4273,7 @@ version = "0.0.0"
42734273
dependencies = [
42744274
"rustc_span",
42754275
"tracing",
4276+
"tracing-core",
42764277
"tracing-subscriber",
42774278
"tracing-tree",
42784279
]

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+144-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_errors::{
66
struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan,
77
};
88
use rustc_hir as hir;
9+
use rustc_hir::def::Res;
910
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
1011
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, LangItem};
1112
use rustc_infer::infer::TyCtxtInferExt;
@@ -20,7 +21,7 @@ use rustc_middle::ty::{self, suggest_constraining_type_params, PredicateKind, Ty
2021
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
2122
use rustc_span::def_id::LocalDefId;
2223
use rustc_span::hygiene::DesugaringKind;
23-
use rustc_span::symbol::sym;
24+
use rustc_span::symbol::{kw, sym};
2425
use rustc_span::{BytePos, Span, Symbol};
2526
use rustc_trait_selection::infer::InferCtxtExt;
2627

@@ -29,6 +30,7 @@ use crate::borrowck_errors;
2930

3031
use crate::diagnostics::conflict_errors::StorageDeadOrDrop::LocalStorageDead;
3132
use crate::diagnostics::find_all_local_uses;
33+
use crate::diagnostics::mutability_errors::mut_borrow_of_mutable_ref;
3234
use crate::{
3335
borrow_set::BorrowData, diagnostics::Instance, prefixes::IsPrefixOf,
3436
InitializationRequiringAction, MirBorrowckCtxt, PrefixSet, WriteKind,
@@ -356,7 +358,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
356358
if let Some(hir::Node::Item(hir::Item {
357359
kind: hir::ItemKind::Fn(_, _, body_id),
358360
..
359-
})) = hir.find(hir.local_def_id_to_hir_id(self.mir_def_id()))
361+
})) = hir.find(self.mir_hir_id())
360362
&& let Some(hir::Node::Expr(expr)) = hir.find(body_id.hir_id)
361363
{
362364
let place = &self.move_data.move_paths[mpi].place;
@@ -948,7 +950,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
948950
}
949951
(BorrowKind::Mut { .. }, BorrowKind::Shared) => {
950952
first_borrow_desc = "immutable ";
951-
self.cannot_reborrow_already_borrowed(
953+
let mut err = self.cannot_reborrow_already_borrowed(
952954
span,
953955
&desc_place,
954956
&msg_place,
@@ -958,7 +960,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
958960
"immutable",
959961
&msg_borrow,
960962
None,
961-
)
963+
);
964+
self.suggest_binding_for_closure_capture_self(
965+
&mut err,
966+
issued_borrow.borrowed_place,
967+
&issued_spans,
968+
);
969+
err
962970
}
963971

964972
(BorrowKind::Mut { .. }, BorrowKind::Mut { .. }) => {
@@ -1240,6 +1248,138 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12401248
}
12411249
}
12421250

1251+
fn suggest_binding_for_closure_capture_self(
1252+
&self,
1253+
err: &mut Diagnostic,
1254+
borrowed_place: Place<'tcx>,
1255+
issued_spans: &UseSpans<'tcx>,
1256+
) {
1257+
let UseSpans::ClosureUse { capture_kind_span, .. } = issued_spans else { return };
1258+
let hir = self.infcx.tcx.hir();
1259+
1260+
// check whether the borrowed place is capturing `self` by mut reference
1261+
let local = borrowed_place.local;
1262+
let Some(_) = self
1263+
.body
1264+
.local_decls
1265+
.get(local)
1266+
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])) else { return };
1267+
1268+
struct ExpressionFinder<'hir> {
1269+
capture_span: Span,
1270+
closure_change_spans: Vec<Span>,
1271+
closure_arg_span: Option<Span>,
1272+
in_closure: bool,
1273+
suggest_arg: String,
1274+
hir: rustc_middle::hir::map::Map<'hir>,
1275+
closure_local_id: Option<hir::HirId>,
1276+
closure_call_changes: Vec<(Span, String)>,
1277+
}
1278+
impl<'hir> Visitor<'hir> for ExpressionFinder<'hir> {
1279+
fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
1280+
if e.span.contains(self.capture_span) {
1281+
if let hir::ExprKind::Closure(&hir::Closure {
1282+
movability: None,
1283+
body,
1284+
fn_arg_span,
1285+
fn_decl: hir::FnDecl{ inputs, .. },
1286+
..
1287+
}) = e.kind &&
1288+
let Some(hir::Node::Expr(body )) = self.hir.find(body.hir_id) {
1289+
self.suggest_arg = "this: &Self".to_string();
1290+
if inputs.len() > 0 {
1291+
self.suggest_arg.push_str(", ");
1292+
}
1293+
self.in_closure = true;
1294+
self.closure_arg_span = fn_arg_span;
1295+
self.visit_expr(body);
1296+
self.in_closure = false;
1297+
}
1298+
}
1299+
if let hir::Expr { kind: hir::ExprKind::Path(path), .. } = e {
1300+
if let hir::QPath::Resolved(_, hir::Path { segments: [seg], ..}) = path &&
1301+
seg.ident.name == kw::SelfLower && self.in_closure {
1302+
self.closure_change_spans.push(e.span);
1303+
}
1304+
}
1305+
hir::intravisit::walk_expr(self, e);
1306+
}
1307+
1308+
fn visit_local(&mut self, local: &'hir hir::Local<'hir>) {
1309+
if let hir::Pat { kind: hir::PatKind::Binding(_, hir_id, _ident, _), .. } = local.pat &&
1310+
let Some(init) = local.init
1311+
{
1312+
if let hir::Expr { kind: hir::ExprKind::Closure(&hir::Closure {
1313+
movability: None,
1314+
..
1315+
}), .. } = init &&
1316+
init.span.contains(self.capture_span) {
1317+
self.closure_local_id = Some(*hir_id);
1318+
}
1319+
}
1320+
hir::intravisit::walk_local(self, local);
1321+
}
1322+
1323+
fn visit_stmt(&mut self, s: &'hir hir::Stmt<'hir>) {
1324+
if let hir::StmtKind::Semi(e) = s.kind &&
1325+
let hir::ExprKind::Call(hir::Expr { kind: hir::ExprKind::Path(path), ..}, args) = e.kind &&
1326+
let hir::QPath::Resolved(_, hir::Path { segments: [seg], ..}) = path &&
1327+
let Res::Local(hir_id) = seg.res &&
1328+
Some(hir_id) == self.closure_local_id {
1329+
let (span, arg_str) = if args.len() > 0 {
1330+
(args[0].span.shrink_to_lo(), "self, ".to_string())
1331+
} else {
1332+
let span = e.span.trim_start(seg.ident.span).unwrap_or(e.span);
1333+
(span, "(self)".to_string())
1334+
};
1335+
self.closure_call_changes.push((span, arg_str));
1336+
}
1337+
hir::intravisit::walk_stmt(self, s);
1338+
}
1339+
}
1340+
1341+
if let Some(hir::Node::ImplItem(
1342+
hir::ImplItem { kind: hir::ImplItemKind::Fn(_fn_sig, body_id), .. }
1343+
)) = hir.find(self.mir_hir_id()) &&
1344+
let Some(hir::Node::Expr(expr)) = hir.find(body_id.hir_id) {
1345+
let mut finder = ExpressionFinder {
1346+
capture_span: *capture_kind_span,
1347+
closure_change_spans: vec![],
1348+
closure_arg_span: None,
1349+
in_closure: false,
1350+
suggest_arg: String::new(),
1351+
closure_local_id: None,
1352+
closure_call_changes: vec![],
1353+
hir,
1354+
};
1355+
finder.visit_expr(expr);
1356+
1357+
if finder.closure_change_spans.is_empty() || finder.closure_call_changes.is_empty() {
1358+
return;
1359+
}
1360+
1361+
let mut sugg = vec![];
1362+
let sm = self.infcx.tcx.sess.source_map();
1363+
1364+
if let Some(span) = finder.closure_arg_span {
1365+
sugg.push((sm.next_point(span.shrink_to_lo()).shrink_to_hi(), finder.suggest_arg));
1366+
}
1367+
for span in finder.closure_change_spans {
1368+
sugg.push((span, "this".to_string()));
1369+
}
1370+
1371+
for (span, suggest) in finder.closure_call_changes {
1372+
sugg.push((span, suggest));
1373+
}
1374+
1375+
err.multipart_suggestion_verbose(
1376+
"try explicitly pass `&Self` into the Closure as an argument",
1377+
sugg,
1378+
Applicability::MachineApplicable,
1379+
);
1380+
}
1381+
}
1382+
12431383
/// Returns the description of the root place for a conflicting borrow and the full
12441384
/// descriptions of the places that caused the conflict.
12451385
///

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10941094
}
10951095
}
10961096

1097-
fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symbol>) -> bool {
1097+
pub fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symbol>) -> bool {
10981098
debug!("local_info: {:?}, ty.kind(): {:?}", local_decl.local_info, local_decl.ty.kind());
10991099

11001100
match local_decl.local_info.as_deref() {

compiler/rustc_codegen_ssa/src/back/link.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1231,12 +1231,21 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12311231
sess.emit_fatal(errors::LinkerFileStem);
12321232
});
12331233

1234+
// Remove any version postfix.
1235+
let stem = stem
1236+
.rsplit_once('-')
1237+
.and_then(|(lhs, rhs)| rhs.chars().all(char::is_numeric).then_some(lhs))
1238+
.unwrap_or(stem);
1239+
1240+
// GCC can have an optional target prefix.
12341241
let flavor = if stem == "emcc" {
12351242
LinkerFlavor::EmCc
12361243
} else if stem == "gcc"
12371244
|| stem.ends_with("-gcc")
1245+
|| stem == "g++"
1246+
|| stem.ends_with("-g++")
12381247
|| stem == "clang"
1239-
|| stem.ends_with("-clang")
1248+
|| stem == "clang++"
12401249
{
12411250
LinkerFlavor::from_cli(LinkerFlavorCli::Gcc, &sess.target)
12421251
} else if stem == "wasm-ld" || stem.ends_with("-wasm-ld") {

compiler/rustc_codegen_ssa/src/back/metadata.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,23 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
165165
};
166166
e_flags
167167
}
168-
Architecture::Riscv64 if sess.target.options.features.contains("+d") => {
169-
// copied from `riscv64-linux-gnu-gcc foo.c -c`, note though
170-
// that the `+d` target feature represents whether the double
171-
// float abi is enabled.
172-
let e_flags = elf::EF_RISCV_RVC | elf::EF_RISCV_FLOAT_ABI_DOUBLE;
168+
Architecture::Riscv32 | Architecture::Riscv64 => {
169+
// Source: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/079772828bd10933d34121117a222b4cc0ee2200/riscv-elf.adoc
170+
let mut e_flags: u32 = 0x0;
171+
let features = &sess.target.options.features;
172+
// Check if compressed is enabled
173+
if features.contains("+c") {
174+
e_flags |= elf::EF_RISCV_RVC;
175+
}
176+
177+
// Select the appropriate floating-point ABI
178+
if features.contains("+d") {
179+
e_flags |= elf::EF_RISCV_FLOAT_ABI_DOUBLE;
180+
} else if features.contains("+f") {
181+
e_flags |= elf::EF_RISCV_FLOAT_ABI_SINGLE;
182+
} else {
183+
e_flags |= elf::EF_RISCV_FLOAT_ABI_SOFT;
184+
}
173185
e_flags
174186
}
175187
_ => 0,

compiler/rustc_driver/src/lib.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ fn run_compiler(
231231
registry: diagnostics_registry(),
232232
};
233233

234+
if !tracing::dispatcher::has_been_set() {
235+
init_rustc_env_logger_with_backtrace_option(&config.opts.unstable_opts.log_backtrace);
236+
}
237+
234238
match make_input(config.opts.error_format, &matches.free) {
235239
Err(reported) => return Err(reported),
236240
Ok(Some((input, input_file_path))) => {
@@ -1300,7 +1304,14 @@ pub fn install_ice_hook() {
13001304
/// This allows tools to enable rust logging without having to magically match rustc's
13011305
/// tracing crate version.
13021306
pub fn init_rustc_env_logger() {
1303-
if let Err(error) = rustc_log::init_rustc_env_logger() {
1307+
init_rustc_env_logger_with_backtrace_option(&None);
1308+
}
1309+
1310+
/// This allows tools to enable rust logging without having to magically match rustc's
1311+
/// tracing crate version. In contrast to `init_rustc_env_logger` it allows you to
1312+
/// choose a target module you wish to show backtraces along with its logging.
1313+
pub fn init_rustc_env_logger_with_backtrace_option(backtrace_target: &Option<String>) {
1314+
if let Err(error) = rustc_log::init_rustc_env_logger_with_backtrace_option(backtrace_target) {
13041315
early_error(ErrorOutputType::default(), &error.to_string());
13051316
}
13061317
}
@@ -1366,7 +1377,6 @@ mod signal_handler {
13661377
pub fn main() -> ! {
13671378
let start_time = Instant::now();
13681379
let start_rss = get_resident_set_size();
1369-
init_rustc_env_logger();
13701380
signal_handler::install();
13711381
let mut callbacks = TimePassesCallbacks::default();
13721382
install_ice_hook();

compiler/rustc_error_messages/locales/en-US/interface.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,6 @@ interface_rustc_error_unexpected_annotation =
4141
4242
interface_failed_writing_file =
4343
failed to write file {$path}: {$error}"
44+
45+
interface_proc_macro_crate_panic_abort =
46+
building proc macro crate with `panic=abort` may crash the compiler should the proc-macro panic

0 commit comments

Comments
 (0)