about summary refs log tree commit diff
path: root/compiler/rustc_ast/src/attr/mod.rs
AgeCommit message (Collapse)AuthorLines
2023-12-11Add spacing information to delimiters.Nicholas Nethercote-3/+3
This is an extension of the previous commit. It means the output of something like this: ``` stringify!(let a: Vec<u32> = vec![];) ``` goes from this: ``` let a: Vec<u32> = vec![] ; ``` With this PR, it now produces this string: ``` let a: Vec<u32> = vec![]; ```
2023-11-21Fix `clippy::needless_borrow` in the compilerNilstrieb-1/+1
`x clippy compiler -Aclippy::all -Wclippy::needless_borrow --fix`. Then I had to remove a few unnecessary parens and muts that were exposed now.
2023-11-16More detail when expecting expression but encountering bad macro argumentEsteban Küber-1/+1
Partially address #71039.
2023-10-13Format all the let chains in compilerMichael Goulet-3/+1
2023-10-12Use `TokenStream::token_alone` in one place.Nicholas Nethercote-4/+4
2023-09-12`#[diagnostic::on_unimplemented]` without filtersGeorg Semmler-0/+16
This commit adds support for a `#[diagnostic::on_unimplemented]` attribute with the following options: * `message` to customize the primary error message * `note` to add a customized note message to an error message * `label` to customize the label part of the error message Co-authored-by: León Orell Valerian Liehr <me@fmease.dev> Co-authored-by: Michael Goulet <michael@errs.io>
2023-08-03Remove `MacDelimiter`.Nicholas Nethercote-8/+6
It's the same as `Delimiter`, minus the `Invisible` variant. I'm generally in favour of using types to make impossible states unrepresentable, but this one feels very low-value, and the conversions between the two types are annoying and confusing. Look at the change in `src/tools/rustfmt/src/expr.rs` for an example: the old code converted from `MacDelimiter` to `Delimiter` and back again, for no good reason. This suggests the author was confused about the types.
2023-07-27Make `TokenTree::uninterpolate` take `&self` and return a `Cow`.Nicholas Nethercote-7/+7
Making it similar to `Token::uninterpolate`. This avoids some more token tree cloning.
2023-07-27Use `TokenStream::trees` instead of `into_trees` for attributes.Nicholas Nethercote-14/+13
This avoids cloning some token trees. A couple of `clone` calls were inserted, but only on some paths, and the next commit will remove them.
2023-05-24Use `Option::is_some_and` and `Result::is_ok_and` in the compilerMaybe Waffle-3/+3
2023-04-16Remove WorkerLocal from AttrIdGeneratorJohn Kåre Alsaker-32/+4
2023-03-22rustc: Remove unused `Session` argument from some attribute functionsVadim Petrochenkov-0/+22
2023-02-21Use `ThinVec` in various AST types.Nicholas Nethercote-5/+5
This commit changes the sequence parsers to produce `ThinVec`, which triggers numerous conversions.
2023-02-11rustc_ast: Merge impls and reorder methods for attributes and meta itemsVadim Petrochenkov-311/+307
2023-02-01ast: Optimize list and value extraction primitives for attributesVadim Petrochenkov-23/+43
It's not necessary to convert the whole attribute into a meta item to extract something specific
2023-01-05Fix `uninlined_format_args` for some compiler cratesnils-1/+1
Convert all the crates that have had their diagnostic migration completed (except save_analysis because that will be deleted soon and apfloat because of the licensing problem).
2022-12-05Remove three uses of `LitKind::synthesize_token_lit`.Nicholas Nethercote-2/+3
2022-12-05Remove `mk_name_value_item{,_str}`.Nicholas Nethercote-15/+0
There are better ways to create the meta items. - In the rustdoc tests, the commit adds `dummy_meta_item_name_value`, which matches the existing `dummy_meta_item_word` function and `dummy_meta_item_list` macro. - In `types.rs` the commit clones the existing meta item and then modifies the clone.
2022-12-02Remove `token::Lit` from `ast::MetaItemLit`.Nicholas Nethercote-1/+3
`token::Lit` contains a `kind` field that indicates what kind of literal it is. `ast::MetaItemLit` currently wraps a `token::Lit` but also has its own `kind` field. This means that `ast::MetaItemLit` encodes the literal kind in two different ways. This commit changes `ast::MetaItemLit` so it no longer wraps `token::Lit`. It now contains the `symbol` and `suffix` fields from `token::Lit`, but not the `kind` field, eliminating the redundancy.
2022-12-02Rename `LitKind::to_token_lit` as `LitKind::synthesize_token_lit`.Nicholas Nethercote-2/+2
This makes it clearer that it's not a lossless conversion, which I find helpful.
2022-11-29Avoid more `MetaItem`-to-`Attribute` conversions.Nicholas Nethercote-115/+52
There is code for converting `Attribute` (syntactic) to `MetaItem` (semantic). There is also code for the reverse direction. The reverse direction isn't really necessary; it's currently only used when generating attributes, e.g. in `derive` code. This commit adds some new functions for creating `Attributes`s directly, without involving `MetaItem`s: `mk_attr_word`, `mk_attr_name_value_str`, `mk_attr_nested_word`, and `ExtCtxt::attr_{word,name_value_str,nested_word}`. These new methods replace the old functions for creating `Attribute`s: `mk_attr_inner`, `mk_attr_outer`, and `ExtCtxt::attribute`. Those functions took `MetaItem`s as input, and relied on many other functions that created `MetaItems`, which are also removed: `mk_name_value_item`, `mk_list_item`, `mk_word_item`, `mk_nested_word_item`, `{MetaItem,MetaItemKind,NestedMetaItem}::token_trees`, `MetaItemKind::attr_args`, `MetaItemLit::{from_lit_kind,to_token}`, `ExtCtxt::meta_word`. Overall this cuts more than 100 lines of code and makes thing simpler.
2022-11-29Inline and remove `MetaItemLit::from_lit_kind`.Nicholas Nethercote-2/+2
It has a single call site.
2022-11-28Rename `NestedMetaItem::[Ll]iteral` as `NestedMetaItem::[Ll]it`.Nicholas Nethercote-6/+6
We already use a mix of `Literal` and `Lit`. The latter is better because it is shorter without causing any ambiguity.
2022-11-28Rename `ast::Lit` as `ast::MetaItemLit`.Nicholas Nethercote-9/+9
2022-11-27Prefer doc comments over `//`-comments in compilerMaybe Waffle-5/+7
2022-11-23Fix an ICE parsing a malformed attribute.Nicholas Nethercote-3/+6
Fixes #104620.
2022-11-22Split `MacArgs` in two.Nicholas Nethercote-26/+28
`MacArgs` is an enum with three variants: `Empty`, `Delimited`, and `Eq`. It's used in two ways: - For representing attribute macro arguments (e.g. in `AttrItem`), where all three variants are used. - For representing function-like macros (e.g. in `MacCall` and `MacroDef`), where only the `Delimited` variant is used. In other words, `MacArgs` is used in two quite different places due to them having partial overlap. I find this makes the code hard to read. It also leads to various unreachable code paths, and allows invalid values (such as accidentally using `MacArgs::Empty` in a `MacCall`). This commit splits `MacArgs` in two: - `DelimArgs` is a new struct just for the "delimited arguments" case. It is now used in `MacCall` and `MacroDef`. - `AttrArgs` is a renaming of the old `MacArgs` enum for the attribute macro case. Its `Delimited` variant now contains a `DelimArgs`. Various other related things are renamed as well. These changes make the code clearer, avoids several unreachable paths, and disallows the invalid values.
2022-11-21Remove `ref` patterns from `rustc_ast`Maybe Waffle-46/+44
Also use if let chains in one case.
2022-11-17Use `ThinVec` in `ast::Path`.Nicholas Nethercote-4/+3
2022-11-16Use `token::Lit` in `ast::ExprKind::Lit`.Nicholas Nethercote-5/+7
Instead of `ast::Lit`. Literal lowering now happens at two different times. Expression literals are lowered when HIR is crated. Attribute literals are lowered during parsing. This commit changes the language very slightly. Some programs that used to not compile now will compile. This is because some invalid literals that are removed by `cfg` or attribute macros will no longer trigger errors. See this comment for more details: https://github.com/rust-lang/rust/pull/102944#issuecomment-1277476773
2022-09-14add debug assertion for max `attr_id`SparrowLii-1/+24
2022-09-14make `mk_attr_id` part of `ParseSess`SparrowLii-15/+32
2022-09-13Rollup merge of #101752 - GuillaumeGomez:improve-attr-docs, r=lqdMatthias Krüger-1/+11
Improve Attribute doc methods r? `@lqd`
2022-09-12Improve Attribute doc methodsGuillaume Gomez-1/+11
2022-09-09Rename `{Create,Lazy}TokenStream` as `{To,Lazy}AttrTokenStream`.Nicholas Nethercote-3/+3
`To` is better than `Create` for indicating that this is a non-consuming conversion, rather than creating something out of nothing. And the addition of `Attr` is because the current names makes them sound like they relate to `TokenStream`, but really they relate to `AttrTokenStream`.
2022-09-09Change return type of `Attribute::tokens`.Nicholas Nethercote-9/+7
The `AttrTokenStream` is always immediately turned into a `TokenStream`.
2022-09-09Rename `AttrAnnotatedToken{Stream,Tree}`.Nicholas Nethercote-3/+3
These two type names are long and have long matching prefixes. I find them hard to read, especially in combinations like `AttrAnnotatedTokenStream::new(vec![AttrAnnotatedTokenTree::Token(..)])`. This commit renames them as `AttrToken{Stream,Tree}`.
2022-09-09Move `Spacing` out of `AttrAnnotatedTokenStream`.Nicholas Nethercote-7/+6
And into `AttrAnnotatedTokenTree::Token`. PR #99887 did the same thing for `TokenStream`.
2022-09-07Add documentation for Attr::is_doc_commentGuillaume Gomez-0/+2
2022-08-22Use `AttrVec` in more places.Nicholas Nethercote-2/+1
In some places we use `Vec<Attribute>` and some places we use `ThinVec<Attribute>` (a.k.a. `AttrVec`). This results in various points where we have to convert between `Vec` and `ThinVec`. This commit changes the places that use `Vec<Attribute>` to use `AttrVec`. A lot of this is mechanical and boring, but there are some interesting parts: - It adds a few new methods to `ThinVec`. - It implements `MapInPlace` for `ThinVec`, and introduces a macro to avoid the repetition of this trait for `Vec`, `SmallVec`, and `ThinVec`. Overall, it makes the code a little nicer, and has little effect on performance. But it is a precursor to removing `rustc_data_structures::thin_vec::ThinVec` and replacing it with `thin_vec::ThinVec`, which is implemented more efficiently.
2022-08-17Rollup merge of #100018 - nnethercote:clean-up-LitKind, r=petrochenkovMatthias Krüger-7/+1
Clean up `LitKind` r? ``@petrochenkov``
2022-08-16Avoid code duplication in `{MetaItem,MetaItemKind}::value_str`.Nicholas Nethercote-7/+1
The two methods are almost identical.
2022-08-16Shrink `ast::Attribute`.Nicholas Nethercote-17/+26
2022-07-29Remove `TreeAndSpacing`.Nicholas Nethercote-35/+36
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is not quite right. `Spacing` makes sense for `TokenTree::Token`, but does not make sense for `TokenTree::Delimited`, because a `TokenTree::Delimited` cannot be joined with another `TokenTree`. This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`, changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the `TreeAndSpacing` typedef. The commit removes these two impls: - `impl From<TokenTree> for TokenStream` - `impl From<TokenTree> for TreeAndSpacing` These were useful, but also resulted in code with many `.into()` calls that was hard to read, particularly for anyone not highly familiar with the relevant types. This commit makes some other changes to compensate: - `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`. - `TokenStream::token_{alone,joint}()` are added. - `TokenStream::delimited` is added. This results in things like this: ```rust TokenTree::token(token::Semi, stmt.span).into() ``` changing to this: ```rust TokenStream::token_alone(token::Semi, stmt.span) ``` This makes the type of the result, and its spacing, clearer. These changes also simplifies `Cursor` and `CursorRef`, because they no longer need to distinguish between `next` and `next_with_spacing`.
2022-05-20Remove `crate` visibility usage in compilerJacob Pratt-1/+1
2022-05-18use `CursorRef` more, to not to clone `Tree`sklensy-1/+1
2022-05-05Overhaul `MacArgs::Eq`.Nicholas Nethercote-4/+19
The value in `MacArgs::Eq` is currently represented as a `Token`. Because of `TokenKind::Interpolated`, `Token` can be either a token or an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a literal or macro call AST fragment, and then is later lowered to a literal token. But this is very non-obvious. `Token` is a much more general type than what is needed. This commit restricts things, by introducing a new type `MacArgsEqKind` that is either an AST expression (pre-lowering) or an AST literal (post-lowering). The downside is that the code is a bit more verbose in a few places. The benefit is that makes it much clearer what the possibilities are (though also shorter in some other places). Also, it removes one use of `TokenKind::Interpolated`, taking us a step closer to removing that variant, which will let us make `Token` impl `Copy` and remove many "handle Interpolated" code paths in the parser. Things to note: - Error messages have improved. Messages like this: ``` unexpected token: `"bug" + "found"` ``` now say "unexpected expression", which makes more sense. Although arbitrary expressions can exist within tokens thanks to `TokenKind::Interpolated`, that's not obvious to anyone who doesn't know compiler internals. - In `parse_mac_args_common`, we no longer need to collect tokens for the value expression.
2022-04-28rustc_ast: Harmonize delimiter naming with `proc_macro::Delimiter`Vadim Petrochenkov-5/+5
2022-04-19rustc_metadata: Store a flag telling whether an item may have doc links in ↵Vadim Petrochenkov-0/+5
its attributes This should be cheap on rustc side, but it's significant optimization for rustdoc that won't need to decode and process attributes unnecessarily
2022-04-07Shrink `Nonterminal`.Nicholas Nethercote-1/+1
By heap allocating the argument within `NtPath`, `NtVis`, and `NtStmt`. This slightly reduces cumulative and peak allocation amounts, most notably on `deep-vector`.