Skip to content

Commit 50517d2

Browse files
committed
move rustc_codegen_ssa::target_features somewhere where it can be used by all codegen backends
1 parent b105556 commit 50517d2

File tree

10 files changed

+47
-44
lines changed

10 files changed

+47
-44
lines changed

compiler/rustc_codegen_ssa/messages.ftl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ codegen_ssa_failed_to_write = failed to write {$path}: {$error}
6868
6969
codegen_ssa_field_associated_value_expected = associated value expected for `{$name}`
7070
71-
codegen_ssa_forbidden_target_feature_attr =
72-
target feature `{$feature}` cannot be enabled with `#[target_feature]`: {$reason}
73-
7471
codegen_ssa_ignoring_emit_path = ignoring emit path because multiple .{$extension} files were produced
7572
7673
codegen_ssa_ignoring_output = ignoring -o because multiple .{$extension} files were produced
@@ -276,10 +273,6 @@ codegen_ssa_symbol_file_write_failure = failed to write symbols file: {$error}
276273
codegen_ssa_target_feature_disable_or_enable =
277274
the target features {$features} must all be either enabled or disabled together
278275
279-
codegen_ssa_target_feature_safe_trait = `#[target_feature(..)]` cannot be applied to safe trait method
280-
.label = cannot be applied to safe trait method
281-
.label_def = not an `unsafe` function
282-
283276
codegen_ssa_thorin_decompress_data = failed to decompress compressed section
284277
285278
codegen_ssa_thorin_duplicate_unit = duplicate split compilation unit ({$unit})

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_middle::middle::codegen_fn_attrs::{
1616
use rustc_middle::mir::mono::Linkage;
1717
use rustc_middle::query::Providers;
1818
use rustc_middle::span_bug;
19+
use rustc_middle::target_features::{check_target_feature_trait_unsafe, from_target_feature_attr};
1920
use rustc_middle::ty::{self as ty, TyCtxt};
2021
use rustc_session::parse::feature_err;
2122
use rustc_session::{Session, lint};
@@ -24,7 +25,6 @@ use rustc_target::spec::SanitizerSet;
2425
use tracing::debug;
2526

2627
use crate::errors;
27-
use crate::target_features::{check_target_feature_trait_unsafe, from_target_feature_attr};
2828

2929
fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
3030
use rustc_middle::mir::mono::Linkage::*;

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,25 +1182,6 @@ pub(crate) struct InvalidLinkOrdinalFormat {
11821182
pub span: Span,
11831183
}
11841184

1185-
#[derive(Diagnostic)]
1186-
#[diag(codegen_ssa_target_feature_safe_trait)]
1187-
pub(crate) struct TargetFeatureSafeTrait {
1188-
#[primary_span]
1189-
#[label]
1190-
pub span: Span,
1191-
#[label(codegen_ssa_label_def)]
1192-
pub def: Span,
1193-
}
1194-
1195-
#[derive(Diagnostic)]
1196-
#[diag(codegen_ssa_forbidden_target_feature_attr)]
1197-
pub struct ForbiddenTargetFeatureAttr<'a> {
1198-
#[primary_span]
1199-
pub span: Span,
1200-
pub feature: &'a str,
1201-
pub reason: &'a str,
1202-
}
1203-
12041185
#[derive(Diagnostic)]
12051186
#[diag(codegen_ssa_failed_to_get_layout)]
12061187
pub struct FailedToGetLayout<'tcx> {

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ pub mod meth;
5959
pub mod mir;
6060
pub mod mono_item;
6161
pub mod size_of_val;
62-
pub mod target_features;
6362
pub mod traits;
6463

6564
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
@@ -274,7 +273,6 @@ pub enum CodegenErrors {
274273
pub fn provide(providers: &mut Providers) {
275274
crate::back::symbol_export::provide(providers);
276275
crate::base::provide(providers);
277-
crate::target_features::provide(providers);
278276
crate::codegen_attrs::provide(providers);
279277
providers.queries.global_backend_features = |_tcx: TyCtxt<'_>, ()| vec![];
280278
}

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
779779
limits::provide(providers);
780780
proc_macro_decls::provide(providers);
781781
rustc_const_eval::provide(providers);
782-
rustc_middle::hir::provide(providers);
782+
rustc_middle::provide(providers);
783783
rustc_borrowck::provide(providers);
784784
rustc_incremental::provide(providers);
785785
rustc_mir_build::provide(providers);

compiler/rustc_middle/messages.ftl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ middle_erroneous_constant = erroneous constant encountered
7878
middle_failed_writing_file =
7979
failed to write file {$path}: {$error}"
8080
81+
middle_forbidden_target_feature_attr =
82+
target feature `{$feature}` cannot be enabled with `#[target_feature]`: {$reason}
83+
8184
middle_layout_cycle =
8285
a cycle occurred during layout computation
8386
@@ -112,6 +115,10 @@ middle_strict_coherence_needs_negative_coherence =
112115
to use `strict_coherence` on this trait, the `with_negative_coherence` feature must be enabled
113116
.label = due to this attribute
114117
118+
middle_target_feature_safe_trait = `#[target_feature(..)]` cannot be applied to safe trait method
119+
.label = cannot be applied to safe trait method
120+
.label_def = not an `unsafe` function
121+
115122
middle_type_length_limit = reached the type-length limit while instantiating `{$shrunk}`
116123
117124
middle_unsupported_union = we don't support unions yet: '{$ty_name}'

compiler/rustc_middle/src/error.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,22 @@ pub(crate) struct TypeLengthLimit {
170170
pub path: PathBuf,
171171
pub type_length: usize,
172172
}
173+
174+
#[derive(Diagnostic)]
175+
#[diag(middle_forbidden_target_feature_attr)]
176+
pub struct ForbiddenTargetFeatureAttr<'a> {
177+
#[primary_span]
178+
pub span: Span,
179+
pub feature: &'a str,
180+
pub reason: &'a str,
181+
}
182+
183+
#[derive(Diagnostic)]
184+
#[diag(middle_target_feature_safe_trait)]
185+
pub(crate) struct TargetFeatureSafeTrait {
186+
#[primary_span]
187+
#[label]
188+
pub span: Span,
189+
#[label(middle_label_def)]
190+
pub def: Span,
191+
}

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'tcx> TyCtxt<'tcx> {
177177
}
178178
}
179179

180-
pub fn provide(providers: &mut Providers) {
180+
pub(crate) fn provide(providers: &mut Providers) {
181181
providers.hir_crate_items = map::hir_crate_items;
182182
providers.crate_hash = map::crate_hash;
183183
providers.hir_module_items = map::hir_module_items;

compiler/rustc_middle/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub mod lint;
7979
pub mod metadata;
8080
pub mod middle;
8181
pub mod mir;
82+
pub mod target_features;
8283
pub mod thir;
8384
pub mod traits;
8485
pub mod ty;
@@ -94,3 +95,8 @@ pub mod dep_graph;
9495
extern crate self as rustc_middle;
9596

9697
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
98+
99+
pub fn provide(providers: &mut util::Providers) {
100+
hir::provide(providers);
101+
target_features::provide(providers);
102+
}

compiler/rustc_codegen_ssa/src/target_features.rs renamed to compiler/rustc_middle/src/target_features.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
use rustc_attr_parsing::InstructionSetAttr;
1+
//! Shared utilities for dealing with target features that haven't (yet) found a better home.
2+
use rustc_attr_data_structures::InstructionSetAttr;
23
use rustc_data_structures::fx::FxIndexSet;
34
use rustc_data_structures::unord::{UnordMap, UnordSet};
45
use rustc_errors::Applicability;
56
use rustc_hir as hir;
67
use rustc_hir::def::DefKind;
78
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
8-
use rustc_middle::middle::codegen_fn_attrs::TargetFeature;
9-
use rustc_middle::query::Providers;
10-
use rustc_middle::ty::TyCtxt;
119
use rustc_session::parse::feature_err;
1210
use rustc_span::{Span, Symbol, sym};
1311
use rustc_target::target_features::{self, Stability};
1412

15-
use crate::errors;
13+
use crate::error;
14+
use crate::middle::codegen_fn_attrs::TargetFeature;
15+
use crate::query::Providers;
16+
use crate::ty::TyCtxt;
1617

1718
/// Compute the enabled target features from the `#[target_feature]` function attribute.
1819
/// Enabled target features are added to `target_features`.
19-
pub(crate) fn from_target_feature_attr(
20+
pub fn from_target_feature_attr(
2021
tcx: TyCtxt<'_>,
2122
attr: &hir::Attribute,
2223
rust_target_features: &UnordMap<String, target_features::Stability>,
@@ -65,7 +66,7 @@ pub(crate) fn from_target_feature_attr(
6566
// Only allow target features whose feature gates have been enabled
6667
// and which are permitted to be toggled.
6768
if let Err(reason) = stability.toggle_allowed() {
68-
tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr {
69+
tcx.dcx().emit_err(error::ForbiddenTargetFeatureAttr {
6970
span: item.span(),
7071
feature,
7172
reason,
@@ -92,7 +93,7 @@ pub(crate) fn from_target_feature_attr(
9293
// generating code so "it's fine".
9394
if !tcx.sess.opts.actually_rustdoc {
9495
if abi_feature_constraints.incompatible.contains(&name.as_str()) {
95-
tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr {
96+
tcx.dcx().emit_err(error::ForbiddenTargetFeatureAttr {
9697
span: item.span(),
9798
feature: name.as_str(),
9899
reason: "this feature is incompatible with the target ABI",
@@ -130,14 +131,12 @@ fn asm_target_features(tcx: TyCtxt<'_>, did: DefId) -> &FxIndexSet<Symbol> {
130131

131132
/// Checks the function annotated with `#[target_feature]` is not a safe
132133
/// trait method implementation, reporting an error if it is.
133-
pub(crate) fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
134+
pub fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
134135
if let DefKind::AssocFn = tcx.def_kind(id) {
135136
let parent_id = tcx.local_parent(id);
136137
if let DefKind::Trait | DefKind::Impl { of_trait: true } = tcx.def_kind(parent_id) {
137-
tcx.dcx().emit_err(errors::TargetFeatureSafeTrait {
138-
span: attr_span,
139-
def: tcx.def_span(id),
140-
});
138+
tcx.dcx()
139+
.emit_err(error::TargetFeatureSafeTrait { span: attr_span, def: tcx.def_span(id) });
141140
}
142141
}
143142
}

0 commit comments

Comments
 (0)