summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_ast/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_ast/src')
-rw-r--r--compiler/rustc_ast/src/ast.rs77
-rw-r--r--compiler/rustc_ast/src/attr/mod.rs22
-rw-r--r--compiler/rustc_ast/src/format.rs6
-rw-r--r--compiler/rustc_ast/src/mut_visit.rs31
-rw-r--r--compiler/rustc_ast/src/util/classify.rs1
-rw-r--r--compiler/rustc_ast/src/util/parser.rs4
-rw-r--r--compiler/rustc_ast/src/visit.rs12
7 files changed, 97 insertions, 56 deletions
diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs
index 03c375c46..df1a71675 100644
--- a/compiler/rustc_ast/src/ast.rs
+++ b/compiler/rustc_ast/src/ast.rs
@@ -231,15 +231,15 @@ impl AngleBracketedArg {
}
}
-impl Into<Option<P<GenericArgs>>> for AngleBracketedArgs {
- fn into(self) -> Option<P<GenericArgs>> {
- Some(P(GenericArgs::AngleBracketed(self)))
+impl Into<P<GenericArgs>> for AngleBracketedArgs {
+ fn into(self) -> P<GenericArgs> {
+ P(GenericArgs::AngleBracketed(self))
}
}
-impl Into<Option<P<GenericArgs>>> for ParenthesizedArgs {
- fn into(self) -> Option<P<GenericArgs>> {
- Some(P(GenericArgs::Parenthesized(self)))
+impl Into<P<GenericArgs>> for ParenthesizedArgs {
+ fn into(self) -> P<GenericArgs> {
+ P(GenericArgs::Parenthesized(self))
}
}
@@ -1184,6 +1184,15 @@ impl Expr {
expr
}
+ pub fn peel_parens_and_refs(&self) -> &Expr {
+ let mut expr = self;
+ while let ExprKind::Paren(inner) | ExprKind::AddrOf(BorrowKind::Ref, _, inner) = &expr.kind
+ {
+ expr = inner;
+ }
+ expr
+ }
+
/// Attempts to reparse as `Ty` (for diagnostic purposes).
pub fn to_ty(&self) -> Option<P<Ty>> {
let kind = match &self.kind {
@@ -1230,7 +1239,6 @@ impl Expr {
pub fn precedence(&self) -> ExprPrecedence {
match self.kind {
- ExprKind::Box(_) => ExprPrecedence::Box,
ExprKind::Array(_) => ExprPrecedence::Array,
ExprKind::ConstBlock(_) => ExprPrecedence::ConstBlock,
ExprKind::Call(..) => ExprPrecedence::Call,
@@ -1291,8 +1299,7 @@ impl Expr {
/// To a first-order approximation, is this a pattern?
pub fn is_approximately_pattern(&self) -> bool {
match &self.peel_parens().kind {
- ExprKind::Box(_)
- | ExprKind::Array(_)
+ ExprKind::Array(_)
| ExprKind::Call(_, _)
| ExprKind::Tup(_)
| ExprKind::Lit(_)
@@ -1363,8 +1370,6 @@ pub struct StructExpr {
#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ExprKind {
- /// A `box x` expression.
- Box(P<Expr>),
/// An array (`[a, b, c, d]`)
Array(ThinVec<P<Expr>>),
/// Allow anonymous constants from an inline `const` block
@@ -1421,13 +1426,9 @@ pub enum ExprKind {
Block(P<Block>, Option<Label>),
/// An async block (`async move { ... }`).
///
- /// The `NodeId` is the `NodeId` for the closure that results from
- /// desugaring an async block, just like the NodeId field in the
- /// `Async::Yes` variant. This is necessary in order to create a def for the
- /// closure which can be used as a parent of any child defs. Defs
- /// created during lowering cannot be made the parent of any other
- /// preexisting defs.
- Async(CaptureBy, NodeId, P<Block>),
+ /// The async block used to have a `NodeId`, which was removed in favor of
+ /// using the parent `NodeId` of the parent `Expr`.
+ Async(CaptureBy, P<Block>),
/// An await expression (`my_future.await`).
Await(P<Expr>),
@@ -2569,7 +2570,7 @@ pub enum AttrStyle {
rustc_index::newtype_index! {
#[custom_encodable]
- #[debug_format = "AttrId({})]"]
+ #[debug_format = "AttrId({})"]
pub struct AttrId {}
}
@@ -2886,6 +2887,20 @@ pub struct Fn {
}
#[derive(Clone, Encodable, Decodable, Debug)]
+pub struct StaticItem {
+ pub ty: P<Ty>,
+ pub mutability: Mutability,
+ pub expr: Option<P<Expr>>,
+}
+
+#[derive(Clone, Encodable, Decodable, Debug)]
+pub struct ConstItem {
+ pub defaultness: Defaultness,
+ pub ty: P<Ty>,
+ pub expr: Option<P<Expr>>,
+}
+
+#[derive(Clone, Encodable, Decodable, Debug)]
pub enum ItemKind {
/// An `extern crate` item, with the optional *original* crate name if the crate was renamed.
///
@@ -2898,11 +2913,11 @@ pub enum ItemKind {
/// A static item (`static`).
///
/// E.g., `static FOO: i32 = 42;` or `static FOO: &'static str = "bar";`.
- Static(P<Ty>, Mutability, Option<P<Expr>>),
+ Static(Box<StaticItem>),
/// A constant item (`const`).
///
/// E.g., `const FOO: i32 = 42;`.
- Const(Defaultness, P<Ty>, Option<P<Expr>>),
+ Const(Box<ConstItem>),
/// A function declaration (`fn`).
///
/// E.g., `fn foo(bar: usize) -> usize { .. }`.
@@ -3018,7 +3033,7 @@ pub type AssocItem = Item<AssocItemKind>;
pub enum AssocItemKind {
/// An associated constant, `const $ident: $ty $def?;` where `def ::= "=" $expr? ;`.
/// If `def` is parsed, then the constant is provided, and otherwise required.
- Const(Defaultness, P<Ty>, Option<P<Expr>>),
+ Const(Box<ConstItem>),
/// An associated function.
Fn(Box<Fn>),
/// An associated type.
@@ -3030,7 +3045,7 @@ pub enum AssocItemKind {
impl AssocItemKind {
pub fn defaultness(&self) -> Defaultness {
match *self {
- Self::Const(defaultness, ..)
+ Self::Const(box ConstItem { defaultness, .. })
| Self::Fn(box Fn { defaultness, .. })
| Self::Type(box TyAlias { defaultness, .. }) => defaultness,
Self::MacCall(..) => Defaultness::Final,
@@ -3041,7 +3056,7 @@ impl AssocItemKind {
impl From<AssocItemKind> for ItemKind {
fn from(assoc_item_kind: AssocItemKind) -> ItemKind {
match assoc_item_kind {
- AssocItemKind::Const(a, b, c) => ItemKind::Const(a, b, c),
+ AssocItemKind::Const(item) => ItemKind::Const(item),
AssocItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
AssocItemKind::Type(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
AssocItemKind::MacCall(a) => ItemKind::MacCall(a),
@@ -3054,7 +3069,7 @@ impl TryFrom<ItemKind> for AssocItemKind {
fn try_from(item_kind: ItemKind) -> Result<AssocItemKind, ItemKind> {
Ok(match item_kind {
- ItemKind::Const(a, b, c) => AssocItemKind::Const(a, b, c),
+ ItemKind::Const(item) => AssocItemKind::Const(item),
ItemKind::Fn(fn_kind) => AssocItemKind::Fn(fn_kind),
ItemKind::TyAlias(ty_kind) => AssocItemKind::Type(ty_kind),
ItemKind::MacCall(a) => AssocItemKind::MacCall(a),
@@ -3079,7 +3094,9 @@ pub enum ForeignItemKind {
impl From<ForeignItemKind> for ItemKind {
fn from(foreign_item_kind: ForeignItemKind) -> ItemKind {
match foreign_item_kind {
- ForeignItemKind::Static(a, b, c) => ItemKind::Static(a, b, c),
+ ForeignItemKind::Static(a, b, c) => {
+ ItemKind::Static(StaticItem { ty: a, mutability: b, expr: c }.into())
+ }
ForeignItemKind::Fn(fn_kind) => ItemKind::Fn(fn_kind),
ForeignItemKind::TyAlias(ty_alias_kind) => ItemKind::TyAlias(ty_alias_kind),
ForeignItemKind::MacCall(a) => ItemKind::MacCall(a),
@@ -3092,7 +3109,9 @@ impl TryFrom<ItemKind> for ForeignItemKind {
fn try_from(item_kind: ItemKind) -> Result<ForeignItemKind, ItemKind> {
Ok(match item_kind {
- ItemKind::Static(a, b, c) => ForeignItemKind::Static(a, b, c),
+ ItemKind::Static(box StaticItem { ty: a, mutability: b, expr: c }) => {
+ ForeignItemKind::Static(a, b, c)
+ }
ItemKind::Fn(fn_kind) => ForeignItemKind::Fn(fn_kind),
ItemKind::TyAlias(ty_alias_kind) => ForeignItemKind::TyAlias(ty_alias_kind),
ItemKind::MacCall(a) => ForeignItemKind::MacCall(a),
@@ -3109,8 +3128,8 @@ mod size_asserts {
use super::*;
use rustc_data_structures::static_assert_size;
// tidy-alphabetical-start
- static_assert_size!(AssocItem, 104);
- static_assert_size!(AssocItemKind, 32);
+ static_assert_size!(AssocItem, 88);
+ static_assert_size!(AssocItemKind, 16);
static_assert_size!(Attribute, 32);
static_assert_size!(Block, 32);
static_assert_size!(Expr, 72);
diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs
index 2e83b3e62..c4771115c 100644
--- a/compiler/rustc_ast/src/attr/mod.rs
+++ b/compiler/rustc_ast/src/attr/mod.rs
@@ -180,6 +180,12 @@ impl Attribute {
self.doc_str().map_or(false, |s| comments::may_have_doc_links(s.as_str()))
}
+ pub fn is_proc_macro_attr(&self) -> bool {
+ [sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]
+ .iter()
+ .any(|kind| self.has_name(*kind))
+ }
+
/// Extracts the MetaItem from inside this Attribute.
pub fn meta(&self) -> Option<MetaItem> {
match &self.kind {
@@ -627,6 +633,22 @@ pub fn mk_attr_name_value_str(
mk_attr(g, style, path, args, span)
}
+pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
+ attrs.iter().filter(move |attr| attr.has_name(name))
+}
+
+pub fn find_by_name(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> {
+ filter_by_name(attrs, name).next()
+}
+
+pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option<Symbol> {
+ find_by_name(attrs, name).and_then(|attr| attr.value_str())
+}
+
+pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {
+ find_by_name(attrs, name).is_some()
+}
+
pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
items.iter().any(|item| item.has_name(name))
}
diff --git a/compiler/rustc_ast/src/format.rs b/compiler/rustc_ast/src/format.rs
index d021bea5e..699946f30 100644
--- a/compiler/rustc_ast/src/format.rs
+++ b/compiler/rustc_ast/src/format.rs
@@ -94,7 +94,7 @@ impl FormatArguments {
}
if !matches!(arg.kind, FormatArgumentKind::Captured(..)) {
// This is an explicit argument.
- // Make sure that all arguments so far are explcit.
+ // Make sure that all arguments so far are explicit.
assert_eq!(
self.num_explicit_args,
self.arguments.len(),
@@ -131,8 +131,8 @@ impl FormatArguments {
&self.arguments[..]
}
- pub fn all_args_mut(&mut self) -> &mut [FormatArgument] {
- &mut self.arguments[..]
+ pub fn all_args_mut(&mut self) -> &mut Vec<FormatArgument> {
+ &mut self.arguments
}
}
diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs
index 7dcb03b4c..694d31d8f 100644
--- a/compiler/rustc_ast/src/mut_visit.rs
+++ b/compiler/rustc_ast/src/mut_visit.rs
@@ -7,12 +7,12 @@
//! a `MutVisitor` renaming item names in a module will miss all of those
//! that are created by the expansion of a macro.
-use crate::ast::*;
use crate::ptr::P;
use crate::token::{self, Token};
use crate::tokenstream::*;
+use crate::{ast::*, StaticItem};
-use rustc_data_structures::map_in_place::MapInPlace;
+use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
use rustc_data_structures::sync::Lrc;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::Ident;
@@ -1029,14 +1029,12 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
match kind {
ItemKind::ExternCrate(_orig_name) => {}
ItemKind::Use(use_tree) => vis.visit_use_tree(use_tree),
- ItemKind::Static(ty, _, expr) => {
+ ItemKind::Static(box StaticItem { ty, mutability: _, expr }) => {
vis.visit_ty(ty);
visit_opt(expr, |expr| vis.visit_expr(expr));
}
- ItemKind::Const(defaultness, ty, expr) => {
- visit_defaultness(defaultness, vis);
- vis.visit_ty(ty);
- visit_opt(expr, |expr| vis.visit_expr(expr));
+ ItemKind::Const(item) => {
+ visit_const_item(item, vis);
}
ItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(defaultness, vis);
@@ -1119,10 +1117,8 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
match kind {
- AssocItemKind::Const(defaultness, ty, expr) => {
- visit_defaultness(defaultness, visitor);
- visitor.visit_ty(ty);
- visit_opt(expr, |expr| visitor.visit_expr(expr));
+ AssocItemKind::Const(item) => {
+ visit_const_item(item, visitor);
}
AssocItemKind::Fn(box Fn { defaultness, generics, sig, body }) => {
visit_defaultness(defaultness, visitor);
@@ -1152,6 +1148,15 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
smallvec![item]
}
+fn visit_const_item<T: MutVisitor>(
+ ConstItem { defaultness, ty, expr }: &mut ConstItem,
+ visitor: &mut T,
+) {
+ visit_defaultness(defaultness, visitor);
+ visitor.visit_ty(ty);
+ visit_opt(expr, |expr| visitor.visit_expr(expr));
+}
+
pub fn noop_visit_fn_header<T: MutVisitor>(header: &mut FnHeader, vis: &mut T) {
let FnHeader { unsafety, asyncness, constness, ext: _ } = header;
visit_constness(constness, vis);
@@ -1316,7 +1321,6 @@ pub fn noop_visit_expr<T: MutVisitor>(
vis: &mut T,
) {
match kind {
- ExprKind::Box(expr) => vis.visit_expr(expr),
ExprKind::Array(exprs) => visit_thin_exprs(exprs, vis),
ExprKind::ConstBlock(anon_const) => {
vis.visit_anon_const(anon_const);
@@ -1408,8 +1412,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
vis.visit_block(blk);
visit_opt(label, |label| vis.visit_label(label));
}
- ExprKind::Async(_capture_by, node_id, body) => {
- vis.visit_id(node_id);
+ ExprKind::Async(_capture_by, body) => {
vis.visit_block(body);
}
ExprKind::Await(expr) => vis.visit_expr(expr),
diff --git a/compiler/rustc_ast/src/util/classify.rs b/compiler/rustc_ast/src/util/classify.rs
index cdc244c12..607b77705 100644
--- a/compiler/rustc_ast/src/util/classify.rs
+++ b/compiler/rustc_ast/src/util/classify.rs
@@ -35,7 +35,6 @@ pub fn expr_trailing_brace(mut expr: &ast::Expr) -> Option<&ast::Expr> {
| Assign(_, e, _)
| AssignOp(_, _, e)
| Binary(_, _, e)
- | Box(e)
| Break(_, Some(e))
| Let(_, e, _)
| Range(_, Some(e), _)
diff --git a/compiler/rustc_ast/src/util/parser.rs b/compiler/rustc_ast/src/util/parser.rs
index 3a0af04f9..3893875e9 100644
--- a/compiler/rustc_ast/src/util/parser.rs
+++ b/compiler/rustc_ast/src/util/parser.rs
@@ -259,7 +259,6 @@ pub enum ExprPrecedence {
Assign,
AssignOp,
- Box,
AddrOf,
Let,
Unary,
@@ -319,8 +318,7 @@ impl ExprPrecedence {
ExprPrecedence::AssignOp => AssocOp::Assign.precedence() as i8,
// Unary, prefix
- ExprPrecedence::Box
- | ExprPrecedence::AddrOf
+ ExprPrecedence::AddrOf
// Here `let pats = expr` has `let pats =` as a "unary" prefix of `expr`.
// However, this is not exactly right. When `let _ = a` is the LHS of a binop we
// need parens sometimes. E.g. we can print `(let _ = a) && b` as `let _ = a && b`
diff --git a/compiler/rustc_ast/src/visit.rs b/compiler/rustc_ast/src/visit.rs
index bdb1879ec..ac9b321b7 100644
--- a/compiler/rustc_ast/src/visit.rs
+++ b/compiler/rustc_ast/src/visit.rs
@@ -13,7 +13,7 @@
//! instance, a walker looking for item names in a module will miss all of
//! those that are created by the expansion of a macro.
-use crate::ast::*;
+use crate::{ast::*, StaticItem};
use rustc_span::symbol::Ident;
use rustc_span::Span;
@@ -305,8 +305,9 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
match &item.kind {
ItemKind::ExternCrate(_) => {}
ItemKind::Use(use_tree) => visitor.visit_use_tree(use_tree, item.id, false),
- ItemKind::Static(typ, _, expr) | ItemKind::Const(_, typ, expr) => {
- visitor.visit_ty(typ);
+ ItemKind::Static(box StaticItem { ty, mutability: _, expr })
+ | ItemKind::Const(box ConstItem { ty, expr, .. }) => {
+ visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
}
ItemKind::Fn(box Fn { defaultness: _, generics, sig, body }) => {
@@ -673,7 +674,7 @@ pub fn walk_assoc_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a AssocItem,
visitor.visit_ident(ident);
walk_list!(visitor, visit_attribute, attrs);
match kind {
- AssocItemKind::Const(_, ty, expr) => {
+ AssocItemKind::Const(box ConstItem { ty, expr, .. }) => {
visitor.visit_ty(ty);
walk_list!(visitor, visit_expr, expr);
}
@@ -772,7 +773,6 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
walk_list!(visitor, visit_attribute, expression.attrs.iter());
match &expression.kind {
- ExprKind::Box(subexpression) => visitor.visit_expr(subexpression),
ExprKind::Array(subexpressions) => {
walk_list!(visitor, visit_expr, subexpressions);
}
@@ -861,7 +861,7 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
walk_list!(visitor, visit_label, opt_label);
visitor.visit_block(block);
}
- ExprKind::Async(_, _, body) => {
+ ExprKind::Async(_, body) => {
visitor.visit_block(body);
}
ExprKind::Await(expr) => visitor.visit_expr(expr),