Skip to content

Commit 4b869bf

Browse files
committed
Auto merge of #141465 - RalfJung:target-feature-in-trait, r=<try>
forbid #[target_feature] in traits `target_feature` in trait *implementations* makes basically no sense -- it's almost always unsound since a generic caller has no way of knowing the required target features! In particular, now that we can have *safe* target feature functions, that's *definitely* unsound in combination with `dyn`, see #139368. As sound version of this would have to check that the same target features are already set in the trait declaration. But only functions with bodies can even have target features in the trait declaration! So, this was never actually designed in a way that makes any sense, it just kinda did something. Interestingly there were tests for `#[target_feature]` in traits and trait impls... not sure how that didn't set off alarm bells. Oh well. I propose we just entirely forbid target features in trait impls and trait declarations. This will need an FCW, but before we do that let's get an idea for how big the fallout is by doing a crater run. The diagnostics that result from PR are terrible but it's good enough for crater.
2 parents e88e854 + 3b43b6a commit 4b869bf

File tree

7 files changed

+41
-103
lines changed

7 files changed

+41
-103
lines changed

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
861861
attrs: &[Attribute],
862862
) {
863863
match target {
864-
Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
865-
| Target::Fn => {
864+
Target::Method(MethodKind::Inherent) | Target::Fn => {
866865
// `#[target_feature]` is not allowed in lang items.
867866
if let Some((lang_item, _)) = hir::lang_items::extract(attrs)
868867
// Calling functions with `#[target_feature]` is

tests/ui/rfcs/rfc-2396-target_feature-11/trait-impl.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

tests/ui/rfcs/rfc-2396-target_feature-11/trait-impl.stderr

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/ui/target-feature/invalid-attribute.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,6 @@ impl Foo {}
9090

9191
trait Quux {
9292
fn foo(); //~ NOTE `foo` from trait
93-
//~^ NOTE: type in trait
94-
}
95-
96-
impl Quux for Foo {
97-
#[target_feature(enable = "sse2")]
98-
//~^ ERROR `#[target_feature(..)]` cannot be applied to safe trait method
99-
//~| NOTE cannot be applied to safe trait method
100-
fn foo() {}
101-
//~^ NOTE not an `unsafe` function
102-
//~| ERROR: incompatible type for trait
103-
//~| NOTE: expected safe fn, found unsafe fn
104-
//~| NOTE: expected signature `fn()`
10593
}
10694

10795
fn main() {

tests/ui/target-feature/invalid-attribute.stderr

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ LL | impl Foo {}
132132
| ----------- not a function definition
133133

134134
error: attribute should be applied to a function definition
135-
--> $DIR/invalid-attribute.rs:108:5
135+
--> $DIR/invalid-attribute.rs:96:5
136136
|
137137
LL | #[target_feature(enable = "sse2")]
138138
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -143,7 +143,7 @@ LL | | }
143143
| |_____- not a function definition
144144

145145
error: attribute should be applied to a function definition
146-
--> $DIR/invalid-attribute.rs:115:5
146+
--> $DIR/invalid-attribute.rs:103:5
147147
|
148148
LL | #[target_feature(enable = "sse2")]
149149
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -178,30 +178,6 @@ LL | impl Quux for u8 {}
178178
LL | fn foo();
179179
| --------- `foo` from trait
180180

181-
error: `#[target_feature(..)]` cannot be applied to safe trait method
182-
--> $DIR/invalid-attribute.rs:97:5
183-
|
184-
LL | #[target_feature(enable = "sse2")]
185-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot be applied to safe trait method
186-
...
187-
LL | fn foo() {}
188-
| -------- not an `unsafe` function
189-
190-
error[E0053]: method `foo` has an incompatible type for trait
191-
--> $DIR/invalid-attribute.rs:100:5
192-
|
193-
LL | fn foo() {}
194-
| ^^^^^^^^ expected safe fn, found unsafe fn
195-
|
196-
note: type in trait
197-
--> $DIR/invalid-attribute.rs:92:5
198-
|
199-
LL | fn foo();
200-
| ^^^^^^^^^
201-
= note: expected signature `fn()`
202-
found signature `#[target_features] fn()`
203-
204-
error: aborting due to 23 previous errors
181+
error: aborting due to 21 previous errors
205182

206-
Some errors have detailed explanations: E0046, E0053.
207-
For more information about an error, try `rustc --explain E0046`.
183+
For more information about this error, try `rustc --explain E0046`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ compile-flags: --crate-type=lib
2+
//@ compile-flags: --target=aarch64-unknown-linux-gnu
3+
//@ needs-llvm-components: aarch64
4+
#![feature(no_core, lang_items)]
5+
#![no_core]
6+
7+
#[lang = "sized"]
8+
pub trait Sized {}
9+
10+
pub trait T {
11+
#[target_feature(enable = "aes")]
12+
unsafe fn foo() {} //~^ERROR: should be applied to a function definition
13+
}
14+
15+
impl T for i32 {
16+
#[target_feature(enable = "aes")]
17+
unsafe fn foo() {} //~^ERROR: should be applied to a function definition
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: attribute should be applied to a function definition
2+
--> $DIR/not-in-trait.rs:11:5
3+
|
4+
LL | #[target_feature(enable = "aes")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
LL | unsafe fn foo() {}
7+
| ------------------ not a function definition
8+
9+
error: attribute should be applied to a function definition
10+
--> $DIR/not-in-trait.rs:16:5
11+
|
12+
LL | #[target_feature(enable = "aes")]
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
LL | unsafe fn foo() {}
15+
| ------------------ not a function definition
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)