Skip to content

Commit fc9da8f

Browse files
committed
rustc: Whitelist upstream target_features
When compiling crates we'll be calculating and parsing `#[target_feature]` for upstream crates. We'll also be checking the stability of listed features, but we only want to check the listed stability during the actual crate that wrote the relevant code. This commit updates the `target_feature` process to ignore foreign `DefId` instances and only check the feature whitelist for local functions. Closes #50094
1 parent 8830a03 commit fc9da8f

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/librustc_typeck/collect.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,7 @@ fn is_foreign_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
16821682

16831683
fn from_target_feature(
16841684
tcx: TyCtxt,
1685+
id: DefId,
16851686
attr: &ast::Attribute,
16861687
whitelist: &FxHashMap<String, Option<String>>,
16871688
target_features: &mut Vec<Symbol>,
@@ -1752,7 +1753,7 @@ fn from_target_feature(
17521753
Some(name) => bug!("unknown target feature gate {}", name),
17531754
None => true,
17541755
};
1755-
if !allowed {
1756+
if !allowed && id.is_local() {
17561757
feature_gate::emit_feature_err(
17571758
&tcx.sess.parse_sess,
17581759
feature_gate.as_ref().unwrap(),
@@ -1877,7 +1878,7 @@ fn trans_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> TransFnAt
18771878
`unsafe` function";
18781879
tcx.sess.span_err(attr.span, msg);
18791880
}
1880-
from_target_feature(tcx, attr, &whitelist, &mut trans_fn_attrs.target_features);
1881+
from_target_feature(tcx, id, attr, &whitelist, &mut trans_fn_attrs.target_features);
18811882
} else if attr.check_name("linkage") {
18821883
if let Some(val) = attr.value_str() {
18831884
trans_fn_attrs.linkage = Some(linkage_by_name(tcx, id, &val.as_str()));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(mmx_target_feature)]
12+
13+
#[inline]
14+
#[target_feature(enable = "mmx")]
15+
pub unsafe fn foo() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// only-x86_64
12+
// aux-build:using-target-feature-unstable.rs
13+
14+
extern crate using_target_feature_unstable;
15+
16+
fn main() {
17+
unsafe {
18+
using_target_feature_unstable::foo();
19+
}
20+
}

0 commit comments

Comments
 (0)