diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-11-10 00:44:59 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-11-16 21:21:37 +0300 |
| commit | 00bc4496026a3168eed95e88c29f17dac2739d48 (patch) | |
| tree | 8848a81d29817d48c21111a119637703cae0656f /src/libsyntax | |
| parent | 266f5471272532989332117e8a2e0bacb5b94ccf (diff) | |
ast: Keep string literals in ABIs precisely
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 42 | ||||
| -rw-r--r-- | src/libsyntax/feature_gate/check.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 10 |
3 files changed, 37 insertions, 21 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 575795758ae..dee493a708e 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -1422,6 +1422,33 @@ pub struct Lit { pub span: Span, } +/// Same as `Lit`, but restricted to string literals. +#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)] +pub struct StrLit { + /// The original literal token as written in source code. + pub style: StrStyle, + pub symbol: Symbol, + pub suffix: Option<Symbol>, + pub span: Span, + /// The unescaped "semantic" representation of the literal lowered from the original token. + /// FIXME: Remove this and only create the semantic representation during lowering to HIR. + pub symbol_unescaped: Symbol, +} + +impl StrLit { + crate fn as_lit(&self) -> Lit { + let token_kind = match self.style { + StrStyle::Cooked => token::Str, + StrStyle::Raw(n) => token::StrRaw(n), + }; + Lit { + token: token::Lit::new(token_kind, self.symbol, self.suffix), + span: self.span, + kind: LitKind::Str(self.symbol_unescaped, self.style), + } + } +} + // Clippy uses Hash and PartialEq /// Type of the integer literal based on provided suffix. #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug, Hash, PartialEq)] @@ -2128,7 +2155,7 @@ pub struct Mod { /// E.g., `extern { .. }` or `extern C { .. }`. #[derive(Clone, RustcEncodable, RustcDecodable, Debug)] pub struct ForeignMod { - pub abi: Option<Abi>, + pub abi: Option<StrLit>, pub items: Vec<ForeignItem>, } @@ -2411,25 +2438,16 @@ impl Item { } } -/// A reference to an ABI. -/// -/// In AST our notion of an ABI is still syntactic unlike in `rustc_target::spec::abi::Abi`. -#[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug, PartialEq)] -pub struct Abi { - pub symbol: Symbol, - pub span: Span, -} - /// `extern` qualifier on a function item or function type. #[derive(Clone, Copy, RustcEncodable, RustcDecodable, Debug)] pub enum Extern { None, Implicit, - Explicit(Abi), + Explicit(StrLit), } impl Extern { - pub fn from_abi(abi: Option<Abi>) -> Extern { + pub fn from_abi(abi: Option<StrLit>) -> Extern { match abi { Some(abi) => Extern::Explicit(abi), None => Extern::Implicit, diff --git a/src/libsyntax/feature_gate/check.rs b/src/libsyntax/feature_gate/check.rs index d90fa9addf7..abf9adefd3c 100644 --- a/src/libsyntax/feature_gate/check.rs +++ b/src/libsyntax/feature_gate/check.rs @@ -191,10 +191,10 @@ macro_rules! gate_feature_post { } impl<'a> PostExpansionVisitor<'a> { - fn check_abi(&self, abi: ast::Abi) { - let ast::Abi { symbol, span } = abi; + fn check_abi(&self, abi: ast::StrLit) { + let ast::StrLit { symbol_unescaped, span, .. } = abi; - match &*symbol.as_str() { + match &*symbol_unescaped.as_str() { // Stable "Rust" | "C" | diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index de28bd6cf83..17a7cbddff9 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1233,7 +1233,8 @@ impl<'a> State<'a> { ast::ItemKind::ForeignMod(ref nmod) => { self.head("extern"); if let Some(abi) = nmod.abi { - self.print_abi(abi); + self.print_literal(&abi.as_lit()); + self.nbsp(); } self.bopen(); self.print_foreign_mod(nmod, &item.attrs); @@ -2875,17 +2876,14 @@ impl<'a> State<'a> { } ast::Extern::Explicit(abi) => { self.word_nbsp("extern"); - self.print_abi(abi); + self.print_literal(&abi.as_lit()); + self.nbsp(); } } self.s.word("fn") } - fn print_abi(&mut self, abi: ast::Abi) { - self.word_nbsp(format!("\"{}\"", abi.symbol)); - } - crate fn print_unsafety(&mut self, s: ast::Unsafety) { match s { ast::Unsafety::Normal => {}, |
