Skip to content

Commit 0d50ab7

Browse files
committed
Auto merge of #113391 - fee1-dead-contrib:rollup-9bqlw9z, r=fee1-dead
Rollup of 9 pull requests Successful merges: - #111119 (style-guide: Add chapter about formatting for nightly-only syntax) - #112791 (llvm ffi: Expose `CallInst->setTailCallKind`) - #113145 (style-guide: Document newline rules for assignment operators) - #113163 (Add a regression test for #112895) - #113332 (resolve: Use `Interned` for some interned structures) - #113334 (Revert the lexing of `c"…"` string literals) - #113350 (Fix the issue of wrong diagnosis for extern pub fn) - #113371 (Fix submodule handling when the current branch is named after a tag) - #113384 (style-guide: Clarify grammar for small patterns (not a semantic change)) r? `@ghost` `@rustbot` modify labels: rollup
2 parents bd8aabe + c668eb0 commit 0d50ab7

Some content is hidden

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

53 files changed

+572
-370
lines changed

Cargo.lock

+11
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,16 @@ dependencies = [
766766
"typenum",
767767
]
768768

769+
[[package]]
770+
name = "cstr"
771+
version = "0.2.8"
772+
source = "registry+https://github.com/rust-lang/crates.io-index"
773+
checksum = "c11a39d776a3b35896711da8a04dc1835169dcd36f710878187637314e47941b"
774+
dependencies = [
775+
"proc-macro2",
776+
"quote",
777+
]
778+
769779
[[package]]
770780
name = "ctrlc"
771781
version = "3.4.0"
@@ -3016,6 +3026,7 @@ name = "rustc_codegen_llvm"
30163026
version = "0.0.0"
30173027
dependencies = [
30183028
"bitflags 1.3.2",
3029+
"cstr",
30193030
"libc",
30203031
"measureme",
30213032
"object",

compiler/rustc_codegen_llvm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test = false
88

99
[dependencies]
1010
bitflags = "1.0"
11+
cstr = "0.2"
1112
libc = "0.2"
1213
measureme = "10.0.0"
1314
object = { version = "0.31.1", default-features = false, features = [

compiler/rustc_codegen_llvm/src/allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) unsafe fn codegen(
7777
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
7878
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
7979

80-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast());
80+
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
8181

8282
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
8383
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
@@ -129,7 +129,7 @@ pub(crate) unsafe fn codegen(
129129
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
130130
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
131131

132-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast());
132+
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
133133

134134
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
135135
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);

compiler/rustc_codegen_llvm/src/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ pub(crate) fn run_pass_manager(
601601
llvm::LLVMRustAddModuleFlag(
602602
module.module_llvm.llmod(),
603603
llvm::LLVMModFlagBehavior::Error,
604-
c"LTOPostLink".as_ptr().cast(),
604+
"LTOPostLink\0".as_ptr().cast(),
605605
1,
606606
);
607607
}

compiler/rustc_codegen_llvm/src/back/write.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -931,16 +931,16 @@ unsafe fn embed_bitcode(
931931
let llglobal = llvm::LLVMAddGlobal(
932932
llmod,
933933
common::val_ty(llconst),
934-
c"rustc.embedded.module".as_ptr().cast(),
934+
"rustc.embedded.module\0".as_ptr().cast(),
935935
);
936936
llvm::LLVMSetInitializer(llglobal, llconst);
937937

938938
let section = if is_apple {
939-
c"__LLVM,__bitcode"
939+
"__LLVM,__bitcode\0"
940940
} else if is_aix {
941-
c".ipa"
941+
".ipa\0"
942942
} else {
943-
c".llvmbc"
943+
".llvmbc\0"
944944
};
945945
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
946946
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
@@ -950,15 +950,15 @@ unsafe fn embed_bitcode(
950950
let llglobal = llvm::LLVMAddGlobal(
951951
llmod,
952952
common::val_ty(llconst),
953-
c"rustc.embedded.cmdline".as_ptr().cast(),
953+
"rustc.embedded.cmdline\0".as_ptr().cast(),
954954
);
955955
llvm::LLVMSetInitializer(llglobal, llconst);
956956
let section = if is_apple {
957-
c"__LLVM,__cmdline"
957+
"__LLVM,__cmdline\0"
958958
} else if is_aix {
959-
c".info"
959+
".info\0"
960960
} else {
961-
c".llvmcmd"
961+
".llvmcmd\0"
962962
};
963963
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
964964
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);

compiler/rustc_codegen_llvm/src/base.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use crate::context::CodegenCx;
1919
use crate::llvm;
2020
use crate::value::Value;
2121

22+
use cstr::cstr;
23+
2224
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
2325
use rustc_codegen_ssa::mono_item::MonoItemExt;
2426
use rustc_codegen_ssa::traits::*;
@@ -108,11 +110,11 @@ pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol) -> (ModuleCodegen
108110

109111
// Create the llvm.used and llvm.compiler.used variables.
110112
if !cx.used_statics.borrow().is_empty() {
111-
cx.create_used_variable_impl(c"llvm.used", &*cx.used_statics.borrow());
113+
cx.create_used_variable_impl(cstr!("llvm.used"), &*cx.used_statics.borrow());
112114
}
113115
if !cx.compiler_used_statics.borrow().is_empty() {
114116
cx.create_used_variable_impl(
115-
c"llvm.compiler.used",
117+
cstr!("llvm.compiler.used"),
116118
&*cx.compiler_used_statics.borrow(),
117119
);
118120
}

compiler/rustc_codegen_llvm/src/builder.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, True}
66
use crate::type_::Type;
77
use crate::type_of::LayoutLlvmExt;
88
use crate::value::Value;
9+
use cstr::cstr;
910
use libc::{c_char, c_uint};
1011
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind};
1112
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
@@ -25,6 +26,7 @@ use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2526
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
2627
use smallvec::SmallVec;
2728
use std::borrow::Cow;
29+
use std::ffi::CStr;
2830
use std::iter;
2931
use std::ops::Deref;
3032
use std::ptr;
@@ -44,10 +46,13 @@ impl Drop for Builder<'_, '_, '_> {
4446
}
4547
}
4648

49+
// FIXME(eddyb) use a checked constructor when they become `const fn`.
50+
const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
51+
4752
/// Empty string, to be used where LLVM expects an instruction name, indicating
4853
/// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
4954
// FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
50-
const UNNAMED: *const c_char = c"".as_ptr();
55+
const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
5156

5257
impl<'ll, 'tcx> BackendTypes for Builder<'_, 'll, 'tcx> {
5358
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
@@ -1002,13 +1007,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10021007
}
10031008

10041009
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
1010+
let name = cstr!("cleanuppad");
10051011
let ret = unsafe {
10061012
llvm::LLVMBuildCleanupPad(
10071013
self.llbuilder,
10081014
parent,
10091015
args.as_ptr(),
10101016
args.len() as c_uint,
1011-
c"cleanuppad".as_ptr(),
1017+
name.as_ptr(),
10121018
)
10131019
};
10141020
Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
@@ -1022,13 +1028,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10221028
}
10231029

10241030
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
1031+
let name = cstr!("catchpad");
10251032
let ret = unsafe {
10261033
llvm::LLVMBuildCatchPad(
10271034
self.llbuilder,
10281035
parent,
10291036
args.as_ptr(),
10301037
args.len() as c_uint,
1031-
c"catchpad".as_ptr(),
1038+
name.as_ptr(),
10321039
)
10331040
};
10341041
Funclet::new(ret.expect("LLVM does not have support for catchpad"))
@@ -1040,13 +1047,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10401047
unwind: Option<&'ll BasicBlock>,
10411048
handlers: &[&'ll BasicBlock],
10421049
) -> &'ll Value {
1050+
let name = cstr!("catchswitch");
10431051
let ret = unsafe {
10441052
llvm::LLVMBuildCatchSwitch(
10451053
self.llbuilder,
10461054
parent,
10471055
unwind,
10481056
handlers.len() as c_uint,
1049-
c"catchswitch".as_ptr(),
1057+
name.as_ptr(),
10501058
)
10511059
};
10521060
let ret = ret.expect("LLVM does not have support for catchswitch");

compiler/rustc_codegen_llvm/src/consts.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::llvm::{self, True};
88
use crate::type_::Type;
99
use crate::type_of::LayoutLlvmExt;
1010
use crate::value::Value;
11+
use cstr::cstr;
1112
use rustc_codegen_ssa::traits::*;
1213
use rustc_hir::def_id::DefId;
1314
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
@@ -481,9 +482,9 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
481482
.all(|&byte| byte == 0);
482483

483484
let sect_name = if all_bytes_are_zero {
484-
c"__DATA,__thread_bss"
485+
cstr!("__DATA,__thread_bss")
485486
} else {
486-
c"__DATA,__thread_data"
487+
cstr!("__DATA,__thread_data")
487488
};
488489
llvm::LLVMSetSection(g, sect_name.as_ptr());
489490
}
@@ -512,7 +513,7 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
512513
let val = llvm::LLVMMetadataAsValue(self.llcx, meta);
513514
llvm::LLVMAddNamedMetadataOperand(
514515
self.llmod,
515-
c"wasm.custom_sections".as_ptr().cast(),
516+
"wasm.custom_sections\0".as_ptr().cast(),
516517
val,
517518
);
518519
}

compiler/rustc_codegen_llvm/src/context.rs

+20-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::llvm_util;
88
use crate::type_::Type;
99
use crate::value::Value;
1010

11+
use cstr::cstr;
1112
use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
1213
use rustc_codegen_ssa::traits::*;
1314
use rustc_data_structures::base_n;
@@ -223,42 +224,36 @@ pub unsafe fn create_module<'ll>(
223224
// If skipping the PLT is enabled, we need to add some module metadata
224225
// to ensure intrinsic calls don't use it.
225226
if !sess.needs_plt() {
226-
llvm::LLVMRustAddModuleFlag(
227-
llmod,
228-
llvm::LLVMModFlagBehavior::Warning,
229-
c"RtLibUseGOT".as_ptr().cast(),
230-
1,
231-
);
227+
let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
228+
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
232229
}
233230

234231
// Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
235232
if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
233+
let canonical_jump_tables = "CFI Canonical Jump Tables\0".as_ptr().cast();
236234
llvm::LLVMRustAddModuleFlag(
237235
llmod,
238236
llvm::LLVMModFlagBehavior::Override,
239-
c"CFI Canonical Jump Tables".as_ptr().cast(),
237+
canonical_jump_tables,
240238
1,
241239
);
242240
}
243241

244242
// Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.)
245243
if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
244+
let enable_split_lto_unit = "EnableSplitLTOUnit\0".as_ptr().cast();
246245
llvm::LLVMRustAddModuleFlag(
247246
llmod,
248247
llvm::LLVMModFlagBehavior::Override,
249-
c"EnableSplitLTOUnit".as_ptr().cast(),
248+
enable_split_lto_unit,
250249
1,
251250
);
252251
}
253252

254253
// Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
255254
if sess.is_sanitizer_kcfi_enabled() {
256-
llvm::LLVMRustAddModuleFlag(
257-
llmod,
258-
llvm::LLVMModFlagBehavior::Override,
259-
c"kcfi".as_ptr().cast(),
260-
1,
261-
);
255+
let kcfi = "kcfi\0".as_ptr().cast();
256+
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
262257
}
263258

264259
// Control Flow Guard is currently only supported by the MSVC linker on Windows.
@@ -270,7 +265,7 @@ pub unsafe fn create_module<'ll>(
270265
llvm::LLVMRustAddModuleFlag(
271266
llmod,
272267
llvm::LLVMModFlagBehavior::Warning,
273-
c"cfguard".as_ptr() as *const _,
268+
"cfguard\0".as_ptr() as *const _,
274269
1,
275270
)
276271
}
@@ -279,7 +274,7 @@ pub unsafe fn create_module<'ll>(
279274
llvm::LLVMRustAddModuleFlag(
280275
llmod,
281276
llvm::LLVMModFlagBehavior::Warning,
282-
c"cfguard".as_ptr() as *const _,
277+
"cfguard\0".as_ptr() as *const _,
283278
2,
284279
)
285280
}
@@ -297,26 +292,26 @@ pub unsafe fn create_module<'ll>(
297292
llvm::LLVMRustAddModuleFlag(
298293
llmod,
299294
behavior,
300-
c"branch-target-enforcement".as_ptr().cast(),
295+
"branch-target-enforcement\0".as_ptr().cast(),
301296
bti.into(),
302297
);
303298
llvm::LLVMRustAddModuleFlag(
304299
llmod,
305300
behavior,
306-
c"sign-return-address".as_ptr().cast(),
301+
"sign-return-address\0".as_ptr().cast(),
307302
pac_ret.is_some().into(),
308303
);
309304
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
310305
llvm::LLVMRustAddModuleFlag(
311306
llmod,
312307
behavior,
313-
c"sign-return-address-all".as_ptr().cast(),
308+
"sign-return-address-all\0".as_ptr().cast(),
314309
pac_opts.leaf.into(),
315310
);
316311
llvm::LLVMRustAddModuleFlag(
317312
llmod,
318313
behavior,
319-
c"sign-return-address-with-bkey".as_ptr().cast(),
314+
"sign-return-address-with-bkey\0".as_ptr().cast(),
320315
u32::from(pac_opts.key == PAuthKey::B),
321316
);
322317
} else {
@@ -332,15 +327,15 @@ pub unsafe fn create_module<'ll>(
332327
llvm::LLVMRustAddModuleFlag(
333328
llmod,
334329
llvm::LLVMModFlagBehavior::Override,
335-
c"cf-protection-branch".as_ptr().cast(),
330+
"cf-protection-branch\0".as_ptr().cast(),
336331
1,
337332
)
338333
}
339334
if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
340335
llvm::LLVMRustAddModuleFlag(
341336
llmod,
342337
llvm::LLVMModFlagBehavior::Override,
343-
c"cf-protection-return".as_ptr().cast(),
338+
"cf-protection-return\0".as_ptr().cast(),
344339
1,
345340
)
346341
}
@@ -349,7 +344,7 @@ pub unsafe fn create_module<'ll>(
349344
llvm::LLVMRustAddModuleFlag(
350345
llmod,
351346
llvm::LLVMModFlagBehavior::Error,
352-
c"Virtual Function Elim".as_ptr().cast(),
347+
"Virtual Function Elim\0".as_ptr().cast(),
353348
1,
354349
);
355350
}
@@ -481,13 +476,14 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
481476
}
482477

483478
pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
479+
let section = cstr!("llvm.metadata");
484480
let array = self.const_array(self.type_ptr_to(self.type_i8()), values);
485481

486482
unsafe {
487483
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
488484
llvm::LLVMSetInitializer(g, array);
489485
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
490-
llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
486+
llvm::LLVMSetSection(g, section.as_ptr());
491487
}
492488
}
493489
}

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
3838
unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) };
3939

4040
section_var.unwrap_or_else(|| {
41+
let section_name = b".debug_gdb_scripts\0";
4142
let mut section_contents = Vec::new();
4243

4344
// Add the pretty printers for the standard library first.
@@ -70,7 +71,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
7071
let section_var = cx
7172
.define_global(section_var_name, llvm_type)
7273
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
73-
llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr().cast());
74+
llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
7475
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
7576
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
7677
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);

0 commit comments

Comments
 (0)