Skip to content

Commit c8a73e4

Browse files
authored
Rollup merge of rust-lang#48752 - alexcrichton:fix-target-feature, r=michaelwoerister
rustc: Fix ICE with `#[target_feature]` on module This commit fixes an ICE in rustc when `#[target_feature]` was applied to items other than functions due to the way the feature was validated.
2 parents 7c581b0 + 4bde92c commit c8a73e4

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

src/librustc/hir/check_attr.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ struct CheckAttrVisitor<'a, 'tcx: 'a> {
4747
impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
4848
/// Check any attribute.
4949
fn check_attributes(&self, item: &hir::Item, target: Target) {
50-
self.tcx.trans_fn_attrs(self.tcx.hir.local_def_id(item.id));
50+
if target == Target::Fn {
51+
self.tcx.trans_fn_attrs(self.tcx.hir.local_def_id(item.id));
52+
} else if let Some(a) = item.attrs.iter().find(|a| a.check_name("target_feature")) {
53+
self.tcx.sess.struct_span_err(a.span, "attribute should be applied to a function")
54+
.span_label(item.span, "not a function")
55+
.emit();
56+
}
5157

5258
for attr in &item.attrs {
5359
if let Some(name) = attr.name() {

src/librustc_typeck/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,7 @@ fn trans_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> TransFnAt
18661866
.emit();
18671867
}
18681868
} else if attr.check_name("target_feature") {
1869+
// handle deprecated #[target_feature = "..."]
18691870
if let Some(val) = attr.value_str() {
18701871
for feat in val.as_str().split(",").map(|f| f.trim()) {
18711872
if !feat.is_empty() && !feat.contains('\0') {

src/test/ui/target-feature-wrong.rs

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ unsafe fn foo() {}
2929
//~^ ERROR: can only be applied to `unsafe` function
3030
fn bar() {}
3131

32+
#[target_feature(enable = "sse2")]
33+
//~^ ERROR: should be applied to a function
34+
mod another {}
35+
3236
fn main() {
3337
unsafe {
3438
foo();

src/test/ui/target-feature-wrong.stderr

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,14 @@ error: #[target_feature(..)] can only be applied to `unsafe` function
2828
LL | #[target_feature(enable = "sse2")]
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3030

31-
error: aborting due to 4 previous errors
31+
error: attribute should be applied to a function
32+
--> $DIR/target-feature-wrong.rs:32:1
33+
|
34+
LL | #[target_feature(enable = "sse2")]
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
LL | //~^ ERROR: should be applied to a function
37+
LL | mod another {}
38+
| -------------- not a function
39+
40+
error: aborting due to 5 previous errors
3241

0 commit comments

Comments
 (0)