Skip to content

Commit df40e61

Browse files
committed
Auto merge of #50307 - petrochenkov:keyhyg2, r=nikomatsakis
Implement edition hygiene for keywords Determine "keywordness" of an identifier in its hygienic context. cc #49611 I've resurrected `proc` as an Edition-2015-only keyword for testing purposes, but it should probably be buried again. EDIT: `proc` is removed again.
2 parents fd18d25 + d8bbc1e commit df40e61

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+896
-119
lines changed

src/libproc_macro/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ impl Ident {
818818
pub fn new_raw(string: &str, span: Span) -> Ident {
819819
let mut ident = Ident::new(string, span);
820820
if ident.sym == keywords::Underscore.name() ||
821-
token::is_path_segment_keyword(ast::Ident::with_empty_ctxt(ident.sym)) {
821+
ast::Ident::with_empty_ctxt(ident.sym).is_path_segment_keyword() {
822822
panic!("`{:?}` is not a valid raw identifier", string)
823823
}
824824
ident.is_raw = true;

src/librustc/hir/lowering.rs

+1
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ impl<'a> LoweringContext<'a> {
593593
span: Some(span),
594594
allow_internal_unstable: true,
595595
allow_internal_unsafe: false,
596+
edition: codemap::hygiene::default_edition(),
596597
},
597598
});
598599
span.with_ctxt(SyntaxContext::empty().apply_mark(mark))

src/librustc/hir/print.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub use self::AnnNode::*;
1313
use rustc_target::spec::abi::Abi;
1414
use syntax::ast;
1515
use syntax::codemap::{CodeMap, Spanned};
16-
use syntax::parse::{token, ParseSess};
16+
use syntax::parse::ParseSess;
1717
use syntax::parse::lexer::comments;
1818
use syntax::print::pp::{self, Breaks};
1919
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
@@ -1559,7 +1559,7 @@ impl<'a> State<'a> {
15591559
}
15601560

15611561
pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
1562-
if token::is_raw_guess(ast::Ident::with_empty_ctxt(name)) {
1562+
if name.to_ident().is_raw_guess() {
15631563
self.s.word(&format!("r#{}", name))?;
15641564
} else {
15651565
self.s.word(&name.as_str())?;

src/librustc/ich/impls_syntax.rs

+10
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,15 @@ impl_stable_hash_for!(struct ::syntax::attr::Stability {
131131
rustc_const_unstable
132132
});
133133

134+
impl<'a> HashStable<StableHashingContext<'a>>
135+
for ::syntax::edition::Edition {
136+
fn hash_stable<W: StableHasherResult>(&self,
137+
hcx: &mut StableHashingContext<'a>,
138+
hasher: &mut StableHasher<W>) {
139+
mem::discriminant(self).hash_stable(hcx, hasher);
140+
}
141+
}
142+
134143
impl<'a> HashStable<StableHashingContext<'a>>
135144
for ::syntax::attr::StabilityLevel {
136145
fn hash_stable<W: StableHasherResult>(&self,
@@ -389,6 +398,7 @@ impl_stable_hash_for!(struct ::syntax_pos::hygiene::NameAndSpan {
389398
format,
390399
allow_internal_unstable,
391400
allow_internal_unsafe,
401+
edition,
392402
span
393403
});
394404

src/librustc/middle/cstore.rs

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use session::search_paths::PathKind;
3434
use std::any::Any;
3535
use std::path::{Path, PathBuf};
3636
use syntax::ast;
37+
use syntax::edition::Edition;
3738
use syntax::ext::base::SyntaxExtension;
3839
use syntax::symbol::Symbol;
3940
use syntax_pos::Span;
@@ -235,6 +236,7 @@ pub trait CrateStore {
235236
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
236237
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator;
237238
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
239+
fn crate_edition_untracked(&self, cnum: CrateNum) -> Edition;
238240
fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name>;
239241
fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
240242
fn load_macro_untracked(&self, did: DefId, sess: &Session) -> LoadedMacro;
@@ -309,6 +311,7 @@ impl CrateStore for DummyCrateStore {
309311
bug!("crate_disambiguator")
310312
}
311313
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh { bug!("crate_hash") }
314+
fn crate_edition_untracked(&self, cnum: CrateNum) -> Edition { bug!("crate_edition_untracked") }
312315

313316
// resolve
314317
fn def_key(&self, def: DefId) -> DefKey { bug!("def_key") }

src/librustc_allocator/expand.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use syntax::ext::base::ExtCtxt;
2121
use syntax::ext::base::Resolver;
2222
use syntax::ext::build::AstBuilder;
2323
use syntax::ext::expand::ExpansionConfig;
24-
use syntax::ext::hygiene::{Mark, SyntaxContext};
24+
use syntax::ext::hygiene::{self, Mark, SyntaxContext};
2525
use syntax::fold::{self, Folder};
2626
use syntax::parse::ParseSess;
2727
use syntax::ptr::P;
@@ -86,6 +86,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> {
8686
span: None,
8787
allow_internal_unstable: true,
8888
allow_internal_unsafe: false,
89+
edition: hygiene::default_edition(),
8990
},
9091
});
9192
let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark));

src/librustc_driver/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ use syntax::ast;
108108
use syntax::codemap::{CodeMap, FileLoader, RealFileLoader};
109109
use syntax::feature_gate::{GatedCfg, UnstableFeatures};
110110
use syntax::parse::{self, PResult};
111-
use syntax_pos::{DUMMY_SP, MultiSpan, FileName};
111+
use syntax_pos::{hygiene, DUMMY_SP, MultiSpan, FileName};
112112

113113
#[cfg(test)]
114114
mod test;
@@ -466,6 +466,7 @@ pub fn run_compiler<'a>(args: &[String],
466466
};
467467

468468
let (sopts, cfg) = config::build_session_options_and_crate_config(&matches);
469+
hygiene::set_default_edition(sopts.edition);
469470

470471
driver::spawn_thread_pool(sopts, |sopts| {
471472
run_compiler_with_pool(matches, sopts, cfg, callbacks, file_loader, emitter_dest)

src/librustc_metadata/creader.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::{cmp, fs};
3535

3636
use syntax::ast;
3737
use syntax::attr;
38+
use syntax::edition::Edition;
3839
use syntax::ext::base::SyntaxExtension;
3940
use syntax::symbol::Symbol;
4041
use syntax::visit;
@@ -535,7 +536,10 @@ impl<'a> CrateLoader<'a> {
535536
mem::transmute::<*mut u8, fn(&mut Registry)>(sym)
536537
};
537538

538-
struct MyRegistrar(Vec<(ast::Name, Lrc<SyntaxExtension>)>);
539+
struct MyRegistrar {
540+
extensions: Vec<(ast::Name, Lrc<SyntaxExtension>)>,
541+
edition: Edition,
542+
}
539543

540544
impl Registry for MyRegistrar {
541545
fn register_custom_derive(&mut self,
@@ -544,36 +548,38 @@ impl<'a> CrateLoader<'a> {
544548
attributes: &[&'static str]) {
545549
let attrs = attributes.iter().cloned().map(Symbol::intern).collect::<Vec<_>>();
546550
let derive = ProcMacroDerive::new(expand, attrs.clone());
547-
let derive = SyntaxExtension::ProcMacroDerive(Box::new(derive), attrs);
548-
self.0.push((Symbol::intern(trait_name), Lrc::new(derive)));
551+
let derive = SyntaxExtension::ProcMacroDerive(
552+
Box::new(derive), attrs, self.edition
553+
);
554+
self.extensions.push((Symbol::intern(trait_name), Lrc::new(derive)));
549555
}
550556

551557
fn register_attr_proc_macro(&mut self,
552558
name: &str,
553559
expand: fn(TokenStream, TokenStream) -> TokenStream) {
554560
let expand = SyntaxExtension::AttrProcMacro(
555-
Box::new(AttrProcMacro { inner: expand })
561+
Box::new(AttrProcMacro { inner: expand }), self.edition
556562
);
557-
self.0.push((Symbol::intern(name), Lrc::new(expand)));
563+
self.extensions.push((Symbol::intern(name), Lrc::new(expand)));
558564
}
559565

560566
fn register_bang_proc_macro(&mut self,
561567
name: &str,
562568
expand: fn(TokenStream) -> TokenStream) {
563569
let expand = SyntaxExtension::ProcMacro(
564-
Box::new(BangProcMacro { inner: expand })
570+
Box::new(BangProcMacro { inner: expand }), self.edition
565571
);
566-
self.0.push((Symbol::intern(name), Lrc::new(expand)));
572+
self.extensions.push((Symbol::intern(name), Lrc::new(expand)));
567573
}
568574
}
569575

570-
let mut my_registrar = MyRegistrar(Vec::new());
576+
let mut my_registrar = MyRegistrar { extensions: Vec::new(), edition: root.edition };
571577
registrar(&mut my_registrar);
572578

573579
// Intentionally leak the dynamic library. We can't ever unload it
574580
// since the library can make things that will live arbitrarily long.
575581
mem::forget(lib);
576-
my_registrar.0
582+
my_registrar.extensions
577583
}
578584

579585
/// Look for a plugin registrar. Returns library path, crate

src/librustc_metadata/cstore.rs

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc::util::nodemap::{FxHashMap, NodeMap};
2424

2525
use rustc_data_structures::sync::{Lrc, RwLock, Lock};
2626
use syntax::{ast, attr};
27+
use syntax::edition::Edition;
2728
use syntax::ext::base::SyntaxExtension;
2829
use syntax::symbol::Symbol;
2930
use syntax_pos;
@@ -234,4 +235,8 @@ impl CrateMetadata {
234235
pub fn panic_strategy(&self) -> PanicStrategy {
235236
self.root.panic_strategy.clone()
236237
}
238+
239+
pub fn edition(&self) -> Edition {
240+
self.root.edition
241+
}
237242
}

src/librustc_metadata/cstore_impl.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use std::sync::Arc;
3838
use syntax::ast;
3939
use syntax::attr;
4040
use syntax::codemap;
41+
use syntax::edition::Edition;
4142
use syntax::ext::base::SyntaxExtension;
4243
use syntax::parse::filemap_to_stream;
4344
use syntax::symbol::Symbol;
@@ -464,6 +465,11 @@ impl CrateStore for cstore::CStore {
464465
self.get_crate_data(cnum).hash()
465466
}
466467

468+
fn crate_edition_untracked(&self, cnum: CrateNum) -> Edition
469+
{
470+
self.get_crate_data(cnum).edition()
471+
}
472+
467473
/// Returns the `DefKey` for a given `DefId`. This indicates the
468474
/// parent `DefId` as well as some idea of what kind of data the
469475
/// `DefId` refers to.
@@ -512,7 +518,8 @@ impl CrateStore for cstore::CStore {
512518
return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone());
513519
} else if data.name == "proc_macro" &&
514520
self.get_crate_data(id.krate).item_name(id.index) == "quote" {
515-
let ext = SyntaxExtension::ProcMacro(Box::new(::proc_macro::__internal::Quoter));
521+
let ext = SyntaxExtension::ProcMacro(Box::new(::proc_macro::__internal::Quoter),
522+
data.edition());
516523
return LoadedMacro::ProcMacro(Lrc::new(ext));
517524
}
518525

src/librustc_metadata/encoder.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use syntax::ast::{self, CRATE_NODE_ID};
4444
use syntax::codemap::Spanned;
4545
use syntax::attr;
4646
use syntax::symbol::Symbol;
47-
use syntax_pos::{self, FileName, FileMap, Span, DUMMY_SP};
47+
use syntax_pos::{self, hygiene, FileName, FileMap, Span, DUMMY_SP};
4848

4949
use rustc::hir::{self, PatKind};
5050
use rustc::hir::itemlikevisit::ItemLikeVisitor;
@@ -496,6 +496,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
496496
hash: link_meta.crate_hash,
497497
disambiguator: tcx.sess.local_crate_disambiguator(),
498498
panic_strategy: tcx.sess.panic_strategy(),
499+
edition: hygiene::default_edition(),
499500
has_global_allocator: has_global_allocator,
500501
has_default_lib_allocator: has_default_lib_allocator,
501502
plugin_registrar_fn: tcx.sess

src/librustc_metadata/schema.rs

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple};
2323

2424
use rustc_serialize as serialize;
2525
use syntax::{ast, attr};
26+
use syntax::edition::Edition;
2627
use syntax::symbol::Symbol;
2728
use syntax_pos::{self, Span};
2829

@@ -189,6 +190,7 @@ pub struct CrateRoot {
189190
pub hash: hir::svh::Svh,
190191
pub disambiguator: CrateDisambiguator,
191192
pub panic_strategy: PanicStrategy,
193+
pub edition: Edition,
192194
pub has_global_allocator: bool,
193195
pub has_default_lib_allocator: bool,
194196
pub plugin_registrar_fn: Option<DefIndex>,

src/librustc_passes/ast_validation.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use rustc::session::Session;
2121
use syntax::ast::*;
2222
use syntax::attr;
2323
use syntax::codemap::Spanned;
24-
use syntax::parse::token;
2524
use syntax::symbol::keywords;
2625
use syntax::visit::{self, Visitor};
2726
use syntax_pos::Span;
@@ -40,14 +39,13 @@ impl<'a> AstValidator<'a> {
4039
let valid_names = [keywords::UnderscoreLifetime.name(),
4140
keywords::StaticLifetime.name(),
4241
keywords::Invalid.name()];
43-
if !valid_names.contains(&ident.name) &&
44-
token::is_reserved_ident(ident.without_first_quote()) {
42+
if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() {
4543
self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
4644
}
4745
}
4846

4947
fn check_label(&self, ident: Ident) {
50-
if token::is_reserved_ident(ident.without_first_quote()) {
48+
if ident.without_first_quote().is_reserved() {
5149
self.err_handler()
5250
.span_err(ident.span, &format!("invalid label name `{}`", ident.name));
5351
}

src/librustc_plugin/registry.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc::session::Session;
1515

1616
use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
1717
use syntax::ext::base::MacroExpanderFn;
18+
use syntax::ext::hygiene;
1819
use syntax::symbol::Symbol;
1920
use syntax::ast;
2021
use syntax::feature_gate::AttributeType;
@@ -107,15 +108,17 @@ impl<'a> Registry<'a> {
107108
def_info: _,
108109
allow_internal_unstable,
109110
allow_internal_unsafe,
110-
unstable_feature
111+
unstable_feature,
112+
edition,
111113
} => {
112114
let nid = ast::CRATE_NODE_ID;
113115
NormalTT {
114116
expander,
115117
def_info: Some((nid, self.krate_span)),
116118
allow_internal_unstable,
117119
allow_internal_unsafe,
118-
unstable_feature
120+
unstable_feature,
121+
edition,
119122
}
120123
}
121124
IdentTT(ext, _, allow_internal_unstable) => {
@@ -150,6 +153,7 @@ impl<'a> Registry<'a> {
150153
allow_internal_unstable: false,
151154
allow_internal_unsafe: false,
152155
unstable_feature: None,
156+
edition: hygiene::default_edition(),
153157
});
154158
}
155159

src/librustc_resolve/build_reduced_graph.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,8 @@ impl<'a> Resolver<'a> {
588588

589589
let ext = Lrc::new(macro_rules::compile(&self.session.parse_sess,
590590
&self.session.features_untracked(),
591-
&macro_def));
591+
&macro_def,
592+
self.cstore.crate_edition_untracked(def_id.krate)));
592593
self.macro_map.insert(def_id, ext.clone());
593594
ext
594595
}

src/librustc_resolve/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
5858
use syntax::ast::{Label, Local, Mutability, Pat, PatKind, Path};
5959
use syntax::ast::{QSelf, TraitItemKind, TraitRef, Ty, TyKind};
6060
use syntax::feature_gate::{feature_err, GateIssue};
61-
use syntax::parse::token;
6261
use syntax::ptr::P;
6362

6463
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
@@ -3274,7 +3273,7 @@ impl<'a> Resolver<'a> {
32743273
// `$crate::a::b`
32753274
module = Some(self.resolve_crate_root(ident.span.ctxt(), true));
32763275
continue
3277-
} else if i == 1 && !token::is_path_segment_keyword(ident) {
3276+
} else if i == 1 && !ident.is_path_segment_keyword() {
32783277
let prev_name = path[0].name;
32793278
if prev_name == keywords::Extern.name() ||
32803279
prev_name == keywords::CrateRoot.name() &&

0 commit comments

Comments
 (0)