Skip to content

Commit 601e588

Browse files
committed
Auto merge of #143983 - samueltardieu:rollup-dnoe5xp, r=samueltardieu
Rollup of 12 pull requests Successful merges: - #142936 (rustdoc-json: Structured attributes) - #143355 (wrapping shift: remove first bitmask and table) - #143448 (remote-test-client: Exit code `128 + <signal-number>` instead of `3`) - #143692 (miri: fix out-of-bounds error for ptrs with negative offsets) - #143719 (Emit warning when there is no space between `-o` and arg) - #143738 (Move several float tests to floats/mod.rs) - #143891 (Port `#[coverage]` to the new attribute system) - #143920 (Make more of codegen_llvm safe) - #143921 (Constify `Index` traits) - #143939 (Add 0323pin as maintainer of NetBSD targets, fix link to pkgsrc-wip and explain.) - #143948 (Update mdbook to 0.4.52) - #143968 (Add tracing to `InterpCx::fn_abi_of_instance/fn_abi_of_fn_ptr`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3014e79 + 2c0f4f6 commit 601e588

File tree

100 files changed

+1588
-1563
lines changed

Some content is hidden

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

100 files changed

+1588
-1563
lines changed

compiler/rustc_attr_data_structures/src/attributes.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ pub enum DeprecatedSince {
110110
Err,
111111
}
112112

113+
#[derive(
114+
Copy,
115+
Debug,
116+
Eq,
117+
PartialEq,
118+
Encodable,
119+
Decodable,
120+
Clone,
121+
HashStable_Generic,
122+
PrintAttribute
123+
)]
124+
pub enum CoverageStatus {
125+
On,
126+
Off,
127+
}
128+
113129
impl Deprecation {
114130
/// Whether an item marked with #[deprecated(since = "X")] is currently
115131
/// deprecated (i.e., whether X is not greater than the current rustc
@@ -274,6 +290,9 @@ pub enum AttributeKind {
274290
/// Represents `#[const_trait]`.
275291
ConstTrait(Span),
276292

293+
/// Represents `#[coverage]`.
294+
Coverage(Span, CoverageStatus),
295+
277296
///Represents `#[rustc_deny_explicit_impl]`.
278297
DenyExplicitImpl(Span),
279298

compiler/rustc_attr_data_structures/src/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ impl AttributeKind {
2828
ConstStability { .. } => Yes,
2929
ConstStabilityIndirect => No,
3030
ConstTrait(..) => No,
31+
Coverage(..) => No,
3132
DenyExplicitImpl(..) => No,
3233
Deprecation { .. } => Yes,
3334
DoNotImplementViaObject(..) => No,

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_attr_data_structures::{AttributeKind, OptimizeAttr, UsedBy};
1+
use rustc_attr_data_structures::{AttributeKind, CoverageStatus, OptimizeAttr, UsedBy};
22
use rustc_feature::{AttributeTemplate, template};
33
use rustc_session::parse::feature_err;
44
use rustc_span::{Span, Symbol, sym};
@@ -52,6 +52,45 @@ impl<S: Stage> NoArgsAttributeParser<S> for ColdParser {
5252
const CREATE: fn(Span) -> AttributeKind = AttributeKind::Cold;
5353
}
5454

55+
pub(crate) struct CoverageParser;
56+
57+
impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
58+
const PATH: &[Symbol] = &[sym::coverage];
59+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
60+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
61+
const TEMPLATE: AttributeTemplate = template!(OneOf: &[sym::off, sym::on]);
62+
63+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
64+
let Some(args) = args.list() else {
65+
cx.expected_specific_argument_and_list(cx.attr_span, vec!["on", "off"]);
66+
return None;
67+
};
68+
69+
let Some(arg) = args.single() else {
70+
cx.expected_single_argument(args.span);
71+
return None;
72+
};
73+
74+
let fail_incorrect_argument = |span| cx.expected_specific_argument(span, vec!["on", "off"]);
75+
76+
let Some(arg) = arg.meta_item() else {
77+
fail_incorrect_argument(args.span);
78+
return None;
79+
};
80+
81+
let status = match arg.path().word_sym() {
82+
Some(sym::off) => CoverageStatus::Off,
83+
Some(sym::on) => CoverageStatus::On,
84+
None | Some(_) => {
85+
fail_incorrect_argument(arg.span());
86+
return None;
87+
}
88+
};
89+
90+
Some(AttributeKind::Coverage(cx.attr_span, status))
91+
}
92+
}
93+
5594
pub(crate) struct ExportNameParser;
5695

5796
impl<S: Stage> SingleAttributeParser<S> for ExportNameParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, Symbol, sym};
1515

1616
use crate::attributes::allow_unstable::{AllowConstFnUnstableParser, AllowInternalUnstableParser};
1717
use crate::attributes::codegen_attrs::{
18-
ColdParser, ExportNameParser, NakedParser, NoMangleParser, OmitGdbPrettyPrinterSectionParser,
19-
OptimizeParser, TargetFeatureParser, TrackCallerParser, UsedParser,
18+
ColdParser, CoverageParser, ExportNameParser, NakedParser, NoMangleParser,
19+
OmitGdbPrettyPrinterSectionParser, OptimizeParser, TargetFeatureParser, TrackCallerParser,
20+
UsedParser,
2021
};
2122
use crate::attributes::confusables::ConfusablesParser;
2223
use crate::attributes::deprecation::DeprecationParser;
@@ -136,6 +137,7 @@ attribute_parsers!(
136137
// tidy-alphabetical-end
137138

138139
// tidy-alphabetical-start
140+
Single<CoverageParser>,
139141
Single<DeprecationParser>,
140142
Single<DummyParser>,
141143
Single<ExportNameParser>,
@@ -449,6 +451,25 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
449451
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
450452
possibilities,
451453
strings: false,
454+
list: false,
455+
},
456+
})
457+
}
458+
459+
pub(crate) fn expected_specific_argument_and_list(
460+
&self,
461+
span: Span,
462+
possibilities: Vec<&'static str>,
463+
) -> ErrorGuaranteed {
464+
self.emit_err(AttributeParseError {
465+
span,
466+
attr_span: self.attr_span,
467+
template: self.template.clone(),
468+
attribute: self.attr_path.clone(),
469+
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
470+
possibilities,
471+
strings: false,
472+
list: true,
452473
},
453474
})
454475
}
@@ -466,6 +487,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
466487
reason: AttributeParseErrorReason::ExpectedSpecificArgument {
467488
possibilities,
468489
strings: true,
490+
list: false,
469491
},
470492
})
471493
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,22 @@ pub(crate) struct LinkOrdinalOutOfRange {
525525

526526
pub(crate) enum AttributeParseErrorReason {
527527
ExpectedNoArgs,
528-
ExpectedStringLiteral { byte_string: Option<Span> },
528+
ExpectedStringLiteral {
529+
byte_string: Option<Span>,
530+
},
529531
ExpectedIntegerLiteral,
530532
ExpectedAtLeastOneArgument,
531533
ExpectedSingleArgument,
532534
ExpectedList,
533535
UnexpectedLiteral,
534536
ExpectedNameValue(Option<Symbol>),
535537
DuplicateKey(Symbol),
536-
ExpectedSpecificArgument { possibilities: Vec<&'static str>, strings: bool },
538+
ExpectedSpecificArgument {
539+
possibilities: Vec<&'static str>,
540+
strings: bool,
541+
/// Should we tell the user to write a list when they didn't?
542+
list: bool,
543+
},
537544
}
538545

539546
pub(crate) struct AttributeParseError {
@@ -607,7 +614,11 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
607614
format!("expected this to be of the form `{name} = \"...\"`"),
608615
);
609616
}
610-
AttributeParseErrorReason::ExpectedSpecificArgument { possibilities, strings } => {
617+
AttributeParseErrorReason::ExpectedSpecificArgument {
618+
possibilities,
619+
strings,
620+
list: false,
621+
} => {
611622
let quote = if strings { '"' } else { '`' };
612623
match possibilities.as_slice() {
613624
&[] => {}
@@ -633,6 +644,38 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
633644
}
634645
}
635646
}
647+
AttributeParseErrorReason::ExpectedSpecificArgument {
648+
possibilities,
649+
strings,
650+
list: true,
651+
} => {
652+
let quote = if strings { '"' } else { '`' };
653+
match possibilities.as_slice() {
654+
&[] => {}
655+
&[x] => {
656+
diag.span_label(
657+
self.span,
658+
format!(
659+
"this attribute is only valid with {quote}{x}{quote} as an argument"
660+
),
661+
);
662+
}
663+
[first, second] => {
664+
diag.span_label(self.span, format!("this attribute is only valid with either {quote}{first}{quote} or {quote}{second}{quote} as an argument"));
665+
}
666+
[first @ .., second_to_last, last] => {
667+
let mut res = String::new();
668+
for i in first {
669+
res.push_str(&format!("{quote}{i}{quote}, "));
670+
}
671+
res.push_str(&format!(
672+
"{quote}{second_to_last}{quote} or {quote}{last}{quote}"
673+
));
674+
675+
diag.span_label(self.span, format!("this attribute is only valid with one of the following arguments: {res}"));
676+
}
677+
}
678+
}
636679
}
637680

638681
let suggestions = self.template.suggestions(false, &name);

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,7 @@ pub(crate) fn codegen(
879879
.generic_activity_with_arg("LLVM_module_codegen_embed_bitcode", &*module.name);
880880
let thin_bc =
881881
module.thin_lto_buffer.as_deref().expect("cannot find embedded bitcode");
882-
unsafe {
883-
embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &thin_bc);
884-
}
882+
embed_bitcode(cgcx, llcx, llmod, &config.bc_cmdline, &thin_bc);
885883
}
886884
}
887885

@@ -945,7 +943,7 @@ pub(crate) fn codegen(
945943
// binaries. So we must clone the module to produce the asm output
946944
// if we are also producing object code.
947945
let llmod = if let EmitObj::ObjectCode(_) = config.emit_obj {
948-
unsafe { llvm::LLVMCloneModule(llmod) }
946+
llvm::LLVMCloneModule(llmod)
949947
} else {
950948
llmod
951949
};
@@ -1073,7 +1071,7 @@ pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) ->
10731071
}
10741072

10751073
/// Embed the bitcode of an LLVM module for LTO in the LLVM module itself.
1076-
unsafe fn embed_bitcode(
1074+
fn embed_bitcode(
10771075
cgcx: &CodegenContext<LlvmCodegenBackend>,
10781076
llcx: &llvm::Context,
10791077
llmod: &llvm::Module,
@@ -1115,43 +1113,40 @@ unsafe fn embed_bitcode(
11151113
// Unfortunately, LLVM provides no way to set custom section flags. For ELF
11161114
// and COFF we emit the sections using module level inline assembly for that
11171115
// reason (see issue #90326 for historical background).
1118-
unsafe {
1119-
if cgcx.target_is_like_darwin
1120-
|| cgcx.target_is_like_aix
1121-
|| cgcx.target_arch == "wasm32"
1122-
|| cgcx.target_arch == "wasm64"
1123-
{
1124-
// We don't need custom section flags, create LLVM globals.
1125-
let llconst = common::bytes_in_context(llcx, bitcode);
1126-
let llglobal =
1127-
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
1128-
llvm::set_initializer(llglobal, llconst);
1129-
1130-
llvm::set_section(llglobal, bitcode_section_name(cgcx));
1131-
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1132-
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
1133-
1134-
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
1135-
let llglobal =
1136-
llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
1137-
llvm::set_initializer(llglobal, llconst);
1138-
let section = if cgcx.target_is_like_darwin {
1139-
c"__LLVM,__cmdline"
1140-
} else if cgcx.target_is_like_aix {
1141-
c".info"
1142-
} else {
1143-
c".llvmcmd"
1144-
};
1145-
llvm::set_section(llglobal, section);
1146-
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1116+
1117+
if cgcx.target_is_like_darwin
1118+
|| cgcx.target_is_like_aix
1119+
|| cgcx.target_arch == "wasm32"
1120+
|| cgcx.target_arch == "wasm64"
1121+
{
1122+
// We don't need custom section flags, create LLVM globals.
1123+
let llconst = common::bytes_in_context(llcx, bitcode);
1124+
let llglobal = llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.module");
1125+
llvm::set_initializer(llglobal, llconst);
1126+
1127+
llvm::set_section(llglobal, bitcode_section_name(cgcx));
1128+
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1129+
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
1130+
1131+
let llconst = common::bytes_in_context(llcx, cmdline.as_bytes());
1132+
let llglobal = llvm::add_global(llmod, common::val_ty(llconst), c"rustc.embedded.cmdline");
1133+
llvm::set_initializer(llglobal, llconst);
1134+
let section = if cgcx.target_is_like_darwin {
1135+
c"__LLVM,__cmdline"
1136+
} else if cgcx.target_is_like_aix {
1137+
c".info"
11471138
} else {
1148-
// We need custom section flags, so emit module-level inline assembly.
1149-
let section_flags = if cgcx.is_pe_coff { "n" } else { "e" };
1150-
let asm = create_section_with_flags_asm(".llvmbc", section_flags, bitcode);
1151-
llvm::append_module_inline_asm(llmod, &asm);
1152-
let asm = create_section_with_flags_asm(".llvmcmd", section_flags, cmdline.as_bytes());
1153-
llvm::append_module_inline_asm(llmod, &asm);
1154-
}
1139+
c".llvmcmd"
1140+
};
1141+
llvm::set_section(llglobal, section);
1142+
llvm::set_linkage(llglobal, llvm::Linkage::PrivateLinkage);
1143+
} else {
1144+
// We need custom section flags, so emit module-level inline assembly.
1145+
let section_flags = if cgcx.is_pe_coff { "n" } else { "e" };
1146+
let asm = create_section_with_flags_asm(".llvmbc", section_flags, bitcode);
1147+
llvm::append_module_inline_asm(llmod, &asm);
1148+
let asm = create_section_with_flags_asm(".llvmcmd", section_flags, cmdline.as_bytes());
1149+
llvm::append_module_inline_asm(llmod, &asm);
11551150
}
11561151
}
11571152

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
302302
return;
303303
}
304304

305-
let id_str = "branch_weights";
306-
let id = unsafe {
307-
llvm::LLVMMDStringInContext2(self.cx.llcx, id_str.as_ptr().cast(), id_str.len())
308-
};
305+
let id = self.cx.create_metadata(b"branch_weights");
309306

310307
// For switch instructions with 2 targets, the `llvm.expect` intrinsic is used.
311308
// This function handles switch instructions with more than 2 targets and it needs to
@@ -637,17 +634,16 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
637634
} else if place.layout.is_llvm_immediate() {
638635
let mut const_llval = None;
639636
let llty = place.layout.llvm_type(self);
640-
unsafe {
641-
if let Some(global) = llvm::LLVMIsAGlobalVariable(place.val.llval) {
642-
if llvm::LLVMIsGlobalConstant(global) == llvm::True {
643-
if let Some(init) = llvm::LLVMGetInitializer(global) {
644-
if self.val_ty(init) == llty {
645-
const_llval = Some(init);
646-
}
637+
if let Some(global) = llvm::LLVMIsAGlobalVariable(place.val.llval) {
638+
if llvm::LLVMIsGlobalConstant(global) == llvm::True {
639+
if let Some(init) = llvm::LLVMGetInitializer(global) {
640+
if self.val_ty(init) == llty {
641+
const_llval = Some(init);
647642
}
648643
}
649644
}
650645
}
646+
651647
let llval = const_llval.unwrap_or_else(|| {
652648
let load = self.load(llty, place.val.llval, place.val.align);
653649
if let abi::BackendRepr::Scalar(scalar) = place.layout.backend_repr {
@@ -1721,7 +1717,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17211717
} else {
17221718
cfi::typeid_for_fnabi(self.tcx, fn_abi, options)
17231719
};
1724-
let typeid_metadata = self.cx.typeid_metadata(typeid).unwrap();
1720+
let typeid_metadata = self.cx.create_metadata(typeid.as_bytes());
17251721
let dbg_loc = self.get_dbg_loc();
17261722

17271723
// Test whether the function pointer is associated with the type identifier using the

0 commit comments

Comments
 (0)