Skip to content

Minor AST cleanups #124324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2786,6 +2786,7 @@ pub enum AttrKind {
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct NormalAttr {
pub item: AttrItem,
// Tokens for the full attribute, e.g. `#[foo]`, `#![bar]`.
pub tokens: Option<LazyAttrTokenStream>,
}

Expand All @@ -2802,6 +2803,7 @@ impl NormalAttr {
pub struct AttrItem {
pub path: Path,
pub args: AttrArgs,
// Tokens for the meta item, e.g. just the `foo` within `#[foo]` or `#![foo]`.
pub tokens: Option<LazyAttrTokenStream>,
}

Expand Down
17 changes: 7 additions & 10 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ impl MetaItem {
}

pub fn value_str(&self) -> Option<Symbol> {
self.kind.value_str()
match &self.kind {
MetaItemKind::NameValue(v) => v.kind.str(),
_ => None,
}
}

fn from_tokens<'a, I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
Expand Down Expand Up @@ -362,13 +365,6 @@ impl MetaItem {
}

impl MetaItemKind {
pub fn value_str(&self) -> Option<Symbol> {
match self {
MetaItemKind::NameValue(v) => v.kind.str(),
_ => None,
}
}

fn list_from_tokens(tokens: TokenStream) -> Option<ThinVec<NestedMetaItem>> {
let mut tokens = tokens.trees().peekable();
let mut result = ThinVec::new();
Expand Down Expand Up @@ -468,8 +464,9 @@ impl NestedMetaItem {
self.meta_item().and_then(|meta_item| meta_item.meta_item_list())
}

/// Returns a name and single literal value tuple of the `MetaItem`.
pub fn name_value_literal(&self) -> Option<(Symbol, &MetaItemLit)> {
/// If it's a singleton list of the form `foo(lit)`, returns the `foo` and
/// the `lit`.
pub fn singleton_lit_list(&self) -> Option<(Symbol, &MetaItemLit)> {
self.meta_item().and_then(|meta_item| {
meta_item.meta_item_list().and_then(|meta_item_list| {
if meta_item_list.len() == 1
Expand Down
Loading
Loading