Skip to content

Commit 956e06c

Browse files
committed
Auto merge of #77004 - RalfJung:rollup-usac4nv, r=RalfJung
Rollup of 13 pull requests Successful merges: - #76135 (Stabilize some Option methods as const) - #76628 (Add sample defaults for config.toml ) - #76846 (Avoiding unnecesary allocations at rustc_errors) - #76867 (Use intra-doc links in core/src/iter when possible) - #76868 (Finish moving to intra doc links for std::sync) - #76872 (Remove DeclareMethods) - #76936 (Add non-`unsafe` `.get_mut()` for `Unsafecell`) - #76958 (Replace manual as_nanos and as_secs_f64 reimplementations) - #76959 (Replace write_fmt with write!) - #76961 (Add test for issue #34634) - #76962 (Use const_cstr macro in consts.rs) - #76963 (Remove unused static_assert macro) - #77000 (update Miri) Failed merges: r? `@ghost`
2 parents 0433fdf + d337074 commit 956e06c

Some content is hidden

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

46 files changed

+592
-504
lines changed

Cargo.lock

+23
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ dependencies = [
207207
"ignore",
208208
"lazy_static",
209209
"libc",
210+
"merge",
210211
"num_cpus",
211212
"opener",
212213
"pretty_assertions",
@@ -1909,6 +1910,28 @@ dependencies = [
19091910
"autocfg",
19101911
]
19111912

1913+
[[package]]
1914+
name = "merge"
1915+
version = "0.1.0"
1916+
source = "registry+https://github.com/rust-lang/crates.io-index"
1917+
checksum = "10bbef93abb1da61525bbc45eeaff6473a41907d19f8f9aa5168d214e10693e9"
1918+
dependencies = [
1919+
"merge_derive",
1920+
"num-traits",
1921+
]
1922+
1923+
[[package]]
1924+
name = "merge_derive"
1925+
version = "0.1.0"
1926+
source = "registry+https://github.com/rust-lang/crates.io-index"
1927+
checksum = "209d075476da2e63b4b29e72a2ef627b840589588e71400a25e3565c4f849d07"
1928+
dependencies = [
1929+
"proc-macro-error",
1930+
"proc-macro2",
1931+
"quote",
1932+
"syn",
1933+
]
1934+
19121935
[[package]]
19131936
name = "minifier"
19141937
version = "0.0.33"

compiler/rustc_codegen_llvm/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn compile_codegen_unit(
108108

109109
// We assume that the cost to run LLVM on a CGU is proportional to
110110
// the time we needed for codegenning it.
111-
let cost = time_to_codegen.as_secs() * 1_000_000_000 + time_to_codegen.subsec_nanos() as u64;
111+
let cost = time_to_codegen.as_nanos() as u64;
112112

113113
fn module_codegen(tcx: TyCtxt<'_>, cgu_name: Symbol) -> ModuleCodegen<ModuleLlvm> {
114114
let cgu = tcx.codegen_unit(cgu_name);

compiler/rustc_codegen_llvm/src/consts.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::type_of::LayoutLlvmExt;
77
use crate::value::Value;
88
use libc::c_uint;
99
use rustc_codegen_ssa::traits::*;
10+
use rustc_data_structures::const_cstr;
1011
use rustc_hir as hir;
1112
use rustc_hir::def_id::DefId;
1213
use rustc_hir::Node;
@@ -22,8 +23,6 @@ use rustc_span::Span;
2223
use rustc_target::abi::{AddressSpace, Align, HasDataLayout, LayoutOf, Primitive, Scalar, Size};
2324
use tracing::debug;
2425

25-
use std::ffi::CStr;
26-
2726
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
2827
let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
2928
let dl = cx.data_layout();
@@ -454,9 +453,9 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
454453
.all(|&byte| byte == 0);
455454

456455
let sect_name = if all_bytes_are_zero {
457-
CStr::from_bytes_with_nul_unchecked(b"__DATA,__thread_bss\0")
456+
const_cstr!("__DATA,__thread_bss")
458457
} else {
459-
CStr::from_bytes_with_nul_unchecked(b"__DATA,__thread_data\0")
458+
const_cstr!("__DATA,__thread_data")
460459
};
461460
llvm::LLVMSetSection(g, sect_name.as_ptr());
462461
}

compiler/rustc_codegen_llvm/src/context.rs

+11
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,17 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
433433
llvm::LLVMSetSection(g, section.as_ptr());
434434
}
435435
}
436+
437+
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function> {
438+
if self.get_declared_value("main").is_none() {
439+
Some(self.declare_cfn("main", fn_type))
440+
} else {
441+
// If the symbol already exists, it is an error: for example, the user wrote
442+
// #[no_mangle] extern "C" fn main(..) {..}
443+
// instead of #[start]
444+
None
445+
}
446+
}
436447
}
437448

438449
impl CodegenCx<'b, 'tcx> {

compiler/rustc_codegen_llvm/src/declare.rs

+35-8
Original file line numberDiff line numberDiff line change
@@ -51,42 +51,69 @@ fn declare_raw_fn(
5151
llfn
5252
}
5353

54-
impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
55-
fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
54+
impl CodegenCx<'ll, 'tcx> {
55+
/// Declare a global value.
56+
///
57+
/// If there’s a value with the same name already declared, the function will
58+
/// return its Value instead.
59+
pub fn declare_global(&self, name: &str, ty: &'ll Type) -> &'ll Value {
5660
debug!("declare_global(name={:?})", name);
5761
unsafe { llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty) }
5862
}
5963

60-
fn declare_cfn(&self, name: &str, fn_type: &'ll Type) -> &'ll Value {
64+
/// Declare a C ABI function.
65+
///
66+
/// Only use this for foreign function ABIs and glue. For Rust functions use
67+
/// `declare_fn` instead.
68+
///
69+
/// If there’s a value with the same name already declared, the function will
70+
/// update the declaration and return existing Value instead.
71+
pub fn declare_cfn(&self, name: &str, fn_type: &'ll Type) -> &'ll Value {
6172
declare_raw_fn(self, name, llvm::CCallConv, fn_type)
6273
}
6374

64-
fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
75+
/// Declare a Rust function.
76+
///
77+
/// If there’s a value with the same name already declared, the function will
78+
/// update the declaration and return existing Value instead.
79+
pub fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> &'ll Value {
6580
debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
6681

6782
let llfn = declare_raw_fn(self, name, fn_abi.llvm_cconv(), fn_abi.llvm_type(self));
6883
fn_abi.apply_attrs_llfn(self, llfn);
6984
llfn
7085
}
7186

72-
fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
87+
/// Declare a global with an intention to define it.
88+
///
89+
/// Use this function when you intend to define a global. This function will
90+
/// return `None` if the name already has a definition associated with it. In that
91+
/// case an error should be reported to the user, because it usually happens due
92+
/// to user’s fault (e.g., misuse of `#[no_mangle]` or `#[export_name]` attributes).
93+
pub fn define_global(&self, name: &str, ty: &'ll Type) -> Option<&'ll Value> {
7394
if self.get_defined_value(name).is_some() {
7495
None
7596
} else {
7697
Some(self.declare_global(name, ty))
7798
}
7899
}
79100

80-
fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
101+
/// Declare a private global
102+
///
103+
/// Use this function when you intend to define a global without a name.
104+
pub fn define_private_global(&self, ty: &'ll Type) -> &'ll Value {
81105
unsafe { llvm::LLVMRustInsertPrivateGlobal(self.llmod, ty) }
82106
}
83107

84-
fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
108+
/// Gets declared value by name.
109+
pub fn get_declared_value(&self, name: &str) -> Option<&'ll Value> {
85110
debug!("get_declared_value(name={:?})", name);
86111
unsafe { llvm::LLVMRustGetNamedValue(self.llmod, name.as_ptr().cast(), name.len()) }
87112
}
88113

89-
fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
114+
/// Gets defined or externally defined (AvailableExternally linkage) value by
115+
/// name.
116+
pub fn get_defined_value(&self, name: &str) -> Option<&'ll Value> {
90117
self.get_declared_value(name).and_then(|val| {
91118
let declaration = unsafe { llvm::LLVMIsDeclaration(val) != 0 };
92119
if !declaration { Some(val) } else { None }

compiler/rustc_codegen_ssa/src/base.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -407,16 +407,18 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
407407
// listing.
408408
let main_ret_ty = cx.tcx().erase_regions(&main_ret_ty.no_bound_vars().unwrap());
409409

410-
if cx.get_declared_value("main").is_some() {
411-
// FIXME: We should be smart and show a better diagnostic here.
412-
cx.sess()
413-
.struct_span_err(sp, "entry symbol `main` declared multiple times")
414-
.help("did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead")
415-
.emit();
416-
cx.sess().abort_if_errors();
417-
bug!();
418-
}
419-
let llfn = cx.declare_cfn("main", llfty);
410+
let llfn = match cx.declare_c_main(llfty) {
411+
Some(llfn) => llfn,
412+
None => {
413+
// FIXME: We should be smart and show a better diagnostic here.
414+
cx.sess()
415+
.struct_span_err(sp, "entry symbol `main` declared multiple times")
416+
.help("did you use `#[no_mangle]` on `fn main`? Use `#[start]` instead")
417+
.emit();
418+
cx.sess().abort_if_errors();
419+
bug!();
420+
}
421+
};
420422

421423
// `main` should respect same config for frame pointer elimination as rest of code
422424
cx.set_frame_pointer_elimination(llfn);

compiler/rustc_codegen_ssa/src/traits/declare.rs

+1-45
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,7 @@
11
use super::BackendTypes;
22
use rustc_hir::def_id::DefId;
33
use rustc_middle::mir::mono::{Linkage, Visibility};
4-
use rustc_middle::ty::{Instance, Ty};
5-
use rustc_target::abi::call::FnAbi;
6-
7-
pub trait DeclareMethods<'tcx>: BackendTypes {
8-
/// Declare a global value.
9-
///
10-
/// If there’s a value with the same name already declared, the function will
11-
/// return its Value instead.
12-
fn declare_global(&self, name: &str, ty: Self::Type) -> Self::Value;
13-
14-
/// Declare a C ABI function.
15-
///
16-
/// Only use this for foreign function ABIs and glue. For Rust functions use
17-
/// `declare_fn` instead.
18-
///
19-
/// If there’s a value with the same name already declared, the function will
20-
/// update the declaration and return existing Value instead.
21-
fn declare_cfn(&self, name: &str, fn_type: Self::Type) -> Self::Function;
22-
23-
/// Declare a Rust function.
24-
///
25-
/// If there’s a value with the same name already declared, the function will
26-
/// update the declaration and return existing Value instead.
27-
fn declare_fn(&self, name: &str, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) -> Self::Function;
28-
29-
/// Declare a global with an intention to define it.
30-
///
31-
/// Use this function when you intend to define a global. This function will
32-
/// return `None` if the name already has a definition associated with it. In that
33-
/// case an error should be reported to the user, because it usually happens due
34-
/// to user’s fault (e.g., misuse of `#[no_mangle]` or `#[export_name]` attributes).
35-
fn define_global(&self, name: &str, ty: Self::Type) -> Option<Self::Value>;
36-
37-
/// Declare a private global
38-
///
39-
/// Use this function when you intend to define a global without a name.
40-
fn define_private_global(&self, ty: Self::Type) -> Self::Value;
41-
42-
/// Gets declared value by name.
43-
fn get_declared_value(&self, name: &str) -> Option<Self::Value>;
44-
45-
/// Gets defined or externally defined (AvailableExternally linkage) value by
46-
/// name.
47-
fn get_defined_value(&self, name: &str) -> Option<Self::Value>;
48-
}
4+
use rustc_middle::ty::Instance;
495

506
pub trait PreDefineMethods<'tcx>: BackendTypes {
517
fn predefine_static(

compiler/rustc_codegen_ssa/src/traits/misc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ pub trait MiscMethods<'tcx>: BackendTypes {
1919
fn set_frame_pointer_elimination(&self, llfn: Self::Function);
2020
fn apply_target_cpu_attr(&self, llfn: Self::Function);
2121
fn create_used_variable(&self);
22+
/// Declares the extern "C" main function for the entry point. Returns None if the symbol already exists.
23+
fn declare_c_main(&self, fn_type: Self::Type) -> Option<Self::Function>;
2224
}

compiler/rustc_codegen_ssa/src/traits/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub use self::builder::{BuilderMethods, OverflowOp};
3535
pub use self::consts::ConstMethods;
3636
pub use self::coverageinfo::{CoverageInfoBuilderMethods, CoverageInfoMethods};
3737
pub use self::debuginfo::{DebugInfoBuilderMethods, DebugInfoMethods};
38-
pub use self::declare::{DeclareMethods, PreDefineMethods};
38+
pub use self::declare::PreDefineMethods;
3939
pub use self::intrinsic::IntrinsicCallMethods;
4040
pub use self::misc::MiscMethods;
4141
pub use self::statics::{StaticBuilderMethods, StaticMethods};
@@ -60,7 +60,6 @@ pub trait CodegenMethods<'tcx>:
6060
+ StaticMethods
6161
+ CoverageInfoMethods
6262
+ DebugInfoMethods<'tcx>
63-
+ DeclareMethods<'tcx>
6463
+ AsmMethods
6564
+ PreDefineMethods<'tcx>
6665
+ HasParamEnv<'tcx>
@@ -77,7 +76,6 @@ impl<'tcx, T> CodegenMethods<'tcx> for T where
7776
+ StaticMethods
7877
+ CoverageInfoMethods
7978
+ DebugInfoMethods<'tcx>
80-
+ DeclareMethods<'tcx>
8179
+ AsmMethods
8280
+ PreDefineMethods<'tcx>
8381
+ HasParamEnv<'tcx>

compiler/rustc_data_structures/src/macros.rs

-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
/// A simple static assertion macro.
2-
#[macro_export]
3-
#[allow_internal_unstable(type_ascription)]
4-
macro_rules! static_assert {
5-
($test:expr) => {
6-
// Use the bool to access an array such that if the bool is false, the access
7-
// is out-of-bounds.
8-
#[allow(dead_code)]
9-
const _: () = [()][!($test: bool) as usize];
10-
};
11-
}
12-
131
/// Type size assertion. The first argument is a type and the second argument is its expected size.
142
#[macro_export]
153
macro_rules! static_assert_size {

compiler/rustc_data_structures/src/profiling.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,7 @@ pub fn print_time_passes_entry(do_it: bool, what: &str, dur: Duration) {
600600
// Hack up our own formatting for the duration to make it easier for scripts
601601
// to parse (always use the same number of decimal places and the same unit).
602602
pub fn duration_to_secs_str(dur: std::time::Duration) -> String {
603-
const NANOS_PER_SEC: f64 = 1_000_000_000.0;
604-
let secs = dur.as_secs() as f64 + dur.subsec_nanos() as f64 / NANOS_PER_SEC;
605-
606-
format!("{:.3}", secs)
603+
format!("{:.3}", dur.as_secs_f64())
607604
}
608605

609606
// Memory reporting

compiler/rustc_errors/src/emitter.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -1227,26 +1227,22 @@ impl EmitterWriter {
12271227
}
12281228
draw_note_separator(&mut buffer, 0, max_line_num_len + 1);
12291229
if *level != Level::FailureNote {
1230-
let level_str = level.to_string();
1231-
if !level_str.is_empty() {
1232-
buffer.append(0, &level_str, Style::MainHeaderMsg);
1233-
buffer.append(0, ": ", Style::NoStyle);
1234-
}
1230+
buffer.append(0, level.to_str(), Style::MainHeaderMsg);
1231+
buffer.append(0, ": ", Style::NoStyle);
12351232
}
12361233
self.msg_to_buffer(&mut buffer, msg, max_line_num_len, "note", None);
12371234
} else {
1238-
let level_str = level.to_string();
12391235
// The failure note level itself does not provide any useful diagnostic information
1240-
if *level != Level::FailureNote && !level_str.is_empty() {
1241-
buffer.append(0, &level_str, Style::Level(*level));
1236+
if *level != Level::FailureNote {
1237+
buffer.append(0, level.to_str(), Style::Level(*level));
12421238
}
12431239
// only render error codes, not lint codes
12441240
if let Some(DiagnosticId::Error(ref code)) = *code {
12451241
buffer.append(0, "[", Style::Level(*level));
12461242
buffer.append(0, &code, Style::Level(*level));
12471243
buffer.append(0, "]", Style::Level(*level));
12481244
}
1249-
if *level != Level::FailureNote && !level_str.is_empty() {
1245+
if *level != Level::FailureNote {
12501246
buffer.append(0, ": ", header_style);
12511247
}
12521248
for &(ref text, _) in msg.iter() {
@@ -1548,11 +1544,9 @@ impl EmitterWriter {
15481544
let mut buffer = StyledBuffer::new();
15491545

15501546
// Render the suggestion message
1551-
let level_str = level.to_string();
1552-
if !level_str.is_empty() {
1553-
buffer.append(0, &level_str, Style::Level(*level));
1554-
buffer.append(0, ": ", Style::HeaderMsg);
1555-
}
1547+
buffer.append(0, level.to_str(), Style::Level(*level));
1548+
buffer.append(0, ": ", Style::HeaderMsg);
1549+
15561550
self.msg_to_buffer(
15571551
&mut buffer,
15581552
&[(suggestion.msg.to_owned(), Style::NoStyle)],

compiler/rustc_errors/src/lib.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -973,16 +973,14 @@ impl HandlerInner {
973973

974974
fn panic_if_treat_err_as_bug(&self) {
975975
if self.treat_err_as_bug() {
976-
let s = match (self.err_count(), self.flags.treat_err_as_bug.unwrap_or(0)) {
977-
(0, _) => return,
978-
(1, 1) => "aborting due to `-Z treat-err-as-bug=1`".to_string(),
979-
(1, _) => return,
980-
(count, as_bug) => format!(
976+
match (self.err_count(), self.flags.treat_err_as_bug.unwrap_or(0)) {
977+
(1, 1) => panic!("aborting due to `-Z treat-err-as-bug=1`"),
978+
(0, _) | (1, _) => {}
979+
(count, as_bug) => panic!(
981980
"aborting after {} errors due to `-Z treat-err-as-bug={}`",
982981
count, as_bug,
983982
),
984-
};
985-
panic!(s);
983+
}
986984
}
987985
}
988986
}

config.toml.example

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
# a custom configuration file can also be specified with `--config` to the build
1010
# system.
1111

12+
# =============================================================================
13+
# Global Settings
14+
# =============================================================================
15+
16+
# Use different pre-set defaults than the global defaults.
17+
#
18+
# See `src/bootstrap/defaults` for more information.
19+
# Note that this has no default value (x.py uses the defaults in `config.toml.example`).
20+
#profile = <none>
21+
1222
# =============================================================================
1323
# Tweaking how LLVM is compiled
1424
# =============================================================================

0 commit comments

Comments
 (0)