Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4010,6 +4010,7 @@ dependencies = [
name = "rustc_feature"
version = "0.0.0"
dependencies = [
"rustc_ast",
"rustc_data_structures",
"rustc_hir",
"rustc_span",
Expand Down
14 changes: 10 additions & 4 deletions compiler/rustc_attr_parsing/src/attributes/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,11 @@ pub fn parse_cfg_attr(
}) {
Ok(r) => return Some(r),
Err(e) => {
let suggestions = CFG_ATTR_TEMPLATE
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr);
let suggestions = CFG_ATTR_TEMPLATE.suggestions(
AttrSuggestionStyle::Attribute(cfg_attr.style),
cfg_attr.get_normal_item().unsafety,
sym::cfg_attr,
);
e.with_span_suggestions(
cfg_attr.span,
"must be of the form",
Expand Down Expand Up @@ -360,8 +363,11 @@ pub fn parse_cfg_attr(
description: ParsedDescription::Attribute,
reason,
suggestions: session_diagnostics::AttributeParseErrorSuggestions::CreatedByTemplate(
CFG_ATTR_TEMPLATE
.suggestions(AttrSuggestionStyle::Attribute(cfg_attr.style), sym::cfg_attr),
CFG_ATTR_TEMPLATE.suggestions(
AttrSuggestionStyle::Attribute(cfg_attr.style),
cfg_attr.get_normal_item().unsafety,
sym::cfg_attr,
),
),
});
}
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::mem;
use std::ops::{Deref, DerefMut};
use std::sync::LazyLock;

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

/// The safety attribute (if any) applied to the attribute.
pub(crate) attr_safety: Safety,

/// The name of the attribute we're currently accepting.
pub(crate) attr_path: AttrPath,
}
Expand Down Expand Up @@ -873,7 +876,7 @@ impl<'a, 'f, 'sess: 'f> AttributeDiagnosticContext<'a, 'f, 'sess> {
ParsedDescription::Macro => AttrSuggestionStyle::Macro,
};

self.template.suggestions(style, &self.attr_path)
self.template.suggestions(style, self.attr_safety, &self.attr_path)
}
}

Expand Down Expand Up @@ -1064,7 +1067,7 @@ impl<'a, 'f, 'sess: 'f> AttributeDiagnosticContext<'a, 'f, 'sess> {
ParsedDescription::Macro => AttrSuggestionStyle::Macro,
};

self.template.suggestions(style, &self.attr_path)
self.template.suggestions(style, self.attr_safety, &self.attr_path)
}
/// Error that a string literal was expected.
/// You can optionally give the literal you did find (which you found not to be a string literal)
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_attr_parsing/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ impl<'sess> AttributeParser<'sess> {
attr_style,
parsed_description,
template,
attr_safety: attr_safety.unwrap_or(Safety::Default),
attr_path,
};
parse_fn(&mut cx, args)
Expand Down Expand Up @@ -404,6 +405,7 @@ impl<'sess> AttributeParser<'sess> {
attr_style: attr.style,
parsed_description: ParsedDescription::Attribute,
template: &accept.template,
attr_safety: n.item.unsafety,
attr_path: attr_path.clone(),
};

Expand Down
40 changes: 17 additions & 23 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,13 @@ pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: &str) {
}

/// Get LLVM attribute for the provided inline heuristic.
pub(crate) fn inline_attr<'ll, 'tcx>(
#[inline]
fn inline_attr<'ll>(
cx: &SimpleCx<'ll>,
tcx: TyCtxt<'tcx>,
instance: ty::Instance<'tcx>,
sess: &Session,
inline: InlineAttr,
) -> Option<&'ll Attribute> {
// `optnone` requires `noinline`
let codegen_fn_attrs = tcx.codegen_fn_attrs(instance.def_id());
let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
(_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
(InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
(inline, _) => inline,
};

if !tcx.sess.opts.unstable_opts.inline_llvm {
if !sess.opts.unstable_opts.inline_llvm {
// disable LLVM inlining
return Some(AttributeKind::NoInline.create_attr(cx.llcx));
}
Expand All @@ -64,7 +57,7 @@ pub(crate) fn inline_attr<'ll, 'tcx>(
Some(AttributeKind::AlwaysInline.create_attr(cx.llcx))
}
InlineAttr::Never => {
if tcx.sess.target.arch != Arch::AmdGpu {
if sess.target.arch != Arch::AmdGpu {
Some(AttributeKind::NoInline.create_attr(cx.llcx))
} else {
None
Expand Down Expand Up @@ -418,6 +411,17 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
OptimizeAttr::Speed => {}
}

if let Some(instance) = instance {
// `optnone` requires `noinline`
let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
(_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
(InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
(inline, _) => inline,
};

to_add.extend(inline_attr(cx, sess, inline));
}

if sess.must_emit_unwind_tables() {
to_add.push(uwtable_attr(cx.llcx, sess.opts.unstable_opts.use_sync_unwind));
}
Expand Down Expand Up @@ -568,16 +572,6 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
let function_features =
codegen_fn_attrs.target_features.iter().map(|f| f.name.as_str()).collect::<Vec<&str>>();

// Apply function attributes as per usual if there are no user defined
// target features otherwise this will get applied at the callsite.
if function_features.is_empty() {
if let Some(instance) = instance
&& let Some(inline_attr) = inline_attr(cx, tcx, instance)
{
to_add.push(inline_attr);
}
}

let function_features = function_features
.iter()
// Convert to LLVMFeatures and filter out unavailable ones
Expand Down
28 changes: 1 addition & 27 deletions compiler/rustc_codegen_llvm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef;
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_hir::def_id::DefId;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature, TargetFeatureKind};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::ty::layout::{
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTypingEnv, LayoutError, LayoutOfHelpers,
TyAndLayout,
Expand Down Expand Up @@ -1419,32 +1419,6 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
)
};

if let Some(callee_instance) = callee_instance {
// Attributes on the function definition being called
let callee_attrs = self.cx.tcx.codegen_fn_attrs(callee_instance.def_id());
if let Some(caller_attrs) = caller_attrs
// If there is an inline attribute and a target feature that matches
// we will add the attribute to the callsite otherwise we'll omit
// this and not add the attribute to prevent soundness issues.
&& let Some(inlining_rule) = attributes::inline_attr(&self.cx, self.cx.tcx, callee_instance)
&& self.cx.tcx.is_target_feature_call_safe(
&callee_attrs.target_features,
&caller_attrs.target_features.iter().cloned().chain(
self.cx.tcx.sess.target_features.iter().map(|feat| TargetFeature {
name: *feat,
kind: TargetFeatureKind::Implied,
})
).collect::<Vec<_>>(),
)
{
attributes::apply_to_callsite(
call,
llvm::AttributePlace::Function,
&[inlining_rule],
);
}
}

if let Some(fn_abi) = fn_abi {
fn_abi.apply_attrs_callsite(self, call);
}
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,16 +419,16 @@ fn check_result(
// llvm/llvm-project#70563).
if !codegen_fn_attrs.target_features.is_empty()
&& matches!(codegen_fn_attrs.inline, InlineAttr::Always)
&& !tcx.features().target_feature_inline_always()
&& let Some(span) = interesting_spans.inline
{
feature_err(
tcx.sess,
sym::target_feature_inline_always,
span,
"cannot use `#[inline(always)]` with `#[target_feature]`",
)
.emit();
let mut diag = tcx
.dcx()
.struct_span_err(span, "cannot use `#[inline(always)]` with `#[target_feature]`");
diag.note(
"See this issue for full discussion: \
https://github.com/rust-lang/rust/issues/145574",
);
diag.emit();
}

// warn that inline has no effect when no_sanitize is present
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_feature/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2024"

[dependencies]
# tidy-alphabetical-start
rustc_ast = { path = "../rustc_ast" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_hir = { path = "../rustc_hir" }
rustc_span = { path = "../rustc_span" }
Expand Down
22 changes: 18 additions & 4 deletions compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::sync::LazyLock;

use AttributeGate::*;
use rustc_ast::ast::Safety;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::AttrStyle;
use rustc_span::{Symbol, sym};
Expand Down Expand Up @@ -113,6 +114,7 @@ impl AttributeTemplate {
pub fn suggestions(
&self,
style: AttrSuggestionStyle,
safety: Safety,
name: impl std::fmt::Display,
) -> Vec<String> {
let (start, macro_call, end) = match style {
Expand All @@ -124,20 +126,32 @@ impl AttributeTemplate {

let mut suggestions = vec![];

let (safety_start, safety_end) = match safety {
Safety::Unsafe(_) => ("unsafe(", ")"),
_ => ("", ""),
};

if self.word {
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
suggestions.push(format!("{start}{name}{end}"));
suggestions.push(format!("{start}{safety_start}{name}{safety_end}{end}"));
}
if let Some(descr) = self.list {
for descr in descr {
suggestions.push(format!("{start}{name}{macro_call}({descr}){end}"));
suggestions.push(format!(
"{start}{safety_start}{name}{macro_call}({descr}){safety_end}{end}"
));
}
}
suggestions.extend(self.one_of.iter().map(|&word| format!("{start}{name}({word}){end}")));
suggestions.extend(
self.one_of
.iter()
.map(|&word| format!("{start}{safety_start}{name}({word}){safety_end}{end}")),
);
if let Some(descr) = self.name_value_str {
debug_assert!(macro_call.is_empty(), "Macro suggestions use list style");
for descr in descr {
suggestions.push(format!("{start}{name} = \"{descr}\"{end}"));
suggestions
.push(format!("{start}{safety_start}{name} = \"{descr}\"{safety_end}{end}"));
}
}
suggestions.sort();
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_feature/src/removed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ declare_features! (
/// Allows string patterns to dereference values to match them.
(removed, string_deref_patterns, "1.94.0", Some(87121), Some("superseded by `deref_patterns`"), 150530),
(removed, struct_inherit, "1.0.0", None, None),
/// Allows the use of target_feature when a function is marked inline(always).
(removed, target_feature_inline_always, "CURRENT_RUSTC_VERSION", Some(145574),
Some("removed because of unfixable soundness issues")),
(removed, test_removed_feature, "1.0.0", None, None),
/// Allows using items which are missing stability attributes
(removed, unmarked_api, "1.0.0", None, None),
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_feature/src/unstable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,6 @@ declare_features! (
(unstable, super_let, "1.88.0", Some(139076)),
/// Allows subtrait items to shadow supertrait items.
(unstable, supertrait_item_shadowing, "1.86.0", Some(89151)),
/// Allows the use of target_feature when a function is marked inline(always).
(unstable, target_feature_inline_always, "1.91.0", Some(145574)),
/// Allows using `#[thread_local]` on `static` items.
(unstable, thread_local, "1.0.0", Some(29594)),
/// Allows defining `trait X = A + B;` alias items.
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,10 @@ fn register_builtins(store: &mut LintStore) {
);
store.register_removed("wasm_c_abi", "the wasm C ABI has been fixed");
store.register_removed("soft_unstable", "the general soft-unstable mechanism has been removed");
store.register_removed(
"inline_always_mismatching_target_features",
"replaced by a hard error for `#[inline(always)]` with `#[target_feature]`",
);
}

fn register_internals(store: &mut LintStore) {
Expand Down
56 changes: 0 additions & 56 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ declare_lint_pass! {
ILL_FORMED_ATTRIBUTE_INPUT,
INCOMPLETE_INCLUDE,
INEFFECTIVE_UNSTABLE_TRAIT_IMPL,
INLINE_ALWAYS_MISMATCHING_TARGET_FEATURES,
INLINE_NO_SANITIZE,
INVALID_DOC_ATTRIBUTES,
INVALID_MACRO_EXPORT_ARGUMENTS,
Expand Down Expand Up @@ -5492,61 +5491,6 @@ declare_lint! {
"detects tail calls of functions marked with `#[track_caller]`",
@feature_gate = explicit_tail_calls;
}
declare_lint! {
/// The `inline_always_mismatching_target_features` lint will trigger when a
/// function with the `#[inline(always)]` and `#[target_feature(enable = "...")]`
/// attributes is called and cannot be inlined due to missing target features in the caller.
///
/// ### Example
///
/// ```rust,ignore (fails on x86_64)
/// #[inline(always)]
/// #[target_feature(enable = "fp16")]
/// unsafe fn callee() {
/// // operations using fp16 types
/// }
///
/// // Caller does not enable the required target feature
/// fn caller() {
/// unsafe { callee(); }
/// }
///
/// fn main() {
/// caller();
/// }
/// ```
///
/// This will produce:
///
/// ```text
/// warning: call to `#[inline(always)]`-annotated `callee` requires the same target features. Function will not have `alwaysinline` attribute applied
/// --> $DIR/builtin.rs:5192:14
/// |
/// 10 | unsafe { callee(); }
/// | ^^^^^^^^
/// |
/// note: `fp16` target feature enabled in `callee` here but missing from `caller`
/// --> $DIR/builtin.rs:5185:1
/// |
/// 3 | #[target_feature(enable = "fp16")]
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// 4 | unsafe fn callee() {
/// | ------------------
/// = note: `#[warn(inline_always_mismatching_target_features)]` on by default
/// warning: 1 warning emitted
/// ```
///
/// ### Explanation
///
/// Inlining a function with a target feature attribute into a caller that
/// lacks the corresponding target feature can lead to unsound behavior.
/// LLVM may select the wrong instructions or registers, or reorder
/// operations, potentially resulting in runtime errors.
pub INLINE_ALWAYS_MISMATCHING_TARGET_FEATURES,
Warn,
r#"detects when a function annotated with `#[inline(always)]` and `#[target_feature(enable = "..")]` is inlined into a caller without the required target feature"#,
}

declare_lint! {
/// The `repr_c_enums_larger_than_int` lint detects `repr(C)` enums with discriminant
/// values that do not fit into a C `int` or `unsigned int`.
Expand Down
Loading
Loading