about summary refs log tree commit diff
path: root/compiler/rustc_ast/src
AgeCommit message (Collapse)AuthorLines
2023-12-13Unify single-char and multi-char `CStrUnit::Char` handling.Nicholas Nethercote-1/+0
The two cases are equivalent. C string literals aren't common so there is no performance need here.
2023-12-13Don't rebuild raw strings when unescaping.Nicholas Nethercote-43/+30
Raw strings don't have escape sequences, so for them "unescaping" just means checking for invalid chars like bare CR. Which means there is no need to rebuild them one char or byte at a time while escaping, because the unescaped version will be the same. This commit removes that rebuilding. Also, the commit changes things so that "unescaping" is unconditional for raw strings and raw byte strings. That's simpler and they're rare enough that the perf effect is negligible.
2023-12-12Don't gate the feature twiceNadrieril-0/+13
2023-12-11Add spacing information to delimiters.Nicholas Nethercote-25/+40
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-12-11Improve `print_tts` by changing `tokenstream::Spacing`.Nicholas Nethercote-18/+64
`tokenstream::Spacing` appears on all `TokenTree::Token` instances, both punct and non-punct. Its current usage: - `Joint` means "can join with the next token *and* that token is a punct". - `Alone` means "cannot join with the next token *or* can join with the next token but that token is not a punct". The fact that `Alone` is used for two different cases is awkward. This commit augments `tokenstream::Spacing` with a new variant `JointHidden`, resulting in: - `Joint` means "can join with the next token *and* that token is a punct". - `JointHidden` means "can join with the next token *and* that token is a not a punct". - `Alone` means "cannot join with the next token". This *drastically* improves the output of `print_tts`. For example, this: ``` stringify!(let a: Vec<u32> = vec![];) ``` currently produces this string: ``` let a : Vec < u32 > = vec! [] ; ``` With this PR, it now produces this string: ``` let a: Vec<u32> = vec![] ; ``` (The space after the `]` is because `TokenTree::Delimited` currently doesn't have spacing information. The subsequent commit fixes this.) The new `print_tts` doesn't replicate original code perfectly. E.g. multiple space characters will be condensed into a single space character. But it's much improved. `print_tts` still produces the old, uglier output for code produced by proc macros. Because we have to translate the generated code from `proc_macro::Spacing` to the more expressive `token::Spacing`, which results in too much `proc_macro::Along` usage and no `proc_macro::JointHidden` usage. So `space_between` still exists and is used by `print_tts` in conjunction with the `Spacing` field. This change will also help with the removal of `Token::Interpolated`. Currently interpolated tokens are pretty-printed nicely via AST pretty printing. `Token::Interpolated` removal will mean they get printed with `print_tts`. Without this change, that would result in much uglier output for code produced by decl macro expansions. With this change, AST pretty printing and `print_tts` produce similar results. The commit also tweaks the comments on `proc_macro::Spacing`. In particular, it refers to "compound tokens" rather than "multi-char operators" because lifetimes aren't operators.
2023-12-10remove redundant importssurechen-3/+3
detects redundant imports that can be eliminated. for #117772 : In order to facilitate review and modification, split the checking code and removing redundant imports code into two PR.
2023-12-08Introduce closure_id method on CoroutineKindMichael Goulet-0/+8
2023-12-08Auto merge of #118420 - compiler-errors:async-gen, r=eholkbors-18/+31
Introduce support for `async gen` blocks I'm delighted to demonstrate that `async gen` block are not very difficult to support. They're simply coroutines that yield `Poll<Option<T>>` and return `()`. **This PR is WIP and in draft mode for now** -- I'm mostly putting it up to show folks that it's possible. This PR needs a lang-team experiment associated with it or possible an RFC, since I don't think it falls under the jurisdiction of the `gen` RFC that was recently authored by oli (https://github.com/rust-lang/rfcs/pull/3513, https://github.com/rust-lang/rust/issues/117078). ### Technical note on the pre-generator-transform yield type: The reason that the underlying coroutines yield `Poll<Option<T>>` and not `Poll<T>` (which would make more sense, IMO, for the pre-transformed coroutine), is because the `TransformVisitor` that is used to turn coroutines into built-in state machine functions would have to destructure and reconstruct the latter into the former, which requires at least inserting a new basic block (for a `switchInt` terminator, to match on the `Poll` discriminant). This does mean that the desugaring (at the `rustc_ast_lowering` level) of `async gen` blocks is a bit more involved. However, since we already need to intercept both `.await` and `yield` operators, I don't consider it much of a technical burden. r? `@ghost`
2023-12-08Support async gen fnMichael Goulet-4/+10
2023-12-08coro_kind -> coroutine_kindMichael Goulet-14/+19
2023-12-08Implement `async gen` blocksMichael Goulet-0/+2
2023-12-08Auto merge of #118527 - Nadrieril:never_patterns_parse, r=compiler-errorsbors-4/+22
never_patterns: Parse match arms with no body Never patterns are meant to signal unreachable cases, and thus don't take bodies: ```rust let ptr: *const Option<!> = ...; match *ptr { None => { foo(); } Some(!), } ``` This PR makes rustc accept the above, and enforces that an arm has a body xor is a never pattern. This affects parsing of match arms even with the feature off, so this is delicate. (Plus this is my first non-trivial change to the parser). ~~The last commit is optional; it introduces a bit of churn to allow the new suggestions to be machine-applicable. There may be a better solution? I'm not sure.~~ EDIT: I removed that commit r? `@compiler-errors`
2023-12-04Address code review feedbackEric Holk-2/+3
2023-12-04Option<CoroutineKind>Eric Holk-20/+10
2023-12-04Merge Async and Gen into CoroutineKindEric Holk-50/+42
2023-12-04Add genness to FnHeaderEric Holk-3/+22
2023-12-03Disallow an arm without a body (except for never patterns)Nadrieril-2/+2
Parsing now accepts a match arm without a body, so we must make sure to only accept that if the pattern is a never pattern.
2023-12-03Parse a pattern with no armNadrieril-4/+22
2023-12-02Auto merge of #117912 - GeorgeWort:master, r=petrochenkovbors-0/+12
Name explicit registers in conflict register errors for inline assembly
2023-11-29Rollup merge of #118157 - Nadrieril:never_pat-feature-gate, r=compiler-errorsMatthias Krüger-2/+6
Add `never_patterns` feature gate This PR adds the feature gate and most basic parsing for the experimental `never_patterns` feature. See the tracking issue (https://github.com/rust-lang/rust/issues/118155) for details on the experiment. `@scottmcm` has agreed to be my lang-team liaison for this experiment.
2023-11-29Rollup merge of #118394 - nnethercote:rm-hir-Ops, r=cjgillotMatthias Krüger-8/+19
Remove HIR opkinds `hir::BinOp`, `hir::BinOpKind`, and `hir::UnOp` are identical to `ast::BinOp`, `ast::BinOpKind`, and `ast::UnOp`, respectively. This seems silly, so this PR removes the HIR ones. (A re-export lets the AST ones be referred to using a `hir::` qualifier, which avoids renaming churn.) r? `@cjgillot`
2023-11-29Add `never_patterns` feature gateNadrieril-2/+6
2023-11-28Name explicit registers in conflict register errors for inline assemblyGeorge Wort-0/+12
2023-11-28Remove `hir::BinOp`, `hir::BinOpKind`, and `hir::UnOp`.Nicholas Nethercote-3/+13
They're identical to the same-named types from `ast`. I find it silly (and inefficient) to have all this boilerplate code to convert one type to an identical type. There is already a small amount of type sharing between the AST and HIR, e.g. `Attribute`, `MacroDef`. The commit adds a `pub use` to `rustc_hir` so that, for example, `ast::BinOp` can also be referred to as `hir::BinOp`. This is so the many existing `hir`-qualified mentions of these types don't need to change. The commit also moves a couple of operations from the (removed) HIR types to the AST types, e.g. `is_by_value`.
2023-11-28Rename `BinOpKind::lazy` as `BinOpKind::is_lazy`.Nicholas Nethercote-1/+2
To match `BinOpKind::is_comparison` and `hir::BinOpKind::is_lazy`.
2023-11-28Rework `ast::BinOpKind::to_string` and `ast::UnOp::to_string`.Nicholas Nethercote-4/+4
- Rename them both `as_str`, which is the typical name for a function that returns a `&str`. (`to_string` is appropriate for functions returning `String` or maybe `Cow<'a, str>`.) - Change `UnOp::as_str` from an associated function (weird!) to a method. - Avoid needless `self` dereferences.
2023-11-27Detect Python-like slicing and suggest how to fixHirochika Matsumoto-0/+5
Fix #108215
2023-11-24Add `Span` to `TraitBoundModifier`Deadbeef-4/+3
2023-11-22Replace `no_ord_impl` with `orderable`.Nicholas Nethercote-0/+2
Similar to the previous commit, this replaces `newtype_index`'s opt-out `no_ord_impl` attribute with the opt-in `orderable` attribute.
2023-11-22Replace `custom_encodable` with `encodable`.Nicholas Nethercote-1/+1
By default, `newtype_index!` types get a default `Encodable`/`Decodable` impl. You can opt out of this with `custom_encodable`. Opting out is the opposite to how Rust normally works with autogenerated (derived) impls. This commit inverts the behaviour, replacing `custom_encodable` with `encodable` which opts into the default `Encodable`/`Decodable` impl. Only 23 of the 59 `newtype_index!` occurrences need `encodable`. Even better, there were eight crates with a dependency on `rustc_serialize` just from unused default `Encodable`/`Decodable` impls. This commit removes that dependency from those eight crates.
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-21Add HashStable_NoContext to simplify HashStable implementations in rustc_type_irMichael Goulet-3/+1
2023-11-17Auto merge of #114292 - estebank:issue-71039, r=b-naberbors-22/+42
More detail when expecting expression but encountering bad macro argument On nested macro invocations where the same macro fragment changes fragment type from one to the next, point at the chain of invocations and at the macro fragment definition place, explaining that the change has occurred. Fix #71039. ``` error: expected expression, found pattern `1 + 1` --> $DIR/trace_faulty_macros.rs:49:37 | LL | (let $p:pat = $e:expr) => {test!(($p,$e))}; | ------- -- this is interpreted as expression, but it is expected to be pattern | | | this macro fragment matcher is expression ... LL | (($p:pat, $e:pat)) => {let $p = $e;}; | ------ ^^ expected expression | | | this macro fragment matcher is pattern ... LL | test!(let x = 1+1); | ------------------ | | | | | this is expected to be expression | in this macro invocation | = note: when forwarding a matched fragment to another macro-by-example, matchers in the second macro will see an opaque AST of the fragment type, not the underlying tokens = note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info) ```
2023-11-17Rollup merge of #117892 - estebank:fat-arrow-typo, r=compiler-errorsMatthias Krüger-1/+2
Detect more `=>` typos Handle and recover `match expr { pat >= { arm } }`.
2023-11-16More detail when expecting expression but encountering bad macro argumentEsteban Küber-22/+42
Partially address #71039.
2023-11-15Bump cfg(bootstrap)sMark Rousskov-3/+3
2023-11-14Detect more `=>` typosEsteban Küber-1/+2
Handle and recover `match expr { pat >= { arm } }`.
2023-11-13Fix commentSleep_AllDay-2/+2
Gt => Greater than => `>` Ge => Greater equal => `>=`
2023-11-04Add the vis.visit_capture_by() in noop_visit_exprDinu Blanovschi-1/+2
2023-11-04add `fn visit_capture_by` to MutVisitor and fix pprust-expr-roundtrip.rsDinu Blanovschi-0/+13
2023-11-04fixes for rustfmt + ast visitorDinu Blanovschi-1/+5
2023-11-04feat(hir): Store the `Span` of the `move` keywordDinu Blanovschi-1/+4
2023-10-29Auto merge of #116447 - oli-obk:gen_fn, r=compiler-errorsbors-11/+38
Implement `gen` blocks in the 2024 edition Coroutines tracking issue https://github.com/rust-lang/rust/issues/43122 `gen` block tracking issue https://github.com/rust-lang/rust/issues/117078 This PR implements `gen` blocks that implement `Iterator`. Most of the logic with `async` blocks is shared, and thus I renamed various types that were referring to `async` specifically. An example usage of `gen` blocks is ```rust fn foo() -> impl Iterator<Item = i32> { gen { yield 42; for i in 5..18 { if i.is_even() { continue } yield i * 2; } } } ``` The limitations (to be resolved) of the implementation are listed in the tracking issue
2023-10-27Add gen blocks to ast and do some broken ast loweringOli Scherer-11/+31
2023-10-26Rollup merge of #117114 - nnethercote:improve-stringify-test, r=petrochenkovMatthias Krüger-0/+7
Improve `stringify.rs` test Best reviewed one commit at a time. r? `@petrochenkov`
2023-10-26Reserve `gen` keyword for `gen {}` blocks and `gen fn` in 2024 editionOli Scherer-0/+7
2023-10-24Augment `stringify.rs` test.Nicholas Nethercote-0/+7
By adding tests (or placeholders, or comments) for missing AST variants.
2023-10-22use visibility to check unused imports and delete some stmtsbohan-1/+0
2023-10-19Uplift movability and mutability, the simple wayMichael Goulet-63/+4
2023-10-15Auto merge of #116688 - compiler-errors:rustfmt-up, r=WaffleLapkin,Nilstriebbors-11/+15
Format all the let-chains in compiler crates Since rust-lang/rustfmt#5910 has landed, soon we will have support for formatting let-chains (as soon as rustfmt syncs and beta gets bumped). This PR applies the changes [from master rustfmt to rust-lang/rust eagerly](https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/out.20formatting.20of.20prs/near/374997516), so that the next beta bump does not have to deal with a 200+ file diff and can remain concerned with other things like `cfg(bootstrap)` -- #113637 was a pain to land, for example, because of let-else. I will also add this commit to the ignore list after it has landed. The commands that were run -- I'm not great at bash-foo, but this applies rustfmt to every compiler crate, and then reverts the two crates that should probably be formatted out-of-tree. ``` ~/rustfmt $ ls -1d ~/rust/compiler/* | xargs -I@ cargo run --bin rustfmt -- `@/src/lib.rs` --config-path ~/rust --edition=2021 # format all of the compiler crates ~/rust $ git checkout HEAD -- compiler/rustc_codegen_{gcc,cranelift} # revert changes to cg-gcc and cg-clif ``` cc `@rust-lang/rustfmt` r? `@WaffleLapkin` or `@Nilstrieb` who said they may be able to review this purely mechanical PR :> cc `@Mark-Simulacrum` and `@petrochenkov,` who had some thoughts on the order of operations with big formatting changes in https://github.com/rust-lang/rust/pull/95262#issue-1178993801. I think the situation has changed since then, given that let-chains support exists on master rustfmt now, and I'm fairly confident that this formatting PR should land even if *bootstrap* rustfmt doesn't yet format let-chains in order to lessen the burden of the next beta bump.