Skip to content

Commit

Permalink
Require a list of features to allow in allow_internal_unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 11, 2019
1 parent 57d7cfc commit d3c212c
Show file tree
Hide file tree
Showing 35 changed files with 182 additions and 93 deletions.
3 changes: 2 additions & 1 deletion src/liballoc/macros.rs
Expand Up @@ -34,7 +34,8 @@
#[cfg(not(test))]
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
#[cfg_attr(not(stage0), allow_internal_unstable(box_syntax))]
#[cfg_attr(stage0, allow_internal_unstable)]
macro_rules! vec {
($elem:expr; $n:expr) => (
$crate::vec::from_elem($elem, $n)
Expand Down
6 changes: 4 additions & 2 deletions src/libcore/macros.rs
@@ -1,6 +1,7 @@
/// Entry point of thread panic, for details, see std::macros
#[macro_export]
#[allow_internal_unstable]
#[cfg_attr(not(stage0), allow_internal_unstable(core_panic, __rust_unstable_column))]
#[cfg_attr(stage0, allow_internal_unstable)]
#[stable(feature = "core", since = "1.6.0")]
macro_rules! panic {
() => (
Expand Down Expand Up @@ -409,7 +410,8 @@ macro_rules! write {
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(format_args_nl))]
macro_rules! writeln {
($dst:expr) => (
write!($dst, "\n")
Expand Down
37 changes: 30 additions & 7 deletions src/librustc/hir/lowering.rs
Expand Up @@ -681,13 +681,18 @@ impl<'a> LoweringContext<'a> {
Ident::with_empty_ctxt(Symbol::gensym(s))
}

fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, span: Span) -> Span {
fn allow_internal_unstable(
&self,
reason: CompilerDesugaringKind,
span: Span,
allow_internal_unstable: Vec<Symbol>,
) -> Span {
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(source_map::ExpnInfo {
call_site: span,
def_site: Some(span),
format: source_map::CompilerDesugaring(reason),
allow_internal_unstable: true,
allow_internal_unstable,
allow_internal_unsafe: false,
local_inner_macros: false,
edition: source_map::hygiene::default_edition(),
Expand Down Expand Up @@ -964,7 +969,13 @@ impl<'a> LoweringContext<'a> {
attrs: ThinVec::new(),
};

let unstable_span = self.allow_internal_unstable(CompilerDesugaringKind::Async, span);
let unstable_span = self.allow_internal_unstable(
CompilerDesugaringKind::Async,
span,
vec![
Symbol::intern("gen_future"),
],
);
let gen_future = self.expr_std_path(
unstable_span, &["future", "from_generator"], None, ThinVec::new());
hir::ExprKind::Call(P(gen_future), hir_vec![generator])
Expand Down Expand Up @@ -1363,6 +1374,7 @@ impl<'a> LoweringContext<'a> {
let exist_ty_span = self.allow_internal_unstable(
CompilerDesugaringKind::ExistentialReturnType,
span,
Vec::new(), // doesn'c actually allow anything unstable
);

let exist_ty_def_index = self
Expand Down Expand Up @@ -3927,8 +3939,13 @@ impl<'a> LoweringContext<'a> {
}),
ExprKind::TryBlock(ref body) => {
self.with_catch_scope(body.id, |this| {
let unstable_span =
this.allow_internal_unstable(CompilerDesugaringKind::TryBlock, body.span);
let unstable_span = this.allow_internal_unstable(
CompilerDesugaringKind::TryBlock,
body.span,
vec![
Symbol::intern("try_trait"),
],
);
let mut block = this.lower_block(body, true).into_inner();
let tail = block.expr.take().map_or_else(
|| {
Expand Down Expand Up @@ -4363,6 +4380,7 @@ impl<'a> LoweringContext<'a> {
let desugared_span = self.allow_internal_unstable(
CompilerDesugaringKind::ForLoop,
head_sp,
Vec::new(),
);

let iter = self.str_to_ident("iter");
Expand Down Expand Up @@ -4525,8 +4543,13 @@ impl<'a> LoweringContext<'a> {
// return Try::from_error(From::from(err)),
// }

let unstable_span =
self.allow_internal_unstable(CompilerDesugaringKind::QuestionMark, e.span);
let unstable_span = self.allow_internal_unstable(
CompilerDesugaringKind::QuestionMark,
e.span,
vec![
Symbol::intern("try_trait")
],
);

// `Try::into_result(<expr>)`
let discr = {
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/middle/stability.rs
Expand Up @@ -561,11 +561,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
/// deprecated. If the item is indeed deprecated, we will emit a deprecation lint attached to
/// `id`.
pub fn eval_stability(self, def_id: DefId, id: Option<NodeId>, span: Span) -> EvalResult {
if span.allows_unstable() {
debug!("stability: skipping span={:?} since it is internal", span);
return EvalResult::Allow;
}

let lint_deprecated = |def_id: DefId,
id: NodeId,
note: Option<Symbol>,
Expand Down Expand Up @@ -694,6 +689,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {

match stability {
Some(&Stability { level: attr::Unstable { reason, issue }, feature, .. }) => {
if span.allows_unstable(&feature.as_str()) {
debug!("stability: skipping span={:?} since it is internal", span);
return EvalResult::Allow;
}
if self.stability().active_features.contains(&feature) {
return EvalResult::Allow;
}
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_allocator/expand.rs
Expand Up @@ -91,7 +91,9 @@ impl MutVisitor for ExpandAllocatorDirectives<'_> {
call_site: item.span, // use the call site of the static
def_site: None,
format: MacroAttribute(Symbol::intern(name)),
allow_internal_unstable: true,
allow_internal_unstable: vec![
Symbol::intern("rustc_attrs"),
],
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/creader.rs
Expand Up @@ -570,7 +570,7 @@ impl<'a> CrateLoader<'a> {
ProcMacro::Bang { name, client } => {
(name, SyntaxExtension::ProcMacro {
expander: Box::new(BangProcMacro { client }),
allow_internal_unstable: false,
allow_internal_unstable: Vec::new(),
edition: root.edition,
})
}
Expand Down
4 changes: 3 additions & 1 deletion src/librustc_metadata/cstore_impl.rs
Expand Up @@ -425,7 +425,9 @@ impl cstore::CStore {
let client = proc_macro::bridge::client::Client::expand1(proc_macro::quote);
let ext = SyntaxExtension::ProcMacro {
expander: Box::new(BangProcMacro { client }),
allow_internal_unstable: true,
allow_internal_unstable: vec![
Symbol::intern("proc_macro_def_site"),
],
edition: data.root.edition,
};
return LoadedMacro::ProcMacro(Lrc::new(ext));
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/transform/qualify_consts.rs
Expand Up @@ -907,7 +907,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
// Check `#[unstable]` const fns or `#[rustc_const_unstable]`
// functions without the feature gate active in this crate in
// order to report a better error message than the one below.
if self.span.allows_unstable() {
if self.span.allows_unstable(&feature.as_str()) {
// `allow_internal_unstable` can make such calls stable.
is_const_fn = true;
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_plugin/registry.rs
Expand Up @@ -110,8 +110,8 @@ impl<'a> Registry<'a> {
edition,
}
}
IdentTT(ext, _, allow_internal_unstable) => {
IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
IdentTT { ext, span: _, allow_internal_unstable } => {
IdentTT { ext, span: Some(self.krate_span), allow_internal_unstable }
}
_ => extension,
}));
Expand All @@ -126,7 +126,7 @@ impl<'a> Registry<'a> {
self.register_syntax_extension(Symbol::intern(name), NormalTT {
expander: Box::new(expander),
def_info: None,
allow_internal_unstable: false,
allow_internal_unstable: Vec::new(),
allow_internal_unsafe: false,
local_inner_macros: false,
unstable_feature: None,
Expand Down
18 changes: 12 additions & 6 deletions src/libstd/macros.rs
Expand Up @@ -53,7 +53,8 @@
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(__rust_unstable_column, libstd_sys_internals))]
macro_rules! panic {
() => ({
panic!("explicit panic")
Expand Down Expand Up @@ -111,7 +112,8 @@ macro_rules! panic {
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(print_internals))]
macro_rules! print {
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
}
Expand Down Expand Up @@ -143,7 +145,8 @@ macro_rules! print {
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(print_internals, format_args_nl))]
macro_rules! println {
() => (print!("\n"));
($($arg:tt)*) => ({
Expand Down Expand Up @@ -174,7 +177,8 @@ macro_rules! println {
/// ```
#[macro_export]
#[stable(feature = "eprint", since = "1.19.0")]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(print_internals))]
macro_rules! eprint {
($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*)));
}
Expand Down Expand Up @@ -202,7 +206,8 @@ macro_rules! eprint {
/// ```
#[macro_export]
#[stable(feature = "eprint", since = "1.19.0")]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(print_internals, format_args_nl))]
macro_rules! eprintln {
() => (eprint!("\n"));
($($arg:tt)*) => ({
Expand Down Expand Up @@ -325,7 +330,8 @@ macro_rules! dbg {
/// A macro to await on an async call.
#[macro_export]
#[unstable(feature = "await_macro", issue = "50547")]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(gen_future, generators))]
#[allow_internal_unsafe]
macro_rules! await {
($e:expr) => { {
Expand Down
8 changes: 6 additions & 2 deletions src/libstd/thread/local.rs
Expand Up @@ -126,7 +126,8 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
/// [`std::thread::LocalKey`]: ../std/thread/struct.LocalKey.html
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(thread_local_internals))]
macro_rules! thread_local {
// empty (base case for the recursion)
() => {};
Expand All @@ -148,7 +149,10 @@ macro_rules! thread_local {
reason = "should not be necessary",
issue = "0")]
#[macro_export]
#[allow_internal_unstable]
#[cfg_attr(stage0, allow_internal_unstable)]
#[cfg_attr(not(stage0), allow_internal_unstable(
thread_local_internals, cfg_target_thread_local, thread_local,
))]
#[allow_internal_unsafe]
macro_rules! __thread_local_inner {
(@key $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {
Expand Down
20 changes: 13 additions & 7 deletions src/libsyntax/ext/base.rs
Expand Up @@ -621,7 +621,8 @@ pub enum SyntaxExtension {
/// A function-like procedural macro. TokenStream -> TokenStream.
ProcMacro {
expander: Box<dyn ProcMacro + sync::Sync + sync::Send>,
allow_internal_unstable: bool,
/// Whitelist of unstable features that are treated as stable inside this macro
allow_internal_unstable: Vec<Symbol>,
edition: Edition,
},

Expand All @@ -638,8 +639,10 @@ pub enum SyntaxExtension {
expander: Box<dyn TTMacroExpander + sync::Sync + sync::Send>,
def_info: Option<(ast::NodeId, Span)>,
/// Whether the contents of the macro can
/// directly use `#[unstable]` things (true == yes).
allow_internal_unstable: bool,
/// directly use `#[unstable]` things.
///
/// Only allows things that require a feature gate in the given whitelist
allow_internal_unstable: Vec<Symbol>,
/// Whether the contents of the macro can use `unsafe`
/// without triggering the `unsafe_code` lint.
allow_internal_unsafe: bool,
Expand All @@ -654,8 +657,11 @@ pub enum SyntaxExtension {

/// A function-like syntax extension that has an extra ident before
/// the block.
///
IdentTT(Box<dyn IdentMacroExpander + sync::Sync + sync::Send>, Option<Span>, bool),
IdentTT {
ext: Box<dyn IdentMacroExpander + sync::Sync + sync::Send>,
span: Option<Span>,
allow_internal_unstable: Vec<Symbol>,
},

/// An attribute-like procedural macro. TokenStream -> TokenStream.
/// The input is the annotated item.
Expand All @@ -682,7 +688,7 @@ impl SyntaxExtension {
match *self {
SyntaxExtension::DeclMacro { .. } |
SyntaxExtension::NormalTT { .. } |
SyntaxExtension::IdentTT(..) |
SyntaxExtension::IdentTT { .. } |
SyntaxExtension::ProcMacro { .. } =>
MacroKind::Bang,
SyntaxExtension::NonMacroAttr { .. } |
Expand Down Expand Up @@ -716,7 +722,7 @@ impl SyntaxExtension {
SyntaxExtension::ProcMacroDerive(.., edition) => edition,
// Unstable legacy stuff
SyntaxExtension::NonMacroAttr { .. } |
SyntaxExtension::IdentTT(..) |
SyntaxExtension::IdentTT { .. } |
SyntaxExtension::MultiDecorator(..) |
SyntaxExtension::MultiModifier(..) |
SyntaxExtension::BuiltinDerive(..) => hygiene::default_edition(),
Expand Down
5 changes: 4 additions & 1 deletion src/libsyntax/ext/derive.rs
Expand Up @@ -58,7 +58,10 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt<'_>, span: Span, traits: &[ast::P
call_site: span,
def_site: None,
format: ExpnFormat::MacroAttribute(Symbol::intern(&pretty_name)),
allow_internal_unstable: true,
allow_internal_unstable: vec![
Symbol::intern("rustc_attrs"),
Symbol::intern("structural_match"),
],
allow_internal_unsafe: false,
local_inner_macros: false,
edition: hygiene::default_edition(),
Expand Down

0 comments on commit d3c212c

Please sign in to comment.