diff options
Diffstat (limited to 'src/libsyntax/ext')
| -rw-r--r-- | src/libsyntax/ext/base.rs | 68 | ||||
| -rw-r--r-- | src/libsyntax/ext/build.rs | 11 | ||||
| -rw-r--r-- | src/libsyntax/ext/derive.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/ext/expand.rs | 31 | ||||
| -rw-r--r-- | src/libsyntax/ext/placeholders.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/ext/quote.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/ext/source_util.rs | 12 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/macro_parser.rs | 12 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/macro_rules.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/quoted.rs | 10 | ||||
| -rw-r--r-- | src/libsyntax/ext/tt/transcribe.rs | 10 |
11 files changed, 52 insertions, 142 deletions
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index b807a65f6ae..2793754e103 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -1,18 +1,8 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - pub use self::SyntaxExtension::*; use ast::{self, Attribute, Name, PatKind, MetaItem}; use attr::HasAttrs; -use source_map::{self, SourceMap, Spanned, respan}; +use source_map::{SourceMap, Spanned, respan}; use syntax_pos::{Span, MultiSpan, DUMMY_SP}; use edition::Edition; use errors::{DiagnosticBuilder, DiagnosticId}; @@ -24,6 +14,7 @@ use parse::token; use ptr::P; use smallvec::SmallVec; use symbol::{keywords, Ident, Symbol}; +use visit::Visitor; use ThinVec; use rustc_data_structures::fx::FxHashMap; @@ -145,6 +136,17 @@ impl Annotatable { _ => false, } } + + pub fn visit_with<'a, V: Visitor<'a>>(&'a self, visitor: &mut V) { + match self { + Annotatable::Item(item) => visitor.visit_item(item), + Annotatable::TraitItem(trait_item) => visitor.visit_trait_item(trait_item), + Annotatable::ImplItem(impl_item) => visitor.visit_impl_item(impl_item), + Annotatable::ForeignItem(foreign_item) => visitor.visit_foreign_item(foreign_item), + Annotatable::Stmt(stmt) => visitor.visit_stmt(stmt), + Annotatable::Expr(expr) => visitor.visit_expr(expr), + } + } } // A more flexible ItemDecorator. @@ -466,7 +468,8 @@ impl MacResult for MacEager { #[derive(Copy, Clone)] pub struct DummyResult { expr_only: bool, - span: Span + is_error: bool, + span: Span, } impl DummyResult { @@ -474,8 +477,13 @@ impl DummyResult { /// /// Use this as a return value after hitting any errors and /// calling `span_err`. - pub fn any(sp: Span) -> Box<dyn MacResult+'static> { - Box::new(DummyResult { expr_only: false, span: sp }) + pub fn any(span: Span) -> Box<dyn MacResult+'static> { + Box::new(DummyResult { expr_only: false, is_error: true, span }) + } + + /// Same as `any`, but must be a valid fragment, not error. + pub fn any_valid(span: Span) -> Box<dyn MacResult+'static> { + Box::new(DummyResult { expr_only: false, is_error: false, span }) } /// Create a default MacResult that can only be an expression. @@ -483,15 +491,15 @@ impl DummyResult { /// Use this for macros that must expand to an expression, so even /// if an error is encountered internally, the user will receive /// an error that they also used it in the wrong place. - pub fn expr(sp: Span) -> Box<dyn MacResult+'static> { - Box::new(DummyResult { expr_only: true, span: sp }) + pub fn expr(span: Span) -> Box<dyn MacResult+'static> { + Box::new(DummyResult { expr_only: true, is_error: true, span }) } /// A plain dummy expression. - pub fn raw_expr(sp: Span) -> P<ast::Expr> { + pub fn raw_expr(sp: Span, is_error: bool) -> P<ast::Expr> { P(ast::Expr { id: ast::DUMMY_NODE_ID, - node: ast::ExprKind::Lit(source_map::respan(sp, ast::LitKind::Bool(false))), + node: if is_error { ast::ExprKind::Err } else { ast::ExprKind::Tup(Vec::new()) }, span: sp, attrs: ThinVec::new(), }) @@ -506,10 +514,11 @@ impl DummyResult { } } - pub fn raw_ty(sp: Span) -> P<ast::Ty> { + /// A plain dummy type. + pub fn raw_ty(sp: Span, is_error: bool) -> P<ast::Ty> { P(ast::Ty { id: ast::DUMMY_NODE_ID, - node: ast::TyKind::Infer, + node: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(Vec::new()) }, span: sp }) } @@ -517,7 +526,7 @@ impl DummyResult { impl MacResult for DummyResult { fn make_expr(self: Box<DummyResult>) -> Option<P<ast::Expr>> { - Some(DummyResult::raw_expr(self.span)) + Some(DummyResult::raw_expr(self.span, self.is_error)) } fn make_pat(self: Box<DummyResult>) -> Option<P<ast::Pat>> { @@ -560,13 +569,13 @@ impl MacResult for DummyResult { fn make_stmts(self: Box<DummyResult>) -> Option<SmallVec<[ast::Stmt; 1]>> { Some(smallvec![ast::Stmt { id: ast::DUMMY_NODE_ID, - node: ast::StmtKind::Expr(DummyResult::raw_expr(self.span)), + node: ast::StmtKind::Expr(DummyResult::raw_expr(self.span, self.is_error)), span: self.span, }]) } fn make_ty(self: Box<DummyResult>) -> Option<P<ast::Ty>> { - Some(DummyResult::raw_ty(self.span)) + Some(DummyResult::raw_ty(self.span, self.is_error)) } } @@ -733,6 +742,7 @@ pub trait Resolver { fn next_node_id(&mut self) -> ast::NodeId; fn get_module_scope(&mut self, id: ast::NodeId) -> Mark; + fn resolve_dollar_crates(&mut self, annotatable: &Annotatable); fn visit_ast_fragment_with_placeholders(&mut self, mark: Mark, fragment: &AstFragment, derives: &[Mark]); fn add_builtin(&mut self, ident: ast::Ident, ext: Lrc<SyntaxExtension>); @@ -766,6 +776,7 @@ impl Resolver for DummyResolver { fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID } fn get_module_scope(&mut self, _id: ast::NodeId) -> Mark { Mark::root() } + fn resolve_dollar_crates(&mut self, _annotatable: &Annotatable) {} fn visit_ast_fragment_with_placeholders(&mut self, _invoc: Mark, _fragment: &AstFragment, _derives: &[Mark]) {} fn add_builtin(&mut self, _ident: ast::Ident, _ext: Lrc<SyntaxExtension>) {} @@ -806,7 +817,6 @@ pub struct ExtCtxt<'a> { pub ecfg: expand::ExpansionConfig<'a>, pub root_path: PathBuf, pub resolver: &'a mut dyn Resolver, - pub resolve_err_count: usize, pub current_expansion: ExpansionData, pub expansions: FxHashMap<Span, Vec<String>>, } @@ -821,7 +831,6 @@ impl<'a> ExtCtxt<'a> { ecfg, root_path: PathBuf::new(), resolver, - resolve_err_count: 0, current_expansion: ExpansionData { mark: Mark::root(), depth: 0, @@ -986,7 +995,7 @@ pub fn expr_to_spanned_string<'a>( cx: &'a mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str, -) -> Result<Spanned<(Symbol, ast::StrStyle)>, DiagnosticBuilder<'a>> { +) -> Result<Spanned<(Symbol, ast::StrStyle)>, Option<DiagnosticBuilder<'a>>> { // Update `expr.span`'s ctxt now in case expr is an `include!` macro invocation. let expr = expr.map(|mut expr| { expr.span = expr.span.apply_mark(cx.current_expansion.mark); @@ -998,16 +1007,17 @@ pub fn expr_to_spanned_string<'a>( Err(match expr.node { ast::ExprKind::Lit(ref l) => match l.node { ast::LitKind::Str(s, style) => return Ok(respan(expr.span, (s, style))), - _ => cx.struct_span_err(l.span, err_msg) + _ => Some(cx.struct_span_err(l.span, err_msg)) }, - _ => cx.struct_span_err(expr.span, err_msg) + ast::ExprKind::Err => None, + _ => Some(cx.struct_span_err(expr.span, err_msg)) }) } pub fn expr_to_string(cx: &mut ExtCtxt, expr: P<ast::Expr>, err_msg: &str) -> Option<(Symbol, ast::StrStyle)> { expr_to_spanned_string(cx, expr, err_msg) - .map_err(|mut err| err.emit()) + .map_err(|err| err.map(|mut err| err.emit())) .ok() .map(|s| s.node) } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 5770f6bb8a2..a8eec1a74dd 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -1,13 +1,3 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use rustc_target::spec::abi::Abi; use ast::{self, Ident, Generics, Expr, BlockCheckMode, UnOp, PatKind}; use attr; @@ -597,7 +587,6 @@ impl<'a> AstBuilder for ExtCtxt<'a> { id: ast::DUMMY_NODE_ID, rules: BlockCheckMode::Default, span, - recovered: false, }) } diff --git a/src/libsyntax/ext/derive.rs b/src/libsyntax/ext/derive.rs index 684cee38874..d5a51bc1703 100644 --- a/src/libsyntax/ext/derive.rs +++ b/src/libsyntax/ext/derive.rs @@ -1,13 +1,3 @@ -// Copyright 2012-2017 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use attr::HasAttrs; use ast; use source_map::{hygiene, ExpnInfo, ExpnFormat}; diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 57ccc3e9817..9369e66cf83 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -1,13 +1,3 @@ -// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use ast::{self, Block, Ident, LitKind, NodeId, PatKind, Path}; use ast::{MacStmtStyle, StmtKind, ItemKind}; use attr::{self, HasAttrs}; @@ -354,8 +344,10 @@ impl<'a, 'b> MacroExpander<'a, 'b> { // FIXME(jseyfried): Refactor out the following logic let (expanded_fragment, new_invocations) = if let Some(ext) = ext { if let Some(ext) = ext { - let dummy = invoc.fragment_kind.dummy(invoc.span()).unwrap(); - let fragment = self.expand_invoc(invoc, &*ext).unwrap_or(dummy); + let (invoc_fragment_kind, invoc_span) = (invoc.fragment_kind, invoc.span()); + let fragment = self.expand_invoc(invoc, &*ext).unwrap_or_else(|| { + invoc_fragment_kind.dummy(invoc_span).unwrap() + }); self.collect_invocations(fragment, &[]) } else if let InvocationKind::Attr { attr: None, traits, item, .. } = invoc.kind { if !item.derive_allowed() { @@ -441,9 +433,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { fn resolve_imports(&mut self) { if self.monotonic { - let err_count = self.cx.parse_sess.span_diagnostic.err_count(); self.cx.resolver.resolve_imports(); - self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count; } } @@ -467,11 +457,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> { }; if self.monotonic { - let err_count = self.cx.parse_sess.span_diagnostic.err_count(); - let mark = self.cx.current_expansion.mark; - self.cx.resolver.visit_ast_fragment_with_placeholders(mark, &fragment_with_placeholders, - derives); - self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count; + self.cx.resolver.visit_ast_fragment_with_placeholders( + self.cx.current_expansion.mark, &fragment_with_placeholders, derives + ); } (fragment_with_placeholders, invocations) @@ -586,6 +574,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { Some(invoc.fragment_kind.expect_from_annotatables(items)) } AttrProcMacro(ref mac, ..) => { + // Resolve `$crate`s in case we have to go though stringification. + self.cx.resolver.resolve_dollar_crates(&item); self.gate_proc_macro_attr_item(attr.span, &item); let item_tok = TokenTree::Token(DUMMY_SP, Token::interpolated(match item { Annotatable::Item(item) => token::NtItem(item), @@ -734,7 +724,6 @@ impl<'a, 'b> MacroExpander<'a, 'b> { emit_feature_err(this.cx.parse_sess, &*feature.as_str(), span, GateIssue::Library(Some(issue)), &explain); this.cx.trace_macros_diag(); - return Err(kind.dummy(span)); } } @@ -928,6 +917,8 @@ impl<'a, 'b> MacroExpander<'a, 'b> { match *ext { ProcMacroDerive(ref ext, ..) => { + // Resolve `$crate`s in case we have to go though stringification. + self.cx.resolver.resolve_dollar_crates(&item); invoc.expansion_data.mark.set_expn_info(expn_info); let span = span.with_ctxt(self.cx.backtrace()); let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this diff --git a/src/libsyntax/ext/placeholders.rs b/src/libsyntax/ext/placeholders.rs index 7f31b04ef9c..3b0402d910a 100644 --- a/src/libsyntax/ext/placeholders.rs +++ b/src/libsyntax/ext/placeholders.rs @@ -1,13 +1,3 @@ -// Copyright 2016 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use ast::{self, NodeId}; use source_map::{DUMMY_SP, dummy_spanned}; use ext::base::ExtCtxt; diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index 5820b49ab62..ad8668aca70 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -1,13 +1,3 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use ast::{self, Arg, Arm, Block, Expr, Item, Pat, Stmt, Ty}; use source_map::respan; use syntax_pos::{Span, DUMMY_SP}; diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index ec27ceeb74c..e63042a4208 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -1,13 +1,3 @@ -// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use ast; use syntax_pos::{self, Pos, Span, FileName}; use ext::base::*; @@ -96,7 +86,7 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[tokenstream::T -> Box<dyn base::MacResult+'cx> { let file = match get_single_str_from_tts(cx, sp, tts, "include!") { Some(f) => f, - None => return DummyResult::expr(sp), + None => return DummyResult::any(sp), }; // The file will be added to the code map by the parser let path = res_rel_file(cx, sp, file); diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index 26348d5ec82..d55f785bd9b 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -1,13 +1,3 @@ -// Copyright 2012-2017 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - //! This is an NFA-based parser, which calls out to the main rust parser for named nonterminals //! (which it commits to fully when it hits one in a grammar). There's a set of current NFA threads //! and a set of next ones. Instead of NTs, we have a special case for Kleene star. The big-O, in @@ -171,7 +161,7 @@ struct MatcherPos<'root, 'tt: 'root> { /// The position of the "dot" in this matcher idx: usize, - /// The first span of source source that the beginning of this matcher corresponds to. In other + /// The first span of source that the beginning of this matcher corresponds to. In other /// words, the token in the source whose span is `sp_open` is matched against the first token of /// the matcher. sp_open: Span, diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 30322ff523d..cb8fbce6697 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -1,13 +1,3 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use {ast, attr}; use syntax_pos::{Span, DUMMY_SP}; use edition::Edition; diff --git a/src/libsyntax/ext/tt/quoted.rs b/src/libsyntax/ext/tt/quoted.rs index b142f09cdbc..b56871a1885 100644 --- a/src/libsyntax/ext/tt/quoted.rs +++ b/src/libsyntax/ext/tt/quoted.rs @@ -1,13 +1,3 @@ -// Copyright 2017 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use ast::NodeId; use early_buffered_lints::BufferedEarlyLintId; use ext::tt::macro_parser; diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index a63abd40495..31d87508c6b 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -1,13 +1,3 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - use ast::Ident; use ext::base::ExtCtxt; use ext::expand::Marker; |
