Implement CompilerDesugaringKind enum
This commit is contained in:
parent
045ca8b43b
commit
4acfef8f63
3 changed files with 49 additions and 5 deletions
|
@ -397,11 +397,12 @@ impl<'a> LoweringContext<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn allow_internal_unstable(&self, reason: &'static str, mut span: Span) -> Span {
|
fn allow_internal_unstable(&self, reason: &'static str, mut span: Span) -> Span {
|
||||||
|
let reason = codemap::CompilerDesugaringKind::from(reason);
|
||||||
let mark = Mark::fresh(Mark::root());
|
let mark = Mark::fresh(Mark::root());
|
||||||
mark.set_expn_info(codemap::ExpnInfo {
|
mark.set_expn_info(codemap::ExpnInfo {
|
||||||
call_site: span,
|
call_site: span,
|
||||||
callee: codemap::NameAndSpan {
|
callee: codemap::NameAndSpan {
|
||||||
format: codemap::CompilerDesugaring(Symbol::intern(reason)),
|
format: codemap::CompilerDesugaring(reason),
|
||||||
span: Some(span),
|
span: Some(span),
|
||||||
allow_internal_unstable: true,
|
allow_internal_unstable: true,
|
||||||
allow_internal_unsafe: false,
|
allow_internal_unsafe: false,
|
||||||
|
|
|
@ -323,8 +323,8 @@ impl NameAndSpan {
|
||||||
pub fn name(&self) -> Symbol {
|
pub fn name(&self) -> Symbol {
|
||||||
match self.format {
|
match self.format {
|
||||||
ExpnFormat::MacroAttribute(s) |
|
ExpnFormat::MacroAttribute(s) |
|
||||||
ExpnFormat::MacroBang(s) |
|
ExpnFormat::MacroBang(s) => s,
|
||||||
ExpnFormat::CompilerDesugaring(s) => s,
|
ExpnFormat::CompilerDesugaring(ref kind) => kind.as_symbol(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -337,7 +337,39 @@ pub enum ExpnFormat {
|
||||||
/// e.g. `format!()`
|
/// e.g. `format!()`
|
||||||
MacroBang(Symbol),
|
MacroBang(Symbol),
|
||||||
/// Desugaring done by the compiler during HIR lowering.
|
/// Desugaring done by the compiler during HIR lowering.
|
||||||
CompilerDesugaring(Symbol)
|
CompilerDesugaring(CompilerDesugaringKind)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The kind of compiler desugaring.
|
||||||
|
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
|
||||||
|
pub enum CompilerDesugaringKind {
|
||||||
|
BackArrow,
|
||||||
|
DotFill,
|
||||||
|
QuestionMark,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CompilerDesugaringKind {
|
||||||
|
pub fn as_symbol(&self) -> Symbol {
|
||||||
|
use CompilerDesugaringKind::*;
|
||||||
|
let s = match *self {
|
||||||
|
BackArrow => "<-",
|
||||||
|
DotFill => "...",
|
||||||
|
QuestionMark => "?",
|
||||||
|
};
|
||||||
|
Symbol::intern(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for CompilerDesugaringKind {
|
||||||
|
fn from(s: &'a str) -> Self {
|
||||||
|
use CompilerDesugaringKind::*;
|
||||||
|
match s {
|
||||||
|
"<-" => BackArrow,
|
||||||
|
"..." => DotFill,
|
||||||
|
"?" => QuestionMark,
|
||||||
|
_ => panic!("Invalid compiler desugaring"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Encodable for SyntaxContext {
|
impl Encodable for SyntaxContext {
|
||||||
|
|
|
@ -47,7 +47,7 @@ extern crate serialize;
|
||||||
extern crate serialize as rustc_serialize; // used by deriving
|
extern crate serialize as rustc_serialize; // used by deriving
|
||||||
|
|
||||||
pub mod hygiene;
|
pub mod hygiene;
|
||||||
pub use hygiene::{SyntaxContext, ExpnInfo, ExpnFormat, NameAndSpan};
|
pub use hygiene::{SyntaxContext, ExpnInfo, ExpnFormat, NameAndSpan, CompilerDesugaringKind};
|
||||||
|
|
||||||
pub mod symbol;
|
pub mod symbol;
|
||||||
|
|
||||||
|
@ -153,6 +153,17 @@ impl Span {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check if this span arises from a compiler desugaring of kind `kind`.
|
||||||
|
pub fn is_compiler_desugaring(&self, kind: CompilerDesugaringKind) -> bool {
|
||||||
|
match self.ctxt.outer().expn_info() {
|
||||||
|
Some(info) => match info.callee.format {
|
||||||
|
ExpnFormat::CompilerDesugaring(k) => k == kind,
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
|
None => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if a span is "internal" to a macro in which `unsafe`
|
/// Check if a span is "internal" to a macro in which `unsafe`
|
||||||
/// can be used without triggering the `unsafe_code` lint
|
/// can be used without triggering the `unsafe_code` lint
|
||||||
// (that is, a macro marked with `#[allow_internal_unsafe]`).
|
// (that is, a macro marked with `#[allow_internal_unsafe]`).
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue