about summary refs log tree commit diff
path: root/compiler/rustc_builtin_macros
AgeCommit message (Collapse)AuthorLines
2022-07-04Avoid unnecessary blocks in derive output.Nicholas Nethercote-111/+146
By not committing to either block form or expression form until necessary, we can avoid lots of unnecessary blocks.
2022-07-04Don't use match-destructuring for derived ops on structs.Nicholas Nethercote-44/+80
All derive ops currently use match-destructuring to access fields. This is reasonable for enums, but sub-optimal for structs. E.g.: ``` fn eq(&self, other: &Point) -> bool { match *other { Self { x: ref __self_1_0, y: ref __self_1_1 } => match *self { Self { x: ref __self_0_0, y: ref __self_0_1 } => (*__self_0_0) == (*__self_1_0) && (*__self_0_1) == (*__self_1_1), }, } } ``` This commit changes derive ops on structs to use field access instead, e.g.: ``` fn eq(&self, other: &Point) -> bool { self.x == other.x && self.y == other.y } ``` This is faster to compile, results in smaller binaries, and is simpler to generate. Unfortunately, we have to keep the old pattern generating code around for `repr(packed)` structs because something like `&self.x` (which doesn't show up in `PartialEq` ops, but does show up in `Debug` and `Hash` ops) isn't allowed. But this commit at least changes those cases to use let-destructuring instead of match-destructuring, e.g.: ``` fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () { { let Self(ref __self_0_0) = *self; { ::core::hash::Hash::hash(&(*__self_0_0), state) } } } ``` There are some unnecessary blocks remaining in the generated code, but I will fix them in a follow-up PR.
2022-07-04Comment fixes.Nicholas Nethercote-2/+1
Remove an out-of-date sentence, and fix a typo.
2022-07-01Change `Ty::Tuple` to `Ty::Unit`.Nicholas Nethercote-14/+8
Because that's all that is needed in practice.
2022-07-01Rename `Ty::Literal` as `Ty::Path`.Nicholas Nethercote-29/+18
Because a `Literal` is a type of expression, and is simply the wrong name for this.
2022-07-01Remove lifetime support in deriving code.Nicholas Nethercote-32/+11
It's unused.
2022-07-01Simplify pointer handling.Nicholas Nethercote-100/+43
The existing derive code allows for various possibilities that aren't needed in practice, which complicates the code. There are only a few auto-derived traits and new ones are unlikely, so this commit simplifies things. - `PtrTy` has been eliminated. The `Raw` variant was never used, and the lifetime for the `Borrowed` variant was always `None`. That left just the mutability field, which has been inlined as necessary. - `MethodDef::explicit_self` was a confusing `Option<Option<PtrTy>>`. Indicating either `&self` or nothing. It's now a `bool`. - `borrowed_self` is renamed as `self_ref`. - `Ty::Ptr` is renamed to `Ty::Ref`.
2022-07-01`expand_deriving_clone` tweaks.Nicholas Nethercote-28/+22
Improve a comment, and panic on an impossible code path.
2022-07-01Remove some commented-out code.Nicholas Nethercote-2/+0
This was accidentally left behind in a previous commit.
2022-07-01Remove some unnecessary `pub`s.Nicholas Nethercote-3/+3
2022-07-01Remove `Substructure::self_args`.Nicholas Nethercote-18/+2
It's unused.
2022-07-01Remove `{Method,Trait}Def::is_unsafe`.Nicholas Nethercote-37/+2
They are always `false`.
2022-07-01Remove `Substructure::method_ident`.Nicholas Nethercote-9/+1
It's unused.
2022-07-01Remove unnecessary fields from `EnumNonMatchingCollapsed`.Nicholas Nethercote-30/+14
The `&[ast::Variant]` field isn't used. The `Vec<Ident>` field is only used for its length, but that's always the same as the length of the `&[Ident]` and so isn't necessary.
2022-07-01Use `split_{first,last}` in `cs_fold1`.Nicholas Nethercote-9/+8
It makes the code a little nicer to read.
2022-06-29Auto merge of #98558 - nnethercote:smallvec-1.8.1, r=lqdbors-1/+1
Update `smallvec` to 1.8.1. This pulls in https://github.com/servo/rust-smallvec/pull/282, which gives some small wins for rustc. r? `@lqd`
2022-06-29Auto merge of #98376 - nnethercote:improve-derive-PartialEq, r=petrochenkovbors-131/+85
Improve some deriving code and add a test The `.stdout` test is particularly useful. r? `@petrochenkov`
2022-06-28Rollup merge of #98337 - c410-f3r:assert-compiler, r=oli-obkDylan DPC-15/+81
[RFC 2011] Optimize non-consuming operators Tracking issue: https://github.com/rust-lang/rust/issues/44838 Fifth step of https://github.com/rust-lang/rust/pull/96496 The most non-invasive approach that will probably have very little to no performance impact. ## Current behaviour Captures are handled "on-the-fly", i.e., they are performed in the same place expressions are located. ```rust // `let a = 1; let b = 2; assert!(a > 1 && b < 100);` if !( { ***try capture `a` and then return `a`*** } > 1 && { ***try capture `b` and then return `b`*** } < 100 ) { panic!( ... ); } ``` As such, some overhead is likely to occur (Specially with very large chains of conditions). ## New behaviour for non-consuming operators When an operator is known to not take `self`, then it is possible to capture variables **AFTER** the condition. ```rust // `let a = 1; let b = 2; assert!(a > 1 && b < 100);` if !( a > 1 && b < 100 ) { { ***try capture `a`*** } { ***try capture `b`*** } panic!( ... ); } ``` So the possible impact on the runtime execution time will be diminished. r? ````@oli-obk````
2022-06-27Update `smallvec` to 1.8.1.Nicholas Nethercote-1/+1
This pulls in https://github.com/servo/rust-smallvec/pull/282, which gives some small wins for rustc.
2022-06-27Convert `process_variant` functions into closures.Nicholas Nethercote-16/+12
It makes things a bit nicer.
2022-06-27Factor out the repeated `assert_ty_bounds` function.Nicholas Nethercote-44/+39
2022-06-27Merge `build_enum_match_tuple` into `expand_enum_method_body`.Nicholas Nethercote-52/+20
Because the latter just calls the former. The commit also updates some details in a comment.
2022-06-27Improve derived discriminant testing.Nicholas Nethercote-21/+16
Currently the generated code for methods like `eq`, `ne`, and `partial_cmp` includes stuff like this: ``` let __self_vi = ::core::intrinsics::discriminant_value(&*self); let __arg_1_vi = ::core::intrinsics::discriminant_value(&*other); if true && __self_vi == __arg_1_vi { ... } ``` This commit removes the unnecessary `true &&`, and makes the generating code a little easier to read in the process. It also fixes some errors in comments.
2022-06-26Rollup merge of #98428 - davidtwco:translation-derive-typed-identifiers, ↵Matthias Krüger-2/+2
r=oli-obk macros: use typed identifiers in diag and subdiag derive Using typed identifiers instead of strings with the Fluent identifiers in the diagnostic and subdiagnostic derives - this enables the diagnostic derive to benefit from the compile-time validation that comes with typed identifiers, namely that use of a non-existent Fluent identifier will not compile. r? `````@oli-obk`````
2022-06-26Auto merge of #98190 - nnethercote:optimize-derive-Debug-code, r=scottmcmbors-97/+125
Improve `derive(Debug)` r? `@ghost`
2022-06-24macros: use typed identifiers in diag deriveDavid Wood-2/+2
Using typed identifiers instead of strings with the Fluent identifier enables the diagnostic derive to benefit from the compile-time validation that comes with typed identifiers - use of a non-existent Fluent identifier will not compile. Signed-off-by: David Wood <david.wood@huawei.com>
2022-06-24Rollup merge of #98394 - Enselic:fixup-rustc_main-renames, r=petrochenkovYuki Okushi-5/+6
Fixup missing renames from `#[main]` to `#[rustc_main]` In #84217 `#[main]` was removed and replaced with `#[rustc_main]`. In some places the rename was forgotten, which makes the current code confusing, because at first glance it seems that `#[main]` is still around. Perform the renames also in these places. I noticed this (after first being confused by it) when working on #97802. r? `@petrochenkov` (since you reviewed the other PR)
2022-06-24Optimize the code produced by `derive(Debug)`.Nicholas Nethercote-88/+117
This commit adds new methods that combine sequences of existing formatting methods. - `Formatter::debug_{tuple,struct}_field[12345]_finish`, equivalent to a `Formatter::debug_{tuple,struct}` + N x `Debug{Tuple,Struct}::field` + `Debug{Tuple,Struct}::finish` call sequence. - `Formatter::debug_{tuple,struct}_fields_finish` is similar, but can handle any number of fields by using arrays. These new methods are all marked as `doc(hidden)` and unstable. They are intended for the compiler's own use. Special-casing up to 5 fields gives significantly better performance results than always using arrays (as was tried in #95637). The commit also changes the `Debug` deriving code to use these new methods. For example, where the old `Debug` code for a struct with two fields would be like this: ``` fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { match *self { Self { f1: ref __self_0_0, f2: ref __self_0_1, } => { let debug_trait_builder = &mut ::core::fmt::Formatter::debug_struct(f, "S2"); let _ = ::core::fmt::DebugStruct::field(debug_trait_builder, "f1", &&(*__self_0_0)); let _ = ::core::fmt::DebugStruct::field(debug_trait_builder, "f2", &&(*__self_0_1)); ::core::fmt::DebugStruct::finish(debug_trait_builder) } } } ``` the new code is like this: ``` fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { match *self { Self { f1: ref __self_0_0, f2: ref __self_0_1, } => ::core::fmt::Formatter::debug_struct_field2_finish( f, "S2", "f1", &&(*__self_0_0), "f2", &&(*__self_0_1), ), } } ``` This shrinks the code produced for `Debug` instances considerably, reducing compile times and binary sizes. Co-authored-by: Scott McMurray <scottmcm@users.noreply.github.com>
2022-06-23Rename some `ExtCtxt` methods.Nicholas Nethercote-9/+8
The new names are more accurate. Co-authored-by: Scott McMurray <scottmcm@users.noreply.github.com>
2022-06-22Fixup missing renames from `#[main]` to `#[rustc_main]`Martin Nordholts-5/+6
In fc357039f9 `#[main]` was removed and replaced with `#[rustc_main]`. In some place the rename was forgotten, which makes the current code confusing, because at first glance it seems that `#[main]` is still around. Perform the renames also in these places.
2022-06-21Migrate `builtin-macros-expected-one-cfg-pattern` to `SessionDiagnostic`beetrees-1/+8
2022-06-21Migrate `builtin-macros-requires-cfg-pattern` to `SessionDiagnostic`beetrees-5/+13
2022-06-21[RFC 2011] Optimize non-consuming operatorsCaio-15/+81
2022-06-15[RFC 2011] Expand expressions where possibleCaio-2/+90
2022-06-15[RFC 2011] Minimal initial implementationCaio-26/+282
2022-06-13remove unnecessary `to_string` and `String::new` for `tool_only_span_suggestion`Takayuki Maeda-2/+2
2022-06-13remove unnecessary `to_string` and `String::new`Takayuki Maeda-4/+4
2022-06-03Fully stabilize NLLJack Huey-1/+0
2022-06-02Basic compiler infraCaio-24/+95
2022-05-28Auto merge of #97461 - eddyb:proc-macro-less-payload, r=bjorn3bors-32/+39
proc_macro: don't pass a client-side function pointer through the server. Before this PR, `proc_macro::bridge::Client<F>` contained both: * the C ABI entry-point `run`, that the server can call to start the client * some "payload" `f: F` passed to that entry-point * in practice, this was always a (client-side Rust ABI) `fn` pointer to the actual function the proc macro author wrote, i.e. `#[proc_macro] fn foo(input: TokenStream) -> TokenStream` In other words, the client was passing one of its (Rust) `fn` pointers to the server, which was passing it back to the client, for the client to call (see later below for why that was ever needed). I was inspired by `@nnethercote's` attempt to remove the `get_handle_counters` field from `Client` (see https://github.com/rust-lang/rust/pull/97004#issuecomment-1139273301), which combined with removing the `f` ("payload") field, could theoretically allow for a `#[repr(transparent)]` `Client` that mostly just newtypes the C ABI entry-point `fn` pointer <sub>(and in the context of e.g. wasm isolation, that's *all* you want, since you can reason about it from outside the wasm VM, as just a 32-bit "function table index", that you can pass to the wasm VM to call that function)</sub>. <hr/> So this PR removes that "payload". But it's not a simple refactor: the reason the field existed in the first place is because monomorphizing over a function type doesn't let you call the function without having a value of that type, because function types don't implement anything like `Default`, i.e.: ```rust extern "C" fn ffi_wrapper<A, R, F: Fn(A) -> R>(arg: A) -> R { let f: F = ???; // no way to get a value of `F` f(arg) } ``` That could be solved with something like this, if it was allowed: ```rust extern "C" fn ffi_wrapper< A, R, F: Fn(A) -> R, const f: F // not allowed because the type is a generic param >(arg: A) -> R { f(arg) } ``` Instead, this PR contains a workaround in `proc_macro::bridge::selfless_reify` (see its module-level comment for more details) that can provide something similar to the `ffi_wrapper` example above, but limited to `F` being `Copy` and ZST (and requiring an `F` value to prove the caller actually can create values of `F` and it's not uninhabited or some other unsound situation). <hr/> Hopefully this time we don't have a performance regression, and this has a chance to land. cc `@mystor` `@bjorn3`
2022-05-28Rollup merge of #97458 - estebank:use-self-in-derive-macro, r=compiler-errorsMatthias Krüger-1/+3
Modify `derive(Debug)` to use `Self` in struct literal to avoid redundant error Reduce verbosity in #97343.
2022-05-27proc_macro: don't pass a client-side function pointer through the server.Eduard-Mihai Burtescu-32/+39
2022-05-27Modify `derive(Debug)` to use `Self` in struct literal to avoid redundant errorEsteban Küber-1/+3
#97343
2022-05-27Simplify types in `proc_macro_harness.rs`.Nicholas Nethercote-27/+17
This gives the more obvious derive/attr/bang distinction, and reduces code size slightly.
2022-05-25Fix a typo on Struct `Substructure`Yuki Okushi-1/+1
2022-05-22rustc_parse: Move AST -> TokenStream conversion logic to `rustc_ast`Vadim Petrochenkov-10/+1
2022-05-20Remove `crate` visibility usage in compilerJacob Pratt-7/+6
2022-05-11ast: Introduce some traits to get AST node properties genericallyVadim Petrochenkov-8/+3
And use them to avoid constructing some artificial `Nonterminal` tokens during expansion
2022-05-07Auto merge of #96094 - Elliot-Roberts:fix_doctests, r=compiler-errorsbors-11/+14
Begin fixing all the broken doctests in `compiler/` Begins to fix #95994. All of them pass now but 24 of them I've marked with `ignore HELP (<explanation>)` (asking for help) as I'm unsure how to get them to work / if we should leave them as they are. There are also a few that I marked `ignore` that could maybe be made to work but seem less important. Each `ignore` has a rough "reason" for ignoring after it parentheses, with - `(pseudo-rust)` meaning "mostly rust-like but contains foreign syntax" - `(illustrative)` a somewhat catchall for either a fragment of rust that doesn't stand on its own (like a lone type), or abbreviated rust with ellipses and undeclared types that would get too cluttered if made compile-worthy. - `(not-rust)` stuff that isn't rust but benefits from the syntax highlighting, like MIR. - `(internal)` uses `rustc_*` code which would be difficult to make work with the testing setup. Those reason notes are a bit inconsistently applied and messy though. If that's important I can go through them again and try a more principled approach. When I run `rg '```ignore \(' .` on the repo, there look to be lots of different conventions other people have used for this sort of thing. I could try unifying them all if that would be helpful. I'm not sure if there was a better existing way to do this but I wrote my own script to help me run all the doctests and wade through the output. If that would be useful to anyone else, I put it here: https://github.com/Elliot-Roberts/rust_doctest_fixing_tool
2022-05-05Auto merge of #91779 - ridwanabdillahi:natvis, r=michaelwoeristerbors-43/+4
Add a new Rust attribute to support embedding debugger visualizers Implemented [this RFC](https://github.com/rust-lang/rfcs/pull/3191) to add support for embedding debugger visualizers into a PDB. Added a new attribute `#[debugger_visualizer]` and updated the `CrateMetadata` to store debugger visualizers for crate dependencies. RFC: https://github.com/rust-lang/rfcs/pull/3191