Skip to content

Commit 729275d

Browse files
Forbid the use of #[target_feature] on main
1 parent 13471d3 commit 729275d

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

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

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 {

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
}

tests/ui/asm/x86_64/issue-89875.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use std::arch::asm;
88

99
#[target_feature(enable = "avx")]
10-
fn main() {
10+
fn foo() {
1111
unsafe {
1212
asm!(
1313
"/* {} */",
1414
out(ymm_reg) _,
1515
);
1616
}
1717
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// only-x86_64
2+
3+
#![feature(target_feature_11)]
4+
5+
#[target_feature(enable = "avx2")]
6+
fn main() {}
7+
//~^ 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:6: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)