Skip to content

Commit 3a1d3f2

Browse files
committed
Auto merge of #147158 - matthiaskrgr:rollup-rc03jo1, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #143613 (Fix backtraces with `-C panic=abort` on linux; emit unwind tables by default) - #146937 (std: implement `hostname`) - #147040 (mbe: macro_check: Fix function comments referencing non-existent parameters) - #147131 (Use MirPatch in simplify_branches.) - #147133 (Remove one loop in `extract_cfg_from_attrs`) - #147150 (Emit allocator attributes for allocator shim) r? `@ghost` `@rustbot` modify labels: rollup
2 parents dc2c356 + 4d62bdb commit 3a1d3f2

File tree

23 files changed

+365
-121
lines changed

23 files changed

+365
-121
lines changed

compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use rustc_ast::expand::allocator::{
55
};
66
use rustc_codegen_ssa::traits::BaseTypeCodegenMethods as _;
77
use rustc_middle::bug;
8-
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
8+
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
99
use rustc_middle::ty::TyCtxt;
1010
use rustc_session::config::{DebugInfo, OomStrategy};
11+
use rustc_span::sym;
1112
use rustc_symbol_mangling::mangle_internal_symbol;
1213

1314
use crate::attributes::llfn_attrs_from_instance;
@@ -59,7 +60,26 @@ pub(crate) unsafe fn codegen(
5960
let from_name = mangle_internal_symbol(tcx, &global_fn_name(method.name));
6061
let to_name = mangle_internal_symbol(tcx, &default_fn_name(method.name));
6162

62-
create_wrapper_function(tcx, &cx, &from_name, Some(&to_name), &args, output, false);
63+
let alloc_attr_flag = match method.name {
64+
sym::alloc => CodegenFnAttrFlags::ALLOCATOR,
65+
sym::dealloc => CodegenFnAttrFlags::DEALLOCATOR,
66+
sym::realloc => CodegenFnAttrFlags::REALLOCATOR,
67+
sym::alloc_zeroed => CodegenFnAttrFlags::ALLOCATOR_ZEROED,
68+
_ => unreachable!("Unknown allocator method!"),
69+
};
70+
71+
let mut attrs = CodegenFnAttrs::new();
72+
attrs.flags |= alloc_attr_flag;
73+
create_wrapper_function(
74+
tcx,
75+
&cx,
76+
&from_name,
77+
Some(&to_name),
78+
&args,
79+
output,
80+
false,
81+
&attrs,
82+
);
6383
}
6484
}
6585

@@ -72,6 +92,7 @@ pub(crate) unsafe fn codegen(
7292
&[usize, usize], // size, align
7393
None,
7494
true,
95+
&CodegenFnAttrs::new(),
7596
);
7697

7798
unsafe {
@@ -93,6 +114,7 @@ pub(crate) unsafe fn codegen(
93114
&[],
94115
None,
95116
false,
117+
&CodegenFnAttrs::new(),
96118
);
97119
}
98120

@@ -139,6 +161,7 @@ fn create_wrapper_function(
139161
args: &[&Type],
140162
output: Option<&Type>,
141163
no_return: bool,
164+
attrs: &CodegenFnAttrs,
142165
) {
143166
let ty = cx.type_func(args, output.unwrap_or_else(|| cx.type_void()));
144167
let llfn = declare_simple_fn(
@@ -150,8 +173,7 @@ fn create_wrapper_function(
150173
ty,
151174
);
152175

153-
let attrs = CodegenFnAttrs::new();
154-
llfn_attrs_from_instance(cx, tcx, llfn, &attrs, None);
176+
llfn_attrs_from_instance(cx, tcx, llfn, attrs, None);
155177

156178
let no_return = if no_return {
157179
// -> ! DIFlagNoReturn

compiler/rustc_expand/src/mbe/macro_check.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ pub(super) fn check_meta_variables(
210210
guar.map_or(Ok(()), Err)
211211
}
212212

213-
/// Checks `lhs` as part of the LHS of a macro definition, extends `binders` with new binders, and
214-
/// sets `valid` to false in case of errors.
213+
/// Checks `lhs` as part of the LHS of a macro definition.
215214
///
216215
/// Arguments:
217216
/// - `psess` is used to emit diagnostics and lints
@@ -306,8 +305,7 @@ fn get_binder_info<'a>(
306305
binders.get(&name).or_else(|| macros.find_map(|state| state.binders.get(&name)))
307306
}
308307

309-
/// Checks `rhs` as part of the RHS of a macro definition and sets `valid` to false in case of
310-
/// errors.
308+
/// Checks `rhs` as part of the RHS of a macro definition.
311309
///
312310
/// Arguments:
313311
/// - `psess` is used to emit diagnostics and lints
@@ -372,7 +370,7 @@ enum NestedMacroState {
372370
}
373371

374372
/// Checks `tts` as part of the RHS of a macro definition, tries to recognize nested macro
375-
/// definitions, and sets `valid` to false in case of errors.
373+
/// definitions.
376374
///
377375
/// Arguments:
378376
/// - `psess` is used to emit diagnostics and lints
@@ -491,8 +489,7 @@ fn check_nested_occurrences(
491489
}
492490
}
493491

494-
/// Checks the body of nested macro, returns where the check stopped, and sets `valid` to false in
495-
/// case of errors.
492+
/// Checks the body of nested macro, returns where the check stopped.
496493
///
497494
/// The token trees are checked as long as they look like a list of (LHS) => {RHS} token trees. This
498495
/// check is a best-effort to detect a macro definition. It returns the position in `tts` where we

compiler/rustc_mir_transform/src/patch.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use tracing::debug;
1111
/// once with `apply`. This is useful for MIR transformation passes.
1212
pub(crate) struct MirPatch<'tcx> {
1313
term_patch_map: FxHashMap<BasicBlock, TerminatorKind<'tcx>>,
14+
/// Set of statements that should be replaced by `Nop`.
15+
nop_statements: Vec<Location>,
1416
new_blocks: Vec<BasicBlockData<'tcx>>,
1517
new_statements: Vec<(Location, StatementKind<'tcx>)>,
1618
new_locals: Vec<LocalDecl<'tcx>>,
@@ -33,6 +35,7 @@ impl<'tcx> MirPatch<'tcx> {
3335
pub(crate) fn new(body: &Body<'tcx>) -> Self {
3436
let mut result = MirPatch {
3537
term_patch_map: Default::default(),
38+
nop_statements: vec![],
3639
new_blocks: vec![],
3740
new_statements: vec![],
3841
new_locals: vec![],
@@ -212,6 +215,15 @@ impl<'tcx> MirPatch<'tcx> {
212215
self.term_patch_map.insert(block, new);
213216
}
214217

218+
/// Mark given statement to be replaced by a `Nop`.
219+
///
220+
/// This method only works on statements from the initial body, and cannot be used to remove
221+
/// statements from `add_statement` or `add_assign`.
222+
#[tracing::instrument(level = "debug", skip(self))]
223+
pub(crate) fn nop_statement(&mut self, loc: Location) {
224+
self.nop_statements.push(loc);
225+
}
226+
215227
/// Queues the insertion of a statement at a given location. The statement
216228
/// currently at that location, and all statements that follow, are shifted
217229
/// down. If multiple statements are queued for addition at the same
@@ -257,11 +269,8 @@ impl<'tcx> MirPatch<'tcx> {
257269
bbs.extend(self.new_blocks);
258270
body.local_decls.extend(self.new_locals);
259271

260-
// The order in which we patch terminators does not change the result.
261-
#[allow(rustc::potential_query_instability)]
262-
for (src, patch) in self.term_patch_map {
263-
debug!("MirPatch: patching block {:?}", src);
264-
bbs[src].terminator_mut().kind = patch;
272+
for loc in self.nop_statements {
273+
bbs[loc.block].statements[loc.statement_index].make_nop();
265274
}
266275

267276
let mut new_statements = self.new_statements;
@@ -285,6 +294,17 @@ impl<'tcx> MirPatch<'tcx> {
285294
.insert(loc.statement_index, Statement::new(source_info, stmt));
286295
delta += 1;
287296
}
297+
298+
// The order in which we patch terminators does not change the result.
299+
#[allow(rustc::potential_query_instability)]
300+
for (src, patch) in self.term_patch_map {
301+
debug!("MirPatch: patching block {:?}", src);
302+
let bb = &mut bbs[src];
303+
if let TerminatorKind::Unreachable = patch {
304+
bb.statements.clear();
305+
}
306+
bb.terminator_mut().kind = patch;
307+
}
288308
}
289309

290310
fn source_info_for_index(data: &BasicBlockData<'_>, loc: Location) -> SourceInfo {

compiler/rustc_mir_transform/src/simplify_branches.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use rustc_middle::mir::*;
22
use rustc_middle::ty::TyCtxt;
33
use tracing::trace;
44

5+
use crate::patch::MirPatch;
6+
57
pub(super) enum SimplifyConstCondition {
68
AfterConstProp,
79
Final,
@@ -19,26 +21,27 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyConstCondition {
1921
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2022
trace!("Running SimplifyConstCondition on {:?}", body.source);
2123
let typing_env = body.typing_env(tcx);
22-
'blocks: for block in body.basic_blocks_mut() {
23-
for stmt in block.statements.iter_mut() {
24+
let mut patch = MirPatch::new(body);
25+
26+
'blocks: for (bb, block) in body.basic_blocks.iter_enumerated() {
27+
for (statement_index, stmt) in block.statements.iter().enumerate() {
2428
// Simplify `assume` of a known value: either a NOP or unreachable.
2529
if let StatementKind::Intrinsic(box ref intrinsic) = stmt.kind
2630
&& let NonDivergingIntrinsic::Assume(discr) = intrinsic
2731
&& let Operand::Constant(c) = discr
2832
&& let Some(constant) = c.const_.try_eval_bool(tcx, typing_env)
2933
{
3034
if constant {
31-
stmt.make_nop();
35+
patch.nop_statement(Location { block: bb, statement_index });
3236
} else {
33-
block.statements.clear();
34-
block.terminator_mut().kind = TerminatorKind::Unreachable;
37+
patch.patch_terminator(bb, TerminatorKind::Unreachable);
3538
continue 'blocks;
3639
}
3740
}
3841
}
3942

40-
let terminator = block.terminator_mut();
41-
terminator.kind = match terminator.kind {
43+
let terminator = block.terminator();
44+
let terminator = match terminator.kind {
4245
TerminatorKind::SwitchInt {
4346
discr: Operand::Constant(ref c), ref targets, ..
4447
} => {
@@ -58,7 +61,9 @@ impl<'tcx> crate::MirPass<'tcx> for SimplifyConstCondition {
5861
},
5962
_ => continue,
6063
};
64+
patch.patch_terminator(bb, terminator);
6165
}
66+
patch.apply(body);
6267
}
6368

6469
fn is_required(&self) -> bool {

compiler/rustc_session/src/session.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ impl Session {
758758
// ELF x86-64 abi, but it can be disabled for some compilation units.
759759
//
760760
// Typically when we're compiling with `-C panic=abort` we don't need
761-
// `uwtable` because we can't generate any exceptions!
761+
// `uwtable` because we can't generate any exceptions! But note that
762+
// some targets require unwind tables to generate backtraces.
762763
// Unwind tables are needed when compiling with `-C panic=unwind`, but
763764
// LLVM won't omit unwind tables unless the function is also marked as
764765
// `nounwind`, so users are allowed to disable `uwtable` emission.

compiler/rustc_target/src/spec/base/android.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ pub(crate) fn opts() -> TargetOptions {
88
base.tls_model = TlsModel::Emulated;
99
base.has_thread_local = false;
1010
base.supported_sanitizers = SanitizerSet::ADDRESS;
11-
// This is for backward compatibility, see https://github.com/rust-lang/rust/issues/49867
12-
// for context. (At that time, there was no `-C force-unwind-tables`, so the only solution
13-
// was to always emit `uwtable`).
14-
base.default_uwtable = true;
1511
base.crt_static_respected = true;
1612
base
1713
}

compiler/rustc_target/src/spec/base/linux.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ pub(crate) fn opts() -> TargetOptions {
1212
relro_level: RelroLevel::Full,
1313
has_thread_local: true,
1414
crt_static_respected: true,
15+
// We want backtraces to work by default and they rely on unwind tables
16+
// (regardless of `-C panic` strategy).
17+
default_uwtable: true,
1518
supported_split_debuginfo: Cow::Borrowed(&[
1619
SplitDebuginfo::Packed,
1720
SplitDebuginfo::Unpacked,

compiler/rustc_target/src/spec/targets/arm_unknown_linux_gnueabihf.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ pub(crate) fn target() -> Target {
1919
max_atomic_width: Some(64),
2020
mcount: "\u{1}__gnu_mcount_nc".into(),
2121
llvm_mcount_intrinsic: Some("llvm.arm.gnu.eabi.mcount".into()),
22+
// The default on linux is to have `default_uwtable=true`, but on
23+
// this target we get an "`__aeabi_unwind_cpp_pr0` not defined"
24+
// linker error, so set it to `true` here.
25+
// FIXME(#146996): Remove this override once #146996 has been fixed.
26+
default_uwtable: false,
2227
..base::linux_gnu::opts()
2328
},
2429
}

library/std/src/net/hostname.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::ffi::OsString;
2+
3+
/// Returns the system hostname.
4+
///
5+
/// This can error out in platform-specific error cases;
6+
/// for example, uefi and wasm, where hostnames aren't
7+
/// supported.
8+
///
9+
/// # Underlying system calls
10+
///
11+
/// | Platform | System call |
12+
/// |----------|---------------------------------------------------------------------------------------------------------|
13+
/// | UNIX | [`gethostname`](https://www.man7.org/linux/man-pages/man2/gethostname.2.html) |
14+
/// | Windows | [`GetHostNameW`](https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-gethostnamew) |
15+
///
16+
/// Note that platform-specific behavior [may change in the future][changes].
17+
///
18+
/// [changes]: crate::io#platform-specific-behavior
19+
#[unstable(feature = "gethostname", issue = "135142")]
20+
pub fn hostname() -> crate::io::Result<OsString> {
21+
crate::sys::net::hostname()
22+
}

library/std/src/net/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Networking primitives for TCP/UDP communication.
22
//!
33
//! This module provides networking functionality for the Transmission Control and User
4-
//! Datagram Protocols, as well as types for IP and socket addresses.
4+
//! Datagram Protocols, as well as types for IP and socket addresses and functions related
5+
//! to network properties.
56
//!
67
//! # Organization
78
//!
@@ -24,6 +25,8 @@
2425
#[stable(feature = "rust1", since = "1.0.0")]
2526
pub use core::net::AddrParseError;
2627

28+
#[unstable(feature = "gethostname", issue = "135142")]
29+
pub use self::hostname::hostname;
2730
#[stable(feature = "rust1", since = "1.0.0")]
2831
pub use self::ip_addr::{IpAddr, Ipv4Addr, Ipv6Addr, Ipv6MulticastScope};
2932
#[stable(feature = "rust1", since = "1.0.0")]
@@ -35,6 +38,7 @@ pub use self::tcp::{Incoming, TcpListener, TcpStream};
3538
#[stable(feature = "rust1", since = "1.0.0")]
3639
pub use self::udp::UdpSocket;
3740

41+
mod hostname;
3842
mod ip_addr;
3943
mod socket_addr;
4044
mod tcp;

0 commit comments

Comments
 (0)