Skip to content

Commit 6add46f

Browse files
committed
Auto merge of rust-lang#131808 - jdonszelmann:hir-attributes, r=oli-obk,petrochenkov
Hir attributes This PR needs some explanation, it's somewhat large. - This is step one as described in rust-lang/compiler-team#796. I've added a new `hir::Attribute` which is a lowered version of `ast::Attribute`. Right now, this has few concrete effects, however every place that after this PR parses a `hir::Attribute` should later get a pre-parsed attribute as described in rust-lang/compiler-team#796 and transitively rust-lang#131229. - an extension trait `AttributeExt` is added, which is implemented for both `ast::Attribute` and `hir::Atribute`. This makes `hir::Attributes` mostly compatible with code that used to parse `ast::Attribute`. All its methods are also added as inherent methods to avoid having to import the trait everywhere in the compiler. - Incremental can not not hash `ast::Attribute` at all.
2 parents c26db43 + 1d5ec2c commit 6add46f

File tree

89 files changed

+1152
-716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+1152
-716
lines changed

Cargo.lock

+3-1
Original file line numberDiff line numberDiff line change
@@ -3521,6 +3521,7 @@ dependencies = [
35213521
"rustc_fluent_macro",
35223522
"rustc_fs_util",
35233523
"rustc_hir",
3524+
"rustc_hir_pretty",
35243525
"rustc_incremental",
35253526
"rustc_index",
35263527
"rustc_macros",
@@ -3786,6 +3787,7 @@ dependencies = [
37863787
"rustc_span",
37873788
"rustc_target",
37883789
"smallvec",
3790+
"thin-vec",
37893791
"tracing",
37903792
]
37913793

@@ -4454,9 +4456,9 @@ version = "0.0.0"
44544456
dependencies = [
44554457
"rustc_abi",
44564458
"rustc_ast",
4457-
"rustc_ast_pretty",
44584459
"rustc_data_structures",
44594460
"rustc_hir",
4461+
"rustc_hir_pretty",
44604462
"rustc_middle",
44614463
"rustc_session",
44624464
"rustc_span",

compiler/rustc_ast/src/ast.rs

+5-62
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::*;
@@ -1758,53 +1758,16 @@ pub enum AttrArgs {
17581758
Eq {
17591759
/// Span of the `=` token.
17601760
eq_span: Span,
1761-
1762-
value: AttrArgsEq,
1761+
expr: P<Expr>,
17631762
},
17641763
}
17651764

1766-
// The RHS of an `AttrArgs::Eq` starts out as an expression. Once macro
1767-
// expansion is completed, all cases end up either as a meta item literal,
1768-
// which is the form used after lowering to HIR, or as an error.
1769-
#[derive(Clone, Encodable, Decodable, Debug)]
1770-
pub enum AttrArgsEq {
1771-
Ast(P<Expr>),
1772-
Hir(MetaItemLit),
1773-
}
1774-
1775-
impl AttrArgsEq {
1776-
pub fn span(&self) -> Span {
1777-
match self {
1778-
AttrArgsEq::Ast(p) => p.span,
1779-
AttrArgsEq::Hir(lit) => lit.span,
1780-
}
1781-
}
1782-
1783-
pub fn unwrap_ast(&self) -> &Expr {
1784-
match self {
1785-
AttrArgsEq::Ast(p) => p,
1786-
AttrArgsEq::Hir(lit) => {
1787-
unreachable!("in literal form when getting inner tokens: {lit:?}")
1788-
}
1789-
}
1790-
}
1791-
1792-
pub fn unwrap_ast_mut(&mut self) -> &mut P<Expr> {
1793-
match self {
1794-
AttrArgsEq::Ast(p) => p,
1795-
AttrArgsEq::Hir(lit) => {
1796-
unreachable!("in literal form when getting inner tokens: {lit:?}")
1797-
}
1798-
}
1799-
}
1800-
}
1801-
18021765
impl AttrArgs {
18031766
pub fn span(&self) -> Option<Span> {
18041767
match self {
18051768
AttrArgs::Empty => None,
18061769
AttrArgs::Delimited(args) => Some(args.dspan.entire()),
1807-
AttrArgs::Eq { eq_span, value } => Some(eq_span.to(value.span())),
1770+
AttrArgs::Eq { eq_span, expr } => Some(eq_span.to(expr.span)),
18081771
}
18091772
}
18101773

@@ -1814,27 +1777,7 @@ impl AttrArgs {
18141777
match self {
18151778
AttrArgs::Empty => TokenStream::default(),
18161779
AttrArgs::Delimited(args) => args.tokens.clone(),
1817-
AttrArgs::Eq { value, .. } => TokenStream::from_ast(value.unwrap_ast()),
1818-
}
1819-
}
1820-
}
1821-
1822-
impl<CTX> HashStable<CTX> for AttrArgs
1823-
where
1824-
CTX: crate::HashStableContext,
1825-
{
1826-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
1827-
mem::discriminant(self).hash_stable(ctx, hasher);
1828-
match self {
1829-
AttrArgs::Empty => {}
1830-
AttrArgs::Delimited(args) => args.hash_stable(ctx, hasher),
1831-
AttrArgs::Eq { value: AttrArgsEq::Ast(expr), .. } => {
1832-
unreachable!("hash_stable {:?}", expr);
1833-
}
1834-
AttrArgs::Eq { eq_span, value: AttrArgsEq::Hir(lit) } => {
1835-
eq_span.hash_stable(ctx, hasher);
1836-
lit.hash_stable(ctx, hasher);
1837-
}
1780+
AttrArgs::Eq { expr, .. } => TokenStream::from_ast(expr),
18381781
}
18391782
}
18401783
}
@@ -3051,7 +2994,7 @@ impl NormalAttr {
30512994
}
30522995
}
30532996

3054-
#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
2997+
#[derive(Clone, Encodable, Decodable, Debug)]
30552998
pub struct AttrItem {
30562999
pub unsafety: Safety,
30573000
pub path: Path,

0 commit comments

Comments
 (0)