Skip to content

Commit 03a6420

Browse files
authored
Rollup merge of #108651 - LeSeulArtichaut:108645-target-feature-on-main, r=Nilstrieb
Forbid the use of `#[target_feature]` on `main` Fixes #108645.
2 parents 8ccbbbe + faebedf commit 03a6420

File tree

8 files changed

+72
-1
lines changed

8 files changed

+72
-1
lines changed

compiler/rustc_hir_analysis/locales/en-US.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,14 @@ 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
133+
hir_analysis_start_not_target_feature = `start` is not allowed to have `#[target_feature]`
134+
.label = `start` is not allowed to have `#[target_feature]`
135+
131136
hir_analysis_start_not_async = `start` is not allowed to be `async`
132137
.label = `start` is not allowed to be `async`
133138

compiler/rustc_hir_analysis/src/errors.rs

+17
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 {
@@ -324,6 +332,15 @@ pub(crate) struct StartTrackCaller {
324332
pub start: Span,
325333
}
326334

335+
#[derive(Diagnostic)]
336+
#[diag(hir_analysis_start_not_target_feature)]
337+
pub(crate) struct StartTargetFeature {
338+
#[primary_span]
339+
pub span: Span,
340+
#[label]
341+
pub start: Span,
342+
}
343+
327344
#[derive(Diagnostic)]
328345
#[diag(hir_analysis_start_not_async, code = "E0752")]
329346
pub(crate) struct StartAsync {

compiler/rustc_hir_analysis/src/lib.rs

+12
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
}
@@ -373,6 +378,13 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
373378
});
374379
error = true;
375380
}
381+
if attr.has_name(sym::target_feature) {
382+
tcx.sess.emit_err(errors::StartTargetFeature {
383+
span: attr.span,
384+
start: start_span,
385+
});
386+
error = true;
387+
}
376388
}
377389

378390
if error {

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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// only-x86_64
2+
3+
#![feature(start)]
4+
#![feature(target_feature_11)]
5+
6+
#[start]
7+
#[target_feature(enable = "avx2")]
8+
//~^ ERROR `start` is not allowed to have `#[target_feature]`
9+
fn start(_argc: isize, _argv: *const *const u8) -> isize { 0 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: `start` is not allowed to have `#[target_feature]`
2+
--> $DIR/issue-108645-target-feature-on-start.rs:7:1
3+
|
4+
LL | #[target_feature(enable = "avx2")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
LL |
7+
LL | fn start(_argc: isize, _argv: *const *const u8) -> isize { 0 }
8+
| -------------------------------------------------------- `start` is not allowed to have `#[target_feature]`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)