Skip to content

Commit 4ac7646

Browse files
committed
Auto merge of #43832 - huntiep:compiler-desugaring-enum, r=nikomatsakis
Implement CompilerDesugaringKind enum This is the first step outlined in #35946. I think that the variants of `CompilerDesugaringKind` should be changed, I didn't know what the official names for `...` and `<-` are. I'm not to sure how tests for the compiler work, but I would imagine that tests should be added such that `Symbol::intern(s) == CompilerDesugaringKind::from(s).as_symbol()` for valid `s`.
2 parents 59ccba9 + ff047a8 commit 4ac7646

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/librustc/hir/lowering.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use syntax::ast::*;
5959
use syntax::errors;
6060
use syntax::ext::hygiene::{Mark, SyntaxContext};
6161
use syntax::ptr::P;
62-
use syntax::codemap::{self, respan, Spanned};
62+
use syntax::codemap::{self, respan, Spanned, CompilerDesugaringKind};
6363
use syntax::std_inject;
6464
use syntax::symbol::{Symbol, keywords};
6565
use syntax::util::small_vector::SmallVector;
@@ -414,12 +414,14 @@ impl<'a> LoweringContext<'a> {
414414
Symbol::gensym(s)
415415
}
416416

417-
fn allow_internal_unstable(&self, reason: &'static str, mut span: Span) -> Span {
417+
fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, mut span: Span)
418+
-> Span
419+
{
418420
let mark = Mark::fresh(Mark::root());
419421
mark.set_expn_info(codemap::ExpnInfo {
420422
call_site: span,
421423
callee: codemap::NameAndSpan {
422-
format: codemap::CompilerDesugaring(Symbol::intern(reason)),
424+
format: codemap::CompilerDesugaring(reason),
423425
span: Some(span),
424426
allow_internal_unstable: true,
425427
allow_internal_unsafe: false,
@@ -1790,7 +1792,8 @@ impl<'a> LoweringContext<'a> {
17901792
let move_val_init = ["intrinsics", "move_val_init"];
17911793
let inplace_finalize = ["ops", "InPlace", "finalize"];
17921794

1793-
let unstable_span = self.allow_internal_unstable("<-", e.span);
1795+
let unstable_span =
1796+
self.allow_internal_unstable(CompilerDesugaringKind::BackArrow, e.span);
17941797
let make_call = |this: &mut LoweringContext, p, args| {
17951798
let path = P(this.expr_std_path(unstable_span, p, ThinVec::new()));
17961799
P(this.expr_call(e.span, path, args))
@@ -2007,12 +2010,14 @@ impl<'a> LoweringContext<'a> {
20072010
e1.iter().map(|e| ("start", e)).chain(e2.iter().map(|e| ("end", e)))
20082011
.map(|(s, e)| {
20092012
let expr = P(self.lower_expr(&e));
2010-
let unstable_span = self.allow_internal_unstable("...", e.span);
2013+
let unstable_span =
2014+
self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
20112015
self.field(Symbol::intern(s), expr, unstable_span)
20122016
}).collect::<P<[hir::Field]>>();
20132017

20142018
let is_unit = fields.is_empty();
2015-
let unstable_span = self.allow_internal_unstable("...", e.span);
2019+
let unstable_span =
2020+
self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
20162021
let struct_path =
20172022
iter::once("ops").chain(iter::once(path))
20182023
.collect::<Vec<_>>();
@@ -2353,7 +2358,8 @@ impl<'a> LoweringContext<'a> {
23532358
// return Try::from_error(From::from(err)),
23542359
// }
23552360

2356-
let unstable_span = self.allow_internal_unstable("?", e.span);
2361+
let unstable_span =
2362+
self.allow_internal_unstable(CompilerDesugaringKind::QuestionMark, e.span);
23572363

23582364
// Try::into_result(<expr>)
23592365
let discr = {

src/libsyntax_pos/hygiene.rs

+23-3
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,27 @@ 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+
}
341361
}
342362

343363
impl Encodable for SyntaxContext {

src/libsyntax_pos/lib.rs

+12-1
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)