Skip to content

Commit

Permalink
rustc: Whitelist upstream target_features
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alexcrichton committed Apr 20, 2018
1 parent 8830a03 commit fc9da8f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ fn is_foreign_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

fn from_target_feature(
tcx: TyCtxt,
id: DefId,
attr: &ast::Attribute,
whitelist: &FxHashMap<String, Option<String>>,
target_features: &mut Vec<Symbol>,
Expand Down Expand Up @@ -1752,7 +1753,7 @@ fn from_target_feature(
Some(name) => bug!("unknown target feature gate {}", name),
None => true,
};
if !allowed {
if !allowed && id.is_local() {
feature_gate::emit_feature_err(
&tcx.sess.parse_sess,
feature_gate.as_ref().unwrap(),
Expand Down Expand Up @@ -1877,7 +1878,7 @@ fn trans_fn_attrs<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, id: DefId) -> TransFnAt
`unsafe` function";
tcx.sess.span_err(attr.span, msg);
}
from_target_feature(tcx, attr, &whitelist, &mut trans_fn_attrs.target_features);
from_target_feature(tcx, id, attr, &whitelist, &mut trans_fn_attrs.target_features);
} else if attr.check_name("linkage") {
if let Some(val) = attr.value_str() {
trans_fn_attrs.linkage = Some(linkage_by_name(tcx, id, &val.as_str()));
Expand Down
15 changes: 15 additions & 0 deletions src/test/run-pass/auxiliary/using-target-feature-unstable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(mmx_target_feature)]

#[inline]
#[target_feature(enable = "mmx")]
pub unsafe fn foo() {}
20 changes: 20 additions & 0 deletions src/test/run-pass/using-target-feature-unstable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// only-x86_64
// aux-build:using-target-feature-unstable.rs

extern crate using_target_feature_unstable;

fn main() {
unsafe {
using_target_feature_unstable::foo();
}
}

0 comments on commit fc9da8f

Please sign in to comment.