Skip to content

Commit aa67dbe

Browse files
Forbid the use of #[target_feature] on main
1 parent 18caf88 commit aa67dbe

File tree

6 files changed

+31
-1
lines changed

6 files changed

+31
-1
lines changed

Diff for: compiler/rustc_hir_analysis/locales/en-US.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ hir_analysis_where_clause_on_main = `main` function is not allowed to have a `wh
125125
hir_analysis_track_caller_on_main = `main` function is not allowed to be `#[track_caller]`
126126
.suggestion = remove this annotation
127127
128+
hir_analysis_target_feature_on_main = `main` function is not allowed to have `#[target_feature]`
129+
128130
hir_analysis_start_not_track_caller = `start` is not allowed to be `#[track_caller]`
129131
.label = `start` is not allowed to be `#[track_caller]`
130132

Diff for: compiler/rustc_hir_analysis/src/errors.rs

+8
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ pub(crate) struct TrackCallerOnMain {
315315
pub annotated: Span,
316316
}
317317

318+
#[derive(Diagnostic)]
319+
#[diag(hir_analysis_target_feature_on_main)]
320+
pub(crate) struct TargetFeatureOnMain {
321+
#[primary_span]
322+
#[label(hir_analysis_target_feature_on_main)]
323+
pub main: Span,
324+
}
325+
318326
#[derive(Diagnostic)]
319327
#[diag(hir_analysis_start_not_track_caller)]
320328
pub(crate) struct StartTrackCaller {

Diff for: compiler/rustc_hir_analysis/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
283283
error = true;
284284
}
285285

286+
if !tcx.codegen_fn_attrs(main_def_id).target_features.is_empty() {
287+
tcx.sess.emit_err(errors::TargetFeatureOnMain { main: main_span });
288+
error = true;
289+
}
290+
286291
if error {
287292
return;
288293
}

Diff for: tests/ui/asm/x86_64/issue-89875.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
use std::arch::asm;
66

77
#[target_feature(enable = "avx")]
8-
fn main() {
8+
fn foo() {
99
unsafe {
1010
asm!(
1111
"/* {} */",
1212
out(ymm_reg) _,
1313
);
1414
}
1515
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// only-x86_64
2+
3+
#[target_feature(enable = "avx2")]
4+
fn main() {}
5+
//~^ ERROR `main` function is not allowed to have `#[target_feature]`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: `main` function is not allowed to have `#[target_feature]`
2+
--> $DIR/issue-108645-target-feature-on-main.rs:4:1
3+
|
4+
LL | fn main() {}
5+
| ^^^^^^^^^ `main` function is not allowed to have `#[target_feature]`
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)