about summary refs log tree commit diff
path: root/compiler/rustc_ast/src/util
AgeCommit message (Collapse)AuthorLines
2025-07-31Deduplicate `IntTy`/`UintTy`/`FloatTy`.Nicholas Nethercote-3/+3
There are identical definitions in `rustc_type_ir` and `rustc_ast`. This commit removes them and places a single definition in `rustc_ast_ir`. This requires adding `rust_span` as a dependency of `rustc_ast_ir`, but means a bunch of silly conversion functions can be removed. The one annoying wrinkle is that the old version had differences in their `Debug` impls, e.g. one printed `u32` while the other printed `U32`. Some compiler error messages rely on the former (yuk), and some clippy output depends on the latter. So the commit also changes clippy to not rely on `Debug` and just implement what it needs itself.
2025-07-08update to literal-escaper-0.0.5Marijn Schouten-3/+3
2025-07-06compiler: rename {ast,hir}::BareFn* to FnPtr*Jubilee Young-1/+1
Fix some comments and related types and locals where it is obvious, e.g. - bare_fn -> fn_ptr - LifetimeBinderKind::BareFnType -> LifetimeBinderKind::FnPtrType Co-authored-by: León Orell Valerian Liehr <me@fmease.dev>
2025-06-30Introduce `ByteSymbol`.Nicholas Nethercote-13/+12
It's like `Symbol` but for byte strings. The interner is now used for both `Symbol` and `ByteSymbol`. E.g. if you intern `"dog"` and `b"dog"` you'll get a `Symbol` and a `ByteSymbol` with the same index and the characters will only be stored once. The motivation for this is to eliminate the `Arc`s in `ast::LitKind`, to make `ast::LitKind` impl `Copy`, and to avoid the need to arena-allocate `ast::LitKind` in HIR. The latter change reduces peak memory by a non-trivial amount on literal-heavy benchmarks such as `deep-vector` and `tuple-stress`. `Encoder`, `Decoder`, `SpanEncoder`, and `SpanDecoder` all get some changes so that they can handle normal strings and byte strings. This change does slow down compilation of programs that use `include_bytes!` on large files, because the contents of those files are now interned (hashed). This makes `include_bytes!` more similar to `include_str!`, though `include_bytes!` contents still aren't escaped, and hashing is still much cheaper than escaping.
2025-06-23update to literal-escaper 0.0.4 for better API without `unreachable` and ↵Marijn Schouten-8/+7
faster string parsing
2025-04-04Replace `rustc_lexer/unescape` with `rustc-literal-escaper` crateGuillaume Gomez-1/+1
2025-04-03Tighten up assignment operator representations.Nicholas Nethercote-12/+12
In the AST, currently we use `BinOpKind` within `ExprKind::AssignOp` and `AssocOp::AssignOp`, even though this allows some nonsensical combinations. E.g. there is no `&&=` operator. Likewise for HIR and THIR. This commit introduces `AssignOpKind` which only includes the ten assignable operators, and uses it in `ExprKind::AssignOp` and `AssocOp::AssignOp`. (And does similar things for `hir::ExprKind` and `thir::ExprKind`.) This avoids the possibility of nonsensical combinations, as seen by the removal of the `bug!` case in `lang_item_for_binop`. The commit is mostly plumbing, including: - Adds an `impl From<AssignOpKind> for BinOpKind` (AST) and `impl From<AssignOp> for BinOp` (MIR/THIR). - `BinOpCategory` can now be created from both `BinOpKind` and `AssignOpKind`. - Replaces the `IsAssign` type with `Op`, which has more information and a few methods. - `suggest_swapping_lhs_and_rhs`: moves the condition to the call site, it's easier that way. - `check_expr_inner`: had to factor out some code into a separate method. I'm on the fence about whether avoiding the nonsensical combinations is worth the extra code.
2025-03-18Refactor YieldKind so postfix yield must have an expressionEric Holk-3/+7
2025-03-14Preserve yield position during pretty printingEric Holk-2/+2
2025-03-06Implement .use keyword as an alias of cloneSantiago Pastorino-0/+2
2025-03-03Replace `ast::TokenKind::BinOp{,Eq}` and remove `BinOpToken`.Nicholas Nethercote-21/+21
`BinOpToken` is badly named, because it only covers the assignable binary ops and excludes comparisons and `&&`/`||`. Its use in `ast::TokenKind` does allow a small amount of code sharing, but it's a clumsy factoring. This commit removes `ast::TokenKind::BinOp{,Eq}`, replacing each one with 10 individual variants. This makes `ast::TokenKind` more similar to `rustc_lexer::TokenKind`, which has individual variants for all operators. Although the number of lines of code increases, the number of chars decreases due to the frequent use of shorter names like `token::Plus` instead of `token::BinOp(BinOpToken::Plus)`.
2025-02-27Rename `AssocOp::As` as `AssocOp::Cast`.Nicholas Nethercote-7/+7
To match `ExprKind::Cast`, and because a semantic name makes more sense here than a syntactic name.
2025-02-27Replace `AssocOp::DotDot{,Eq}` with `AssocOp::Range`.Nicholas Nethercote-12/+9
It makes `AssocOp` more similar to `ExprKind` and makes things a little simpler. And the semantic names make more sense here than the syntactic names.
2025-02-27Introduce `AssocOp::Binary`.Nicholas Nethercote-140/+42
It mirrors `ExprKind::Binary`, and contains a `BinOpKind`. This makes `AssocOp` more like `ExprKind`. Note that the variants removed from `AssocOp` are all named differently to `BinOpToken`, e.g. `Multiply` instead of `Mul`, so that's an inconsistency removed. The commit adds `precedence` and `fixity` methods to `BinOpKind`, and calls them from the corresponding methods in `AssocOp`. This avoids the need to create an `AssocOp` from a `BinOpKind` in a bunch of places, and `AssocOp::from_ast_binop` is removed. `AssocOp::to_ast_binop` is also no longer needed. Overall things are shorter and nicer.
2025-02-27In `AssocOp::AssignOp`, use `BinOpKind` instead of `BinOpToken`Nicholas Nethercote-8/+17
`AssocOp::AssignOp` contains a `BinOpToken`. `ExprKind::AssignOp` contains a `BinOpKind`. Given that `AssocOp` is basically a cut-down version of `ExprKind`, it makes sense to make `AssocOp` more like `ExprKind`. Especially given that `AssocOp` and `BinOpKind` use semantic operation names (e.g. `Mul`, `Div`), but `BinOpToken` uses syntactic names (e.g. `Star`, `Slash`). This results in more concise code, and removes the need for various conversions. (Note that the removed functions `hirbinop2assignop` and `astbinop2assignop` are semantically identical, because `hir::BinOp` is just a synonum for `ast::BinOp`!) The only downside to this is that it allows the possibility of some nonsensical combinations, such as `AssocOp::AssignOp(BinOpKind::Lt)`. But `ExprKind::AssignOp` already has that problem. The problem can be fixed for both types in the future with some effort, by introducing an `AssignOpKind` type.
2025-02-03tree-wide: parallel: Fully removed all `Lrc`, replaced with `Arc`Askar Safin-2/+2
2025-01-26rustc_ast: replace some len-checks + indexing with slice patterns etc. 🧹Yotam Ofek-2/+2
2025-01-08update cfg(bootstrap)Pietro Albini-1/+1
2024-12-20Change comparison operators to have Fixity::NoneDavid Tolnay-3/+4
2024-12-21Rollup merge of #133782 - dtolnay:closuresjumps, r=spastorino,traviscrossMatthias Krüger-2/+1
Precedence improvements: closures and jumps This PR fixes some cases where rustc's pretty printers would redundantly parenthesize expressions that didn't need it. <table> <tr><th>Before</th><th>After</th></tr> <tr><td><code>return (|x: i32| x)</code></td><td><code>return |x: i32| x</code></td></tr> <tr><td><code>(|| -> &mut () { std::process::abort() }).clone()</code></td><td><code>|| -> &mut () { std::process::abort() }.clone()</code></td></tr> <tr><td><code>(continue) + 1</code></td><td><code>continue + 1</code></td></tr> </table> Tested by `echo "fn main() { let _ = $AFTER; }" | rustc -Zunpretty=expanded /dev/stdin`. The pretty-printer aims to render the syntax tree as it actually exists in rustc, as faithfully as possible, in Rust syntax. It can insert parentheses where forced by Rust's grammar in order to preserve the meaning of a macro-generated syntax tree, for example in the case of `a * $rhs` where $rhs is `b + c`. But for any expression parsed from source code, without a macro involved, there should never be a reason for inserting additional parentheses not present in the original. For closures and jumps (return, break, continue, yield, do yeet, become) the unneeded parentheses came from the precedence of some of these expressions being misidentified. In the same order as the table above: - Jumps and closures are supposed to have equal precedence. The [Rust Reference](https://doc.rust-lang.org/1.83.0/reference/expressions.html#expression-precedence) says so, and in Syn they do. There is no Rust syntax that would require making a precedence distinction between jumps and closures. But in rustc these were previously 2 distinct levels with the closure being lower, hence the parentheses around a closure inside a jump (but not a jump inside a closure). - When a closure is written with an explicit return type, the grammar [requires](https://doc.rust-lang.org/1.83.0/reference/expressions/closure-expr.html) that the closure body consists of exactly one block expression, not any other arbitrary expression as usual for closures. Parsing of the closure body does not continue after the block expression. So in `|| { 0 }.clone()` the clone is inside the closure body and applies to `{ 0 }`, whereas in `|| -> _ { 0 }.clone()` the clone is outside and applies to the closure as a whole. - Continue never needs parentheses. It was previously marked as having the lowest possible precedence but it should have been the highest, next to paths and loops and function calls, not next to jumps.
2024-12-18Re-export more `rustc_span::symbol` things from `rustc_span`.Nicholas Nethercote-3/+2
`rustc_span::symbol` defines some things that are re-exported from `rustc_span`, such as `Symbol` and `sym`. But it doesn't re-export some closely related things such as `Ident` and `kw`. So you can do `use rustc_span::{Symbol, sym}` but you have to do `use rustc_span::symbol::{Ident, kw}`, which is inconsistent for no good reason. This commit re-exports `Ident`, `kw`, and `MacroRulesNormalizedIdent`, and changes many `rustc_span::symbol::` qualifiers in `compiler/` to `rustc_span::`. This is a 200+ net line of code reduction, mostly because many files with two `use rustc_span` items can be reduced to one.
2024-12-12Add unwrap_unsafe_binder and wrap_unsafe_binder macro operatorsMichael Goulet-0/+2
2024-12-12Parsing unsafe bindersMichael Goulet-0/+4
2024-12-11allow `symbol_intern_string_literal` lint in test modulesonur-ozkan-0/+2
Signed-off-by: onur-ozkan <work@onurozkan.dev>
2024-12-02Squash closures and jumps into a single precedence levelDavid Tolnay-2/+1
2024-11-30Eliminate magic numbers from expression precedenceDavid Tolnay-23/+51
2024-11-30Eliminate PREC_FORCE_PARENDavid Tolnay-1/+0
2024-11-17Inline ExprPrecedence::order into Expr::precedenceDavid Tolnay-115/+0
2024-10-15Auto merge of #131723 - matthiaskrgr:rollup-krcslig, r=matthiaskrgrbors-1/+1
Rollup of 9 pull requests Successful merges: - #122670 (Fix bug where `option_env!` would return `None` when env var is present but not valid Unicode) - #131095 (Use environment variables instead of command line arguments for merged doctests) - #131339 (Expand set_ptr_value / with_metadata_of docs) - #131652 (Move polarity into `PolyTraitRef` rather than storing it on the side) - #131675 (Update lint message for ABI not supported) - #131681 (Fix up-to-date checking for run-make tests) - #131702 (Suppress import errors for traits that couldve applied for method lookup error) - #131703 (Resolved python deprecation warning in publish_toolstate.py) - #131710 (Remove `'apostrophes'` from `rustc_parse_format`) r? `@ghost` `@rustbot` modify labels: rollup
2024-10-15Rollup merge of #130635 - eholk:pin-reborrow-sugar, r=compiler-errorsMatthias Krüger-1/+3
Add `&pin (mut|const) T` type position sugar This adds parser support for `&pin mut T` and `&pin const T` references. These are desugared to `Pin<&mut T>` and `Pin<&T>` in the AST lowering phases. This PR currently includes #130526 since that one is in the commit queue. Only the most recent commits (bd450027eb4a94b814a7dd9c0fa29102e6361149 and following) are new. Tracking: - #130494 r? `@compiler-errors`
2024-10-14Move trait bound modifiers into ast::PolyTraitRefMichael Goulet-1/+1
2024-10-07Add sugar for &pin (const|mut) typesEric Holk-1/+3
2024-10-01Remove anon struct and union typesMichael Goulet-6/+0
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-2/+2
2024-09-13Use the same precedence for all macro-like exprsMichael Goulet-6/+0
2024-07-29Reformat `use` declarations.Nicholas Nethercote-6/+11
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-07-03Rollup merge of #127092 - compiler-errors:rtn-dots-redux, r=estebankMatthias Krüger-1/+1
Change return-type-notation to use `(..)` Aligns the syntax with the current wording of [RFC 3654](https://github.com/rust-lang/rfcs/pull/3654). Also implements rustfmt support (along with making a match exhaustive). Tracking: * https://github.com/rust-lang/rust/issues/109417
2024-07-02Rollup merge of #126883 - dtolnay:breakvalue, r=fmeaseMatthias Krüger-1/+78
Parenthesize break values containing leading label The AST pretty printer previously produced invalid syntax in the case of `break` expressions with a value that begins with a loop or block label. ```rust macro_rules! expr { ($e:expr) => { $e }; } fn main() { loop { break expr!('a: loop { break 'a 1; } + 1); }; } ``` `rustc -Zunpretty=expanded main.rs `: ```console #![feature(prelude_import)] #![no_std] #[prelude_import] use ::std::prelude::rust_2015::*; #[macro_use] extern crate std; macro_rules! expr { ($e:expr) => { $e }; } fn main() { loop { break 'a: loop { break 'a 1; } + 1; }; } ``` The expanded code is not valid Rust syntax. Printing invalid syntax is bad because it blocks `cargo expand` from being able to format the output as Rust syntax using rustfmt. ```console error: parentheses are required around this expression to avoid confusion with a labeled break expression --> <anon>:9:26 | 9 | fn main() { loop { break 'a: loop { break 'a 1; } + 1; }; } | ^^^^^^^^^^^^^^^^^^^^^^^^ | help: wrap the expression in parentheses | 9 | fn main() { loop { break ('a: loop { break 'a 1; }) + 1; }; } | + + ``` This PR updates the AST pretty-printer to insert parentheses around the value of a `break` expression as required to avoid this edge case.
2024-07-01Parenthesize break values containing leading labelDavid Tolnay-1/+78
2024-06-28Change RTN to use .. againMichael Goulet-1/+1
2024-06-23Rename the 2 unambiguous precedence levels to PREC_UNAMBIGUOUSDavid Tolnay-24/+21
2024-06-23Unify the precedence level for PREC_POSTFIX and PREC_PARENDavid Tolnay-1/+1
2024-06-17Rework precise capturing syntaxMichael Goulet-2/+4
2024-05-12Disallow cast with trailing braced macro in let-elseDavid Tolnay-4/+91
2024-05-11Add classify::expr_is_completeDavid Tolnay-39/+57
2024-05-11Macro call with braces does not require semicolon to be statementDavid Tolnay-13/+17
This commit by itself is supposed to have no effect on behavior. All of the call sites are updated to preserve their previous behavior. The behavior changes are in the commits that follow.
2024-05-11Mark expr_requires_semi_to_be_stmt call sitesDavid Tolnay-1/+2
For each of these, we need to decide whether they need to be using `expr_requires_semi_to_be_stmt`, or `expr_requires_comma_to_be_match_arm`, which are supposed to be 2 different behaviors. Previously they were conflated into one, causing either too much or too little parenthesization.
2024-05-11Expand on expr_requires_semi_to_be_stmt documentationDavid Tolnay-10/+41
2024-04-30Remove `extern crate tracing` from numerous crates.Nicholas Nethercote-0/+1
2024-04-02Fix contains_exterior_struct_litMichael Goulet-1/+2