Add #[allow_internal_unstable] to track stability for macros better.

Unstable items used in a macro expansion will now always trigger
stability warnings, *unless* the unstable items are directly inside a
macro marked with `#[allow_internal_unstable]`. IOW, the compiler warns
unless the span of the unstable item is a subspan of the definition of a
macro marked with that attribute.

E.g.

    #[allow_internal_unstable]
    macro_rules! foo {
        ($e: expr) => {{
            $e;
            unstable(); // no warning
            only_called_by_foo!();
        }}
    }

    macro_rules! only_called_by_foo {
        () => { unstable() } // warning
    }

    foo!(unstable()) // warning

The unstable inside `foo` is fine, due to the attribute. But the
`unstable` inside `only_called_by_foo` is not, since that macro doesn't
have the attribute, and the `unstable` passed into `foo` is also not
fine since it isn't contained in the macro itself (that is, even though
it is only used directly in the macro).

In the process this makes the stability tracking much more precise,
e.g. previously `println!("{}", unstable())` got no warning, but now it
does. As such, this is a bug fix that may cause [breaking-change]s.

The attribute is definitely feature gated, since it explicitly allows
side-stepping the feature gating system.
This commit is contained in:
Huon Wilson 2015-03-01 14:09:28 +11:00
parent 68740b4054
commit 84b060ce29
21 changed files with 328 additions and 58 deletions

View File

@ -2555,6 +2555,14 @@ The currently implemented features of the reference compiler are:
types, e.g. as the return type of a public function. types, e.g. as the return type of a public function.
This capability may be removed in the future. This capability may be removed in the future.
* `allow_internal_unstable` - Allows `macro_rules!` macros to be tagged with the
`#[allow_internal_unstable]` attribute, designed
to allow `std` macros to call
`#[unstable]`/feature-gated functionality
internally without imposing on callers
(i.e. making them behave like function calls in
terms of encapsulation).
If a feature is promoted to a language feature, then all existing programs will If a feature is promoted to a language feature, then all existing programs will
start to receive compilation warnings about #[feature] directives which enabled start to receive compilation warnings about #[feature] directives which enabled
the new feature (because the directive is no longer necessary). However, if a the new feature (because the directive is no longer necessary). However, if a

View File

@ -1238,6 +1238,7 @@ pub fn from_f64<A: FromPrimitive>(n: f64) -> Option<A> {
macro_rules! impl_from_primitive { macro_rules! impl_from_primitive {
($T:ty, $to_ty:ident) => ( ($T:ty, $to_ty:ident) => (
#[allow(deprecated)]
impl FromPrimitive for $T { impl FromPrimitive for $T {
#[inline] fn from_int(n: int) -> Option<$T> { n.$to_ty() } #[inline] fn from_int(n: int) -> Option<$T> { n.$to_ty() }
#[inline] fn from_i8(n: i8) -> Option<$T> { n.$to_ty() } #[inline] fn from_i8(n: i8) -> Option<$T> { n.$to_ty() }
@ -1299,6 +1300,7 @@ macro_rules! impl_num_cast {
($T:ty, $conv:ident) => ( ($T:ty, $conv:ident) => (
impl NumCast for $T { impl NumCast for $T {
#[inline] #[inline]
#[allow(deprecated)]
fn from<N: ToPrimitive>(n: N) -> Option<$T> { fn from<N: ToPrimitive>(n: N) -> Option<$T> {
// `$conv` could be generated using `concat_idents!`, but that // `$conv` could be generated using `concat_idents!`, but that
// macro seems to be broken at the moment // macro seems to be broken at the moment

View File

@ -542,6 +542,7 @@ impl<'a> CrateReader<'a> {
// overridden in plugin/load.rs // overridden in plugin/load.rs
export: false, export: false,
use_locally: false, use_locally: false,
allow_internal_unstable: false,
body: body, body: body,
}); });

View File

@ -166,6 +166,9 @@ impl<'a> MacroLoader<'a> {
Some(sel) => sel.contains_key(&name), Some(sel) => sel.contains_key(&name),
}; };
def.export = reexport.contains_key(&name); def.export = reexport.contains_key(&name);
def.allow_internal_unstable = attr::contains_name(&def.attrs,
"allow_internal_unstable");
debug!("load_macros: loaded: {:?}", def);
self.macros.push(def); self.macros.push(def);
} }

View File

@ -362,8 +362,6 @@ pub fn check_item(tcx: &ty::ctxt, item: &ast::Item, warn_about_defns: bool,
/// Helper for discovering nodes to check for stability /// Helper for discovering nodes to check for stability
pub fn check_expr(tcx: &ty::ctxt, e: &ast::Expr, pub fn check_expr(tcx: &ty::ctxt, e: &ast::Expr,
cb: &mut FnMut(ast::DefId, Span, &Option<Stability>)) { cb: &mut FnMut(ast::DefId, Span, &Option<Stability>)) {
if is_internal(tcx, e.span) { return; }
let span; let span;
let id = match e.node { let id = match e.node {
ast::ExprMethodCall(i, _, _) => { ast::ExprMethodCall(i, _, _) => {
@ -527,12 +525,13 @@ pub fn check_pat(tcx: &ty::ctxt, pat: &ast::Pat,
fn maybe_do_stability_check(tcx: &ty::ctxt, id: ast::DefId, span: Span, fn maybe_do_stability_check(tcx: &ty::ctxt, id: ast::DefId, span: Span,
cb: &mut FnMut(ast::DefId, Span, &Option<Stability>)) { cb: &mut FnMut(ast::DefId, Span, &Option<Stability>)) {
if !is_staged_api(tcx, id) { return } if !is_staged_api(tcx, id) { return }
if is_internal(tcx, span) { return }
let ref stability = lookup(tcx, id); let ref stability = lookup(tcx, id);
cb(id, span, stability); cb(id, span, stability);
} }
fn is_internal(tcx: &ty::ctxt, span: Span) -> bool { fn is_internal(tcx: &ty::ctxt, span: Span) -> bool {
tcx.sess.codemap().span_is_internal(span) tcx.sess.codemap().span_allows_unstable(span)
} }
fn is_staged_api(tcx: &ty::ctxt, id: DefId) -> bool { fn is_staged_api(tcx: &ty::ctxt, id: DefId) -> bool {

View File

@ -81,8 +81,12 @@ impl<'a> Registry<'a> {
/// This is the most general hook into `libsyntax`'s expansion behavior. /// This is the most general hook into `libsyntax`'s expansion behavior.
pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) { pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxExtension) {
self.syntax_exts.push((name, match extension { self.syntax_exts.push((name, match extension {
NormalTT(ext, _) => NormalTT(ext, Some(self.krate_span)), NormalTT(ext, _, allow_internal_unstable) => {
IdentTT(ext, _) => IdentTT(ext, Some(self.krate_span)), NormalTT(ext, Some(self.krate_span), allow_internal_unstable)
}
IdentTT(ext, _, allow_internal_unstable) => {
IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
}
Decorator(ext) => Decorator(ext), Decorator(ext) => Decorator(ext),
Modifier(ext) => Modifier(ext), Modifier(ext) => Modifier(ext),
MultiModifier(ext) => MultiModifier(ext), MultiModifier(ext) => MultiModifier(ext),
@ -99,7 +103,8 @@ impl<'a> Registry<'a> {
/// It builds for you a `NormalTT` that calls `expander`, /// It builds for you a `NormalTT` that calls `expander`,
/// and also takes care of interning the macro's name. /// and also takes care of interning the macro's name.
pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) { pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
self.register_syntax_extension(token::intern(name), NormalTT(Box::new(expander), None)); self.register_syntax_extension(token::intern(name),
NormalTT(Box::new(expander), None, false));
} }
/// Register a compiler lint pass. /// Register a compiler lint pass.

View File

@ -1743,6 +1743,7 @@ pub struct MacroDef {
pub imported_from: Option<Ident>, pub imported_from: Option<Ident>,
pub export: bool, pub export: bool,
pub use_locally: bool, pub use_locally: bool,
pub allow_internal_unstable: bool,
pub body: Vec<TokenTree>, pub body: Vec<TokenTree>,
} }

View File

@ -243,6 +243,10 @@ pub struct NameAndSpan {
pub name: String, pub name: String,
/// The format with which the macro was invoked. /// The format with which the macro was invoked.
pub format: MacroFormat, pub format: MacroFormat,
/// Whether the macro is allowed to use #[unstable]/feature-gated
/// features internally without forcing the whole crate to opt-in
/// to them.
pub allow_internal_unstable: bool,
/// The span of the macro definition itself. The macro may not /// The span of the macro definition itself. The macro may not
/// have a sensible definition span (e.g. something defined /// have a sensible definition span (e.g. something defined
/// completely inside libsyntax) in which case this is None. /// completely inside libsyntax) in which case this is None.
@ -830,41 +834,43 @@ impl CodeMap {
} }
} }
/// Check if a span is "internal" to a macro. This means that it is entirely generated by a /// Check if a span is "internal" to a macro in which #[unstable]
/// macro expansion and contains no code that was passed in as an argument. /// items can be used (that is, a macro marked with
pub fn span_is_internal(&self, span: Span) -> bool { /// `#[allow_internal_unstable]`).
// first, check if the given expression was generated by a macro or not pub fn span_allows_unstable(&self, span: Span) -> bool {
// we need to go back the expn_info tree to check only the arguments debug!("span_allows_unstable(span = {:?})", span);
// of the initial macro call, not the nested ones. let mut allows_unstable = false;
let mut is_internal = false; let mut expn_id = span.expn_id;
let mut expnid = span.expn_id; loop {
while self.with_expn_info(expnid, |expninfo| { let quit = self.with_expn_info(expn_id, |expninfo| {
match expninfo { debug!("span_allows_unstable: expninfo = {:?}", expninfo);
Some(ref info) => { expninfo.map_or(/* hit the top level */ true, |info| {
// save the parent expn_id for next loop iteration
expnid = info.call_site.expn_id; let span_comes_from_this_expansion =
if info.callee.name == "format_args" { info.callee.span.map_or(span == info.call_site, |mac_span| {
// This is a hack because the format_args builtin calls unstable APIs. mac_span.lo <= span.lo && span.hi < mac_span.hi
// I spent like 6 hours trying to solve this more generally but am stupid. });
is_internal = true;
false debug!("span_allows_unstable: from this expansion? {}, allows unstable? {}",
} else if info.callee.span.is_none() { span_comes_from_this_expansion,
// it's a compiler built-in, we *really* don't want to mess with it info.callee.allow_internal_unstable);
// so we skip it, unless it was called by a regular macro, in which case if span_comes_from_this_expansion {
// we will handle the caller macro next turn allows_unstable = info.callee.allow_internal_unstable;
is_internal = true; // we've found the right place, stop looking
true // continue looping true
} else { } else {
// was this expression from the current macro arguments ? // not the right place, keep looking
is_internal = !( span.lo > info.call_site.lo && expn_id = info.call_site.expn_id;
span.hi < info.call_site.hi ); false
true // continue looping
} }
}, })
_ => false // stop looping });
if quit {
break
} }
}) { /* empty while loop body */ } }
return is_internal; debug!("span_allows_unstable? {}", allows_unstable);
allows_unstable
} }
} }

View File

@ -214,6 +214,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
name: "asm".to_string(), name: "asm".to_string(),
format: codemap::MacroBang, format: codemap::MacroBang,
span: None, span: None,
allow_internal_unstable: false,
}, },
}); });

View File

@ -430,12 +430,15 @@ pub enum SyntaxExtension {
/// A normal, function-like syntax extension. /// A normal, function-like syntax extension.
/// ///
/// `bytes!` is a `NormalTT`. /// `bytes!` is a `NormalTT`.
NormalTT(Box<TTMacroExpander + 'static>, Option<Span>), ///
/// The `bool` dictates whether the contents of the macro can
/// directly use `#[unstable]` things (true == yes).
NormalTT(Box<TTMacroExpander + 'static>, Option<Span>, bool),
/// A function-like syntax extension that has an extra ident before /// A function-like syntax extension that has an extra ident before
/// the block. /// the block.
/// ///
IdentTT(Box<IdentMacroExpander + 'static>, Option<Span>), IdentTT(Box<IdentMacroExpander + 'static>, Option<Span>, bool),
/// Represents `macro_rules!` itself. /// Represents `macro_rules!` itself.
MacroRulesTT, MacroRulesTT,
@ -465,14 +468,14 @@ fn initial_syntax_expander_table<'feat>(ecfg: &expand::ExpansionConfig<'feat>)
-> SyntaxEnv { -> SyntaxEnv {
// utility function to simplify creating NormalTT syntax extensions // utility function to simplify creating NormalTT syntax extensions
fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension { fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
NormalTT(Box::new(f), None) NormalTT(Box::new(f), None, false)
} }
let mut syntax_expanders = SyntaxEnv::new(); let mut syntax_expanders = SyntaxEnv::new();
syntax_expanders.insert(intern("macro_rules"), MacroRulesTT); syntax_expanders.insert(intern("macro_rules"), MacroRulesTT);
syntax_expanders.insert(intern("format_args"), syntax_expanders.insert(intern("format_args"),
builtin_normal_expander( // format_args uses `unstable` things internally.
ext::format::expand_format_args)); NormalTT(Box::new(ext::format::expand_format_args), None, true));
syntax_expanders.insert(intern("env"), syntax_expanders.insert(intern("env"),
builtin_normal_expander( builtin_normal_expander(
ext::env::expand_env)); ext::env::expand_env));

View File

@ -1216,7 +1216,8 @@ impl<'a> TraitDef<'a> {
callee: codemap::NameAndSpan { callee: codemap::NameAndSpan {
name: format!("derive({})", trait_name), name: format!("derive({})", trait_name),
format: codemap::MacroAttribute, format: codemap::MacroAttribute,
span: Some(self.span) span: Some(self.span),
allow_internal_unstable: false,
} }
}); });
to_set to_set

View File

@ -22,7 +22,7 @@ use attr::AttrMetaMethods;
use codemap; use codemap;
use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute}; use codemap::{Span, Spanned, ExpnInfo, NameAndSpan, MacroBang, MacroAttribute};
use ext::base::*; use ext::base::*;
use feature_gate::{Features}; use feature_gate::{self, Features};
use fold; use fold;
use fold::*; use fold::*;
use parse; use parse;
@ -395,13 +395,14 @@ fn expand_mac_invoc<T, F, G>(mac: ast::Mac, span: codemap::Span,
None None
} }
Some(rc) => match *rc { Some(rc) => match *rc {
NormalTT(ref expandfun, exp_span) => { NormalTT(ref expandfun, exp_span, allow_internal_unstable) => {
fld.cx.bt_push(ExpnInfo { fld.cx.bt_push(ExpnInfo {
call_site: span, call_site: span,
callee: NameAndSpan { callee: NameAndSpan {
name: extnamestr.to_string(), name: extnamestr.to_string(),
format: MacroBang, format: MacroBang,
span: exp_span, span: exp_span,
allow_internal_unstable: allow_internal_unstable,
}, },
}); });
let fm = fresh_mark(); let fm = fresh_mark();
@ -530,6 +531,9 @@ fn expand_item_modifiers(mut it: P<ast::Item>, fld: &mut MacroExpander)
name: mname.to_string(), name: mname.to_string(),
format: MacroAttribute, format: MacroAttribute,
span: None, span: None,
// attributes can do whatever they like,
// for now
allow_internal_unstable: true,
} }
}); });
it = mac.expand(fld.cx, attr.span, &*attr.node.value, it); it = mac.expand(fld.cx, attr.span, &*attr.node.value, it);
@ -614,7 +618,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
} }
Some(rc) => match *rc { Some(rc) => match *rc {
NormalTT(ref expander, span) => { NormalTT(ref expander, span, allow_internal_unstable) => {
if it.ident.name != parse::token::special_idents::invalid.name { if it.ident.name != parse::token::special_idents::invalid.name {
fld.cx fld.cx
.span_err(path_span, .span_err(path_span,
@ -628,14 +632,15 @@ pub fn expand_item_mac(it: P<ast::Item>,
callee: NameAndSpan { callee: NameAndSpan {
name: extnamestr.to_string(), name: extnamestr.to_string(),
format: MacroBang, format: MacroBang,
span: span span: span,
allow_internal_unstable: allow_internal_unstable,
} }
}); });
// mark before expansion: // mark before expansion:
let marked_before = mark_tts(&tts[..], fm); let marked_before = mark_tts(&tts[..], fm);
expander.expand(fld.cx, it.span, &marked_before[..]) expander.expand(fld.cx, it.span, &marked_before[..])
} }
IdentTT(ref expander, span) => { IdentTT(ref expander, span, allow_internal_unstable) => {
if it.ident.name == parse::token::special_idents::invalid.name { if it.ident.name == parse::token::special_idents::invalid.name {
fld.cx.span_err(path_span, fld.cx.span_err(path_span,
&format!("macro {}! expects an ident argument", &format!("macro {}! expects an ident argument",
@ -647,7 +652,8 @@ pub fn expand_item_mac(it: P<ast::Item>,
callee: NameAndSpan { callee: NameAndSpan {
name: extnamestr.to_string(), name: extnamestr.to_string(),
format: MacroBang, format: MacroBang,
span: span span: span,
allow_internal_unstable: allow_internal_unstable,
} }
}); });
// mark before expansion: // mark before expansion:
@ -661,16 +667,35 @@ pub fn expand_item_mac(it: P<ast::Item>,
); );
return SmallVector::zero(); return SmallVector::zero();
} }
fld.cx.bt_push(ExpnInfo { fld.cx.bt_push(ExpnInfo {
call_site: it.span, call_site: it.span,
callee: NameAndSpan { callee: NameAndSpan {
name: extnamestr.to_string(), name: extnamestr.to_string(),
format: MacroBang, format: MacroBang,
span: None, span: None,
// `macro_rules!` doesn't directly allow
// unstable (this is orthogonal to whether
// the macro it creates allows it)
allow_internal_unstable: false,
} }
}); });
// DON'T mark before expansion. // DON'T mark before expansion.
let allow_internal_unstable = attr::contains_name(&it.attrs,
"allow_internal_unstable");
// ensure any #[allow_internal_unstable]s are
// detected (including nested macro definitions
// etc.)
if allow_internal_unstable && !fld.cx.ecfg.enable_allow_internal_unstable() {
feature_gate::emit_feature_err(
&fld.cx.parse_sess.span_diagnostic,
"allow_internal_unstable",
it.span,
feature_gate::EXPLAIN_ALLOW_INTERNAL_UNSTABLE)
}
let def = ast::MacroDef { let def = ast::MacroDef {
ident: it.ident, ident: it.ident,
attrs: it.attrs.clone(), attrs: it.attrs.clone(),
@ -679,6 +704,7 @@ pub fn expand_item_mac(it: P<ast::Item>,
imported_from: None, imported_from: None,
export: attr::contains_name(&it.attrs, "macro_export"), export: attr::contains_name(&it.attrs, "macro_export"),
use_locally: true, use_locally: true,
allow_internal_unstable: allow_internal_unstable,
body: tts, body: tts,
}; };
fld.cx.insert_macro(def); fld.cx.insert_macro(def);
@ -959,13 +985,14 @@ fn expand_pat(p: P<ast::Pat>, fld: &mut MacroExpander) -> P<ast::Pat> {
} }
Some(rc) => match *rc { Some(rc) => match *rc {
NormalTT(ref expander, tt_span) => { NormalTT(ref expander, tt_span, allow_internal_unstable) => {
fld.cx.bt_push(ExpnInfo { fld.cx.bt_push(ExpnInfo {
call_site: span, call_site: span,
callee: NameAndSpan { callee: NameAndSpan {
name: extnamestr.to_string(), name: extnamestr.to_string(),
format: MacroBang, format: MacroBang,
span: tt_span span: tt_span,
allow_internal_unstable: allow_internal_unstable,
} }
}); });
@ -1094,7 +1121,10 @@ fn expand_annotatable(a: Annotatable,
callee: NameAndSpan { callee: NameAndSpan {
name: mname.to_string(), name: mname.to_string(),
format: MacroAttribute, format: MacroAttribute,
span: None span: None,
// attributes can do whatever they like,
// for now.
allow_internal_unstable: true,
} }
}); });
@ -1244,6 +1274,9 @@ fn expand_item_multi_modifier(mut it: Annotatable,
name: mname.to_string(), name: mname.to_string(),
format: MacroAttribute, format: MacroAttribute,
span: None, span: None,
// attributes can do whatever they like,
// for now
allow_internal_unstable: true,
} }
}); });
it = mac.expand(fld.cx, attr.span, &*attr.node.value, it); it = mac.expand(fld.cx, attr.span, &*attr.node.value, it);
@ -1457,6 +1490,13 @@ impl<'feat> ExpansionConfig<'feat> {
_ => false, _ => false,
} }
} }
pub fn enable_allow_internal_unstable(&self) -> bool {
match self.features {
Some(&Features { allow_internal_unstable: true, .. }) => true,
_ => false
}
}
} }
pub fn expand_crate<'feat>(parse_sess: &parse::ParseSess, pub fn expand_crate<'feat>(parse_sess: &parse::ParseSess,

View File

@ -267,7 +267,7 @@ pub fn compile<'cx>(cx: &'cx mut ExtCtxt,
rhses: rhses, rhses: rhses,
}; };
NormalTT(exp, Some(def.span)) NormalTT(exp, Some(def.span), def.allow_internal_unstable)
} }
fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &NamedMatch, sp: Span) { fn check_lhs_nt_follows(cx: &mut ExtCtxt, lhs: &NamedMatch, sp: Span) {

View File

@ -142,6 +142,12 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Status)] = &[
// Allows the use of `static_assert` // Allows the use of `static_assert`
("static_assert", "1.0.0", Active), ("static_assert", "1.0.0", Active),
// Allows the use of #[allow_internal_unstable]. This is an
// attribute on macro_rules! and can't use the attribute handling
// below (it has to be checked before expansion possibly makes
// macros disappear).
("allow_internal_unstable", "1.0.0", Active),
]; ];
// (changing above list without updating src/doc/reference.md makes @cmr sad) // (changing above list without updating src/doc/reference.md makes @cmr sad)
@ -308,6 +314,7 @@ pub struct Features {
pub allow_log_syntax: bool, pub allow_log_syntax: bool,
pub allow_concat_idents: bool, pub allow_concat_idents: bool,
pub allow_trace_macros: bool, pub allow_trace_macros: bool,
pub allow_internal_unstable: bool,
pub old_orphan_check: bool, pub old_orphan_check: bool,
pub simd_ffi: bool, pub simd_ffi: bool,
pub unmarked_api: bool, pub unmarked_api: bool,
@ -328,6 +335,7 @@ impl Features {
allow_log_syntax: false, allow_log_syntax: false,
allow_concat_idents: false, allow_concat_idents: false,
allow_trace_macros: false, allow_trace_macros: false,
allow_internal_unstable: false,
old_orphan_check: false, old_orphan_check: false,
simd_ffi: false, simd_ffi: false,
unmarked_api: false, unmarked_api: false,
@ -387,6 +395,8 @@ pub const EXPLAIN_CONCAT_IDENTS: &'static str =
pub const EXPLAIN_TRACE_MACROS: &'static str = pub const EXPLAIN_TRACE_MACROS: &'static str =
"`trace_macros` is not stable enough for use and is subject to change"; "`trace_macros` is not stable enough for use and is subject to change";
pub const EXPLAIN_ALLOW_INTERNAL_UNSTABLE: &'static str =
"allow_internal_unstable side-steps feature gating and stability checks";
struct MacroVisitor<'a> { struct MacroVisitor<'a> {
context: &'a Context<'a> context: &'a Context<'a>
@ -421,6 +431,13 @@ impl<'a, 'v> Visitor<'v> for MacroVisitor<'a> {
self.context.gate_feature("concat_idents", path.span, EXPLAIN_CONCAT_IDENTS); self.context.gate_feature("concat_idents", path.span, EXPLAIN_CONCAT_IDENTS);
} }
} }
fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
if attr.name() == "allow_internal_unstable" {
self.context.gate_feature("allow_internal_unstable", attr.span,
EXPLAIN_ALLOW_INTERNAL_UNSTABLE)
}
}
} }
struct PostExpansionVisitor<'a> { struct PostExpansionVisitor<'a> {
@ -429,7 +446,7 @@ struct PostExpansionVisitor<'a> {
impl<'a> PostExpansionVisitor<'a> { impl<'a> PostExpansionVisitor<'a> {
fn gate_feature(&self, feature: &str, span: Span, explain: &str) { fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
if !self.context.cm.span_is_internal(span) { if !self.context.cm.span_allows_unstable(span) {
self.context.gate_feature(feature, span, explain) self.context.gate_feature(feature, span, explain)
} }
} }
@ -754,6 +771,7 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::C
allow_log_syntax: cx.has_feature("log_syntax"), allow_log_syntax: cx.has_feature("log_syntax"),
allow_concat_idents: cx.has_feature("concat_idents"), allow_concat_idents: cx.has_feature("concat_idents"),
allow_trace_macros: cx.has_feature("trace_macros"), allow_trace_macros: cx.has_feature("trace_macros"),
allow_internal_unstable: cx.has_feature("allow_internal_unstable"),
old_orphan_check: cx.has_feature("old_orphan_check"), old_orphan_check: cx.has_feature("old_orphan_check"),
simd_ffi: cx.has_feature("simd_ffi"), simd_ffi: cx.has_feature("simd_ffi"),
unmarked_api: cx.has_feature("unmarked_api"), unmarked_api: cx.has_feature("unmarked_api"),

View File

@ -256,7 +256,8 @@ fn generate_test_harness(sess: &ParseSess,
callee: NameAndSpan { callee: NameAndSpan {
name: "test".to_string(), name: "test".to_string(),
format: MacroAttribute, format: MacroAttribute,
span: None span: None,
allow_internal_unstable: false,
} }
}); });
@ -288,7 +289,8 @@ fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
callee: NameAndSpan { callee: NameAndSpan {
name: "test".to_string(), name: "test".to_string(),
format: MacroAttribute, format: MacroAttribute,
span: None span: None,
allow_internal_unstable: true,
} }
}; };
let expn_id = cx.sess.span_diagnostic.cm.record_expansion(info); let expn_id = cx.sess.span_diagnostic.cm.record_expansion(info);

View File

@ -0,0 +1,60 @@
// Copyright 2015 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(staged_api, allow_internal_unstable)]
#![staged_api]
#![stable(feature = "stable", since = "1.0.0")]
#[unstable(feature = "function")]
pub fn unstable() {}
#[stable(feature = "stable", since = "1.0.0")]
pub struct Foo {
#[unstable(feature = "struct_field")]
pub x: u8
}
#[allow_internal_unstable]
#[macro_export]
macro_rules! call_unstable_allow {
() => { $crate::unstable() }
}
#[allow_internal_unstable]
#[macro_export]
macro_rules! construct_unstable_allow {
($e: expr) => {
$crate::Foo { x: $e }
}
}
#[allow_internal_unstable]
#[macro_export]
macro_rules! pass_through_allow {
($e: expr) => { $e }
}
#[macro_export]
macro_rules! call_unstable_noallow {
() => { $crate::unstable() }
}
#[macro_export]
macro_rules! construct_unstable_noallow {
($e: expr) => {
$crate::Foo { x: $e }
}
}
#[macro_export]
macro_rules! pass_through_noallow {
($e: expr) => { $e }
}

View File

@ -47,5 +47,5 @@ pub fn plugin_registrar(reg: &mut Registry) {
let args = reg.args().clone(); let args = reg.args().clone();
reg.register_syntax_extension(token::intern("plugin_args"), reg.register_syntax_extension(token::intern("plugin_args"),
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible. // FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
NormalTT(Box::new(Expander { args: args, }), None)); NormalTT(Box::new(Expander { args: args, }), None, false));
} }

View File

@ -0,0 +1,23 @@
// Copyright 2015 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.
macro_rules! bar {
() => {
// more layers don't help:
#[allow_internal_unstable]
macro_rules! baz { //~ ERROR allow_internal_unstable side-steps
() => {}
}
}
}
bar!();
fn main() {}

View File

@ -0,0 +1,16 @@
// Copyright 2015 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.
#[allow_internal_unstable] //~ ERROR allow_internal_unstable side-steps
macro_rules! foo {
() => {}
}
fn main() {}

View File

@ -0,0 +1,30 @@
// Copyright 2015 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.
// this has to be separate to internal-unstable.rs because these tests
// have error messages pointing deep into the internals of the
// cross-crate macros, and hence need to use error-pattern instead of
// the // ~ form.
// aux-build:internal_unstable.rs
// error-pattern:use of unstable library feature 'function'
// error-pattern:use of unstable library feature 'struct_field'
// error-pattern:compilation successful
#![feature(rustc_attrs)]
#[macro_use]
extern crate internal_unstable;
#[rustc_error]
fn main() {
call_unstable_noallow!();
construct_unstable_noallow!(0);
}

View File

@ -0,0 +1,51 @@
// Copyright 2015 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.
// aux-build:internal_unstable.rs
#![feature(rustc_attrs, allow_internal_unstable)]
#[macro_use]
extern crate internal_unstable;
macro_rules! foo {
($e: expr, $f: expr) => {{
$e;
$f;
internal_unstable::unstable(); //~ WARN use of unstable
}}
}
#[allow_internal_unstable]
macro_rules! bar {
($e: expr) => {{
foo!($e,
internal_unstable::unstable());
internal_unstable::unstable();
}}
}
#[rustc_error]
fn main() { //~ ERROR
// ok, the instability is contained.
call_unstable_allow!();
construct_unstable_allow!(0);
// bad.
pass_through_allow!(internal_unstable::unstable()); //~ WARN use of unstable
pass_through_noallow!(internal_unstable::unstable()); //~ WARN use of unstable
println!("{:?}", internal_unstable::unstable()); //~ WARN use of unstable
bar!(internal_unstable::unstable()); //~ WARN use of unstable
}