Skip to content

Commit 4ba1acb

Browse files
committed
Auto merge of #58098 - oli-obk:maybe_allow_internal_unstable, r=<try>
Require a list of features in `#[allow_internal_unstable]` The blanket-permission slip is not great and will likely give us trouble some point down the road.
2 parents 8ae730a + deb2d20 commit 4ba1acb

36 files changed

+198
-96
lines changed

src/liballoc/macros.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
#[cfg(not(test))]
3535
#[macro_export]
3636
#[stable(feature = "rust1", since = "1.0.0")]
37-
#[allow_internal_unstable]
37+
#[cfg_attr(not(stage0), allow_internal_unstable(box_syntax))]
38+
#[cfg_attr(stage0, allow_internal_unstable)]
3839
macro_rules! vec {
3940
($elem:expr; $n:expr) => (
4041
$crate::vec::from_elem($elem, $n)

src/libcore/macros.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// Entry point of thread panic, for details, see std::macros
22
#[macro_export]
3-
#[allow_internal_unstable]
3+
#[cfg_attr(not(stage0), allow_internal_unstable(core_panic, __rust_unstable_column))]
4+
#[cfg_attr(stage0, allow_internal_unstable)]
45
#[stable(feature = "core", since = "1.6.0")]
56
macro_rules! panic {
67
() => (
@@ -409,7 +410,8 @@ macro_rules! write {
409410
/// ```
410411
#[macro_export]
411412
#[stable(feature = "rust1", since = "1.0.0")]
412-
#[allow_internal_unstable]
413+
#[cfg_attr(stage0, allow_internal_unstable)]
414+
#[cfg_attr(not(stage0), allow_internal_unstable(format_args_nl))]
413415
macro_rules! writeln {
414416
($dst:expr) => (
415417
write!($dst, "\n")

src/librustc/hir/lowering.rs

+34-9
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,20 @@ impl<'a> LoweringContext<'a> {
681681
Ident::with_empty_ctxt(Symbol::gensym(s))
682682
}
683683

684-
fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, span: Span) -> Span {
684+
/// Reuses the span but adds information like the kind of the desugaring and features that are
685+
/// allowed inside this span.
686+
fn mark_span_with_reason(
687+
&self,
688+
reason: CompilerDesugaringKind,
689+
span: Span,
690+
allow_internal_unstable: Vec<Symbol>,
691+
) -> Span {
685692
let mark = Mark::fresh(Mark::root());
686693
mark.set_expn_info(source_map::ExpnInfo {
687694
call_site: span,
688695
def_site: Some(span),
689696
format: source_map::CompilerDesugaring(reason),
690-
allow_internal_unstable: true,
697+
allow_internal_unstable,
691698
allow_internal_unsafe: false,
692699
local_inner_macros: false,
693700
edition: source_map::hygiene::default_edition(),
@@ -964,7 +971,13 @@ impl<'a> LoweringContext<'a> {
964971
attrs: ThinVec::new(),
965972
};
966973

967-
let unstable_span = self.allow_internal_unstable(CompilerDesugaringKind::Async, span);
974+
let unstable_span = self.mark_span_with_reason(
975+
CompilerDesugaringKind::Async,
976+
span,
977+
vec![
978+
Symbol::intern("gen_future"),
979+
],
980+
);
968981
let gen_future = self.expr_std_path(
969982
unstable_span, &["future", "from_generator"], None, ThinVec::new());
970983
hir::ExprKind::Call(P(gen_future), hir_vec![generator])
@@ -1351,9 +1364,10 @@ impl<'a> LoweringContext<'a> {
13511364
// desugaring that explicitly states that we don't want to track that.
13521365
// Not tracking it makes lints in rustc and clippy very fragile as
13531366
// frequently opened issues show.
1354-
let exist_ty_span = self.allow_internal_unstable(
1367+
let exist_ty_span = self.mark_span_with_reason(
13551368
CompilerDesugaringKind::ExistentialReturnType,
13561369
span,
1370+
Vec::new(), // doesn'c actually allow anything unstable
13571371
);
13581372

13591373
let exist_ty_def_index = self
@@ -3912,8 +3926,13 @@ impl<'a> LoweringContext<'a> {
39123926
}),
39133927
ExprKind::TryBlock(ref body) => {
39143928
self.with_catch_scope(body.id, |this| {
3915-
let unstable_span =
3916-
this.allow_internal_unstable(CompilerDesugaringKind::TryBlock, body.span);
3929+
let unstable_span = this.mark_span_with_reason(
3930+
CompilerDesugaringKind::TryBlock,
3931+
body.span,
3932+
vec![
3933+
Symbol::intern("try_trait"),
3934+
],
3935+
);
39173936
let mut block = this.lower_block(body, true).into_inner();
39183937
let tail = block.expr.take().map_or_else(
39193938
|| {
@@ -4345,9 +4364,10 @@ impl<'a> LoweringContext<'a> {
43454364
// expand <head>
43464365
let head = self.lower_expr(head);
43474366
let head_sp = head.span;
4348-
let desugared_span = self.allow_internal_unstable(
4367+
let desugared_span = self.mark_span_with_reason(
43494368
CompilerDesugaringKind::ForLoop,
43504369
head_sp,
4370+
Vec::new(),
43514371
);
43524372

43534373
let iter = self.str_to_ident("iter");
@@ -4510,8 +4530,13 @@ impl<'a> LoweringContext<'a> {
45104530
// return Try::from_error(From::from(err)),
45114531
// }
45124532

4513-
let unstable_span =
4514-
self.allow_internal_unstable(CompilerDesugaringKind::QuestionMark, e.span);
4533+
let unstable_span = self.mark_span_with_reason(
4534+
CompilerDesugaringKind::QuestionMark,
4535+
e.span,
4536+
vec![
4537+
Symbol::intern("try_trait")
4538+
],
4539+
);
45154540

45164541
// `Try::into_result(<expr>)`
45174542
let discr = {

src/librustc/middle/stability.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -561,11 +561,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
561561
/// deprecated. If the item is indeed deprecated, we will emit a deprecation lint attached to
562562
/// `id`.
563563
pub fn eval_stability(self, def_id: DefId, id: Option<NodeId>, span: Span) -> EvalResult {
564-
if span.allows_unstable() {
565-
debug!("stability: skipping span={:?} since it is internal", span);
566-
return EvalResult::Allow;
567-
}
568-
569564
let lint_deprecated = |def_id: DefId,
570565
id: NodeId,
571566
note: Option<Symbol>,
@@ -694,6 +689,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
694689

695690
match stability {
696691
Some(&Stability { level: attr::Unstable { reason, issue }, feature, .. }) => {
692+
if span.allows_unstable(&feature.as_str()) {
693+
debug!("stability: skipping span={:?} since it is internal", span);
694+
return EvalResult::Allow;
695+
}
697696
if self.stability().active_features.contains(&feature) {
698697
return EvalResult::Allow;
699698
}

src/librustc_allocator/expand.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
9191
call_site: item.span, // use the call site of the static
9292
def_site: None,
9393
format: MacroAttribute(Symbol::intern(name)),
94-
allow_internal_unstable: true,
94+
allow_internal_unstable: vec![
95+
Symbol::intern("rustc_attrs"),
96+
],
9597
allow_internal_unsafe: false,
9698
local_inner_macros: false,
9799
edition: hygiene::default_edition(),

src/librustc_metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl<'a> CrateLoader<'a> {
569569
ProcMacro::Bang { name, client } => {
570570
(name, SyntaxExtension::ProcMacro {
571571
expander: Box::new(BangProcMacro { client }),
572-
allow_internal_unstable: false,
572+
allow_internal_unstable: Vec::new(),
573573
edition: root.edition,
574574
})
575575
}

src/librustc_metadata/cstore_impl.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ impl cstore::CStore {
424424
let client = ::proc_macro::bridge::client::Client::expand1(::proc_macro::quote);
425425
let ext = SyntaxExtension::ProcMacro {
426426
expander: Box::new(BangProcMacro { client }),
427-
allow_internal_unstable: true,
427+
allow_internal_unstable: vec![
428+
Symbol::intern("proc_macro_def_site"),
429+
],
428430
edition: data.root.edition,
429431
};
430432
return LoadedMacro::ProcMacro(Lrc::new(ext));

src/librustc_mir/transform/qualify_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
885885
// Check `#[unstable]` const fns or `#[rustc_const_unstable]`
886886
// functions without the feature gate active in this crate in
887887
// order to report a better error message than the one below.
888-
if self.span.allows_unstable() {
888+
if self.span.allows_unstable(&feature.as_str()) {
889889
// `allow_internal_unstable` can make such calls stable.
890890
is_const_fn = true;
891891
} else {

src/librustc_plugin/registry.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ impl<'a> Registry<'a> {
110110
edition,
111111
}
112112
}
113-
IdentTT(ext, _, allow_internal_unstable) => {
114-
IdentTT(ext, Some(self.krate_span), allow_internal_unstable)
113+
IdentTT { expander, span: _, allow_internal_unstable } => {
114+
IdentTT { expander, span: Some(self.krate_span), allow_internal_unstable }
115115
}
116116
_ => extension,
117117
}));
@@ -126,7 +126,7 @@ impl<'a> Registry<'a> {
126126
self.register_syntax_extension(Symbol::intern(name), NormalTT {
127127
expander: Box::new(expander),
128128
def_info: None,
129-
allow_internal_unstable: false,
129+
allow_internal_unstable: Vec::new(),
130130
allow_internal_unsafe: false,
131131
local_inner_macros: false,
132132
unstable_feature: None,

src/libstd/macros.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
/// ```
5454
#[macro_export]
5555
#[stable(feature = "rust1", since = "1.0.0")]
56-
#[allow_internal_unstable]
56+
#[cfg_attr(stage0, allow_internal_unstable)]
57+
#[cfg_attr(not(stage0), allow_internal_unstable(__rust_unstable_column, libstd_sys_internals))]
5758
macro_rules! panic {
5859
() => ({
5960
panic!("explicit panic")
@@ -111,7 +112,8 @@ macro_rules! panic {
111112
/// ```
112113
#[macro_export]
113114
#[stable(feature = "rust1", since = "1.0.0")]
114-
#[allow_internal_unstable]
115+
#[cfg_attr(stage0, allow_internal_unstable)]
116+
#[cfg_attr(not(stage0), allow_internal_unstable(print_internals))]
115117
macro_rules! print {
116118
($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*)));
117119
}
@@ -143,7 +145,8 @@ macro_rules! print {
143145
/// ```
144146
#[macro_export]
145147
#[stable(feature = "rust1", since = "1.0.0")]
146-
#[allow_internal_unstable]
148+
#[cfg_attr(stage0, allow_internal_unstable)]
149+
#[cfg_attr(not(stage0), allow_internal_unstable(print_internals, format_args_nl))]
147150
macro_rules! println {
148151
() => (print!("\n"));
149152
($($arg:tt)*) => ({
@@ -174,7 +177,8 @@ macro_rules! println {
174177
/// ```
175178
#[macro_export]
176179
#[stable(feature = "eprint", since = "1.19.0")]
177-
#[allow_internal_unstable]
180+
#[cfg_attr(stage0, allow_internal_unstable)]
181+
#[cfg_attr(not(stage0), allow_internal_unstable(print_internals))]
178182
macro_rules! eprint {
179183
($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*)));
180184
}
@@ -202,7 +206,8 @@ macro_rules! eprint {
202206
/// ```
203207
#[macro_export]
204208
#[stable(feature = "eprint", since = "1.19.0")]
205-
#[allow_internal_unstable]
209+
#[cfg_attr(stage0, allow_internal_unstable)]
210+
#[cfg_attr(not(stage0), allow_internal_unstable(print_internals, format_args_nl))]
206211
macro_rules! eprintln {
207212
() => (eprint!("\n"));
208213
($($arg:tt)*) => ({
@@ -325,7 +330,8 @@ macro_rules! dbg {
325330
/// A macro to await on an async call.
326331
#[macro_export]
327332
#[unstable(feature = "await_macro", issue = "50547")]
328-
#[allow_internal_unstable]
333+
#[cfg_attr(stage0, allow_internal_unstable)]
334+
#[cfg_attr(not(stage0), allow_internal_unstable(gen_future, generators))]
329335
#[allow_internal_unsafe]
330336
macro_rules! await {
331337
($e:expr) => { {

src/libstd/thread/local.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
126126
/// [`std::thread::LocalKey`]: ../std/thread/struct.LocalKey.html
127127
#[macro_export]
128128
#[stable(feature = "rust1", since = "1.0.0")]
129-
#[allow_internal_unstable]
129+
#[cfg_attr(stage0, allow_internal_unstable)]
130+
#[cfg_attr(not(stage0), allow_internal_unstable(thread_local_internals))]
130131
macro_rules! thread_local {
131132
// empty (base case for the recursion)
132133
() => {};
@@ -148,7 +149,10 @@ macro_rules! thread_local {
148149
reason = "should not be necessary",
149150
issue = "0")]
150151
#[macro_export]
151-
#[allow_internal_unstable]
152+
#[cfg_attr(stage0, allow_internal_unstable)]
153+
#[cfg_attr(not(stage0), allow_internal_unstable(
154+
thread_local_internals, cfg_target_thread_local, thread_local,
155+
))]
152156
#[allow_internal_unsafe]
153157
macro_rules! __thread_local_inner {
154158
(@key $(#[$attr:meta])* $vis:vis $name:ident, $t:ty, $init:expr) => {

src/libsyntax/ext/base.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,8 @@ pub enum SyntaxExtension {
621621
/// A function-like procedural macro. TokenStream -> TokenStream.
622622
ProcMacro {
623623
expander: Box<dyn ProcMacro + sync::Sync + sync::Send>,
624-
allow_internal_unstable: bool,
624+
/// Whitelist of unstable features that are treated as stable inside this macro
625+
allow_internal_unstable: Vec<Symbol>,
625626
edition: Edition,
626627
},
627628

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

655658
/// A function-like syntax extension that has an extra ident before
656659
/// the block.
657-
///
658-
IdentTT(Box<dyn IdentMacroExpander + sync::Sync + sync::Send>, Option<Span>, bool),
660+
IdentTT {
661+
expander: Box<dyn IdentMacroExpander + sync::Sync + sync::Send>,
662+
span: Option<Span>,
663+
allow_internal_unstable: Vec<Symbol>,
664+
},
659665

660666
/// An attribute-like procedural macro. TokenStream -> TokenStream.
661667
/// The input is the annotated item.
@@ -682,7 +688,7 @@ impl SyntaxExtension {
682688
match *self {
683689
SyntaxExtension::DeclMacro { .. } |
684690
SyntaxExtension::NormalTT { .. } |
685-
SyntaxExtension::IdentTT(..) |
691+
SyntaxExtension::IdentTT { .. } |
686692
SyntaxExtension::ProcMacro { .. } =>
687693
MacroKind::Bang,
688694
SyntaxExtension::NonMacroAttr { .. } |
@@ -716,7 +722,7 @@ impl SyntaxExtension {
716722
SyntaxExtension::ProcMacroDerive(.., edition) => edition,
717723
// Unstable legacy stuff
718724
SyntaxExtension::NonMacroAttr { .. } |
719-
SyntaxExtension::IdentTT(..) |
725+
SyntaxExtension::IdentTT { .. } |
720726
SyntaxExtension::MultiDecorator(..) |
721727
SyntaxExtension::MultiModifier(..) |
722728
SyntaxExtension::BuiltinDerive(..) => hygiene::default_edition(),

src/libsyntax/ext/derive.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ pub fn add_derived_markers<T>(cx: &mut ExtCtxt, span: Span, traits: &[ast::Path]
5757
call_site: span,
5858
def_site: None,
5959
format: ExpnFormat::MacroAttribute(Symbol::intern(&pretty_name)),
60-
allow_internal_unstable: true,
60+
allow_internal_unstable: vec![
61+
Symbol::intern("rustc_attrs"),
62+
Symbol::intern("structural_match"),
63+
],
6164
allow_internal_unsafe: false,
6265
local_inner_macros: false,
6366
edition: hygiene::default_edition(),

0 commit comments

Comments
 (0)