Skip to content

Commit f41796e

Browse files
authoredMar 12, 2023
Rollup merge of #108651 - LeSeulArtichaut:108645-target-feature-on-main, r=Nilstrieb
Forbid the use of `#[target_feature]` on `main` Fixes #108645.
2 parents f41927f + 29b1789 commit f41796e

File tree

9 files changed

+84
-1
lines changed

9 files changed

+84
-1
lines changed
 

‎compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+3
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
242242
// Note that this is also allowed if `actually_rustdoc` so
243243
// if a target is documenting some wasm-specific code then
244244
// it's not spuriously denied.
245+
//
246+
// This exception needs to be kept in sync with allowing
247+
// `#[target_feature]` on `main` and `start`.
245248
} else if !tcx.features().target_feature_11 {
246249
let mut err = feature_err(
247250
&tcx.sess.parse_sess,

‎compiler/rustc_hir_analysis/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,14 @@ hir_analysis_where_clause_on_main = `main` function is not allowed to have a `wh
128128
hir_analysis_track_caller_on_main = `main` function is not allowed to be `#[track_caller]`
129129
.suggestion = remove this annotation
130130
131+
hir_analysis_target_feature_on_main = `main` function is not allowed to have `#[target_feature]`
132+
131133
hir_analysis_start_not_track_caller = `start` is not allowed to be `#[track_caller]`
132134
.label = `start` is not allowed to be `#[track_caller]`
133135
136+
hir_analysis_start_not_target_feature = `start` is not allowed to have `#[target_feature]`
137+
.label = `start` is not allowed to have `#[target_feature]`
138+
134139
hir_analysis_start_not_async = `start` is not allowed to be `async`
135140
.label = `start` is not allowed to be `async`
136141

‎compiler/rustc_hir_analysis/src/errors.rs

+17
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ pub(crate) struct TrackCallerOnMain {
327327
pub annotated: Span,
328328
}
329329

330+
#[derive(Diagnostic)]
331+
#[diag(hir_analysis_target_feature_on_main)]
332+
pub(crate) struct TargetFeatureOnMain {
333+
#[primary_span]
334+
#[label(hir_analysis_target_feature_on_main)]
335+
pub main: Span,
336+
}
337+
330338
#[derive(Diagnostic)]
331339
#[diag(hir_analysis_start_not_track_caller)]
332340
pub(crate) struct StartTrackCaller {
@@ -336,6 +344,15 @@ pub(crate) struct StartTrackCaller {
336344
pub start: Span,
337345
}
338346

347+
#[derive(Diagnostic)]
348+
#[diag(hir_analysis_start_not_target_feature)]
349+
pub(crate) struct StartTargetFeature {
350+
#[primary_span]
351+
pub span: Span,
352+
#[label]
353+
pub start: Span,
354+
}
355+
339356
#[derive(Diagnostic)]
340357
#[diag(hir_analysis_start_not_async, code = "E0752")]
341358
pub(crate) struct StartAsync {

‎compiler/rustc_hir_analysis/src/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,15 @@ 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+
// Calling functions with `#[target_feature]` is not unsafe on WASM, see #84988
288+
&& !tcx.sess.target.is_like_wasm
289+
&& !tcx.sess.opts.actually_rustdoc
290+
{
291+
tcx.sess.emit_err(errors::TargetFeatureOnMain { main: main_span });
292+
error = true;
293+
}
294+
286295
if error {
287296
return;
288297
}
@@ -373,6 +382,18 @@ fn check_start_fn_ty(tcx: TyCtxt<'_>, start_def_id: DefId) {
373382
});
374383
error = true;
375384
}
385+
if attr.has_name(sym::target_feature)
386+
// Calling functions with `#[target_feature]` is
387+
// not unsafe on WASM, see #84988
388+
&& !tcx.sess.target.is_like_wasm
389+
&& !tcx.sess.opts.actually_rustdoc
390+
{
391+
tcx.sess.emit_err(errors::StartTargetFeature {
392+
span: attr.span,
393+
start: start_span,
394+
});
395+
error = true;
396+
}
376397
}
377398

378399
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)
Please sign in to comment.