Skip to content

Commit 792e32b

Browse files
committed
rollup merge of rust-lang#17576 : kmcallister/hide-quotes
2 parents 757fa6f + fc1b908 commit 792e32b

File tree

6 files changed

+74
-68
lines changed

6 files changed

+74
-68
lines changed

src/librustc/driver/driver.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,9 @@ pub fn phase_2_configure_and_expand(sess: &Session,
288288
os::setenv("PATH", os::join_paths(new_path.as_slice()).unwrap());
289289
}
290290
let cfg = syntax::ext::expand::ExpansionConfig {
291-
deriving_hash_type_parameter: sess.features.borrow().default_type_params,
292291
crate_name: crate_name.to_string(),
292+
deriving_hash_type_parameter: sess.features.borrow().default_type_params,
293+
enable_quotes: sess.features.borrow().quote,
293294
};
294295
let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,
295296
cfg,

src/libsyntax/ext/base.rs

+30-27
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl BlockInfo {
344344

345345
/// The base map of methods for expanding syntax extension
346346
/// AST nodes into full ASTs
347-
fn initial_syntax_expander_table() -> SyntaxEnv {
347+
fn initial_syntax_expander_table(ecfg: &expand::ExpansionConfig) -> SyntaxEnv {
348348
// utility function to simplify creating NormalTT syntax extensions
349349
fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
350350
NormalTT(box f, None)
@@ -383,31 +383,33 @@ fn initial_syntax_expander_table() -> SyntaxEnv {
383383
syntax_expanders.insert(intern("deriving"),
384384
Decorator(box ext::deriving::expand_meta_deriving));
385385

386-
// Quasi-quoting expanders
387-
syntax_expanders.insert(intern("quote_tokens"),
388-
builtin_normal_expander(
389-
ext::quote::expand_quote_tokens));
390-
syntax_expanders.insert(intern("quote_expr"),
391-
builtin_normal_expander(
392-
ext::quote::expand_quote_expr));
393-
syntax_expanders.insert(intern("quote_ty"),
394-
builtin_normal_expander(
395-
ext::quote::expand_quote_ty));
396-
syntax_expanders.insert(intern("quote_method"),
397-
builtin_normal_expander(
398-
ext::quote::expand_quote_method));
399-
syntax_expanders.insert(intern("quote_item"),
400-
builtin_normal_expander(
401-
ext::quote::expand_quote_item));
402-
syntax_expanders.insert(intern("quote_pat"),
403-
builtin_normal_expander(
404-
ext::quote::expand_quote_pat));
405-
syntax_expanders.insert(intern("quote_arm"),
406-
builtin_normal_expander(
407-
ext::quote::expand_quote_arm));
408-
syntax_expanders.insert(intern("quote_stmt"),
409-
builtin_normal_expander(
410-
ext::quote::expand_quote_stmt));
386+
if ecfg.enable_quotes {
387+
// Quasi-quoting expanders
388+
syntax_expanders.insert(intern("quote_tokens"),
389+
builtin_normal_expander(
390+
ext::quote::expand_quote_tokens));
391+
syntax_expanders.insert(intern("quote_expr"),
392+
builtin_normal_expander(
393+
ext::quote::expand_quote_expr));
394+
syntax_expanders.insert(intern("quote_ty"),
395+
builtin_normal_expander(
396+
ext::quote::expand_quote_ty));
397+
syntax_expanders.insert(intern("quote_method"),
398+
builtin_normal_expander(
399+
ext::quote::expand_quote_method));
400+
syntax_expanders.insert(intern("quote_item"),
401+
builtin_normal_expander(
402+
ext::quote::expand_quote_item));
403+
syntax_expanders.insert(intern("quote_pat"),
404+
builtin_normal_expander(
405+
ext::quote::expand_quote_pat));
406+
syntax_expanders.insert(intern("quote_arm"),
407+
builtin_normal_expander(
408+
ext::quote::expand_quote_arm));
409+
syntax_expanders.insert(intern("quote_stmt"),
410+
builtin_normal_expander(
411+
ext::quote::expand_quote_stmt));
412+
}
411413

412414
syntax_expanders.insert(intern("line"),
413415
builtin_normal_expander(
@@ -466,6 +468,7 @@ pub struct ExtCtxt<'a> {
466468
impl<'a> ExtCtxt<'a> {
467469
pub fn new<'a>(parse_sess: &'a parse::ParseSess, cfg: ast::CrateConfig,
468470
ecfg: expand::ExpansionConfig) -> ExtCtxt<'a> {
471+
let env = initial_syntax_expander_table(&ecfg);
469472
ExtCtxt {
470473
parse_sess: parse_sess,
471474
cfg: cfg,
@@ -474,7 +477,7 @@ impl<'a> ExtCtxt<'a> {
474477
ecfg: ecfg,
475478
trace_mac: false,
476479
exported_macros: Vec::new(),
477-
syntax_env: initial_syntax_expander_table(),
480+
syntax_env: env,
478481
}
479482
}
480483

src/libsyntax/ext/expand.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,19 @@ fn new_span(cx: &ExtCtxt, sp: Span) -> Span {
975975
}
976976

977977
pub struct ExpansionConfig {
978-
pub deriving_hash_type_parameter: bool,
979978
pub crate_name: String,
979+
pub deriving_hash_type_parameter: bool,
980+
pub enable_quotes: bool,
981+
}
982+
983+
impl ExpansionConfig {
984+
pub fn default(crate_name: String) -> ExpansionConfig {
985+
ExpansionConfig {
986+
crate_name: crate_name,
987+
deriving_hash_type_parameter: false,
988+
enable_quotes: false,
989+
}
990+
}
980991
}
981992

982993
pub struct ExportedMacros {
@@ -1106,7 +1117,7 @@ impl<'a, 'v> Visitor<'v> for MacroExterminator<'a> {
11061117
#[cfg(test)]
11071118
mod test {
11081119
use super::{pattern_bindings, expand_crate, contains_macro_escape};
1109-
use super::{PatIdentFinder, IdentRenamer, PatIdentRenamer};
1120+
use super::{PatIdentFinder, IdentRenamer, PatIdentRenamer, ExpansionConfig};
11101121
use ast;
11111122
use ast::{Attribute_, AttrOuter, MetaWord, Name};
11121123
use attr;
@@ -1171,6 +1182,10 @@ mod test {
11711182
// these following tests are quite fragile, in that they don't test what
11721183
// *kind* of failure occurs.
11731184

1185+
fn test_ecfg() -> ExpansionConfig {
1186+
ExpansionConfig::default("test".to_string())
1187+
}
1188+
11741189
// make sure that macros can't escape fns
11751190
#[should_fail]
11761191
#[test] fn macros_cant_escape_fns_test () {
@@ -1182,11 +1197,7 @@ mod test {
11821197
src,
11831198
Vec::new(), &sess);
11841199
// should fail:
1185-
let cfg = ::syntax::ext::expand::ExpansionConfig {
1186-
deriving_hash_type_parameter: false,
1187-
crate_name: "test".to_string(),
1188-
};
1189-
expand_crate(&sess,cfg,vec!(),vec!(),crate_ast);
1200+
expand_crate(&sess,test_ecfg(),vec!(),vec!(),crate_ast);
11901201
}
11911202

11921203
// make sure that macros can't escape modules
@@ -1199,11 +1210,7 @@ mod test {
11991210
"<test>".to_string(),
12001211
src,
12011212
Vec::new(), &sess);
1202-
let cfg = ::syntax::ext::expand::ExpansionConfig {
1203-
deriving_hash_type_parameter: false,
1204-
crate_name: "test".to_string(),
1205-
};
1206-
expand_crate(&sess,cfg,vec!(),vec!(),crate_ast);
1213+
expand_crate(&sess,test_ecfg(),vec!(),vec!(),crate_ast);
12071214
}
12081215

12091216
// macro_escape modules should allow macros to escape
@@ -1215,11 +1222,7 @@ mod test {
12151222
"<test>".to_string(),
12161223
src,
12171224
Vec::new(), &sess);
1218-
let cfg = ::syntax::ext::expand::ExpansionConfig {
1219-
deriving_hash_type_parameter: false,
1220-
crate_name: "test".to_string(),
1221-
};
1222-
expand_crate(&sess, cfg, vec!(), vec!(), crate_ast);
1225+
expand_crate(&sess, test_ecfg(), vec!(), vec!(), crate_ast);
12231226
}
12241227

12251228
#[test] fn test_contains_flatten (){
@@ -1252,11 +1255,7 @@ mod test {
12521255
let ps = parse::new_parse_sess();
12531256
let crate_ast = string_to_parser(&ps, crate_str).parse_crate_mod();
12541257
// the cfg argument actually does matter, here...
1255-
let cfg = ::syntax::ext::expand::ExpansionConfig {
1256-
deriving_hash_type_parameter: false,
1257-
crate_name: "test".to_string(),
1258-
};
1259-
expand_crate(&ps,cfg,vec!(),vec!(),crate_ast)
1258+
expand_crate(&ps,test_ecfg(),vec!(),vec!(),crate_ast)
12601259
}
12611260

12621261
// find the pat_ident paths in a crate

src/libsyntax/feature_gate.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub struct Features {
102102
pub rustc_diagnostic_macros: bool,
103103
pub import_shadowing: bool,
104104
pub visible_private_types: bool,
105+
pub quote: bool,
105106
}
106107

107108
impl Features {
@@ -112,6 +113,7 @@ impl Features {
112113
rustc_diagnostic_macros: false,
113114
import_shadowing: false,
114115
visible_private_types: false,
116+
quote: false,
115117
}
116118
}
117119
}
@@ -282,10 +284,6 @@ impl<'a, 'v> Visitor<'v> for Context<'a> {
282284
fn visit_mac(&mut self, macro: &ast::Mac) {
283285
let ast::MacInvocTT(ref path, _, _) = macro.node;
284286
let id = path.segments.last().unwrap().identifier;
285-
let quotes = ["quote_tokens", "quote_expr", "quote_ty",
286-
"quote_item", "quote_pat", "quote_stmt"];
287-
let msg = " is not stable enough for use and are subject to change";
288-
289287

290288
if id == token::str_to_ident("macro_rules") {
291289
self.gate_feature("macro_rules", path.span, "macro definitions are \
@@ -311,16 +309,6 @@ impl<'a, 'v> Visitor<'v> for Context<'a> {
311309
self.gate_feature("concat_idents", path.span, "`concat_idents` is not \
312310
stable enough for use and is subject to change");
313311
}
314-
315-
else {
316-
for &quote in quotes.iter() {
317-
if id == token::str_to_ident(quote) {
318-
self.gate_feature("quote",
319-
path.span,
320-
format!("{}{}", quote, msg).as_slice());
321-
}
322-
}
323-
}
324312
}
325313

326314
fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
@@ -483,6 +471,7 @@ pub fn check_crate(span_handler: &SpanHandler, krate: &ast::Crate) -> (Features,
483471
rustc_diagnostic_macros: cx.has_feature("rustc_diagnostic_macros"),
484472
import_shadowing: cx.has_feature("import_shadowing"),
485473
visible_private_types: cx.has_feature("visible_private_types"),
474+
quote: cx.has_feature("quote"),
486475
},
487476
unknown_features)
488477
}

src/libsyntax/test.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,7 @@ fn generate_test_harness(sess: &ParseSess,
235235
sess: sess,
236236
span_diagnostic: sd,
237237
ext_cx: ExtCtxt::new(sess, cfg.clone(),
238-
ExpansionConfig {
239-
deriving_hash_type_parameter: false,
240-
crate_name: "test".to_string(),
241-
}),
238+
ExpansionConfig::default("test".to_string())),
242239
path: Vec::new(),
243240
testfns: Vec::new(),
244241
reexport_test_harness_main: reexport_test_harness_main,
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(macro_rules)]
12+
13+
macro_rules! quote_tokens ( () => (()) )
14+
15+
pub fn main() {
16+
quote_tokens!();
17+
}

0 commit comments

Comments
 (0)