Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CompilerDesugaringKind enum #43832

Merged
merged 2 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use syntax::ast::*;
use syntax::errors;
use syntax::ext::hygiene::{Mark, SyntaxContext};
use syntax::ptr::P;
use syntax::codemap::{self, respan, Spanned};
use syntax::codemap::{self, respan, Spanned, CompilerDesugaringKind};
use syntax::std_inject;
use syntax::symbol::{Symbol, keywords};
use syntax::util::small_vector::SmallVector;
Expand Down Expand Up @@ -396,12 +396,14 @@ impl<'a> LoweringContext<'a> {
Symbol::gensym(s)
}

fn allow_internal_unstable(&self, reason: &'static str, mut span: Span) -> Span {
fn allow_internal_unstable(&self, reason: CompilerDesugaringKind, mut span: Span)
-> Span
{
let mark = Mark::fresh(Mark::root());
mark.set_expn_info(codemap::ExpnInfo {
call_site: span,
callee: codemap::NameAndSpan {
format: codemap::CompilerDesugaring(Symbol::intern(reason)),
format: codemap::CompilerDesugaring(reason),
span: Some(span),
allow_internal_unstable: true,
allow_internal_unsafe: false,
Expand Down Expand Up @@ -1762,7 +1764,8 @@ impl<'a> LoweringContext<'a> {
let move_val_init = ["intrinsics", "move_val_init"];
let inplace_finalize = ["ops", "InPlace", "finalize"];

let unstable_span = self.allow_internal_unstable("<-", e.span);
let unstable_span =
self.allow_internal_unstable(CompilerDesugaringKind::BackArrow, e.span);
let make_call = |this: &mut LoweringContext, p, args| {
let path = P(this.expr_std_path(unstable_span, p, ThinVec::new()));
P(this.expr_call(e.span, path, args))
Expand Down Expand Up @@ -1975,12 +1978,14 @@ impl<'a> LoweringContext<'a> {
e1.iter().map(|e| ("start", e)).chain(e2.iter().map(|e| ("end", e)))
.map(|(s, e)| {
let expr = P(self.lower_expr(&e));
let unstable_span = self.allow_internal_unstable("...", e.span);
let unstable_span =
self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
self.field(Symbol::intern(s), expr, unstable_span)
}).collect::<P<[hir::Field]>>();

let is_unit = fields.is_empty();
let unstable_span = self.allow_internal_unstable("...", e.span);
let unstable_span =
self.allow_internal_unstable(CompilerDesugaringKind::DotFill, e.span);
let struct_path =
iter::once("ops").chain(iter::once(path))
.collect::<Vec<_>>();
Expand Down Expand Up @@ -2316,7 +2321,8 @@ impl<'a> LoweringContext<'a> {
// return Try::from_error(From::from(err)),
// }

let unstable_span = self.allow_internal_unstable("?", e.span);
let unstable_span =
self.allow_internal_unstable(CompilerDesugaringKind::QuestionMark, e.span);

// Try::into_result(<expr>)
let discr = {
Expand Down
26 changes: 23 additions & 3 deletions src/libsyntax_pos/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ impl NameAndSpan {
pub fn name(&self) -> Symbol {
match self.format {
ExpnFormat::MacroAttribute(s) |
ExpnFormat::MacroBang(s) |
ExpnFormat::CompilerDesugaring(s) => s,
ExpnFormat::MacroBang(s) => s,
ExpnFormat::CompilerDesugaring(ref kind) => kind.as_symbol(),
}
}
}
Expand All @@ -337,7 +337,27 @@ pub enum ExpnFormat {
/// e.g. `format!()`
MacroBang(Symbol),
/// 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 Encodable for SyntaxContext {
Expand Down
13 changes: 12 additions & 1 deletion src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extern crate serialize;
extern crate serialize as rustc_serialize; // used by deriving

pub mod hygiene;
pub use hygiene::{SyntaxContext, ExpnInfo, ExpnFormat, NameAndSpan};
pub use hygiene::{SyntaxContext, ExpnInfo, ExpnFormat, NameAndSpan, CompilerDesugaringKind};

pub mod symbol;

Expand Down Expand Up @@ -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`
/// can be used without triggering the `unsafe_code` lint
// (that is, a macro marked with `#[allow_internal_unsafe]`).
Expand Down