Skip to content

Commit 1ee0ce8

Browse files
committed
syntax: Migrate built-in macros to the regular stability checking
1 parent 0817fc6 commit 1ee0ce8

32 files changed

+161
-209
lines changed

src/librustc_resolve/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,9 @@ pub struct Resolver<'a> {
16841684
current_type_ascription: Vec<Span>,
16851685

16861686
injected_crate: Option<Module<'a>>,
1687+
1688+
/// Features enabled for this crate.
1689+
active_features: FxHashSet<Symbol>,
16871690
}
16881691

16891692
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1922,6 +1925,7 @@ impl<'a> Resolver<'a> {
19221925
let mut macro_defs = FxHashMap::default();
19231926
macro_defs.insert(Mark::root(), root_def_id);
19241927

1928+
let features = session.features_untracked();
19251929
let non_macro_attr = |mark_used| Lrc::new(SyntaxExtension::default(
19261930
SyntaxExtensionKind::NonMacroAttr { mark_used }, session.edition()
19271931
));
@@ -2009,6 +2013,10 @@ impl<'a> Resolver<'a> {
20092013
unused_macros: Default::default(),
20102014
current_type_ascription: Vec::new(),
20112015
injected_crate: None,
2016+
active_features:
2017+
features.declared_lib_features.iter().map(|(feat, ..)| *feat)
2018+
.chain(features.declared_lang_features.iter().map(|(feat, ..)| *feat))
2019+
.collect(),
20122020
}
20132021
}
20142022

src/librustc_resolve/macros.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1013,9 +1013,8 @@ impl<'a> Resolver<'a> {
10131013
fn check_stability_and_deprecation(&self, ext: &SyntaxExtension, path: &str, span: Span) {
10141014
if let Some(stability) = &ext.stability {
10151015
if let StabilityLevel::Unstable { reason, issue } = stability.level {
1016-
let (feature, features) = (stability.feature, self.session.features_untracked());
1017-
if !span.allows_unstable(feature) &&
1018-
features.declared_lib_features.iter().all(|(feat, _)| *feat != feature) {
1016+
let feature = stability.feature;
1017+
if !self.active_features.contains(&feature) && !span.allows_unstable(feature) {
10191018
stability::report_unstable(self.session, feature, reason, issue, span);
10201019
}
10211020
}

src/libserialize/json.rs

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
//!
104104
//! ```rust
105105
//! # #![feature(rustc_private)]
106+
//! # #![allow(deprecated)]
106107
//! extern crate serialize;
107108
//! use serialize::json::{self, ToJson, Json};
108109
//!
@@ -143,6 +144,7 @@
143144
//!
144145
//! ```rust
145146
//! # #![feature(rustc_private)]
147+
//! # #![allow(deprecated)]
146148
//! extern crate serialize;
147149
//! use std::collections::BTreeMap;
148150
//! use serialize::json::{self, Json, ToJson};

src/libsyntax/attr/builtin.rs

+13
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ pub enum StabilityLevel {
135135
Stable { since: Symbol },
136136
}
137137

138+
impl Stability {
139+
pub fn unstable(feature: Symbol, reason: Option<Symbol>, issue: u32) -> Stability {
140+
Stability {
141+
level: StabilityLevel::Unstable { reason, issue },
142+
feature,
143+
rustc_depr: None,
144+
const_stability: None,
145+
promotable: false,
146+
allow_const_fn_ptr: false,
147+
}
148+
}
149+
}
150+
138151
impl StabilityLevel {
139152
pub fn is_unstable(&self) -> bool {
140153
if let StabilityLevel::Unstable {..} = *self {

src/libsyntax/ext/expand.rs

+8-28
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
487487

488488
fn expand_invoc(&mut self, invoc: Invocation, ext: &SyntaxExtension) -> Option<AstFragment> {
489489
if invoc.fragment_kind == AstFragmentKind::ForeignItems &&
490-
!self.cx.ecfg.macros_in_extern_enabled() {
490+
!self.cx.ecfg.macros_in_extern() {
491491
if let SyntaxExtensionKind::NonMacroAttr { .. } = ext.kind {} else {
492492
emit_feature_err(&self.cx.parse_sess, sym::macros_in_extern,
493493
invoc.span(), GateIssue::Language,
@@ -919,7 +919,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
919919
})
920920
.map(|i| attrs.remove(i));
921921
if let Some(attr) = &attr {
922-
if !self.cx.ecfg.enable_custom_inner_attributes() &&
922+
if !self.cx.ecfg.custom_inner_attributes() &&
923923
attr.style == ast::AttrStyle::Inner && attr.path != sym::test {
924924
emit_feature_err(&self.cx.parse_sess, sym::custom_inner_attributes,
925925
attr.span, GateIssue::Language,
@@ -1432,19 +1432,6 @@ pub struct ExpansionConfig<'feat> {
14321432
pub keep_macs: bool,
14331433
}
14341434

1435-
macro_rules! feature_tests {
1436-
($( fn $getter:ident = $field:ident, )*) => {
1437-
$(
1438-
pub fn $getter(&self) -> bool {
1439-
match self.features {
1440-
Some(&Features { $field: true, .. }) => true,
1441-
_ => false,
1442-
}
1443-
}
1444-
)*
1445-
}
1446-
}
1447-
14481435
impl<'feat> ExpansionConfig<'feat> {
14491436
pub fn default(crate_name: String) -> ExpansionConfig<'static> {
14501437
ExpansionConfig {
@@ -1458,20 +1445,13 @@ impl<'feat> ExpansionConfig<'feat> {
14581445
}
14591446
}
14601447

1461-
feature_tests! {
1462-
fn enable_asm = asm,
1463-
fn enable_custom_test_frameworks = custom_test_frameworks,
1464-
fn enable_global_asm = global_asm,
1465-
fn enable_log_syntax = log_syntax,
1466-
fn enable_concat_idents = concat_idents,
1467-
fn enable_trace_macros = trace_macros,
1468-
fn enable_allow_internal_unstable = allow_internal_unstable,
1469-
fn enable_format_args_nl = format_args_nl,
1470-
fn macros_in_extern_enabled = macros_in_extern,
1471-
fn proc_macro_hygiene = proc_macro_hygiene,
1448+
fn macros_in_extern(&self) -> bool {
1449+
self.features.map_or(false, |features| features.macros_in_extern)
14721450
}
1473-
1474-
fn enable_custom_inner_attributes(&self) -> bool {
1451+
fn proc_macro_hygiene(&self) -> bool {
1452+
self.features.map_or(false, |features| features.proc_macro_hygiene)
1453+
}
1454+
fn custom_inner_attributes(&self) -> bool {
14751455
self.features.map_or(false, |features| features.custom_inner_attributes)
14761456
}
14771457
}

src/libsyntax/ext/source_util.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::ext::build::AstBuilder;
44
use crate::parse::{self, token, DirectoryOwnership};
55
use crate::print::pprust;
66
use crate::ptr::P;
7-
use crate::symbol::{Symbol, sym};
7+
use crate::symbol::Symbol;
88
use crate::tokenstream;
99

1010
use smallvec::SmallVec;
@@ -41,16 +41,6 @@ pub fn expand_column(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTr
4141
base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32 + 1))
4242
}
4343

44-
/* __rust_unstable_column!(): expands to the current column number */
45-
pub fn expand_column_gated(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree])
46-
-> Box<dyn base::MacResult+'static> {
47-
if sp.allows_unstable(sym::__rust_unstable_column) {
48-
expand_column(cx, sp, tts)
49-
} else {
50-
cx.span_fatal(sp, "the __rust_unstable_column macro is unstable");
51-
}
52-
}
53-
5444
/// file!(): expands to the current filename */
5545
/// The source_file (`loc.file`) contains a bunch more information we could spit
5646
/// out if we wanted.

src/libsyntax/feature_gate.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
15681568
(sym::type_length_limit, CrateLevel, template!(NameValueStr: "N"), Ungated),
15691569
(sym::test_runner, CrateLevel, template!(List: "path"), Gated(Stability::Unstable,
15701570
sym::custom_test_frameworks,
1571-
EXPLAIN_CUSTOM_TEST_FRAMEWORKS,
1571+
"custom test frameworks are an unstable feature",
15721572
cfg_fn!(custom_test_frameworks))),
15731573
];
15741574

@@ -1819,26 +1819,6 @@ const EXPLAIN_BOX_SYNTAX: &str =
18191819
pub const EXPLAIN_STMT_ATTR_SYNTAX: &str =
18201820
"attributes on expressions are experimental";
18211821

1822-
pub const EXPLAIN_ASM: &str =
1823-
"inline assembly is not stable enough for use and is subject to change";
1824-
1825-
pub const EXPLAIN_GLOBAL_ASM: &str =
1826-
"`global_asm!` is not stable enough for use and is subject to change";
1827-
1828-
pub const EXPLAIN_CUSTOM_TEST_FRAMEWORKS: &str =
1829-
"custom test frameworks are an unstable feature";
1830-
1831-
pub const EXPLAIN_LOG_SYNTAX: &str =
1832-
"`log_syntax!` is not stable enough for use and is subject to change";
1833-
1834-
pub const EXPLAIN_CONCAT_IDENTS: &str =
1835-
"`concat_idents` is not stable enough for use and is subject to change";
1836-
1837-
pub const EXPLAIN_FORMAT_ARGS_NL: &str =
1838-
"`format_args_nl` is only for internal language use and is subject to change";
1839-
1840-
pub const EXPLAIN_TRACE_MACROS: &str =
1841-
"`trace_macros` is not stable enough for use and is subject to change";
18421822
pub const EXPLAIN_ALLOW_INTERNAL_UNSTABLE: &str =
18431823
"allow_internal_unstable side-steps feature gating and stability checks";
18441824
pub const EXPLAIN_ALLOW_INTERNAL_UNSAFE: &str =

src/libsyntax_ext/asm.rs

-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use errors::DiagnosticBuilder;
88

99
use syntax::ast;
1010
use syntax::ext::base::{self, *};
11-
use syntax::feature_gate;
1211
use syntax::parse;
1312
use syntax::parse::token::{self, Token};
1413
use syntax::ptr::P;
@@ -46,14 +45,6 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
4645
sp: Span,
4746
tts: &[tokenstream::TokenTree])
4847
-> Box<dyn base::MacResult + 'cx> {
49-
if !cx.ecfg.enable_asm() {
50-
feature_gate::emit_feature_err(&cx.parse_sess,
51-
sym::asm,
52-
sp,
53-
feature_gate::GateIssue::Language,
54-
feature_gate::EXPLAIN_ASM);
55-
}
56-
5748
let mut inline_asm = match parse_inline_asm(cx, sp, tts) {
5849
Ok(Some(inline_asm)) => inline_asm,
5950
Ok(None) => return DummyResult::expr(sp),

src/libsyntax_ext/concat_idents.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,16 @@ use rustc_data_structures::thin_vec::ThinVec;
22

33
use syntax::ast;
44
use syntax::ext::base::{self, *};
5-
use syntax::feature_gate;
65
use syntax::parse::token::{self, Token};
76
use syntax::ptr::P;
87
use syntax_pos::Span;
9-
use syntax_pos::symbol::{Symbol, sym};
8+
use syntax_pos::symbol::Symbol;
109
use syntax::tokenstream::TokenTree;
1110

1211
pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt<'_>,
1312
sp: Span,
1413
tts: &[TokenTree])
1514
-> Box<dyn base::MacResult + 'cx> {
16-
if !cx.ecfg.enable_concat_idents() {
17-
feature_gate::emit_feature_err(&cx.parse_sess,
18-
sym::concat_idents,
19-
sp,
20-
feature_gate::GateIssue::Language,
21-
feature_gate::EXPLAIN_CONCAT_IDENTS);
22-
}
23-
2415
if tts.is_empty() {
2516
cx.span_err(sp, "concat_idents! takes 1 or more arguments.");
2617
return DummyResult::any(sp);

src/libsyntax_ext/deriving/decodable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::deriving::{self, pathvec_std};
44
use crate::deriving::generic::*;
55
use crate::deriving::generic::ty::*;
6-
use crate::deriving::warn_if_deprecated;
76

87
use syntax::ast;
98
use syntax::ast::{Expr, MetaItem, Mutability};
@@ -26,7 +25,6 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt<'_>,
2625
mitem: &MetaItem,
2726
item: &Annotatable,
2827
push: &mut dyn FnMut(Annotatable)) {
29-
warn_if_deprecated(cx, span, "Decodable");
3028
expand_deriving_decodable_imp(cx, span, mitem, item, push, "serialize")
3129
}
3230

src/libsyntax_ext/deriving/encodable.rs

-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
use crate::deriving::{self, pathvec_std};
8686
use crate::deriving::generic::*;
8787
use crate::deriving::generic::ty::*;
88-
use crate::deriving::warn_if_deprecated;
8988

9089
use syntax::ast::{Expr, ExprKind, MetaItem, Mutability};
9190
use syntax::ext::base::{Annotatable, ExtCtxt};
@@ -107,7 +106,6 @@ pub fn expand_deriving_encodable(cx: &mut ExtCtxt<'_>,
107106
mitem: &MetaItem,
108107
item: &Annotatable,
109108
push: &mut dyn FnMut(Annotatable)) {
110-
warn_if_deprecated(cx, span, "Encodable");
111109
expand_deriving_encodable_imp(cx, span, mitem, item, push, "serialize")
112110
}
113111

src/libsyntax_ext/deriving/mod.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use rustc_data_structures::sync::Lrc;
44
use syntax::ast::{self, MetaItem};
5+
use syntax::attr::Deprecation;
56
use syntax::edition::Edition;
67
use syntax::ext::base::{Annotatable, ExtCtxt, Resolver, MultiItemModifier};
78
use syntax::ext::base::{SyntaxExtension, SyntaxExtensionKind};
@@ -60,7 +61,7 @@ impl MultiItemModifier for BuiltinDerive {
6061
}
6162

6263
macro_rules! derive_traits {
63-
($( $name:expr => $func:path, )+) => {
64+
($( [$deprecation:expr] $name:expr => $func:path, )+) => {
6465
pub fn is_builtin_trait(name: ast::Name) -> bool {
6566
match &*name.as_str() {
6667
$( $name )|+ => true,
@@ -81,6 +82,10 @@ macro_rules! derive_traits {
8182
resolver.add_builtin(
8283
ast::Ident::with_empty_ctxt(Symbol::intern($name)),
8384
Lrc::new(SyntaxExtension {
85+
deprecation: $deprecation.map(|msg| Deprecation {
86+
since: Some(Symbol::intern("1.0.0")),
87+
note: Some(Symbol::intern(msg)),
88+
}),
8489
allow_internal_unstable: allow_internal_unstable.clone(),
8590
..SyntaxExtension::default(
8691
SyntaxExtensionKind::LegacyDerive(Box::new(BuiltinDerive($func))),
@@ -94,44 +99,43 @@ macro_rules! derive_traits {
9499
}
95100

96101
derive_traits! {
102+
[None]
97103
"Clone" => clone::expand_deriving_clone,
98104

105+
[None]
99106
"Hash" => hash::expand_deriving_hash,
100107

108+
[None]
101109
"RustcEncodable" => encodable::expand_deriving_rustc_encodable,
102110

111+
[None]
103112
"RustcDecodable" => decodable::expand_deriving_rustc_decodable,
104113

114+
[None]
105115
"PartialEq" => partial_eq::expand_deriving_partial_eq,
116+
[None]
106117
"Eq" => eq::expand_deriving_eq,
118+
[None]
107119
"PartialOrd" => partial_ord::expand_deriving_partial_ord,
120+
[None]
108121
"Ord" => ord::expand_deriving_ord,
109122

123+
[None]
110124
"Debug" => debug::expand_deriving_debug,
111125

126+
[None]
112127
"Default" => default::expand_deriving_default,
113128

129+
[None]
114130
"Copy" => bounds::expand_deriving_copy,
115131

116132
// deprecated
133+
[Some("derive(Encodable) is deprecated in favor of derive(RustcEncodable)")]
117134
"Encodable" => encodable::expand_deriving_encodable,
135+
[Some("derive(Decodable) is deprecated in favor of derive(RustcDecodable)")]
118136
"Decodable" => decodable::expand_deriving_decodable,
119137
}
120138

121-
#[inline] // because `name` is a compile-time constant
122-
fn warn_if_deprecated(ecx: &mut ExtCtxt<'_>, sp: Span, name: &str) {
123-
if let Some(replacement) = match name {
124-
"Encodable" => Some("RustcEncodable"),
125-
"Decodable" => Some("RustcDecodable"),
126-
_ => None,
127-
} {
128-
ecx.span_warn(sp,
129-
&format!("derive({}) is deprecated in favor of derive({})",
130-
name,
131-
replacement));
132-
}
133-
}
134-
135139
/// Construct a name for the inner type parameter that can't collide with any type parameters of
136140
/// the item. This is achieved by starting with a base and then concatenating the names of all
137141
/// other type parameters.

0 commit comments

Comments
 (0)