Skip to content

Commit 037b621

Browse files
committed
Auto merge of #156794 - JonathanBrouwer:rollup-VOF2Ff4, r=JonathanBrouwer
Rollup of 2 pull requests Successful merges: - #156242 (Remove unsound `target_feature_inline_always` feature) - #156791 (Unsafe kept in help text)
2 parents b954122 + f84347a commit 037b621

26 files changed

Lines changed: 92 additions & 465 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,6 +4010,7 @@ dependencies = [
40104010
name = "rustc_feature"
40114011
version = "0.0.0"
40124012
dependencies = [
4013+
"rustc_ast",
40134014
"rustc_data_structures",
40144015
"rustc_hir",
40154016
"rustc_span",

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,11 @@ pub fn parse_cfg_attr(
327327
}) {
328328
Ok(r) => return Some(r),
329329
Err(e) => {
330-
let suggestions = CFG_ATTR_TEMPLATE
331-
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr);
330+
let suggestions = CFG_ATTR_TEMPLATE.suggestions(
331+
AttrSuggestionStyle::Attribute(cfg_attr.style),
332+
cfg_attr.get_normal_item().unsafety,
333+
sym::cfg_attr,
334+
);
332335
e.with_span_suggestions(
333336
cfg_attr.span,
334337
"must be of the form",
@@ -360,8 +363,11 @@ pub fn parse_cfg_attr(
360363
description: ParsedDescription::Attribute,
361364
reason,
362365
suggestions: session_diagnostics::AttributeParseErrorSuggestions::CreatedByTemplate(
363-
CFG_ATTR_TEMPLATE
364-
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr),
366+
CFG_ATTR_TEMPLATE.suggestions(
367+
AttrSuggestionStyle::Attribute(cfg_attr.style),
368+
cfg_attr.get_normal_item().unsafety,
369+
sym::cfg_attr,
370+
),
365371
),
366372
});
367373
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::mem;
55
use std::ops::{Deref, DerefMut};
66
use std::sync::LazyLock;
77

8-
use rustc_ast::{AttrStyle, MetaItemLit};
8+
use rustc_ast::{AttrStyle, MetaItemLit, Safety};
99
use rustc_data_structures::sync::{DynSend, DynSync};
1010
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level, MultiSpan};
1111
use rustc_feature::{AttrSuggestionStyle, AttributeTemplate};
@@ -357,6 +357,9 @@ pub struct AcceptContext<'f, 'sess> {
357357
/// Used in reporting errors to give a hint to users what the attribute *should* look like.
358358
pub(crate) template: &'f AttributeTemplate,
359359

360+
/// The safety attribute (if any) applied to the attribute.
361+
pub(crate) attr_safety: Safety,
362+
360363
/// The name of the attribute we're currently accepting.
361364
pub(crate) attr_path: AttrPath,
362365
}
@@ -873,7 +876,7 @@ impl<'a, 'f, 'sess: 'f> AttributeDiagnosticContext<'a, 'f, 'sess> {
873876
ParsedDescription::Macro => AttrSuggestionStyle::Macro,
874877
};
875878

876-
self.template.suggestions(style, &self.attr_path)
879+
self.template.suggestions(style, self.attr_safety, &self.attr_path)
877880
}
878881
}
879882

@@ -1064,7 +1067,7 @@ impl<'a, 'f, 'sess: 'f> AttributeDiagnosticContext<'a, 'f, 'sess> {
10641067
ParsedDescription::Macro => AttrSuggestionStyle::Macro,
10651068
};
10661069

1067-
self.template.suggestions(style, &self.attr_path)
1070+
self.template.suggestions(style, self.attr_safety, &self.attr_path)
10681071
}
10691072
/// Error that a string literal was expected.
10701073
/// You can optionally give the literal you did find (which you found not to be a string literal)

compiler/rustc_attr_parsing/src/interface.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ impl<'sess> AttributeParser<'sess> {
234234
attr_style,
235235
parsed_description,
236236
template,
237+
attr_safety: attr_safety.unwrap_or(Safety::Default),
237238
attr_path,
238239
};
239240
parse_fn(&mut cx, args)
@@ -404,6 +405,7 @@ impl<'sess> AttributeParser<'sess> {
404405
attr_style: attr.style,
405406
parsed_description: ParsedDescription::Attribute,
406407
template: &accept.template,
408+
attr_safety: n.item.unsafety,
407409
attr_path: attr_path.clone(),
408410
};
409411

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,13 @@ pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: &str) {
4141
}
4242

4343
/// Get LLVM attribute for the provided inline heuristic.
44-
pub(crate) fn inline_attr<'ll, 'tcx>(
44+
#[inline]
45+
fn inline_attr<'ll>(
4546
cx: &SimpleCx<'ll>,
46-
tcx: TyCtxt<'tcx>,
47-
instance: ty::Instance<'tcx>,
47+
sess: &Session,
48+
inline: InlineAttr,
4849
) -> Option<&'ll Attribute> {
49-
// `optnone` requires `noinline`
50-
let codegen_fn_attrs = tcx.codegen_fn_attrs(instance.def_id());
51-
let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
52-
(_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
53-
(InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
54-
(inline, _) => inline,
55-
};
56-
57-
if !tcx.sess.opts.unstable_opts.inline_llvm {
50+
if !sess.opts.unstable_opts.inline_llvm {
5851
// disable LLVM inlining
5952
return Some(AttributeKind::NoInline.create_attr(cx.llcx));
6053
}
@@ -64,7 +57,7 @@ pub(crate) fn inline_attr<'ll, 'tcx>(
6457
Some(AttributeKind::AlwaysInline.create_attr(cx.llcx))
6558
}
6659
InlineAttr::Never => {
67-
if tcx.sess.target.arch != Arch::AmdGpu {
60+
if sess.target.arch != Arch::AmdGpu {
6861
Some(AttributeKind::NoInline.create_attr(cx.llcx))
6962
} else {
7063
None
@@ -418,6 +411,17 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
418411
OptimizeAttr::Speed => {}
419412
}
420413

414+
if let Some(instance) = instance {
415+
// `optnone` requires `noinline`
416+
let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
417+
(_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
418+
(InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
419+
(inline, _) => inline,
420+
};
421+
422+
to_add.extend(inline_attr(cx, sess, inline));
423+
}
424+
421425
if sess.must_emit_unwind_tables() {
422426
to_add.push(uwtable_attr(cx.llcx, sess.opts.unstable_opts.use_sync_unwind));
423427
}
@@ -568,16 +572,6 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
568572
let function_features =
569573
codegen_fn_attrs.target_features.iter().map(|f| f.name.as_str()).collect::<Vec<&str>>();
570574

571-
// Apply function attributes as per usual if there are no user defined
572-
// target features otherwise this will get applied at the callsite.
573-
if function_features.is_empty() {
574-
if let Some(instance) = instance
575-
&& let Some(inline_attr) = inline_attr(cx, tcx, instance)
576-
{
577-
to_add.push(inline_attr);
578-
}
579-
}
580-
581575
let function_features = function_features
582576
.iter()
583577
// Convert to LLVMFeatures and filter out unavailable ones

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef;
1515
use rustc_codegen_ssa::traits::*;
1616
use rustc_data_structures::small_c_str::SmallCStr;
1717
use rustc_hir::def_id::DefId;
18-
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature, TargetFeatureKind};
18+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1919
use rustc_middle::ty::layout::{
2020
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
2121
TyAndLayout,
@@ -1419,32 +1419,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
14191419
)
14201420
};
14211421

1422-
if let Some(callee_instance) = callee_instance {
1423-
// Attributes on the function definition being called
1424-
let callee_attrs = self.cx.tcx.codegen_fn_attrs(callee_instance.def_id());
1425-
if let Some(caller_attrs) = caller_attrs
1426-
// If there is an inline attribute and a target feature that matches
1427-
// we will add the attribute to the callsite otherwise we'll omit
1428-
// this and not add the attribute to prevent soundness issues.
1429-
&& let Some(inlining_rule) = attributes::inline_attr(&self.cx, self.cx.tcx, callee_instance)
1430-
&& self.cx.tcx.is_target_feature_call_safe(
1431-
&callee_attrs.target_features,
1432-
&caller_attrs.target_features.iter().cloned().chain(
1433-
self.cx.tcx.sess.target_features.iter().map(|feat| TargetFeature {
1434-
name: *feat,
1435-
kind: TargetFeatureKind::Implied,
1436-
})
1437-
).collect::<Vec<_>>(),
1438-
)
1439-
{
1440-
attributes::apply_to_callsite(
1441-
call,
1442-
llvm::AttributePlace::Function,
1443-
&[inlining_rule],
1444-
);
1445-
}
1446-
}
1447-
14481422
if let Some(fn_abi) = fn_abi {
14491423
fn_abi.apply_attrs_callsite(self, call);
14501424
}

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -419,16 +419,16 @@ fn check_result(
419419
// llvm/llvm-project#70563).
420420
if !codegen_fn_attrs.target_features.is_empty()
421421
&& matches!(codegen_fn_attrs.inline, InlineAttr::Always)
422-
&& !tcx.features().target_feature_inline_always()
423422
&& let Some(span) = interesting_spans.inline
424423
{
425-
feature_err(
426-
tcx.sess,
427-
sym::target_feature_inline_always,
428-
span,
429-
"cannot use `#[inline(always)]` with `#[target_feature]`",
430-
)
431-
.emit();
424+
let mut diag = tcx
425+
.dcx()
426+
.struct_span_err(span, "cannot use `#[inline(always)]` with `#[target_feature]`");
427+
diag.note(
428+
"See this issue for full discussion: \
429+
https://github.com/rust-lang/rust/issues/145574",
430+
);
431+
diag.emit();
432432
}
433433

434434
// warn that inline has no effect when no_sanitize is present

compiler/rustc_feature/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
# tidy-alphabetical-start
8+
rustc_ast = { path = "../rustc_ast" }
89
rustc_data_structures = { path = "../rustc_data_structures" }
910
rustc_hir = { path = "../rustc_hir" }
1011
rustc_span = { path = "../rustc_span" }

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::sync::LazyLock;
44

55
use AttributeGate::*;
6+
use rustc_ast::ast::Safety;
67
use rustc_data_structures::fx::FxHashMap;
78
use rustc_hir::AttrStyle;
89
use rustc_span::{Symbol, sym};
@@ -113,6 +114,7 @@ impl AttributeTemplate {
113114
pub fn suggestions(
114115
&self,
115116
style: AttrSuggestionStyle,
117+
safety: Safety,
116118
name: impl std::fmt::Display,
117119
) -> Vec<String> {
118120
let (start, macro_call, end) = match style {
@@ -124,20 +126,32 @@ impl AttributeTemplate {
124126

125127
let mut suggestions = vec![];
126128

129+
let (safety_start, safety_end) = match safety {
130+
Safety::Unsafe(_) => ("unsafe(", ")"),
131+
_ => ("", ""),
132+
};
133+
127134
if self.word {
128135
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
129-
suggestions.push(format!("{start}{name}{end}"));
136+
suggestions.push(format!("{start}{safety_start}{name}{safety_end}{end}"));
130137
}
131138
if let Some(descr) = self.list {
132139
for descr in descr {
133-
suggestions.push(format!("{start}{name}{macro_call}({descr}){end}"));
140+
suggestions.push(format!(
141+
"{start}{safety_start}{name}{macro_call}({descr}){safety_end}{end}"
142+
));
134143
}
135144
}
136-
suggestions.extend(self.one_of.iter().map(|&word| format!("{start}{name}({word}){end}")));
145+
suggestions.extend(
146+
self.one_of
147+
.iter()
148+
.map(|&word| format!("{start}{safety_start}{name}({word}){safety_end}{end}")),
149+
);
137150
if let Some(descr) = self.name_value_str {
138151
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
139152
for descr in descr {
140-
suggestions.push(format!("{start}{name} = \"{descr}\"{end}"));
153+
suggestions
154+
.push(format!("{start}{safety_start}{name} = \"{descr}\"{safety_end}{end}"));
141155
}
142156
}
143157
suggestions.sort();

compiler/rustc_feature/src/removed.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ declare_features! (
282282
/// Allows string patterns to dereference values to match them.
283283
(removed, string_deref_patterns, "1.94.0", Some(87121), Some("superseded by `deref_patterns`"), 150530),
284284
(removed, struct_inherit, "1.0.0", None, None),
285+
/// Allows the use of target_feature when a function is marked inline(always).
286+
(removed, target_feature_inline_always, "CURRENT_RUSTC_VERSION", Some(145574),
287+
Some("removed because of unfixable soundness issues")),
285288
(removed, test_removed_feature, "1.0.0", None, None),
286289
/// Allows using items which are missing stability attributes
287290
(removed, unmarked_api, "1.0.0", None, None),

0 commit comments

Comments
 (0)