2018-04-15 14:17:18 +00:00
|
|
|
//! Machinery for hygienic macros, inspired by the `MTWT[1]` paper.
|
2014-02-24 20:47:19 +00:00
|
|
|
//!
|
2018-04-15 14:17:18 +00:00
|
|
|
//! `[1]` Matthew Flatt, Ryan Culpepper, David Darais, and Robert Bruce Findler. 2012.
|
2017-12-31 16:17:01 +00:00
|
|
|
//! *Macros that work together: Compile-time bindings, partial expansion,
|
2014-02-24 20:47:19 +00:00
|
|
|
//! and definition contexts*. J. Funct. Program. 22, 2 (March 2012), 181-216.
|
2018-05-14 05:17:56 +00:00
|
|
|
//! DOI=10.1017/S0956796812000093 <https://doi.org/10.1017/S0956796812000093>
|
2014-02-24 20:47:19 +00:00
|
|
|
|
2019-06-03 03:08:15 +00:00
|
|
|
// Hygiene data is stored in a global variable and accessed via TLS, which
|
|
|
|
// means that accesses are somewhat expensive. (`HygieneData::with`
|
|
|
|
// encapsulates a single access.) Therefore, on hot code paths it is worth
|
|
|
|
// ensuring that multiple HygieneData accesses are combined into a single
|
|
|
|
// `HygieneData::with`.
|
|
|
|
//
|
|
|
|
// This explains why `HygieneData`, `SyntaxContext` and `Mark` have interfaces
|
|
|
|
// with a certain amount of redundancy in them. For example,
|
|
|
|
// `SyntaxContext::outer_expn_info` combines `SyntaxContext::outer` and
|
|
|
|
// `Mark::expn_info` so that two `HygieneData` accesses can be performed within
|
|
|
|
// a single `HygieneData::with` call.
|
|
|
|
//
|
|
|
|
// It also explains why many functions appear in `HygieneData` and again in
|
|
|
|
// `SyntaxContext` or `Mark`. For example, `HygieneData::outer` and
|
|
|
|
// `SyntaxContext::outer` do the same thing, but the former is for use within a
|
|
|
|
// `HygieneData::with` call while the latter is for use outside such a call.
|
|
|
|
// When modifying this file it is important to understand this distinction,
|
|
|
|
// because getting it wrong can lead to nested `HygieneData::with` calls that
|
|
|
|
// trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
|
|
|
|
|
2019-02-03 18:42:27 +00:00
|
|
|
use crate::GLOBALS;
|
2019-06-30 00:05:52 +00:00
|
|
|
use crate::{Span, DUMMY_SP};
|
2019-04-05 22:15:49 +00:00
|
|
|
use crate::edition::Edition;
|
2019-05-11 14:41:37 +00:00
|
|
|
use crate::symbol::{kw, Symbol};
|
2017-03-17 04:04:41 +00:00
|
|
|
|
|
|
|
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
2019-06-20 07:34:51 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2019-02-08 09:21:21 +00:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2019-07-05 00:09:24 +00:00
|
|
|
use std::fmt;
|
2014-02-24 20:47:19 +00:00
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
/// A SyntaxContext represents a chain of macro expansions (represented by marks).
|
2019-06-30 11:57:34 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2018-06-30 16:35:00 +00:00
|
|
|
pub struct SyntaxContext(u32);
|
2014-02-24 20:47:19 +00:00
|
|
|
|
2019-06-30 11:57:34 +00:00
|
|
|
#[derive(Debug)]
|
2018-06-30 16:35:00 +00:00
|
|
|
struct SyntaxContextData {
|
|
|
|
outer_mark: Mark,
|
2018-06-30 16:53:46 +00:00
|
|
|
transparency: Transparency,
|
2018-06-30 16:35:00 +00:00
|
|
|
prev_ctxt: SyntaxContext,
|
2019-04-22 18:29:11 +00:00
|
|
|
/// This context, but with all transparent and semi-transparent marks filtered away.
|
2018-06-30 16:35:00 +00:00
|
|
|
opaque: SyntaxContext,
|
2019-04-22 18:29:11 +00:00
|
|
|
/// This context, but with all transparent marks filtered away.
|
2018-06-30 16:35:00 +00:00
|
|
|
opaque_and_semitransparent: SyntaxContext,
|
2019-04-22 18:29:11 +00:00
|
|
|
/// Name of the crate to which `$crate` with this context would resolve.
|
2018-12-09 14:46:12 +00:00
|
|
|
dollar_crate_name: Symbol,
|
2014-02-24 20:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// A mark is a unique ID associated with a macro expansion.
|
2019-06-30 11:57:34 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
|
2016-06-22 08:03:42 +00:00
|
|
|
pub struct Mark(u32);
|
2014-02-24 20:47:19 +00:00
|
|
|
|
2019-06-30 11:57:34 +00:00
|
|
|
#[derive(Debug)]
|
2017-03-22 08:39:51 +00:00
|
|
|
struct MarkData {
|
|
|
|
parent: Mark,
|
|
|
|
expn_info: Option<ExpnInfo>,
|
|
|
|
}
|
|
|
|
|
2018-06-19 21:54:17 +00:00
|
|
|
/// A property of a macro expansion that determines how identifiers
|
|
|
|
/// produced by that expansion are resolved.
|
2019-06-17 20:55:22 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Hash, Debug, RustcEncodable, RustcDecodable)]
|
2018-06-19 21:54:17 +00:00
|
|
|
pub enum Transparency {
|
|
|
|
/// Identifier produced by a transparent expansion is always resolved at call-site.
|
|
|
|
/// Call-site spans in procedural macros, hygiene opt-out in `macro` should use this.
|
|
|
|
Transparent,
|
|
|
|
/// Identifier produced by a semi-transparent expansion may be resolved
|
|
|
|
/// either at call-site or at definition-site.
|
|
|
|
/// If it's a local variable, label or `$crate` then it's resolved at def-site.
|
|
|
|
/// Otherwise it's resolved at call-site.
|
|
|
|
/// `macro_rules` macros behave like this, built-in macros currently behave like this too,
|
|
|
|
/// but that's an implementation detail.
|
|
|
|
SemiTransparent,
|
|
|
|
/// Identifier produced by an opaque expansion is always resolved at definition-site.
|
|
|
|
/// Def-site spans in procedural macros, identifiers from `macro` by default use this.
|
|
|
|
Opaque,
|
2017-12-12 09:14:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
impl Mark {
|
2017-03-22 08:39:51 +00:00
|
|
|
pub fn fresh(parent: Mark) -> Self {
|
2016-06-22 08:03:42 +00:00
|
|
|
HygieneData::with(|data| {
|
2019-06-17 20:55:22 +00:00
|
|
|
data.marks.push(MarkData { parent, expn_info: None });
|
2018-07-07 20:07:06 +00:00
|
|
|
Mark(data.marks.len() as u32 - 1)
|
2016-06-22 08:03:42 +00:00
|
|
|
})
|
2016-06-20 10:28:32 +00:00
|
|
|
}
|
2016-09-02 09:12:47 +00:00
|
|
|
|
2016-09-07 23:21:59 +00:00
|
|
|
/// The mark of the theoretical expansion that generates freshly parsed, unexpanded AST.
|
2017-12-07 15:46:31 +00:00
|
|
|
#[inline]
|
2016-09-07 23:21:59 +00:00
|
|
|
pub fn root() -> Self {
|
|
|
|
Mark(0)
|
|
|
|
}
|
|
|
|
|
2017-12-07 15:46:31 +00:00
|
|
|
#[inline]
|
2016-12-01 22:49:32 +00:00
|
|
|
pub fn as_u32(self) -> u32 {
|
2016-09-02 09:12:47 +00:00
|
|
|
self.0
|
|
|
|
}
|
2017-03-16 10:23:33 +00:00
|
|
|
|
2017-12-07 15:46:31 +00:00
|
|
|
#[inline]
|
2017-03-16 10:23:33 +00:00
|
|
|
pub fn from_u32(raw: u32) -> Mark {
|
|
|
|
Mark(raw)
|
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
2018-08-28 01:07:31 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn parent(self) -> Mark {
|
|
|
|
HygieneData::with(|data| data.marks[self.0 as usize].parent)
|
|
|
|
}
|
|
|
|
|
2017-12-07 15:46:31 +00:00
|
|
|
#[inline]
|
2017-03-17 04:04:41 +00:00
|
|
|
pub fn expn_info(self) -> Option<ExpnInfo> {
|
2019-06-17 22:36:44 +00:00
|
|
|
HygieneData::with(|data| data.expn_info(self).cloned())
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 15:46:31 +00:00
|
|
|
#[inline]
|
2017-03-17 04:04:41 +00:00
|
|
|
pub fn set_expn_info(self, info: ExpnInfo) {
|
2018-08-23 23:21:52 +00:00
|
|
|
HygieneData::with(|data| data.marks[self.0 as usize].expn_info = Some(info))
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 22:59:22 +00:00
|
|
|
pub fn is_descendant_of(self, ancestor: Mark) -> bool {
|
|
|
|
HygieneData::with(|data| data.is_descendant_of(self, ancestor))
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
2018-04-21 22:20:17 +00:00
|
|
|
|
2019-05-27 03:38:23 +00:00
|
|
|
/// `mark.outer_is_descendant_of(ctxt)` is equivalent to but faster than
|
|
|
|
/// `mark.is_descendant_of(ctxt.outer())`.
|
2019-05-29 22:59:22 +00:00
|
|
|
pub fn outer_is_descendant_of(self, ctxt: SyntaxContext) -> bool {
|
|
|
|
HygieneData::with(|data| data.is_descendant_of(self, data.outer(ctxt)))
|
2019-05-27 03:38:23 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 20:07:06 +00:00
|
|
|
// Used for enabling some compatibility fallback in resolve.
|
|
|
|
#[inline]
|
|
|
|
pub fn looks_like_proc_macro_derive(self) -> bool {
|
|
|
|
HygieneData::with(|data| {
|
2019-06-17 20:55:22 +00:00
|
|
|
if data.default_transparency(self) == Transparency::Opaque {
|
2019-06-30 12:58:56 +00:00
|
|
|
if let Some(expn_info) = data.expn_info(self) {
|
|
|
|
if let ExpnKind::Macro(MacroKind::Derive, _) = expn_info.kind {
|
|
|
|
return true;
|
2018-07-07 20:07:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
})
|
|
|
|
}
|
2014-02-24 20:47:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 21:08:14 +00:00
|
|
|
#[derive(Debug)]
|
2018-06-30 16:35:00 +00:00
|
|
|
crate struct HygieneData {
|
2017-03-22 08:39:51 +00:00
|
|
|
marks: Vec<MarkData>,
|
2016-06-22 08:03:42 +00:00
|
|
|
syntax_contexts: Vec<SyntaxContextData>,
|
2018-08-18 10:55:43 +00:00
|
|
|
markings: FxHashMap<(SyntaxContext, Mark, Transparency), SyntaxContext>,
|
2014-02-24 20:47:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
impl HygieneData {
|
2018-06-30 16:35:00 +00:00
|
|
|
crate fn new() -> Self {
|
2016-06-22 08:03:42 +00:00
|
|
|
HygieneData {
|
2017-12-12 09:14:45 +00:00
|
|
|
marks: vec![MarkData {
|
|
|
|
parent: Mark::root(),
|
|
|
|
expn_info: None,
|
|
|
|
}],
|
|
|
|
syntax_contexts: vec![SyntaxContextData {
|
|
|
|
outer_mark: Mark::root(),
|
2018-06-30 16:53:46 +00:00
|
|
|
transparency: Transparency::Opaque,
|
2017-12-12 09:14:45 +00:00
|
|
|
prev_ctxt: SyntaxContext(0),
|
2018-06-24 16:54:23 +00:00
|
|
|
opaque: SyntaxContext(0),
|
|
|
|
opaque_and_semitransparent: SyntaxContext(0),
|
2019-05-11 14:41:37 +00:00
|
|
|
dollar_crate_name: kw::DollarCrate,
|
2017-12-12 09:14:45 +00:00
|
|
|
}],
|
2018-08-18 10:55:43 +00:00
|
|
|
markings: FxHashMap::default(),
|
2016-06-22 08:03:42 +00:00
|
|
|
}
|
2014-02-24 20:47:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
|
2018-03-07 01:44:10 +00:00
|
|
|
GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut()))
|
2016-06-22 08:03:42 +00:00
|
|
|
}
|
2019-05-29 22:59:22 +00:00
|
|
|
|
2019-06-17 22:36:44 +00:00
|
|
|
fn expn_info(&self, mark: Mark) -> Option<&ExpnInfo> {
|
|
|
|
self.marks[mark.0 as usize].expn_info.as_ref()
|
2019-05-29 22:59:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn is_descendant_of(&self, mut mark: Mark, ancestor: Mark) -> bool {
|
|
|
|
while mark != ancestor {
|
|
|
|
if mark == Mark::root() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mark = self.marks[mark.0 as usize].parent;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2019-05-31 21:19:58 +00:00
|
|
|
|
2019-05-31 21:24:51 +00:00
|
|
|
fn default_transparency(&self, mark: Mark) -> Transparency {
|
2019-06-30 12:58:56 +00:00
|
|
|
self.expn_info(mark).map_or(
|
2019-06-17 20:55:22 +00:00
|
|
|
Transparency::SemiTransparent, |einfo| einfo.default_transparency
|
|
|
|
)
|
2019-05-31 21:24:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-31 21:19:58 +00:00
|
|
|
fn modern(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
|
|
|
self.syntax_contexts[ctxt.0 as usize].opaque
|
|
|
|
}
|
|
|
|
|
|
|
|
fn modern_and_legacy(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
|
|
|
self.syntax_contexts[ctxt.0 as usize].opaque_and_semitransparent
|
|
|
|
}
|
|
|
|
|
|
|
|
fn outer(&self, ctxt: SyntaxContext) -> Mark {
|
|
|
|
self.syntax_contexts[ctxt.0 as usize].outer_mark
|
|
|
|
}
|
|
|
|
|
|
|
|
fn transparency(&self, ctxt: SyntaxContext) -> Transparency {
|
|
|
|
self.syntax_contexts[ctxt.0 as usize].transparency
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prev_ctxt(&self, ctxt: SyntaxContext) -> SyntaxContext {
|
|
|
|
self.syntax_contexts[ctxt.0 as usize].prev_ctxt
|
|
|
|
}
|
2019-05-31 21:28:15 +00:00
|
|
|
|
|
|
|
fn remove_mark(&self, ctxt: &mut SyntaxContext) -> Mark {
|
|
|
|
let outer_mark = self.syntax_contexts[ctxt.0 as usize].outer_mark;
|
|
|
|
*ctxt = self.prev_ctxt(*ctxt);
|
|
|
|
outer_mark
|
|
|
|
}
|
2019-05-31 21:44:14 +00:00
|
|
|
|
2019-06-01 22:27:36 +00:00
|
|
|
fn marks(&self, mut ctxt: SyntaxContext) -> Vec<(Mark, Transparency)> {
|
|
|
|
let mut marks = Vec::new();
|
|
|
|
while ctxt != SyntaxContext::empty() {
|
|
|
|
let outer_mark = self.outer(ctxt);
|
|
|
|
let transparency = self.transparency(ctxt);
|
|
|
|
let prev_ctxt = self.prev_ctxt(ctxt);
|
|
|
|
marks.push((outer_mark, transparency));
|
|
|
|
ctxt = prev_ctxt;
|
|
|
|
}
|
|
|
|
marks.reverse();
|
|
|
|
marks
|
|
|
|
}
|
|
|
|
|
2019-06-03 02:07:51 +00:00
|
|
|
fn walk_chain(&self, mut span: Span, to: SyntaxContext) -> Span {
|
|
|
|
while span.ctxt() != crate::NO_EXPANSION && span.ctxt() != to {
|
|
|
|
if let Some(info) = self.expn_info(self.outer(span.ctxt())) {
|
|
|
|
span = info.call_site;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
span
|
|
|
|
}
|
|
|
|
|
2019-05-31 21:44:14 +00:00
|
|
|
fn adjust(&self, ctxt: &mut SyntaxContext, expansion: Mark) -> Option<Mark> {
|
|
|
|
let mut scope = None;
|
|
|
|
while !self.is_descendant_of(expansion, self.outer(*ctxt)) {
|
|
|
|
scope = Some(self.remove_mark(ctxt));
|
|
|
|
}
|
|
|
|
scope
|
|
|
|
}
|
2019-06-01 22:16:46 +00:00
|
|
|
|
2019-06-01 22:35:37 +00:00
|
|
|
fn apply_mark(&mut self, ctxt: SyntaxContext, mark: Mark) -> SyntaxContext {
|
|
|
|
assert_ne!(mark, Mark::root());
|
|
|
|
self.apply_mark_with_transparency(ctxt, mark, self.default_transparency(mark))
|
|
|
|
}
|
|
|
|
|
2019-06-01 22:23:54 +00:00
|
|
|
fn apply_mark_with_transparency(&mut self, ctxt: SyntaxContext, mark: Mark,
|
|
|
|
transparency: Transparency) -> SyntaxContext {
|
|
|
|
assert_ne!(mark, Mark::root());
|
|
|
|
if transparency == Transparency::Opaque {
|
|
|
|
return self.apply_mark_internal(ctxt, mark, transparency);
|
|
|
|
}
|
|
|
|
|
|
|
|
let call_site_ctxt =
|
|
|
|
self.expn_info(mark).map_or(SyntaxContext::empty(), |info| info.call_site.ctxt());
|
|
|
|
let mut call_site_ctxt = if transparency == Transparency::SemiTransparent {
|
|
|
|
self.modern(call_site_ctxt)
|
|
|
|
} else {
|
|
|
|
self.modern_and_legacy(call_site_ctxt)
|
|
|
|
};
|
|
|
|
|
|
|
|
if call_site_ctxt == SyntaxContext::empty() {
|
|
|
|
return self.apply_mark_internal(ctxt, mark, transparency);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, `mark` is a macros 1.0 definition and the call site is in a
|
|
|
|
// macros 2.0 expansion, i.e., a macros 1.0 invocation is in a macros 2.0 definition.
|
|
|
|
//
|
|
|
|
// In this case, the tokens from the macros 1.0 definition inherit the hygiene
|
|
|
|
// at their invocation. That is, we pretend that the macros 1.0 definition
|
|
|
|
// was defined at its invocation (i.e., inside the macros 2.0 definition)
|
|
|
|
// so that the macros 2.0 definition remains hygienic.
|
|
|
|
//
|
|
|
|
// See the example at `test/run-pass/hygiene/legacy_interaction.rs`.
|
|
|
|
for (mark, transparency) in self.marks(ctxt) {
|
|
|
|
call_site_ctxt = self.apply_mark_internal(call_site_ctxt, mark, transparency);
|
|
|
|
}
|
|
|
|
self.apply_mark_internal(call_site_ctxt, mark, transparency)
|
|
|
|
}
|
|
|
|
|
2019-06-01 22:16:46 +00:00
|
|
|
fn apply_mark_internal(&mut self, ctxt: SyntaxContext, mark: Mark, transparency: Transparency)
|
|
|
|
-> SyntaxContext {
|
|
|
|
let syntax_contexts = &mut self.syntax_contexts;
|
|
|
|
let mut opaque = syntax_contexts[ctxt.0 as usize].opaque;
|
|
|
|
let mut opaque_and_semitransparent =
|
|
|
|
syntax_contexts[ctxt.0 as usize].opaque_and_semitransparent;
|
|
|
|
|
|
|
|
if transparency >= Transparency::Opaque {
|
|
|
|
let prev_ctxt = opaque;
|
|
|
|
opaque = *self.markings.entry((prev_ctxt, mark, transparency)).or_insert_with(|| {
|
|
|
|
let new_opaque = SyntaxContext(syntax_contexts.len() as u32);
|
|
|
|
syntax_contexts.push(SyntaxContextData {
|
|
|
|
outer_mark: mark,
|
|
|
|
transparency,
|
|
|
|
prev_ctxt,
|
|
|
|
opaque: new_opaque,
|
|
|
|
opaque_and_semitransparent: new_opaque,
|
|
|
|
dollar_crate_name: kw::DollarCrate,
|
|
|
|
});
|
|
|
|
new_opaque
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if transparency >= Transparency::SemiTransparent {
|
|
|
|
let prev_ctxt = opaque_and_semitransparent;
|
|
|
|
opaque_and_semitransparent =
|
|
|
|
*self.markings.entry((prev_ctxt, mark, transparency)).or_insert_with(|| {
|
|
|
|
let new_opaque_and_semitransparent =
|
|
|
|
SyntaxContext(syntax_contexts.len() as u32);
|
|
|
|
syntax_contexts.push(SyntaxContextData {
|
|
|
|
outer_mark: mark,
|
|
|
|
transparency,
|
|
|
|
prev_ctxt,
|
|
|
|
opaque,
|
|
|
|
opaque_and_semitransparent: new_opaque_and_semitransparent,
|
|
|
|
dollar_crate_name: kw::DollarCrate,
|
|
|
|
});
|
|
|
|
new_opaque_and_semitransparent
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let prev_ctxt = ctxt;
|
|
|
|
*self.markings.entry((prev_ctxt, mark, transparency)).or_insert_with(|| {
|
|
|
|
let new_opaque_and_semitransparent_and_transparent =
|
|
|
|
SyntaxContext(syntax_contexts.len() as u32);
|
|
|
|
syntax_contexts.push(SyntaxContextData {
|
|
|
|
outer_mark: mark,
|
|
|
|
transparency,
|
|
|
|
prev_ctxt,
|
|
|
|
opaque,
|
|
|
|
opaque_and_semitransparent,
|
|
|
|
dollar_crate_name: kw::DollarCrate,
|
|
|
|
});
|
|
|
|
new_opaque_and_semitransparent_and_transparent
|
|
|
|
})
|
|
|
|
}
|
2014-03-08 22:18:58 +00:00
|
|
|
}
|
2014-02-24 20:47:19 +00:00
|
|
|
|
2017-03-17 04:04:41 +00:00
|
|
|
pub fn clear_markings() {
|
2018-08-18 10:55:43 +00:00
|
|
|
HygieneData::with(|data| data.markings = FxHashMap::default());
|
2014-11-29 04:56:09 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 02:07:51 +00:00
|
|
|
pub fn walk_chain(span: Span, to: SyntaxContext) -> Span {
|
|
|
|
HygieneData::with(|data| data.walk_chain(span, to))
|
|
|
|
}
|
|
|
|
|
2019-07-05 00:09:24 +00:00
|
|
|
pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symbol) {
|
|
|
|
// The new contexts that need updating are at the end of the list and have `$crate` as a name.
|
|
|
|
let (len, to_update) = HygieneData::with(|data| (
|
|
|
|
data.syntax_contexts.len(),
|
|
|
|
data.syntax_contexts.iter().rev()
|
|
|
|
.take_while(|scdata| scdata.dollar_crate_name == kw::DollarCrate).count()
|
|
|
|
));
|
|
|
|
// The callback must be called from outside of the `HygieneData` lock,
|
|
|
|
// since it will try to acquire it too.
|
|
|
|
let range_to_update = len - to_update .. len;
|
|
|
|
let names: Vec<_> =
|
|
|
|
range_to_update.clone().map(|idx| get_name(SyntaxContext::from_u32(idx as u32))).collect();
|
|
|
|
HygieneData::with(|data| range_to_update.zip(names.into_iter()).for_each(|(idx, name)| {
|
|
|
|
data.syntax_contexts[idx].dollar_crate_name = name;
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
impl SyntaxContext {
|
Increase `Span` from 4 bytes to 8 bytes.
This increases the size of some important types, such as `ast::Expr` and
`mir::Statement`. However, it drastically reduces how much the interner
is used, and the fields are more natural sizes that don't require bit
operations to extract.
As a result, instruction counts drop across a range of workloads, by as
much as 12% for incremental "check" builds of `script-servo`.
Peak memory usage goes up a little for some cases, but down by more for
some other cases -- as much as 18% for non-incremental builds of
`packed-simd`.
The commit also:
- removes the `repr(packed)`, because it has negligible effect, but can
cause undefined behaviour;
- replaces explicit impls of common traits (`Copy`, `PartialEq`, etc.)
with derived ones.
2019-04-04 04:46:33 +00:00
|
|
|
#[inline]
|
2016-06-22 08:03:42 +00:00
|
|
|
pub const fn empty() -> Self {
|
|
|
|
SyntaxContext(0)
|
|
|
|
}
|
|
|
|
|
Increase `Span` from 4 bytes to 8 bytes.
This increases the size of some important types, such as `ast::Expr` and
`mir::Statement`. However, it drastically reduces how much the interner
is used, and the fields are more natural sizes that don't require bit
operations to extract.
As a result, instruction counts drop across a range of workloads, by as
much as 12% for incremental "check" builds of `script-servo`.
Peak memory usage goes up a little for some cases, but down by more for
some other cases -- as much as 18% for non-incremental builds of
`packed-simd`.
The commit also:
- removes the `repr(packed)`, because it has negligible effect, but can
cause undefined behaviour;
- replaces explicit impls of common traits (`Copy`, `PartialEq`, etc.)
with derived ones.
2019-04-04 04:46:33 +00:00
|
|
|
#[inline]
|
2018-06-30 16:35:00 +00:00
|
|
|
crate fn as_u32(self) -> u32 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
|
Increase `Span` from 4 bytes to 8 bytes.
This increases the size of some important types, such as `ast::Expr` and
`mir::Statement`. However, it drastically reduces how much the interner
is used, and the fields are more natural sizes that don't require bit
operations to extract.
As a result, instruction counts drop across a range of workloads, by as
much as 12% for incremental "check" builds of `script-servo`.
Peak memory usage goes up a little for some cases, but down by more for
some other cases -- as much as 18% for non-incremental builds of
`packed-simd`.
The commit also:
- removes the `repr(packed)`, because it has negligible effect, but can
cause undefined behaviour;
- replaces explicit impls of common traits (`Copy`, `PartialEq`, etc.)
with derived ones.
2019-04-04 04:46:33 +00:00
|
|
|
#[inline]
|
2018-06-30 16:35:00 +00:00
|
|
|
crate fn from_u32(raw: u32) -> SyntaxContext {
|
|
|
|
SyntaxContext(raw)
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:41:27 +00:00
|
|
|
// Allocate a new SyntaxContext with the given ExpnInfo. This is used when
|
|
|
|
// deserializing Spans from the incr. comp. cache.
|
|
|
|
// FIXME(mw): This method does not restore MarkData::parent or
|
2018-06-24 16:54:23 +00:00
|
|
|
// SyntaxContextData::prev_ctxt or SyntaxContextData::opaque. These things
|
2017-11-22 12:41:27 +00:00
|
|
|
// don't seem to be used after HIR lowering, so everything should be fine
|
|
|
|
// as long as incremental compilation does not kick in before that.
|
|
|
|
pub fn allocate_directly(expansion_info: ExpnInfo) -> Self {
|
|
|
|
HygieneData::with(|data| {
|
|
|
|
data.marks.push(MarkData {
|
|
|
|
parent: Mark::root(),
|
2018-06-19 21:54:17 +00:00
|
|
|
expn_info: Some(expansion_info),
|
2017-11-22 12:41:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let mark = Mark(data.marks.len() as u32 - 1);
|
|
|
|
|
|
|
|
data.syntax_contexts.push(SyntaxContextData {
|
|
|
|
outer_mark: mark,
|
2018-06-30 16:53:46 +00:00
|
|
|
transparency: Transparency::SemiTransparent,
|
2017-11-22 12:41:27 +00:00
|
|
|
prev_ctxt: SyntaxContext::empty(),
|
2018-06-24 16:54:23 +00:00
|
|
|
opaque: SyntaxContext::empty(),
|
|
|
|
opaque_and_semitransparent: SyntaxContext::empty(),
|
2019-05-11 14:41:37 +00:00
|
|
|
dollar_crate_name: kw::DollarCrate,
|
2017-11-22 12:41:27 +00:00
|
|
|
});
|
|
|
|
SyntaxContext(data.syntax_contexts.len() as u32 - 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:07:06 +00:00
|
|
|
/// Extend a syntax context with a given mark and default transparency for that mark.
|
2016-06-22 08:03:42 +00:00
|
|
|
pub fn apply_mark(self, mark: Mark) -> SyntaxContext {
|
2019-06-01 22:35:37 +00:00
|
|
|
HygieneData::with(|data| data.apply_mark(self, mark))
|
2018-06-30 16:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Extend a syntax context with a given mark and transparency
|
|
|
|
pub fn apply_mark_with_transparency(self, mark: Mark, transparency: Transparency)
|
|
|
|
-> SyntaxContext {
|
2019-06-01 22:23:54 +00:00
|
|
|
HygieneData::with(|data| data.apply_mark_with_transparency(self, mark, transparency))
|
2016-06-22 08:03:42 +00:00
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
2018-04-21 22:00:09 +00:00
|
|
|
/// Pulls a single mark off of the syntax context. This effectively moves the
|
|
|
|
/// context up one macro definition level. That is, if we have a nested macro
|
|
|
|
/// definition as follows:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// macro_rules! f {
|
|
|
|
/// macro_rules! g {
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// and we have a SyntaxContext that is referring to something declared by an invocation
|
|
|
|
/// of g (call it g1), calling remove_mark will result in the SyntaxContext for the
|
|
|
|
/// invocation of f that created g1.
|
|
|
|
/// Returns the mark that was removed.
|
2017-03-22 08:39:51 +00:00
|
|
|
pub fn remove_mark(&mut self) -> Mark {
|
2019-05-31 21:28:15 +00:00
|
|
|
HygieneData::with(|data| data.remove_mark(self))
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 22:27:36 +00:00
|
|
|
pub fn marks(self) -> Vec<(Mark, Transparency)> {
|
|
|
|
HygieneData::with(|data| data.marks(self))
|
2017-11-29 09:05:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
/// Adjust this context for resolution in a scope created by the given expansion.
|
|
|
|
/// For example, consider the following three resolutions of `f`:
|
2017-12-31 16:17:01 +00:00
|
|
|
///
|
2017-03-22 08:39:51 +00:00
|
|
|
/// ```rust
|
|
|
|
/// mod foo { pub fn f() {} } // `f`'s `SyntaxContext` is empty.
|
|
|
|
/// m!(f);
|
|
|
|
/// macro m($f:ident) {
|
|
|
|
/// mod bar {
|
|
|
|
/// pub fn f() {} // `f`'s `SyntaxContext` has a single `Mark` from `m`.
|
|
|
|
/// pub fn $f() {} // `$f`'s `SyntaxContext` is empty.
|
|
|
|
/// }
|
|
|
|
/// foo::f(); // `f`'s `SyntaxContext` has a single `Mark` from `m`
|
|
|
|
/// //^ Since `mod foo` is outside this expansion, `adjust` removes the mark from `f`,
|
|
|
|
/// //| and it resolves to `::foo::f`.
|
|
|
|
/// bar::f(); // `f`'s `SyntaxContext` has a single `Mark` from `m`
|
|
|
|
/// //^ Since `mod bar` not outside this expansion, `adjust` does not change `f`,
|
|
|
|
/// //| and it resolves to `::bar::f`.
|
|
|
|
/// bar::$f(); // `f`'s `SyntaxContext` is empty.
|
|
|
|
/// //^ Since `mod bar` is not outside this expansion, `adjust` does not change `$f`,
|
|
|
|
/// //| and it resolves to `::bar::$f`.
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// This returns the expansion whose definition scope we use to privacy check the resolution,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// or `None` if we privacy check as usual (i.e., not w.r.t. a macro definition scope).
|
2017-03-22 08:39:51 +00:00
|
|
|
pub fn adjust(&mut self, expansion: Mark) -> Option<Mark> {
|
2019-05-31 21:44:14 +00:00
|
|
|
HygieneData::with(|data| data.adjust(self, expansion))
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 06:10:03 +00:00
|
|
|
/// Like `SyntaxContext::adjust`, but also modernizes `self`.
|
|
|
|
pub fn modernize_and_adjust(&mut self, expansion: Mark) -> Option<Mark> {
|
|
|
|
HygieneData::with(|data| {
|
|
|
|
*self = data.modern(*self);
|
|
|
|
data.adjust(self, expansion)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-03-22 08:39:51 +00:00
|
|
|
/// Adjust this context for resolution in a scope created by the given expansion
|
|
|
|
/// via a glob import with the given `SyntaxContext`.
|
2017-12-31 16:17:01 +00:00
|
|
|
/// For example:
|
|
|
|
///
|
2017-03-22 08:39:51 +00:00
|
|
|
/// ```rust
|
|
|
|
/// m!(f);
|
|
|
|
/// macro m($i:ident) {
|
|
|
|
/// mod foo {
|
|
|
|
/// pub fn f() {} // `f`'s `SyntaxContext` has a single `Mark` from `m`.
|
|
|
|
/// pub fn $i() {} // `$i`'s `SyntaxContext` is empty.
|
|
|
|
/// }
|
|
|
|
/// n(f);
|
|
|
|
/// macro n($j:ident) {
|
|
|
|
/// use foo::*;
|
|
|
|
/// f(); // `f`'s `SyntaxContext` has a mark from `m` and a mark from `n`
|
|
|
|
/// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::f`.
|
|
|
|
/// $i(); // `$i`'s `SyntaxContext` has a mark from `n`
|
|
|
|
/// //^ `glob_adjust` removes the mark from `n`, so this resolves to `foo::$i`.
|
|
|
|
/// $j(); // `$j`'s `SyntaxContext` has a mark from `m`
|
|
|
|
/// //^ This cannot be glob-adjusted, so this is a resolution error.
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// This returns `None` if the context cannot be glob-adjusted.
|
|
|
|
/// Otherwise, it returns the scope to use when privacy checking (see `adjust` for details).
|
2019-05-31 06:50:44 +00:00
|
|
|
pub fn glob_adjust(&mut self, expansion: Mark, glob_span: Span) -> Option<Option<Mark>> {
|
2019-05-31 22:35:01 +00:00
|
|
|
HygieneData::with(|data| {
|
|
|
|
let mut scope = None;
|
|
|
|
let mut glob_ctxt = data.modern(glob_span.ctxt());
|
|
|
|
while !data.is_descendant_of(expansion, data.outer(glob_ctxt)) {
|
|
|
|
scope = Some(data.remove_mark(&mut glob_ctxt));
|
|
|
|
if data.remove_mark(self) != scope.unwrap() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if data.adjust(self, expansion).is_some() {
|
2017-03-22 08:39:51 +00:00
|
|
|
return None;
|
|
|
|
}
|
2019-05-31 22:35:01 +00:00
|
|
|
Some(scope)
|
|
|
|
})
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Undo `glob_adjust` if possible:
|
2017-12-31 16:17:01 +00:00
|
|
|
///
|
2017-03-22 08:39:51 +00:00
|
|
|
/// ```rust
|
|
|
|
/// if let Some(privacy_checking_scope) = self.reverse_glob_adjust(expansion, glob_ctxt) {
|
|
|
|
/// assert!(self.glob_adjust(expansion, glob_ctxt) == Some(privacy_checking_scope));
|
|
|
|
/// }
|
|
|
|
/// ```
|
2019-05-31 06:50:44 +00:00
|
|
|
pub fn reverse_glob_adjust(&mut self, expansion: Mark, glob_span: Span)
|
2017-03-22 08:39:51 +00:00
|
|
|
-> Option<Option<Mark>> {
|
2019-05-31 22:35:01 +00:00
|
|
|
HygieneData::with(|data| {
|
|
|
|
if data.adjust(self, expansion).is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
2019-05-31 22:35:01 +00:00
|
|
|
let mut glob_ctxt = data.modern(glob_span.ctxt());
|
|
|
|
let mut marks = Vec::new();
|
|
|
|
while !data.is_descendant_of(expansion, data.outer(glob_ctxt)) {
|
|
|
|
marks.push(data.remove_mark(&mut glob_ctxt));
|
|
|
|
}
|
2017-03-22 08:39:51 +00:00
|
|
|
|
2019-05-31 22:35:01 +00:00
|
|
|
let scope = marks.last().cloned();
|
|
|
|
while let Some(mark) = marks.pop() {
|
|
|
|
*self = data.apply_mark(*self, mark);
|
|
|
|
}
|
|
|
|
Some(scope)
|
|
|
|
})
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 23:43:20 +00:00
|
|
|
pub fn hygienic_eq(self, other: SyntaxContext, mark: Mark) -> bool {
|
|
|
|
HygieneData::with(|data| {
|
|
|
|
let mut self_modern = data.modern(self);
|
|
|
|
data.adjust(&mut self_modern, mark);
|
|
|
|
self_modern == data.modern(other)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-07 15:46:31 +00:00
|
|
|
#[inline]
|
2017-03-22 08:39:51 +00:00
|
|
|
pub fn modern(self) -> SyntaxContext {
|
2019-05-31 21:19:58 +00:00
|
|
|
HygieneData::with(|data| data.modern(self))
|
2018-06-24 16:54:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn modern_and_legacy(self) -> SyntaxContext {
|
2019-05-31 21:19:58 +00:00
|
|
|
HygieneData::with(|data| data.modern_and_legacy(self))
|
2017-03-22 08:39:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 15:46:31 +00:00
|
|
|
#[inline]
|
2017-03-17 04:04:41 +00:00
|
|
|
pub fn outer(self) -> Mark {
|
2019-05-29 22:59:22 +00:00
|
|
|
HygieneData::with(|data| data.outer(self))
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
2018-12-09 14:46:12 +00:00
|
|
|
|
2019-05-27 03:52:11 +00:00
|
|
|
/// `ctxt.outer_expn_info()` is equivalent to but faster than
|
|
|
|
/// `ctxt.outer().expn_info()`.
|
|
|
|
#[inline]
|
|
|
|
pub fn outer_expn_info(self) -> Option<ExpnInfo> {
|
2019-06-17 22:36:44 +00:00
|
|
|
HygieneData::with(|data| data.expn_info(data.outer(self)).cloned())
|
2019-05-27 03:52:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-02 23:21:27 +00:00
|
|
|
/// `ctxt.outer_and_expn_info()` is equivalent to but faster than
|
|
|
|
/// `{ let outer = ctxt.outer(); (outer, outer.expn_info()) }`.
|
|
|
|
#[inline]
|
|
|
|
pub fn outer_and_expn_info(self) -> (Mark, Option<ExpnInfo>) {
|
|
|
|
HygieneData::with(|data| {
|
|
|
|
let outer = data.outer(self);
|
2019-06-17 22:36:44 +00:00
|
|
|
(outer, data.expn_info(outer).cloned())
|
2019-06-02 23:21:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-09 14:46:12 +00:00
|
|
|
pub fn dollar_crate_name(self) -> Symbol {
|
|
|
|
HygieneData::with(|data| data.syntax_contexts[self.0 as usize].dollar_crate_name)
|
|
|
|
}
|
2014-02-24 20:47:19 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 08:03:42 +00:00
|
|
|
impl fmt::Debug for SyntaxContext {
|
2019-02-03 18:42:27 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2016-06-22 08:03:42 +00:00
|
|
|
write!(f, "#{}", self.0)
|
|
|
|
}
|
2016-06-26 03:32:45 +00:00
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
|
2019-06-30 12:58:56 +00:00
|
|
|
/// A subset of properties from both macro definition and macro call available through global data.
|
|
|
|
/// Avoid using this if you have access to the original definition or call structures.
|
2019-06-30 11:57:34 +00:00
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2017-03-17 04:04:41 +00:00
|
|
|
pub struct ExpnInfo {
|
2019-06-17 20:55:22 +00:00
|
|
|
// --- The part unique to each expansion.
|
2017-03-17 04:04:41 +00:00
|
|
|
/// The location of the actual macro invocation or syntax sugar , e.g.
|
|
|
|
/// `let x = foo!();` or `if let Some(y) = x {}`
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// This may recursively refer to other macro invocations, e.g., if
|
2017-03-17 04:04:41 +00:00
|
|
|
/// `foo!()` invoked `bar!()` internally, and there was an
|
|
|
|
/// expression inside `bar!`; the call_site of the expression in
|
|
|
|
/// the expansion would point to the `bar!` invocation; that
|
|
|
|
/// call_site span would have its own ExpnInfo, with the call_site
|
|
|
|
/// pointing to the `foo!` invocation.
|
|
|
|
pub call_site: Span,
|
2019-06-30 12:58:56 +00:00
|
|
|
/// The kind of this expansion - macro or compiler desugaring.
|
2019-06-18 22:08:45 +00:00
|
|
|
pub kind: ExpnKind,
|
2019-06-17 20:55:22 +00:00
|
|
|
|
|
|
|
// --- The part specific to the macro/desugaring definition.
|
|
|
|
// --- FIXME: Share it between expansions with the same definition.
|
2019-06-30 00:05:52 +00:00
|
|
|
/// The span of the macro definition (possibly dummy).
|
2018-06-23 18:41:39 +00:00
|
|
|
/// This span serves only informational purpose and is not used for resolution.
|
2019-06-30 00:05:52 +00:00
|
|
|
pub def_site: Span,
|
2019-06-17 20:55:22 +00:00
|
|
|
/// Transparency used by `apply_mark` for mark with this expansion info by default.
|
|
|
|
pub default_transparency: Transparency,
|
2019-02-03 11:55:00 +00:00
|
|
|
/// List of #[unstable]/feature-gated features that the macro is allowed to use
|
|
|
|
/// internally without forcing the whole crate to opt-in
|
2017-03-17 04:04:41 +00:00
|
|
|
/// to them.
|
2019-02-08 09:21:21 +00:00
|
|
|
pub allow_internal_unstable: Option<Lrc<[Symbol]>>,
|
2017-08-08 15:21:20 +00:00
|
|
|
/// Whether the macro is allowed to use `unsafe` internally
|
|
|
|
/// even if the user crate has `#![forbid(unsafe_code)]`.
|
|
|
|
pub allow_internal_unsafe: bool,
|
2018-06-11 11:21:36 +00:00
|
|
|
/// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`)
|
|
|
|
/// for a given macro.
|
|
|
|
pub local_inner_macros: bool,
|
2018-04-27 23:08:16 +00:00
|
|
|
/// Edition of the crate in which the macro is defined.
|
|
|
|
pub edition: Edition,
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 19:18:56 +00:00
|
|
|
impl ExpnInfo {
|
|
|
|
/// Constructs an expansion info with default properties.
|
2019-06-18 22:08:45 +00:00
|
|
|
pub fn default(kind: ExpnKind, call_site: Span, edition: Edition) -> ExpnInfo {
|
2019-06-17 19:18:56 +00:00
|
|
|
ExpnInfo {
|
|
|
|
call_site,
|
2019-06-18 22:08:45 +00:00
|
|
|
kind,
|
2019-06-30 00:05:52 +00:00
|
|
|
def_site: DUMMY_SP,
|
2019-06-17 20:55:22 +00:00
|
|
|
default_transparency: Transparency::SemiTransparent,
|
2019-06-17 19:18:56 +00:00
|
|
|
allow_internal_unstable: None,
|
|
|
|
allow_internal_unsafe: false,
|
|
|
|
local_inner_macros: false,
|
|
|
|
edition,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 22:08:45 +00:00
|
|
|
pub fn with_unstable(kind: ExpnKind, call_site: Span, edition: Edition,
|
2019-06-17 19:18:56 +00:00
|
|
|
allow_internal_unstable: &[Symbol]) -> ExpnInfo {
|
|
|
|
ExpnInfo {
|
|
|
|
allow_internal_unstable: Some(allow_internal_unstable.into()),
|
2019-06-18 22:08:45 +00:00
|
|
|
..ExpnInfo::default(kind, call_site, edition)
|
2019-06-17 19:18:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-30 12:58:56 +00:00
|
|
|
/// Expansion kind.
|
2019-06-30 11:57:34 +00:00
|
|
|
#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
|
2019-06-18 22:08:45 +00:00
|
|
|
pub enum ExpnKind {
|
2019-06-30 12:58:56 +00:00
|
|
|
/// Expansion produced by a macro.
|
|
|
|
/// FIXME: Some code injected by the compiler before HIR lowering also gets this kind.
|
|
|
|
Macro(MacroKind, Symbol),
|
2017-03-17 04:04:41 +00:00
|
|
|
/// Desugaring done by the compiler during HIR lowering.
|
2019-06-18 22:08:45 +00:00
|
|
|
Desugaring(DesugaringKind)
|
2017-08-13 00:43:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 22:08:45 +00:00
|
|
|
impl ExpnKind {
|
|
|
|
pub fn descr(&self) -> Symbol {
|
2018-06-23 18:41:39 +00:00
|
|
|
match *self {
|
2019-06-30 12:58:56 +00:00
|
|
|
ExpnKind::Macro(_, descr) => descr,
|
|
|
|
ExpnKind::Desugaring(kind) => Symbol::intern(kind.descr()),
|
2018-06-23 18:41:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 22:00:49 +00:00
|
|
|
/// The kind of macro invocation or definition.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
|
|
|
|
pub enum MacroKind {
|
|
|
|
/// A bang macro `foo!()`.
|
|
|
|
Bang,
|
|
|
|
/// An attribute macro `#[foo]`.
|
|
|
|
Attr,
|
|
|
|
/// A derive macro `#[derive(Foo)]`
|
|
|
|
Derive,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MacroKind {
|
|
|
|
pub fn descr(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
MacroKind::Bang => "macro",
|
|
|
|
MacroKind::Attr => "attribute macro",
|
|
|
|
MacroKind::Derive => "derive macro",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn article(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
MacroKind::Attr => "an",
|
|
|
|
_ => "a",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 00:43:43 +00:00
|
|
|
/// The kind of compiler desugaring.
|
2019-06-30 11:57:34 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, RustcEncodable, RustcDecodable)]
|
2019-06-18 22:08:45 +00:00
|
|
|
pub enum DesugaringKind {
|
2019-03-11 15:43:27 +00:00
|
|
|
/// We desugar `if c { i } else { e }` to `match $ExprKind::Use(c) { true => i, _ => e }`.
|
|
|
|
/// However, we do not want to blame `c` for unreachability but rather say that `i`
|
|
|
|
/// is unreachable. This desugaring kind allows us to avoid blaming `c`.
|
2019-06-20 08:29:42 +00:00
|
|
|
/// This also applies to `while` loops.
|
|
|
|
CondTemporary,
|
2017-08-13 00:43:43 +00:00
|
|
|
QuestionMark,
|
2018-07-22 01:47:02 +00:00
|
|
|
TryBlock,
|
2018-05-22 12:31:56 +00:00
|
|
|
/// Desugaring of an `impl Trait` in return type position
|
2019-02-08 13:53:55 +00:00
|
|
|
/// to an `existential type Foo: Trait;` and replacing the
|
2018-05-22 12:31:56 +00:00
|
|
|
/// `impl Trait` with `Foo`.
|
2019-05-04 15:09:28 +00:00
|
|
|
ExistentialType,
|
2018-06-06 22:50:59 +00:00
|
|
|
Async,
|
2019-04-18 19:55:23 +00:00
|
|
|
Await,
|
2018-07-16 03:52:41 +00:00
|
|
|
ForLoop,
|
2017-08-13 00:43:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 22:08:45 +00:00
|
|
|
impl DesugaringKind {
|
2019-06-30 12:58:56 +00:00
|
|
|
pub fn descr(self) -> &'static str {
|
|
|
|
match self {
|
2019-06-18 22:08:45 +00:00
|
|
|
DesugaringKind::CondTemporary => "if and while condition",
|
|
|
|
DesugaringKind::Async => "async",
|
|
|
|
DesugaringKind::Await => "await",
|
|
|
|
DesugaringKind::QuestionMark => "?",
|
|
|
|
DesugaringKind::TryBlock => "try block",
|
|
|
|
DesugaringKind::ExistentialType => "existential type",
|
|
|
|
DesugaringKind::ForLoop => "for loop",
|
2019-06-30 12:58:56 +00:00
|
|
|
}
|
2017-08-13 00:43:43 +00:00
|
|
|
}
|
2017-03-17 04:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for SyntaxContext {
|
|
|
|
fn encode<E: Encoder>(&self, _: &mut E) -> Result<(), E::Error> {
|
|
|
|
Ok(()) // FIXME(jseyfried) intercrate hygiene
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for SyntaxContext {
|
|
|
|
fn decode<D: Decoder>(_: &mut D) -> Result<SyntaxContext, D::Error> {
|
|
|
|
Ok(SyntaxContext::empty()) // FIXME(jseyfried) intercrate hygiene
|
|
|
|
}
|
|
|
|
}
|