From 21b8f2ecde8995d51f3f01c434435d04857678b9 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 3 Jan 2021 08:56:49 -0500 Subject: [PATCH] Make `ExpnData` fields `krate` and `orig_id` private These fields are only used by hygiene serialized, and should not be accessed by anything outside of `rustc_span`. --- compiler/rustc_ast_lowering/src/lib.rs | 7 +--- compiler/rustc_expand/src/base.rs | 20 +++++----- compiler/rustc_expand/src/expand.rs | 11 +++--- compiler/rustc_mir/src/transform/inline.rs | 8 ++-- compiler/rustc_span/src/hygiene.rs | 44 +++++++++++++++++++++- 5 files changed, 63 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index f81dc39842c..fe96e1c5c04 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -57,7 +57,7 @@ use rustc_session::lint::{builtin::BARE_TRAIT_OBJECTS, BuiltinLintDiagnostics, L use rustc_session::parse::ParseSess; use rustc_session::Session; use rustc_span::hygiene::ExpnId; -use rustc_span::source_map::{respan, DesugaringKind, ExpnData, ExpnKind}; +use rustc_span::source_map::{respan, DesugaringKind}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::Span; @@ -743,10 +743,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { span: Span, allow_internal_unstable: Option>, ) -> Span { - span.fresh_expansion(ExpnData { - allow_internal_unstable, - ..ExpnData::default(ExpnKind::Desugaring(reason), span, self.sess.edition(), None) - }) + span.mark_with_reason(allow_internal_unstable, reason, self.sess.edition()) } fn with_anonymous_lifetime_mode( diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 774a0764d11..887e12b46f6 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -12,7 +12,7 @@ use rustc_data_structures::sync::{self, Lrc}; use rustc_errors::{DiagnosticBuilder, ErrorReported}; use rustc_parse::{self, nt_to_tokenstream, parser, MACRO_ARGUMENTS}; use rustc_session::{parse::ParseSess, Limit, Session}; -use rustc_span::def_id::{DefId, LOCAL_CRATE}; +use rustc_span::def_id::DefId; use rustc_span::edition::Edition; use rustc_span::hygiene::{AstPass, ExpnData, ExpnId, ExpnKind}; use rustc_span::source_map::SourceMap; @@ -842,19 +842,17 @@ impl SyntaxExtension { descr: Symbol, macro_def_id: Option, ) -> ExpnData { - ExpnData { - kind: ExpnKind::Macro(self.macro_kind(), descr), + ExpnData::new( + ExpnKind::Macro(self.macro_kind(), descr), parent, call_site, - def_site: self.span, - allow_internal_unstable: self.allow_internal_unstable.clone(), - allow_internal_unsafe: self.allow_internal_unsafe, - local_inner_macros: self.local_inner_macros, - edition: self.edition, + self.span, + self.allow_internal_unstable.clone(), + self.allow_internal_unsafe, + self.local_inner_macros, + self.edition, macro_def_id, - krate: LOCAL_CRATE, - orig_id: None, - } + ) } } diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 5d40d478b96..1453627f6e1 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -1020,15 +1020,16 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { // with exception of the derive container case which is not resolved and can get // its expansion data immediately. let expn_data = match &kind { - InvocationKind::DeriveContainer { item, .. } => Some(ExpnData { - parent: self.cx.current_expansion.id, - ..ExpnData::default( + InvocationKind::DeriveContainer { item, .. } => { + let mut expn_data = ExpnData::default( ExpnKind::Macro(MacroKind::Attr, sym::derive), item.span(), self.cx.sess.parse_sess.edition, None, - ) - }), + ); + expn_data.parent = self.cx.current_expansion.id; + Some(expn_data) + } _ => None, }; let expn_id = ExpnId::fresh(expn_data); diff --git a/compiler/rustc_mir/src/transform/inline.rs b/compiler/rustc_mir/src/transform/inline.rs index 6e7575c1d71..df2f69df520 100644 --- a/compiler/rustc_mir/src/transform/inline.rs +++ b/compiler/rustc_mir/src/transform/inline.rs @@ -742,11 +742,11 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> { } fn visit_span(&mut self, span: &mut Span) { + let mut expn_data = + ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None); + expn_data.def_site = self.body_span; // Make sure that all spans track the fact that they were inlined. - *span = self.callsite_span.fresh_expansion(ExpnData { - def_site: self.body_span, - ..ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None) - }); + *span = self.callsite_span.fresh_expansion(expn_data); } fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) { diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index fdc0d225bb8..27dc9e9ecea 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -650,6 +650,20 @@ impl Span { self.with_ctxt(data.apply_mark(SyntaxContext::root(), expn_id, transparency)) }) } + + /// Reuses the span but adds information like the kind of the desugaring and features that are + /// allowed inside this span. + pub fn mark_with_reason( + self, + allow_internal_unstable: Option>, + reason: DesugaringKind, + edition: Edition, + ) -> Span { + self.fresh_expansion(ExpnData { + allow_internal_unstable, + ..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None) + }) + } } /// A subset of properties from both macro definition and macro call available through global data. @@ -699,7 +713,7 @@ pub struct ExpnData { /// created locally - when our serialized metadata is decoded, /// foreign `ExpnId`s will have their `ExpnData` looked up /// from the crate specified by `Crate - pub krate: CrateNum, + krate: CrateNum, /// The raw that this `ExpnData` had in its original crate. /// An `ExpnData` can be created before being assigned an `ExpnId`, /// so this might be `None` until `set_expn_data` is called @@ -707,13 +721,39 @@ pub struct ExpnData { // two `ExpnData`s that differ only in their `orig_id` should // be considered equivalent. #[stable_hasher(ignore)] - pub orig_id: Option, + orig_id: Option, } // This would require special handling of `orig_id` and `parent` impl !PartialEq for ExpnData {} impl ExpnData { + pub fn new( + kind: ExpnKind, + parent: ExpnId, + call_site: Span, + def_site: Span, + allow_internal_unstable: Option>, + allow_internal_unsafe: bool, + local_inner_macros: bool, + edition: Edition, + macro_def_id: Option, + ) -> ExpnData { + ExpnData { + kind, + parent, + call_site, + def_site, + allow_internal_unstable, + allow_internal_unsafe, + local_inner_macros, + edition, + macro_def_id, + krate: LOCAL_CRATE, + orig_id: None, + } + } + /// Constructs expansion data with default properties. pub fn default( kind: ExpnKind,