Skip to content

Commit 4acfef8

Browse files
committed
Implement CompilerDesugaringKind enum
1 parent 045ca8b commit 4acfef8

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/librustc/hir/lowering.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,12 @@ impl<'a> LoweringContext<'a> {
397397
}
398398

399399
fn allow_internal_unstable(&self, reason: &'static str, mut span: Span) -> Span {
400+
let reason = codemap::CompilerDesugaringKind::from(reason);
400401
let mark = Mark::fresh(Mark::root());
401402
mark.set_expn_info(codemap::ExpnInfo {
402403
call_site: span,
403404
callee: codemap::NameAndSpan {
404-
format: codemap::CompilerDesugaring(Symbol::intern(reason)),
405+
format: codemap::CompilerDesugaring(reason),
405406
span: Some(span),
406407
allow_internal_unstable: true,
407408
allow_internal_unsafe: false,

src/libsyntax_pos/hygiene.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ impl NameAndSpan {
323323
pub fn name(&self) -> Symbol {
324324
match self.format {
325325
ExpnFormat::MacroAttribute(s) |
326-
ExpnFormat::MacroBang(s) |
327-
ExpnFormat::CompilerDesugaring(s) => s,
326+
ExpnFormat::MacroBang(s) => s,
327+
ExpnFormat::CompilerDesugaring(ref kind) => kind.as_symbol(),
328328
}
329329
}
330330
}
@@ -337,7 +337,39 @@ pub enum ExpnFormat {
337337
/// e.g. `format!()`
338338
MacroBang(Symbol),
339339
/// Desugaring done by the compiler during HIR lowering.
340-
CompilerDesugaring(Symbol)
340+
CompilerDesugaring(CompilerDesugaringKind)
341+
}
342+
343+
/// The kind of compiler desugaring.
344+
#[derive(Clone, Hash, Debug, PartialEq, Eq)]
345+
pub enum CompilerDesugaringKind {
346+
BackArrow,
347+
DotFill,
348+
QuestionMark,
349+
}
350+
351+
impl CompilerDesugaringKind {
352+
pub fn as_symbol(&self) -> Symbol {
353+
use CompilerDesugaringKind::*;
354+
let s = match *self {
355+
BackArrow => "<-",
356+
DotFill => "...",
357+
QuestionMark => "?",
358+
};
359+
Symbol::intern(s)
360+
}
361+
}
362+
363+
impl<'a> From<&'a str> for CompilerDesugaringKind {
364+
fn from(s: &'a str) -> Self {
365+
use CompilerDesugaringKind::*;
366+
match s {
367+
"<-" => BackArrow,
368+
"..." => DotFill,
369+
"?" => QuestionMark,
370+
_ => panic!("Invalid compiler desugaring"),
371+
}
372+
}
341373
}
342374

343375
impl Encodable for SyntaxContext {

src/libsyntax_pos/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extern crate serialize;
4747
extern crate serialize as rustc_serialize; // used by deriving
4848

4949
pub mod hygiene;
50-
pub use hygiene::{SyntaxContext, ExpnInfo, ExpnFormat, NameAndSpan};
50+
pub use hygiene::{SyntaxContext, ExpnInfo, ExpnFormat, NameAndSpan, CompilerDesugaringKind};
5151

5252
pub mod symbol;
5353

@@ -153,6 +153,17 @@ impl Span {
153153
}
154154
}
155155

156+
/// Check if this span arises from a compiler desugaring of kind `kind`.
157+
pub fn is_compiler_desugaring(&self, kind: CompilerDesugaringKind) -> bool {
158+
match self.ctxt.outer().expn_info() {
159+
Some(info) => match info.callee.format {
160+
ExpnFormat::CompilerDesugaring(k) => k == kind,
161+
_ => false,
162+
},
163+
None => false,
164+
}
165+
}
166+
156167
/// Check if a span is "internal" to a macro in which `unsafe`
157168
/// can be used without triggering the `unsafe_code` lint
158169
// (that is, a macro marked with `#[allow_internal_unsafe]`).

0 commit comments

Comments
 (0)