Skip to content

Commit a91481d

Browse files
committed
change ast::Attribute removing hir-specific parts
1 parent 76067b1 commit a91481d

File tree

5 files changed

+17
-70
lines changed

5 files changed

+17
-70
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! - [`UnOp`], [`BinOp`], and [`BinOpKind`]: Unary and binary operators.
2020
2121
use std::borrow::Cow;
22-
use std::{cmp, fmt, mem};
22+
use std::{cmp, fmt};
2323

2424
pub use GenericArgs::*;
2525
pub use UnsafeSource::*;
@@ -1688,28 +1688,16 @@ pub enum AttrArgs {
16881688
/// Span of the `=` token.
16891689
Span,
16901690
/// The "value".
1691-
AttrArgsEq,
1691+
P<Expr>,
16921692
),
16931693
}
16941694

1695-
// The RHS of an `AttrArgs::Eq` starts out as an expression. Once macro
1696-
// expansion is completed, all cases end up either as a meta item literal,
1697-
// which is the form used after lowering to HIR, or as an error.
1698-
#[derive(Clone, Encodable, Decodable, Debug)]
1699-
pub enum AttrArgsEq {
1700-
Ast(P<Expr>),
1701-
Hir(MetaItemLit),
1702-
}
1703-
17041695
impl AttrArgs {
17051696
pub fn span(&self) -> Option<Span> {
17061697
match self {
17071698
AttrArgs::Empty => None,
17081699
AttrArgs::Delimited(args) => Some(args.dspan.entire()),
1709-
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => Some(eq_span.to(expr.span)),
1710-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
1711-
unreachable!("in literal form when getting span: {:?}", lit);
1712-
}
1700+
AttrArgs::Eq(eq_span, expr) => Some(eq_span.to(expr.span)),
17131701
}
17141702
}
17151703

@@ -1719,30 +1707,7 @@ impl AttrArgs {
17191707
match self {
17201708
AttrArgs::Empty => TokenStream::default(),
17211709
AttrArgs::Delimited(args) => args.tokens.clone(),
1722-
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => TokenStream::from_ast(expr),
1723-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => {
1724-
unreachable!("in literal form when getting inner tokens: {:?}", lit)
1725-
}
1726-
}
1727-
}
1728-
}
1729-
1730-
impl<CTX> HashStable<CTX> for AttrArgs
1731-
where
1732-
CTX: crate::HashStableContext,
1733-
{
1734-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
1735-
mem::discriminant(self).hash_stable(ctx, hasher);
1736-
match self {
1737-
AttrArgs::Empty => {}
1738-
AttrArgs::Delimited(args) => args.hash_stable(ctx, hasher),
1739-
AttrArgs::Eq(_eq_span, AttrArgsEq::Ast(expr)) => {
1740-
unreachable!("hash_stable {:?}", expr);
1741-
}
1742-
AttrArgs::Eq(eq_span, AttrArgsEq::Hir(lit)) => {
1743-
eq_span.hash_stable(ctx, hasher);
1744-
lit.hash_stable(ctx, hasher);
1745-
}
1710+
AttrArgs::Eq(_, expr) => TokenStream::from_ast(expr),
17461711
}
17471712
}
17481713
}
@@ -2892,7 +2857,7 @@ pub enum AttrStyle {
28922857
}
28932858

28942859
/// A list of attributes.
2895-
pub type AttrVec = ThinVec<Attribute>;
2860+
pub type AttrVec<A = Attribute> = ThinVec<A>;
28962861

28972862
/// A syntax-level representation of an attribute.
28982863
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -2937,7 +2902,7 @@ impl NormalAttr {
29372902
}
29382903
}
29392904

2940-
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2905+
#[derive(Clone, Encodable, Decodable, Debug)]
29412906
pub struct AttrItem {
29422907
pub unsafety: Safety,
29432908
pub path: Path,
@@ -3078,8 +3043,8 @@ impl VariantData {
30783043

30793044
/// An item definition.
30803045
#[derive(Clone, Encodable, Decodable, Debug)]
3081-
pub struct Item<K = ItemKind> {
3082-
pub attrs: AttrVec,
3046+
pub struct Item<K = ItemKind, A = Attribute> {
3047+
pub attrs: AttrVec<A>,
30833048
pub id: NodeId,
30843049
pub span: Span,
30853050
pub vis: Visibility,

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ use rustc_span::symbol::{Ident, Symbol, sym};
99
use smallvec::{SmallVec, smallvec};
1010
use thin_vec::{ThinVec, thin_vec};
1111

12+
use crate::MetaItemLit;
1213
use crate::ast::{
13-
AttrArgs, AttrArgsEq, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute, DUMMY_NODE_ID,
14-
DelimArgs, Expr, ExprKind, LitKind, MetaItem, MetaItemInner, MetaItemKind, MetaItemLit,
15-
NormalAttr, Path, PathSegment, Safety,
14+
AttrArgs, AttrId, AttrItem, AttrKind, AttrStyle, AttrVec, Attribute, DUMMY_NODE_ID, DelimArgs,
15+
Expr, ExprKind, LitKind, MetaItem, MetaItemInner, MetaItemKind, NormalAttr, Path, PathSegment,
16+
Safety,
1617
};
1718
use crate::ptr::P;
1819
use crate::token::{self, CommentKind, Delimiter, Token};
@@ -253,20 +254,6 @@ impl AttrItem {
253254
}
254255
}
255256

256-
impl AttrArgsEq {
257-
fn value_str(&self) -> Option<Symbol> {
258-
match self {
259-
AttrArgsEq::Ast(expr) => match expr.kind {
260-
ExprKind::Lit(token_lit) => {
261-
LitKind::from_token_lit(token_lit).ok().and_then(|lit| lit.str())
262-
}
263-
_ => None,
264-
},
265-
AttrArgsEq::Hir(lit) => lit.kind.str(),
266-
}
267-
}
268-
}
269-
270257
impl MetaItem {
271258
/// For a single-segment meta item, returns its name; otherwise, returns `None`.
272259
pub fn ident(&self) -> Option<Ident> {
@@ -446,7 +433,7 @@ impl MetaItemKind {
446433
MetaItemKind::list_from_tokens(tokens.clone()).map(MetaItemKind::List)
447434
}
448435
AttrArgs::Delimited(..) => None,
449-
AttrArgs::Eq(_, AttrArgsEq::Ast(expr)) => match expr.kind {
436+
AttrArgs::Eq(_, expr) => match expr.kind {
450437
ExprKind::Lit(token_lit) => {
451438
// Turn failures to `None`, we'll get parse errors elsewhere.
452439
MetaItemLit::from_token_lit(token_lit, expr.span)
@@ -455,7 +442,6 @@ impl MetaItemKind {
455442
}
456443
_ => None,
457444
},
458-
AttrArgs::Eq(_, AttrArgsEq::Hir(lit)) => Some(MetaItemKind::NameValue(lit.clone())),
459445
}
460446
}
461447
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,10 @@ fn visit_attr_args<T: MutVisitor>(vis: &mut T, args: &mut AttrArgs) {
394394
match args {
395395
AttrArgs::Empty => {}
396396
AttrArgs::Delimited(args) => visit_delim_args(vis, args),
397-
AttrArgs::Eq(eq_span, AttrArgsEq::Ast(expr)) => {
397+
AttrArgs::Eq(eq_span, expr) => {
398398
vis.visit_expr(expr);
399399
vis.visit_span(eq_span);
400400
}
401-
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(lit)) => {
402-
unreachable!("in literal form when visiting mac args eq: {:?}", lit)
403-
}
404401
}
405402
}
406403

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,10 +1234,7 @@ pub fn walk_attr_args<'a, V: Visitor<'a>>(visitor: &mut V, args: &'a AttrArgs) -
12341234
match args {
12351235
AttrArgs::Empty => {}
12361236
AttrArgs::Delimited(_args) => {}
1237-
AttrArgs::Eq(_eq_span, AttrArgsEq::Ast(expr)) => try_visit!(visitor.visit_expr(expr)),
1238-
AttrArgs::Eq(_eq_span, AttrArgsEq::Hir(lit)) => {
1239-
unreachable!("in literal form when walking mac args eq: {:?}", lit)
1240-
}
1237+
AttrArgs::Eq(_eq_span, expr) => try_visit!(visitor.visit_expr(expr)),
12411238
}
12421239
V::Result::output()
12431240
}

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ use crate::mbe::macro_parser::NamedMatch::*;
4141
use crate::mbe::macro_parser::{Error, ErrorReported, Failure, MatcherLoc, Success, TtParser};
4242
use crate::mbe::transcribe::transcribe;
4343

44+
type Item<A> = ast::Item<ast::ItemKind, A>;
45+
4446
pub(crate) struct ParserAnyMacro<'a> {
4547
parser: Parser<'a>,
4648

0 commit comments

Comments
 (0)