Skip to content

Commit 47e6b5d

Browse files
committed
Revert "Auto merge of rust-lang#127537 - veluca93:struct_tf, r=BoxyUwU"
This reverts commit acb4e8b, reversing changes made to 100fde5.
1 parent 1a1cc05 commit 47e6b5d

File tree

25 files changed

+27
-511
lines changed

25 files changed

+27
-511
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+10-115
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem};
22
use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr};
3-
use rustc_data_structures::fx::FxHashSet;
43
use rustc_errors::codes::*;
54
use rustc_errors::{struct_span_code_err, DiagMessage, SubdiagMessage};
65
use rustc_hir as hir;
@@ -9,7 +8,7 @@ use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
98
use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
109
use rustc_hir::{lang_items, LangItem};
1110
use rustc_middle::middle::codegen_fn_attrs::{
12-
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, TargetFeature,
11+
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry,
1312
};
1413
use rustc_middle::mir::mono::Linkage;
1514
use rustc_middle::query::Providers;
@@ -18,7 +17,6 @@ use rustc_session::lint;
1817
use rustc_session::parse::feature_err;
1918
use rustc_span::symbol::Ident;
2019
use rustc_span::{sym, Span};
21-
use rustc_target::abi::VariantIdx;
2220
use rustc_target::spec::{abi, SanitizerSet};
2321

2422
use crate::errors;
@@ -80,26 +78,23 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
8078
let mut link_ordinal_span = None;
8179
let mut no_sanitize_span = None;
8280

83-
let fn_sig_outer = || {
84-
use DefKind::*;
85-
86-
let def_kind = tcx.def_kind(did);
87-
if let Fn | AssocFn | Variant | Ctor(..) = def_kind { Some(tcx.fn_sig(did)) } else { None }
88-
};
89-
9081
for attr in attrs.iter() {
9182
// In some cases, attribute are only valid on functions, but it's the `check_attr`
9283
// pass that check that they aren't used anywhere else, rather this module.
9384
// In these cases, we bail from performing further checks that are only meaningful for
9485
// functions (such as calling `fn_sig`, which ICEs if given a non-function). We also
9586
// report a delayed bug, just in case `check_attr` isn't doing its job.
9687
let fn_sig = || {
97-
let sig = fn_sig_outer();
98-
if sig.is_none() {
88+
use DefKind::*;
89+
90+
let def_kind = tcx.def_kind(did);
91+
if let Fn | AssocFn | Variant | Ctor(..) = def_kind {
92+
Some(tcx.fn_sig(did))
93+
} else {
9994
tcx.dcx()
10095
.span_delayed_bug(attr.span, "this attribute can only be applied to functions");
96+
None
10197
}
102-
sig
10398
};
10499

105100
let Some(Ident { name, .. }) = attr.ident() else {
@@ -618,93 +613,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
618613
}
619614
}
620615

621-
if let Some(sig) = fn_sig_outer() {
622-
// Collect target features from types reachable from arguments.
623-
// We define a type as "reachable" if:
624-
// - it is a function argument
625-
// - it is a field of a reachable struct
626-
// - there is a reachable reference to it
627-
// FIXME(struct_target_features): we may want to cache the result of this computation.
628-
let mut visited_types = FxHashSet::default();
629-
let mut reachable_types: Vec<_> = sig.skip_binder().inputs().skip_binder().to_owned();
630-
let mut additional_tf = vec![];
631-
632-
while let Some(ty) = reachable_types.pop() {
633-
if visited_types.contains(&ty) {
634-
continue;
635-
}
636-
visited_types.insert(ty);
637-
match ty.kind() {
638-
ty::Alias(..) => {
639-
if let Ok(t) =
640-
tcx.try_normalize_erasing_regions(tcx.param_env(did.to_def_id()), ty)
641-
{
642-
reachable_types.push(t)
643-
}
644-
}
645-
646-
ty::Ref(_, inner, _) => reachable_types.push(*inner),
647-
ty::Tuple(tys) => reachable_types.extend(tys.iter()),
648-
ty::Adt(adt_def, args) => {
649-
additional_tf.extend_from_slice(tcx.struct_target_features(adt_def.did()));
650-
// This only recurses into structs as i.e. an Option<TargetFeature> is an ADT
651-
// that doesn't actually always contain a TargetFeature.
652-
if adt_def.is_struct() {
653-
reachable_types.extend(
654-
adt_def
655-
.variant(VariantIdx::from_usize(0))
656-
.fields
657-
.iter()
658-
.map(|field| field.ty(tcx, args)),
659-
);
660-
}
661-
}
662-
ty::Bool
663-
| ty::Char
664-
| ty::Int(..)
665-
| ty::Uint(..)
666-
| ty::Float(..)
667-
| ty::Foreign(..)
668-
| ty::Str
669-
| ty::Array(..)
670-
| ty::Pat(..)
671-
| ty::Slice(..)
672-
| ty::RawPtr(..)
673-
| ty::FnDef(..)
674-
| ty::FnPtr(..)
675-
| ty::Dynamic(..)
676-
| ty::Closure(..)
677-
| ty::CoroutineClosure(..)
678-
| ty::Coroutine(..)
679-
| ty::CoroutineWitness(..)
680-
| ty::Never
681-
| ty::Param(..)
682-
| ty::Bound(..)
683-
| ty::Placeholder(..)
684-
| ty::Infer(..)
685-
| ty::Error(..) => (),
686-
}
687-
}
688-
689-
// FIXME(struct_target_features): is this really necessary?
690-
if !additional_tf.is_empty() && sig.skip_binder().abi() != abi::Abi::Rust {
691-
tcx.dcx().span_err(
692-
tcx.hir().span(tcx.local_def_id_to_hir_id(did)),
693-
"cannot use a struct with target features in a function with non-Rust ABI",
694-
);
695-
}
696-
if !additional_tf.is_empty() && codegen_fn_attrs.inline == InlineAttr::Always {
697-
tcx.dcx().span_err(
698-
tcx.hir().span(tcx.local_def_id_to_hir_id(did)),
699-
"cannot use a struct with target features in a #[inline(always)] function",
700-
);
701-
}
702-
codegen_fn_attrs
703-
.target_features
704-
.extend(additional_tf.iter().map(|tf| TargetFeature { implied: true, ..*tf }));
705-
}
706-
707-
// If a function uses non-default target_features it can't be inlined into general
616+
// If a function uses #[target_feature] it can't be inlined into general
708617
// purpose functions as they wouldn't have the right target features
709618
// enabled. For that reason we also forbid #[inline(always)] as it can't be
710619
// respected.
@@ -849,20 +758,6 @@ fn check_link_name_xor_ordinal(
849758
}
850759
}
851760

852-
fn struct_target_features(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &[TargetFeature] {
853-
let mut features = vec![];
854-
let supported_features = tcx.supported_target_features(LOCAL_CRATE);
855-
for attr in tcx.get_attrs(def_id, sym::target_feature) {
856-
from_target_feature(tcx, attr, supported_features, &mut features);
857-
}
858-
tcx.arena.alloc_slice(&features)
859-
}
860-
861761
pub fn provide(providers: &mut Providers) {
862-
*providers = Providers {
863-
codegen_fn_attrs,
864-
should_inherit_track_caller,
865-
struct_target_features,
866-
..*providers
867-
};
762+
*providers = Providers { codegen_fn_attrs, should_inherit_track_caller, ..*providers };
868763
}

compiler/rustc_feature/src/unstable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,6 @@ declare_features! (
597597
(unstable, strict_provenance, "1.61.0", Some(95228)),
598598
/// Allows string patterns to dereference values to match them.
599599
(unstable, string_deref_patterns, "1.67.0", Some(87121)),
600-
/// Allows structs to carry target_feature information.
601-
(incomplete, struct_target_features, "CURRENT_RUSTC_VERSION", Some(129107)),
602600
/// Allows the use of `#[target_feature]` on safe functions.
603601
(unstable, target_feature_11, "1.45.0", Some(69098)),
604602
/// Allows using `#[thread_local]` on `static` items.

compiler/rustc_hir/src/def.rs

-35
Original file line numberDiff line numberDiff line change
@@ -326,41 +326,6 @@ impl DefKind {
326326
| DefKind::ExternCrate => false,
327327
}
328328
}
329-
330-
/// Whether `query struct_target_features` should be used with this definition.
331-
pub fn has_struct_target_features(self) -> bool {
332-
match self {
333-
DefKind::Struct | DefKind::Union | DefKind::Enum => true,
334-
DefKind::Fn
335-
| DefKind::AssocFn
336-
| DefKind::Ctor(..)
337-
| DefKind::Closure
338-
| DefKind::Static { .. }
339-
| DefKind::Mod
340-
| DefKind::Variant
341-
| DefKind::Trait
342-
| DefKind::TyAlias
343-
| DefKind::ForeignTy
344-
| DefKind::TraitAlias
345-
| DefKind::AssocTy
346-
| DefKind::Const
347-
| DefKind::AssocConst
348-
| DefKind::Macro(..)
349-
| DefKind::Use
350-
| DefKind::ForeignMod
351-
| DefKind::OpaqueTy
352-
| DefKind::Impl { .. }
353-
| DefKind::Field
354-
| DefKind::TyParam
355-
| DefKind::ConstParam
356-
| DefKind::LifetimeParam
357-
| DefKind::AnonConst
358-
| DefKind::InlineConst
359-
| DefKind::SyntheticCoroutineBody
360-
| DefKind::GlobalAsm
361-
| DefKind::ExternCrate => false,
362-
}
363-
}
364329
}
365330

366331
/// The resolution of a path or export.

compiler/rustc_hir_typeck/src/coercion.rs

-2
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,6 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
851851
}
852852

853853
// Safe `#[target_feature]` functions are not assignable to safe fn pointers (RFC 2396).
854-
// FIXME(struct_target_features): should this be true also for functions that inherit
855-
// target features from structs?
856854

857855
if b_hdr.safety == hir::Safety::Safe
858856
&& !self.tcx.codegen_fn_attrs(def_id).target_features.is_empty()

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ provide! { tcx, def_id, other, cdata,
254254
variances_of => { table }
255255
fn_sig => { table }
256256
codegen_fn_attrs => { table }
257-
struct_target_features => { table }
258257
impl_trait_header => { table }
259258
const_param_default => { table }
260259
object_lifetime_default => { table }

compiler/rustc_metadata/src/rmeta/encoder.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1392,9 +1392,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13921392
if def_kind.has_codegen_attrs() {
13931393
record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id));
13941394
}
1395-
if def_kind.has_struct_target_features() {
1396-
record_array!(self.tables.struct_target_features[def_id] <- self.tcx.struct_target_features(def_id));
1397-
}
13981395
if should_encode_visibility(def_kind) {
13991396
let vis =
14001397
self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index);

compiler/rustc_metadata/src/rmeta/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_macros::{
1919
Decodable, Encodable, MetadataDecodable, MetadataEncodable, TyDecodable, TyEncodable,
2020
};
2121
use rustc_middle::metadata::ModChild;
22-
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature};
22+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2323
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
2424
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
2525
use rustc_middle::middle::lib_features::FeatureStability;
@@ -427,7 +427,6 @@ define_tables! {
427427
variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
428428
fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::PolyFnSig<'static>>>>,
429429
codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
430-
struct_target_features: Table<DefIndex, LazyArray<TargetFeature>>,
431430
impl_trait_header: Table<DefIndex, LazyValue<ty::ImplTraitHeader<'static>>>,
432431
const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, rustc_middle::ty::Const<'static>>>>,
433432
object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ pub struct CodegenFnAttrs {
2626
/// be set when `link_name` is set. This is for foreign items with the
2727
/// "raw-dylib" kind.
2828
pub link_ordinal: Option<u16>,
29-
/// All the target features that are enabled for this function. Some features might be enabled
30-
/// implicitly.
29+
/// The `#[target_feature(enable = "...")]` attribute and the enabled
30+
/// features (only enabled features are supported right now).
3131
pub target_features: Vec<TargetFeature>,
3232
/// The `#[linkage = "..."]` attribute on Rust-defined items and the value we found.
3333
pub linkage: Option<Linkage>,
@@ -55,8 +55,8 @@ pub struct CodegenFnAttrs {
5555
pub struct TargetFeature {
5656
/// The name of the target feature (e.g. "avx")
5757
pub name: Symbol,
58-
/// The feature is implied by another feature or by an argument, rather than explicitly
59-
/// added by the `#[target_feature]` attribute
58+
/// The feature is implied by another feature, rather than explicitly added by the
59+
/// `#[target_feature]` attribute
6060
pub implied: bool,
6161
}
6262

compiler/rustc_middle/src/query/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use {rustc_ast as ast, rustc_attr as attr, rustc_hir as hir};
4747
use crate::infer::canonical::{self, Canonical};
4848
use crate::lint::LintExpectation;
4949
use crate::metadata::ModChild;
50-
use crate::middle::codegen_fn_attrs::{CodegenFnAttrs, TargetFeature};
50+
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
5151
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
5252
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
5353
use crate::middle::lib_features::LibFeatures;
@@ -1249,11 +1249,6 @@ rustc_queries! {
12491249
feedable
12501250
}
12511251

1252-
query struct_target_features(def_id: DefId) -> &'tcx [TargetFeature] {
1253-
separate_provide_extern
1254-
desc { |tcx| "computing target features for struct `{}`", tcx.def_path_str(def_id) }
1255-
}
1256-
12571252
query asm_target_features(def_id: DefId) -> &'tcx FxIndexSet<Symbol> {
12581253
desc { |tcx| "computing target features for inline asm of `{}`", tcx.def_path_str(def_id) }
12591254
}

compiler/rustc_middle/src/ty/parameterized.rs

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ trivially_parameterized_over_tcx! {
5959
std::string::String,
6060
crate::metadata::ModChild,
6161
crate::middle::codegen_fn_attrs::CodegenFnAttrs,
62-
crate::middle::codegen_fn_attrs::TargetFeature,
6362
crate::middle::debugger_visualizer::DebuggerVisualizerFile,
6463
crate::middle::exported_symbols::SymbolExportInfo,
6564
crate::middle::lib_features::FeatureStability,

compiler/rustc_mir_build/messages.ftl

-16
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,6 @@ mir_build_initializing_type_with_requires_unsafe_unsafe_op_in_unsafe_fn_allowed
125125
.note = initializing a layout restricted type's field with a value outside the valid range is undefined behavior
126126
.label = initializing type with `rustc_layout_scalar_valid_range` attr
127127
128-
mir_build_initializing_type_with_target_feature_requires_unsafe =
129-
initializing type with `target_feature` attr is unsafe and requires unsafe block
130-
.note = this struct can only be constructed if the corresponding `target_feature`s are available
131-
.label = initializing type with `target_feature` attr
132-
133-
mir_build_initializing_type_with_target_feature_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
134-
initializing type with `target_feature` attr is unsafe and requires unsafe function or block
135-
.note = this struct can only be constructed if the corresponding `target_feature`s are available
136-
.label = initializing type with `target_feature` attr
137-
138-
139128
mir_build_inline_assembly_requires_unsafe =
140129
use of inline assembly is unsafe and requires unsafe block
141130
.note = inline assembly is entirely unchecked and can cause undefined behavior
@@ -398,11 +387,6 @@ mir_build_unsafe_op_in_unsafe_fn_initializing_type_with_requires_unsafe =
398387
.note = initializing a layout restricted type's field with a value outside the valid range is undefined behavior
399388
.label = initializing type with `rustc_layout_scalar_valid_range` attr
400389
401-
mir_build_unsafe_op_in_unsafe_fn_initializing_type_with_target_feature_requires_unsafe =
402-
initializing type with `target_feature` attr is unsafe and requires unsafe block
403-
.note = this struct can only be constructed if the corresponding `target_feature`s are available
404-
.label = initializing type with `target_feature` attr
405-
406390
mir_build_unsafe_op_in_unsafe_fn_inline_assembly_requires_unsafe =
407391
use of inline assembly is unsafe and requires unsafe block
408392
.note = inline assembly is entirely unchecked and can cause undefined behavior

0 commit comments

Comments
 (0)