Skip to content

Commit b5aec1c

Browse files
Rollup merge of rust-lang#108651 - LeSeulArtichaut:108645-target-feature-on-main, r=Nilstrieb
Forbid the use of `#[target_feature]` on `main` Fixes rust-lang#108645.
2 parents c897cbe + dc6e0d9 commit b5aec1c

8 files changed

+67
-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
@@ -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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![feature(start)]
2+
3+
#[start]
4+
#[target_feature(enable = "avx2")]
5+
//~^ ERROR `start` is not allowed to have `#[target_feature]`
6+
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:4: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)