about summary refs log tree commit diff
path: root/compiler/rustc_builtin_macros/src
AgeCommit message (Collapse)AuthorLines
2024-07-05Use `ControlFlow` results for visitors that are only looking for a single valueOli Scherer-1/+1
2024-07-04Rollup merge of #127283 - dingxiangfei2009:check-repr-transparent, r=davidtwcoMatthias Krüger-3/+36
Reject SmartPointer constructions not serving the purpose Tracking issue: #123430 With this PR we will reject a row of malformed `SmartPointer` implementor candidates. cc `@Darksonn` `@davidtwco` for context.
2024-07-04Rollup merge of #126405 - He1pa:translate_builtin_macro_diag, r=davidtwcoMatthias Krüger-23/+65
Migrate some rustc_builtin_macros to SessionDiagnostic <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r​? <reviewer name> --> Part of https://github.com/rust-lang/rust/issues/100717. pick up abandoned pr: #101935 `@rustbot` label +A-translation
2024-07-04reject SmartPointer constructions not serving the purposeDing Xiang Fei-3/+36
2024-07-02Simplify `CfgEval`.Nicholas Nethercote-10/+8
It can contain an owned value instead of a reference.
2024-06-27Tighten spans for async blocksMichael Goulet-1/+1
2024-06-25`sudo CI=green` && Review changes <3xFrednet-1/+1
2024-06-25RFC 2383: Stabilize `lint_reasons` :tada:xFrednet-1/+1
2024-06-25Migrate some rustc_builtin_macros to SessionDiagnostiche1pa-23/+65
Signed-off-by: he1pa <18012015693@163.com>
2024-06-24SmartPointer derive-macroXiangfei Ding-0/+142
Co-authored-by: Wedson Almeida Filho <walmeida@microsoft.com>
2024-06-20Convert some module-level `//` and `///` comments to `//!`.Nicholas Nethercote-2/+3
This makes their intent and expected location clearer. We see some examples where these comments were not clearly separate from `use` declarations, which made it hard to understand what the comment is describing.
2024-06-18Use a dedicated type instead of a reference for the diagnostic contextOli Scherer-6/+8
This paves the way for tracking more state (e.g. error tainting) in the diagnostic context handle
2024-06-18Prefer `dcx` methods over fields or fields' methodsOli Scherer-5/+5
2024-06-12Use `tidy` to sort crate attributes for all compiler crates.Nicholas Nethercote-2/+4
We already do this for a number of crates, e.g. `rustc_middle`, `rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`. For the ones we don't, in many cases the attributes are a mess. - There is no consistency about order of attribute kinds (e.g. `allow`/`deny`/`feature`). - Within attribute kind groups (e.g. the `feature` attributes), sometimes the order is alphabetical, and sometimes there is no particular order. - Sometimes the attributes of a particular kind aren't even grouped all together, e.g. there might be a `feature`, then an `allow`, then another `feature`. This commit extends the existing sorting to all compiler crates, increasing consistency. If any new attribute line is added there is now only one place it can go -- no need for arbitrary decisions. Exceptions: - `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`, because they have no crate attributes. - `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's ignored in `rustfmt.toml`).
2024-06-06Handle safe casecarbotaniuman-0/+1
2024-06-06Fix formattingcarbotaniuman-1/+1
2024-06-06Fix buildcarbotaniuman-4/+4
2024-06-06Disallow unsafe in derivecarbotaniuman-1/+18
2024-06-06Parse unsafe attributescarbotaniuman-1/+3
2024-06-06Rollup merge of #126096 - c410-f3r:tests-tests-tests, r=jhprattJubilee-1/+0
[RFC-2011] Allow `core_intrinsics` when activated Fix #120612
2024-06-06[RFC-2011] Allow `core_intrinsics` when activatedCaio-1/+0
2024-06-06Rollup merge of #125987 - estebank:issue-122622, r=NadrierilMatthias Krüger-1/+11
When `derive`ing, account for HRTB on `BareFn` fields When given ```rust trait SomeTrait { type SomeType<'a>; } #[derive(Clone)] struct Foo<T: SomeTrait> { x: for<'a> fn(T::SomeType<'a>) } ``` expand to ```rust impl<T: ::core::clone::Clone + SomeTrait> ::core::clone::Clone for Foo<T> where for<'a> T::SomeType<'a>: ::core::clone::Clone { #[inline] fn clone(&self) -> Foo<T> { Foo { x: ::core::clone::Clone::clone(&self.x) } } } ``` instead of the previous invalid ``` impl<T: ::core::clone::Clone + SomeTrait> ::core::clone::Clone for Foo<T> where T::SomeType<'a>: ::core::clone::Clone { #[inline] fn clone(&self) -> Foo<T> { Foo { x: ::core::clone::Clone::clone(&self.x) } } } ``` Fix #122622. <!-- If this PR is related to an unstable feature or an otherwise tracked effort, please link to the relevant tracking issue here. If you don't know of a related tracking issue or there are none, feel free to ignore this. This PR will get automatically assigned to a reviewer. In case you would like a specific user to review your work, you can assign it to them by using r​? <reviewer name> -->
2024-06-05Make top-level `rustc_parse` functions fallible.Nicholas Nethercote-4/+5
Currently we have an awkward mix of fallible and infallible functions: ``` new_parser_from_source_str maybe_new_parser_from_source_str new_parser_from_file (maybe_new_parser_from_file) // missing (new_parser_from_source_file) // missing maybe_new_parser_from_source_file source_str_to_stream maybe_source_file_to_stream ``` We could add the two missing functions, but instead this commit removes of all the infallible ones and renames the fallible ones leaving us with these which are all fallible: ``` new_parser_from_source_str new_parser_from_file new_parser_from_source_file source_str_to_stream source_file_to_stream ``` This requires making `unwrap_or_emit_fatal` public so callers of formerly infallible functions can still work. This does make some of the call sites slightly more verbose, but I think it's worth it for the simpler API. Also, there are two `catch_unwind` calls and one `catch_fatal_errors` call in this diff that become removable thanks this change. (I will do that in a follow-up PR.)
2024-06-05Remove `stream_to_parser`.Nicholas Nethercote-1/+1
It's a zero-value wrapper of `Parser::new`.
2024-06-04When `derive`ing, account for HRTB on `BareFn` fieldsEsteban Küber-1/+11
When given ```rust trait SomeTrait { type SomeType<'a>; } struct Foo<T: SomeTrait> { x: for<'a> fn(T::SomeType<'a>) } ``` expand to ```rust impl<T: ::core::clone::Clone + SomeTrait> ::core::clone::Clone for Foo<T> where for<'a> T::SomeType<'a>: ::core::clone::Clone { #[inline] fn clone(&self) -> Foo<T> { Foo { x: ::core::clone::Clone::clone(&self.x) } } } ``` instead of the previous invalid ``` impl<T: ::core::clone::Clone + SomeTrait> ::core::clone::Clone for Foo<T> where T::SomeType<'a>: ::core::clone::Clone { #[inline] fn clone(&self) -> Foo<T> { Foo { x: ::core::clone::Clone::clone(&self.x) } } } ``` Fix #122622.
2024-05-23Rollup merge of #125316 - nnethercote:tweak-Spacing, r=petrochenkovMatthias Krüger-2/+2
Tweak `Spacing` use Some clean-up precursors to #125174. r? ``@petrochenkov``
2024-05-23Rollup merge of #125156 - zachs18:for_loops_over_fallibles_behind_refs, ↵Matthias Krüger-2/+2
r=Nilstrieb Expand `for_loops_over_fallibles` lint to lint on fallibles behind references. Extends the scope of the (warn-by-default) lint `for_loops_over_fallibles` from just `for _ in x` where `x: Option<_>/Result<_, _>` to also cover `x: &(mut) Option<_>/Result<_>` ```rs fn main() { // Current lints for _ in Some(42) {} for _ in Ok::<_, i32>(42) {} // New lints for _ in &Some(42) {} for _ in &mut Some(42) {} for _ in &Ok::<_, i32>(42) {} for _ in &mut Ok::<_, i32>(42) {} // Should not lint for _ in Some(42).into_iter() {} for _ in Some(42).iter() {} for _ in Some(42).iter_mut() {} for _ in Ok::<_, i32>(42).into_iter() {} for _ in Ok::<_, i32>(42).iter() {} for _ in Ok::<_, i32>(42).iter_mut() {} } ``` <details><summary><code>cargo build</code> diff</summary> ```diff diff --git a/old.out b/new.out index 84215aa..ca195a7 100644 --- a/old.out +++ b/new.out `@@` -1,33 +1,93 `@@` warning: for loop over an `Option`. This is more readably written as an `if let` statement --> src/main.rs:3:14 | 3 | for _ in Some(42) {} | ^^^^^^^^ | = note: `#[warn(for_loops_over_fallibles)]` on by default help: to check pattern in a loop use `while let` | 3 | while let Some(_) = Some(42) {} | ~~~~~~~~~~~~~~~ ~~~ help: consider using `if let` to clear intent | 3 | if let Some(_) = Some(42) {} | ~~~~~~~~~~~~ ~~~ warning: for loop over a `Result`. This is more readably written as an `if let` statement --> src/main.rs:4:14 | 4 | for _ in Ok::<_, i32>(42) {} | ^^^^^^^^^^^^^^^^ | help: to check pattern in a loop use `while let` | 4 | while let Ok(_) = Ok::<_, i32>(42) {} | ~~~~~~~~~~~~~ ~~~ help: consider using `if let` to clear intent | 4 | if let Ok(_) = Ok::<_, i32>(42) {} | ~~~~~~~~~~ ~~~ -warning: `for-loops-over-fallibles` (bin "for-loops-over-fallibles") generated 2 warnings - Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s +warning: for loop over a `&Option`. This is more readably written as an `if let` statement + --> src/main.rs:7:14 + | +7 | for _ in &Some(42) {} + | ^^^^^^^^^ + | +help: to check pattern in a loop use `while let` + | +7 | while let Some(_) = &Some(42) {} + | ~~~~~~~~~~~~~~~ ~~~ +help: consider using `if let` to clear intent + | +7 | if let Some(_) = &Some(42) {} + | ~~~~~~~~~~~~ ~~~ + +warning: for loop over a `&mut Option`. This is more readably written as an `if let` statement + --> src/main.rs:8:14 + | +8 | for _ in &mut Some(42) {} + | ^^^^^^^^^^^^^ + | +help: to check pattern in a loop use `while let` + | +8 | while let Some(_) = &mut Some(42) {} + | ~~~~~~~~~~~~~~~ ~~~ +help: consider using `if let` to clear intent + | +8 | if let Some(_) = &mut Some(42) {} + | ~~~~~~~~~~~~ ~~~ + +warning: for loop over a `&Result`. This is more readably written as an `if let` statement + --> src/main.rs:9:14 + | +9 | for _ in &Ok::<_, i32>(42) {} + | ^^^^^^^^^^^^^^^^^ + | +help: to check pattern in a loop use `while let` + | +9 | while let Ok(_) = &Ok::<_, i32>(42) {} + | ~~~~~~~~~~~~~ ~~~ +help: consider using `if let` to clear intent + | +9 | if let Ok(_) = &Ok::<_, i32>(42) {} + | ~~~~~~~~~~ ~~~ + +warning: for loop over a `&mut Result`. This is more readably written as an `if let` statement + --> src/main.rs:10:14 + | +10 | for _ in &mut Ok::<_, i32>(42) {} + | ^^^^^^^^^^^^^^^^^^^^^ + | +help: to check pattern in a loop use `while let` + | +10 | while let Ok(_) = &mut Ok::<_, i32>(42) {} + | ~~~~~~~~~~~~~ ~~~ +help: consider using `if let` to clear intent + | +10 | if let Ok(_) = &mut Ok::<_, i32>(42) {} + | ~~~~~~~~~~ ~~~ + +warning: `for-loops-over-fallibles` (bin "for-loops-over-fallibles") generated 6 warnings + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.02s ``` </details> ----- Question: * ~~Currently, the article `an` is used for `&Option`, and `&mut Option` in the lint diagnostic, since that's what `Option` uses. Is this okay or should it be changed? (likewise, `a` is used for `&Result` and `&mut Result`)~~ The article `a` is used for `&Option`, `&mut Option`, `&Result`, `&mut Result` and (as before) `Result`. Only `Option` uses `an` (as before). `@rustbot` label +A-lint
2024-05-23Convert some `token_joint_hidden` calls to `token_joint`.Nicholas Nethercote-2/+2
This has no noticeable effect, but it makes these cases follow the guidelines in the comments on `Spacing`, which say that `Joint` should be used "for each token that (a) should be pretty-printed without a space after it, and (b) is followed by a punctuation token". These two tokens are both followed by a comma, which is a punctuation token.
2024-05-21Rename buffer_lint_with_diagnostic to buffer_lintXiretza-6/+6
2024-05-21Convert uses of BuiltinLintDiag::Normal to custom variantsXiretza-11/+14
This ensures all diagnostic messages are created at diagnostic emission time, making them translatable.
2024-05-21Generate lint diagnostic message from BuiltinLintDiagXiretza-5/+3
Translation of the lint message happens when the actual diagnostic is created, not when the lint is buffered. Generating the message from BuiltinLintDiag ensures that all required data to construct the message is preserved in the LintBuffer, eventually allowing the messages to be moved to fluent. Remove the `msg` field from BufferedEarlyLint, it is either generated from the data in the BuiltinLintDiag or stored inside BuiltinLintDiag::Normal.
2024-05-18Auto merge of #125077 - spastorino:add-new-fnsafety-enum2, r=jackh726bors-6/+6
Rename Unsafe to Safety Alternative to #124455, which is to just have one Safety enum to use everywhere, this opens the posibility of adding `ast::Safety::Safe` that's useful for unsafe extern blocks. This leaves us today with: ```rust enum ast::Safety { Unsafe(Span), Default, // Safe (going to be added for unsafe extern blocks) } enum hir::Safety { Unsafe, Safe, } ``` We would convert from `ast::Safety::Default` into the right Safety level according the context.
2024-05-18Fix println! ICE when parsing percent prefix numberwuaoxiang-9/+15
2024-05-17Rename Unsafe to SafetySantiago Pastorino-6/+6
2024-05-15Fix new for_loops_over_fallibles hits in compiler.Zachary S-2/+2
2024-05-09Rollup merge of #124919 - nnethercote:Recovered-Yes-ErrorGuaranteed, ↵Matthias Krüger-3/+2
r=compiler-errors Add `ErrorGuaranteed` to `Recovered::Yes` and use it more. The starting point for this was identical comments on two different fields, in `ast::VariantData::Struct` and `hir::VariantData::Struct`: ``` // FIXME: investigate making this a `Option<ErrorGuaranteed>` recovered: bool ``` I tried that, and then found that I needed to add an `ErrorGuaranteed` to `Recovered::Yes`. Then I ended up using `Recovered` instead of `Option<ErrorGuaranteed>` for these two places and elsewhere, which required moving `ErrorGuaranteed` from `rustc_parse` to `rustc_ast`. This makes things more consistent, because `Recovered` is used in more places, and there are fewer uses of `bool` and `Option<ErrorGuaranteed>`. And safer, because it's difficult/impossible to set `recovered` to `Recovered::Yes` without having emitted an error. r? `@oli-obk`
2024-05-09Add `ErrorGuaranteed` to `Recovered::Yes` and use it more.Nicholas Nethercote-3/+2
The starting point for this was identical comments on two different fields, in `ast::VariantData::Struct` and `hir::VariantData::Struct`: ``` // FIXME: investigate making this a `Option<ErrorGuaranteed>` recovered: bool ``` I tried that, and then found that I needed to add an `ErrorGuaranteed` to `Recovered::Yes`. Then I ended up using `Recovered` instead of `Option<ErrorGuaranteed>` for these two places and elsewhere, which required moving `ErrorGuaranteed` from `rustc_parse` to `rustc_ast`. This makes things more consistent, because `Recovered` is used in more places, and there are fewer uses of `bool` and `Option<ErrorGuaranteed>`. And safer, because it's difficult/impossible to set `recovered` to `Recovered::Yes` without having emitted an error.
2024-05-09Auto merge of #124157 - wutchzone:partial_eq, r=estebankbors-7/+6
Do not add leading asterisk in the `PartialEq` I think we should address this issue, however I am not exactly sure, if this is the right way to do it. It is related to the #123056. Imagine the simplified code: ```rust trait MyTrait {} impl PartialEq for dyn MyTrait { fn eq(&self, _other: &Self) -> bool { true } } #[derive(PartialEq)] enum Bar { Foo(Box<dyn MyTrait>), } ``` On the nightly compiler, the `derive` produces invalid code with the weird error message: ``` error[E0507]: cannot move out of `*__arg1_0` which is behind a shared reference --> src/main.rs:11:9 | 9 | #[derive(PartialEq)] | --------- in this derive macro expansion 10 | enum Things { 11 | Foo(Box<dyn MyTrait>), | ^^^^^^^^^^^^^^^^ move occurs because `*__arg1_0` has type `Box<dyn MyTrait>`, which does not implement the `Copy` trait | = note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info) ``` It may be related to the perfect derive problem, although requiring the _type_ to be `Copy` seems unfortunate because it is not necessary. Besides, we are adding the extra dereference only for the diagnostics?
2024-05-08Rollup merge of #123344 - pietroalbini:pa-unused-imports, r=NilstriebMatthias Krüger-4/+7
Remove braces when fixing a nested use tree into a single item [Back in 2019](https://github.com/rust-lang/rust/pull/56645) I added rustfix support for the `unused_imports` lint, to automatically remove them when running `cargo fix`. For the most part this worked great, but when removing all but one childs of a nested use tree it turned `use foo::{Unused, Used}` into `use foo::{Used}`. This is slightly annoying, because it then requires you to run `rustfmt` to get `use foo::Used`. This PR automatically removes braces and the surrouding whitespace when all but one child of a nested use tree are unused. To get it done I had to add the span of the nested use tree to the AST, and refactor a bit the code I wrote back then. A thing I noticed is, there doesn't seem to be any `//@ run-rustfix` test for fixing the `unused_imports` lint. I created a test in `tests/suggestions` (is that the right directory?) that for now tests just what I added in the PR. I can followup in a separate PR to add more tests for fixing `unused_lints`. This PR is best reviewed commit-by-commit.
2024-05-04Various improvements to entrypoint codeNilstrieb-1/+1
This moves some code around and adds some documentation comments to make it easier to understand what's going on with the entrypoint logic, which is a bit complicated. The only change in behavior is consolidating the error messages for unix_sigpipe to make the code slightly simpler.
2024-05-03Use `parse` renaming of `rustc_parse_format`.Nicholas Nethercote-2/+2
This is a case where `rustc_parse_format` is renamed as `parse` but a couple of places don't take advantage.
2024-04-30Remove `extern crate tracing` from numerous crates.Nicholas Nethercote-4/+2
2024-04-27Rollup merge of #124382 - petrochenkov:itemvisit, r=lcnrMatthias Krüger-3/+3
ast: Generalize item kind visiting And avoid duplicating logic for visiting `Item`s with different kinds (regular, associated, foreign). The diff is better viewed with whitespace ignored.
2024-04-26Rollup merge of #124391 - nnethercote:builtin_macros-cleanups, r=fee1-deadJacob Pratt-116/+324
`rustc_builtin_macros` cleanups Some improvements I found while looking over this code. r? ``@fee1-dead``
2024-04-26Adjust some `pub`s.Nicholas Nethercote-81/+81
2024-04-26Move some functions from `rustc_expand` to `rustc_builtin_macros`.Nicholas Nethercote-26/+225
These functions are only used in `rustc_builtin_macros`, so it makes sense for them to live there. This allows them to be changed from `pub` to `pub(crate)`.
2024-04-26Introduce `DeriveResolution`.Nicholas Nethercote-6/+13
Making this a proper struct, and giving its fields names, makes things easier to understand.
2024-04-25ast: Generalize item kind visitingVadim Petrochenkov-3/+3
And avoid duplicating logic for visiting `Item`s with different kinds (regular, associated, foreign).
2024-04-25Name the field in `Expander`.Nicholas Nethercote-4/+6
For code clarity.
2024-04-23Do not add leading asterisk in the `PartialEq`Daniel Sedlak-3/+2
Adding leading asterisk can cause compilation failure for the _types_ that don't implement the `Copy`.