Forbid #[target_feature] on safe default implementations

This commit is contained in:
Léo Lanteri Thauvin 2023-03-10 13:50:51 +01:00
parent 104f4300cf
commit ad2bcb5c0e
3 changed files with 17 additions and 2 deletions

View File

@ -442,7 +442,7 @@ fn asm_target_features(tcx: TyCtxt<'_>, did: DefId) -> &FxIndexSet<Symbol> {
pub fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) { pub fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
if let DefKind::AssocFn = tcx.def_kind(id) { if let DefKind::AssocFn = tcx.def_kind(id) {
let parent_id = tcx.local_parent(id); let parent_id = tcx.local_parent(id);
if let DefKind::Impl { of_trait: true } = tcx.def_kind(parent_id) { if let DefKind::Trait | DefKind::Impl { of_trait: true } = tcx.def_kind(parent_id) {
tcx.sess tcx.sess
.struct_span_err( .struct_span_err(
attr_span, attr_span,

View File

@ -18,4 +18,10 @@ fn foo(&self) {}
unsafe fn unsf_foo(&self) {} unsafe fn unsf_foo(&self) {}
} }
trait Qux {
#[target_feature(enable = "sse2")]
//~^ ERROR cannot be applied to safe trait method
fn foo(&self) {}
}
fn main() {} fn main() {}

View File

@ -1,3 +1,12 @@
error: `#[target_feature(..)]` cannot be applied to safe trait method
--> $DIR/trait-impl.rs:22:5
|
LL | #[target_feature(enable = "sse2")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot be applied to safe trait method
LL |
LL | fn foo(&self) {}
| ------------- not an `unsafe` function
error: `#[target_feature(..)]` cannot be applied to safe trait method error: `#[target_feature(..)]` cannot be applied to safe trait method
--> $DIR/trait-impl.rs:13:5 --> $DIR/trait-impl.rs:13:5
| |
@ -7,5 +16,5 @@ LL |
LL | fn foo(&self) {} LL | fn foo(&self) {}
| ------------- not an `unsafe` function | ------------- not an `unsafe` function
error: aborting due to previous error error: aborting due to 2 previous errors