From 640884bad0199e80a7701469a3d0eae0977b5998 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 28 Apr 2018 02:08:16 +0300 Subject: [PATCH] Add edition to expansion info --- src/librustc/hir/lowering.rs | 1 + src/librustc/ich/impls_syntax.rs | 10 ++++++++++ src/librustc_allocator/expand.rs | 3 ++- src/librustc_driver/lib.rs | 3 ++- src/libsyntax/ext/derive.rs | 3 ++- src/libsyntax/ext/expand.rs | 7 ++++++- src/libsyntax/std_inject.rs | 3 ++- src/libsyntax/test.rs | 3 ++- src/libsyntax_ext/proc_macro_registrar.rs | 3 ++- src/libsyntax_pos/edition.rs | 2 +- src/libsyntax_pos/hygiene.rs | 13 +++++++++++++ 11 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/librustc/hir/lowering.rs b/src/librustc/hir/lowering.rs index 0f4871954d66..45d429612a1d 100644 --- a/src/librustc/hir/lowering.rs +++ b/src/librustc/hir/lowering.rs @@ -593,6 +593,7 @@ impl<'a> LoweringContext<'a> { span: Some(span), allow_internal_unstable: true, allow_internal_unsafe: false, + edition: codemap::hygiene::default_edition(), }, }); span.with_ctxt(SyntaxContext::empty().apply_mark(mark)) diff --git a/src/librustc/ich/impls_syntax.rs b/src/librustc/ich/impls_syntax.rs index f56d701b0287..3a37c1c18c8d 100644 --- a/src/librustc/ich/impls_syntax.rs +++ b/src/librustc/ich/impls_syntax.rs @@ -131,6 +131,15 @@ impl_stable_hash_for!(struct ::syntax::attr::Stability { rustc_const_unstable }); +impl<'a> HashStable> +for ::syntax::edition::Edition { + fn hash_stable(&self, + hcx: &mut StableHashingContext<'a>, + hasher: &mut StableHasher) { + mem::discriminant(self).hash_stable(hcx, hasher); + } +} + impl<'a> HashStable> for ::syntax::attr::StabilityLevel { fn hash_stable(&self, @@ -389,6 +398,7 @@ impl_stable_hash_for!(struct ::syntax_pos::hygiene::NameAndSpan { format, allow_internal_unstable, allow_internal_unsafe, + edition, span }); diff --git a/src/librustc_allocator/expand.rs b/src/librustc_allocator/expand.rs index de8814d3d6a7..497d5fdcac70 100644 --- a/src/librustc_allocator/expand.rs +++ b/src/librustc_allocator/expand.rs @@ -21,7 +21,7 @@ use syntax::ext::base::ExtCtxt; use syntax::ext::base::Resolver; use syntax::ext::build::AstBuilder; use syntax::ext::expand::ExpansionConfig; -use syntax::ext::hygiene::{Mark, SyntaxContext}; +use syntax::ext::hygiene::{self, Mark, SyntaxContext}; use syntax::fold::{self, Folder}; use syntax::parse::ParseSess; use syntax::ptr::P; @@ -86,6 +86,7 @@ impl<'a> Folder for ExpandAllocatorDirectives<'a> { span: None, allow_internal_unstable: true, allow_internal_unsafe: false, + edition: hygiene::default_edition(), }, }); let span = item.span.with_ctxt(SyntaxContext::empty().apply_mark(mark)); diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 0b4b090f1f0b..2f89814032ef 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -108,7 +108,7 @@ use syntax::ast; use syntax::codemap::{CodeMap, FileLoader, RealFileLoader}; use syntax::feature_gate::{GatedCfg, UnstableFeatures}; use syntax::parse::{self, PResult}; -use syntax_pos::{DUMMY_SP, MultiSpan, FileName}; +use syntax_pos::{hygiene, DUMMY_SP, MultiSpan, FileName}; #[cfg(test)] mod test; @@ -466,6 +466,7 @@ pub fn run_compiler<'a>(args: &[String], }; let (sopts, cfg) = config::build_session_options_and_crate_config(&matches); + hygiene::set_default_edition(sopts.edition); driver::spawn_thread_pool(sopts, |sopts| { run_compiler_with_pool(matches, sopts, cfg, callbacks, file_loader, emitter_dest) diff --git a/src/libsyntax/ext/derive.rs b/src/libsyntax/ext/derive.rs index 6bf166dfe950..0b6a7e1c4f49 100644 --- a/src/libsyntax/ext/derive.rs +++ b/src/libsyntax/ext/derive.rs @@ -10,7 +10,7 @@ use attr::HasAttrs; use ast; -use codemap::{ExpnInfo, NameAndSpan, ExpnFormat}; +use codemap::{hygiene, ExpnInfo, NameAndSpan, ExpnFormat}; use ext::base::ExtCtxt; use ext::build::AstBuilder; use parse::parser::PathStyle; @@ -65,6 +65,7 @@ pub fn add_derived_markers(cx: &mut ExtCtxt, span: Span, traits: &[ast::Path] span: None, allow_internal_unstable: true, allow_internal_unsafe: false, + edition: hygiene::default_edition(), }, }); diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 584b9455a93a..1cf0a7077c2d 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -16,7 +16,7 @@ use config::{is_test_or_bench, StripUnconfigured}; use errors::FatalError; use ext::base::*; use ext::derive::{add_derived_markers, collect_derives}; -use ext::hygiene::{Mark, SyntaxContext}; +use ext::hygiene::{self, Mark, SyntaxContext}; use ext::placeholders::{placeholder, PlaceholderExpander}; use feature_gate::{self, Features, GateIssue, is_builtin_attr, emit_feature_err}; use fold; @@ -502,6 +502,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { span: None, allow_internal_unstable: false, allow_internal_unsafe: false, + edition: hygiene::default_edition(), } }); @@ -642,6 +643,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { span: def_site_span, allow_internal_unstable, allow_internal_unsafe, + edition: hygiene::default_edition(), }, }); Ok(()) @@ -688,6 +690,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { span: tt_span, allow_internal_unstable, allow_internal_unsafe: false, + edition: hygiene::default_edition(), } }); @@ -728,6 +731,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { // FIXME probably want to follow macro_rules macros here. allow_internal_unstable: false, allow_internal_unsafe: false, + edition: hygiene::default_edition(), }, }); @@ -802,6 +806,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { span: None, allow_internal_unstable: false, allow_internal_unsafe: false, + edition: hygiene::default_edition(), } }; diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index 53dc19ba37d1..e9cd7adb9c16 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -14,7 +14,7 @@ use std::cell::Cell; use ext::hygiene::{Mark, SyntaxContext}; use symbol::{Symbol, keywords}; use syntax_pos::{DUMMY_SP, Span}; -use codemap::{ExpnInfo, NameAndSpan, MacroAttribute, dummy_spanned, respan}; +use codemap::{ExpnInfo, NameAndSpan, MacroAttribute, dummy_spanned, hygiene, respan}; use ptr::P; use tokenstream::TokenStream; @@ -30,6 +30,7 @@ fn ignored_span(sp: Span) -> Span { span: None, allow_internal_unstable: true, allow_internal_unsafe: false, + edition: hygiene::default_edition(), } }); sp.with_ctxt(SyntaxContext::empty().apply_mark(mark)) diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 1734692f9e75..1dfd48a24c3b 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -29,7 +29,7 @@ use entry::{self, EntryPointType}; use ext::base::{ExtCtxt, Resolver}; use ext::build::AstBuilder; use ext::expand::ExpansionConfig; -use ext::hygiene::{Mark, SyntaxContext}; +use ext::hygiene::{self, Mark, SyntaxContext}; use fold::Folder; use feature_gate::Features; use util::move_map::MoveMap; @@ -300,6 +300,7 @@ fn generate_test_harness(sess: &ParseSess, span: None, allow_internal_unstable: true, allow_internal_unsafe: false, + edition: hygiene::default_edition(), } }); diff --git a/src/libsyntax_ext/proc_macro_registrar.rs b/src/libsyntax_ext/proc_macro_registrar.rs index d684e8b4ffea..3593165023a5 100644 --- a/src/libsyntax_ext/proc_macro_registrar.rs +++ b/src/libsyntax_ext/proc_macro_registrar.rs @@ -14,7 +14,7 @@ use errors; use syntax::ast::{self, Ident, NodeId}; use syntax::attr; -use syntax::codemap::{ExpnInfo, NameAndSpan, MacroAttribute, respan}; +use syntax::codemap::{ExpnInfo, NameAndSpan, MacroAttribute, hygiene, respan}; use syntax::ext::base::ExtCtxt; use syntax::ext::build::AstBuilder; use syntax::ext::expand::ExpansionConfig; @@ -369,6 +369,7 @@ fn mk_registrar(cx: &mut ExtCtxt, span: None, allow_internal_unstable: true, allow_internal_unsafe: false, + edition: hygiene::default_edition(), } }); let span = DUMMY_SP.apply_mark(mark); diff --git a/src/libsyntax_pos/edition.rs b/src/libsyntax_pos/edition.rs index c98b54581f35..18446c109964 100644 --- a/src/libsyntax_pos/edition.rs +++ b/src/libsyntax_pos/edition.rs @@ -12,7 +12,7 @@ use std::fmt; use std::str::FromStr; /// The edition of the compiler (RFC 2052) -#[derive(Clone, Copy, Hash, PartialOrd, Ord, Eq, PartialEq, Debug)] +#[derive(Clone, Copy, Hash, PartialOrd, Ord, Eq, PartialEq, Debug, RustcEncodable, RustcDecodable)] #[non_exhaustive] pub enum Edition { // editions must be kept in order, newest to oldest diff --git a/src/libsyntax_pos/hygiene.rs b/src/libsyntax_pos/hygiene.rs index be031ea98c9d..1365ac396ffd 100644 --- a/src/libsyntax_pos/hygiene.rs +++ b/src/libsyntax_pos/hygiene.rs @@ -17,6 +17,7 @@ use GLOBALS; use Span; +use edition::Edition; use symbol::{Ident, Symbol}; use serialize::{Encodable, Decodable, Encoder, Decoder}; @@ -151,6 +152,7 @@ pub struct HygieneData { syntax_contexts: Vec, markings: HashMap<(SyntaxContext, Mark), SyntaxContext>, gensym_to_ctxt: HashMap, + default_edition: Edition, } impl HygieneData { @@ -168,6 +170,7 @@ impl HygieneData { }], markings: HashMap::new(), gensym_to_ctxt: HashMap::new(), + default_edition: Edition::Edition2015, } } @@ -176,6 +179,14 @@ impl HygieneData { } } +pub fn default_edition() -> Edition { + HygieneData::with(|data| data.default_edition) +} + +pub fn set_default_edition(edition: Edition) { + HygieneData::with(|data| data.default_edition = edition); +} + pub fn clear_markings() { HygieneData::with(|data| data.markings = HashMap::new()); } @@ -443,6 +454,8 @@ pub struct NameAndSpan { /// Whether the macro is allowed to use `unsafe` internally /// even if the user crate has `#![forbid(unsafe_code)]`. pub allow_internal_unsafe: bool, + /// Edition of the crate in which the macro is defined. + pub edition: Edition, /// The span of the macro definition itself. The macro may not /// have a sensible definition span (e.g. something defined /// completely inside libsyntax) in which case this is None.