Skip to content

Commit

Permalink
Rollup merge of #108651 - LeSeulArtichaut:108645-target-feature-on-ma…
Browse files Browse the repository at this point in the history
…in, r=Nilstrieb

Forbid the use of `#[target_feature]` on `main`

Fixes #108645.
  • Loading branch information
compiler-errors authored Mar 2, 2023
2 parents c897cbe + dc6e0d9 commit b5aec1c
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions compiler/rustc_hir_analysis/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ hir_analysis_where_clause_on_main = `main` function is not allowed to have a `wh
hir_analysis_track_caller_on_main = `main` function is not allowed to be `#[track_caller]`
.suggestion = remove this annotation
hir_analysis_target_feature_on_main = `main` function is not allowed to have `#[target_feature]`
hir_analysis_start_not_track_caller = `start` is not allowed to be `#[track_caller]`
.label = `start` is not allowed to be `#[track_caller]`
hir_analysis_start_not_target_feature = `start` is not allowed to have `#[target_feature]`
.label = `start` is not allowed to have `#[target_feature]`
hir_analysis_start_not_async = `start` is not allowed to be `async`
.label = `start` is not allowed to be `async`
Expand Down
17 changes: 17 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ pub(crate) struct TrackCallerOnMain {
pub annotated: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_target_feature_on_main)]
pub(crate) struct TargetFeatureOnMain {
#[primary_span]
#[label(hir_analysis_target_feature_on_main)]
pub main: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_start_not_track_caller)]
pub(crate) struct StartTrackCaller {
Expand All @@ -324,6 +332,15 @@ pub(crate) struct StartTrackCaller {
pub start: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_start_not_target_feature)]
pub(crate) struct StartTargetFeature {
#[primary_span]
pub span: Span,
#[label]
pub start: Span,
}

#[derive(Diagnostic)]
#[diag(hir_analysis_start_not_async, code = "E0752")]
pub(crate) struct StartAsync {
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
error = true;
}

if !tcx.codegen_fn_attrs(main_def_id).target_features.is_empty() {
tcx.sess.emit_err(errors::TargetFeatureOnMain { main: main_span });
error = true;
}

if error {
return;
}
Expand Down Expand Up @@ -373,6 +378,13 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
});
error = true;
}
if attr.has_name(sym::target_feature) {
tcx.sess.emit_err(errors::StartTargetFeature {
span: attr.span,
start: start_span,
});
error = true;
}
}

if error {
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/asm/x86_64/issue-89875.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
use std::arch::asm;

#[target_feature(enable = "avx")]
fn main() {
fn foo() {
unsafe {
asm!(
"/* {} */",
out(ymm_reg) _,
);
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// only-x86_64

#[target_feature(enable = "avx2")]
fn main() {}
//~^ ERROR `main` function is not allowed to have `#[target_feature]`
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: `main` function is not allowed to have `#[target_feature]`
--> $DIR/issue-108645-target-feature-on-main.rs:4:1
|
LL | fn main() {}
| ^^^^^^^^^ `main` function is not allowed to have `#[target_feature]`

error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![feature(start)]

#[start]
#[target_feature(enable = "avx2")]
//~^ ERROR `start` is not allowed to have `#[target_feature]`
fn start(_argc: isize, _argv: *const *const u8) -> isize { 0 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: `start` is not allowed to have `#[target_feature]`
--> $DIR/issue-108645-target-feature-on-start.rs:4:1
|
LL | #[target_feature(enable = "avx2")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | fn start(_argc: isize, _argv: *const *const u8) -> isize { 0 }
| -------------------------------------------------------- `start` is not allowed to have `#[target_feature]`

error: aborting due to previous error

0 comments on commit b5aec1c

Please sign in to comment.