about summary refs log tree commit diff
path: root/compiler/rustc_macros/src
AgeCommit message (Collapse)AuthorLines
2022-06-08Folding revamp.Nicholas Nethercote-2/+2
This commit makes type folding more like the way chalk does it. Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods. - `fold_with` is the standard entry point, and defaults to calling `super_fold_with`. - `super_fold_with` does the actual work of traversing a type. - For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead calls into a `TypeFolder`, which can then call back into `super_fold_with`. With the new approach, `TypeFoldable` has `fold_with` and `TypeSuperFoldable` has `super_fold_with`. - `fold_with` is still the standard entry point, *and* it does the actual work of traversing a type, for all types except types of interest. - `super_fold_with` is only implemented for the types of interest. Benefits of the new model. - I find it easier to understand. The distinction between types of interest and other types is clearer, and `super_fold_with` doesn't exist for most types. - With the current model is easy to get confused and implement a `super_fold_with` method that should be left defaulted. (Some of the precursor commits fixed such cases.) - With the current model it's easy to call `super_fold_with` within `TypeFolder` impls where `fold_with` should be called. The new approach makes this mistake impossible, and this commit fixes a number of such cases. - It's potentially faster, because it avoids the `fold_with` -> `super_fold_with` call in all cases except types of interest. A lot of the time the compile would inline those away, but not necessarily always.
2022-06-08Use delayed error handling for `Encodable` and `Encoder` infallible.Nicholas Nethercote-17/+9
There are two impls of the `Encoder` trait: `opaque::Encoder` and `opaque::FileEncoder`. The former encodes into memory and is infallible, the latter writes to file and is fallible. Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a bit verbose and has non-trivial cost, which is annoying given how rare failures are (especially in the infallible `opaque::Encoder` case). This commit changes how `Encoder` fallibility is handled. All the `emit_*` methods are now infallible. `opaque::Encoder` requires no great changes for this. `opaque::FileEncoder` now implements a delayed error handling strategy. If a failure occurs, it records this via the `res` field, and all subsequent encoding operations are skipped if `res` indicates an error has occurred. Once encoding is complete, the new `finish` method is called, which returns a `Result`. In other words, there is now a single `Result`-producing method instead of many of them. This has very little effect on how any file errors are reported if `opaque::FileEncoder` has any failures. Much of this commit is boring mechanical changes, removing `Result` return values and `?` or `unwrap` from expressions. The more interesting parts are as follows. - serialize.rs: The `Encoder` trait gains an `Ok` associated type. The `into_inner` method is changed into `finish`, which returns `Result<Vec<u8>, !>`. - opaque.rs: The `FileEncoder` adopts the delayed error handling strategy. Its `Ok` type is a `usize`, returning the number of bytes written, replacing previous uses of `FileEncoder::position`. - Various methods that take an encoder now consume it, rather than being passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-03Inline many methods of Encoderbjorn3-14/+8
They aren't overridden anyway
2022-06-03Remove all names from Encoderbjorn3-22/+2
They aren't used anymore now that the json format has been removed
2022-05-30errors: simplify referring to fluent attributesDavid Wood-38/+20
To render the message of a Fluent attribute, the identifier of the Fluent message must be known. `DiagnosticMessage::FluentIdentifier` contains both the message's identifier and optionally the identifier of an attribute. Generated constants for each attribute would therefore need to be named uniquely (amongst all error messages) or be able to refer to only the attribute identifier which will be combined with a message identifier later. In this commit, the latter strategy is implemented as part of the `Diagnostic` type's functions for adding subdiagnostics of various kinds. Signed-off-by: David Wood <david.wood@huawei.com>
2022-05-28Make TyCtxt implement Interner, make HashStable generic and move to ↵Michael Goulet-2/+2
rustc_type_ir
2022-05-28Move things to rustc_type_irWilco Kusee-2/+2
2022-05-24macros: introduce `fluent_messages` macroDavid Wood-0/+315
Adds a new `fluent_messages` macro which performs compile-time validation of the compiler's Fluent resources (i.e. that the resources parse and don't multiply define the same messages) and generates constants that make using those messages in diagnostics more ergonomic. For example, given the following invocation of the macro.. ```ignore (rust) fluent_messages! { typeck => "./typeck.ftl", } ``` ..where `typeck.ftl` has the following contents.. ```fluent typeck-field-multiply-specified-in-initializer = field `{$ident}` specified more than once .label = used more than once .label-previous-use = first use of `{$ident}` ``` ...then the macro parse the Fluent resource, emitting a diagnostic if it fails to do so, and will generate the following code: ```ignore (rust) pub static DEFAULT_LOCALE_RESOURCES: &'static [&'static str] = &[ include_str!("./typeck.ftl"), ]; mod fluent_generated { mod typeck { pub const field_multiply_specified_in_initializer: DiagnosticMessage = DiagnosticMessage::fluent("typeck-field-multiply-specified-in-initializer"); pub const field_multiply_specified_in_initializer_label_previous_use: DiagnosticMessage = DiagnosticMessage::fluent_attr( "typeck-field-multiply-specified-in-initializer", "previous-use-label" ); } } ``` When emitting a diagnostic, the generated constants can be used as follows: ```ignore (rust) let mut err = sess.struct_span_err( span, fluent::typeck::field_multiply_specified_in_initializer ); err.span_default_label(span); err.span_label( previous_use_span, fluent::typeck::field_multiply_specified_in_initializer_label_previous_use ); err.emit(); ``` Signed-off-by: David Wood <david.wood@huawei.com>
2022-05-24macros: change code block languageDavid Wood-4/+4
With `ignore (rust)` rather than `ignore (pseudo-Rust)` my editor highlights the code in the block, which is nicer. Signed-off-by: David Wood <david.wood@huawei.com>
2022-05-19formattingChristian Poveda-1/+2
2022-05-19let `generate_field_attrs_code` create `FieldInfo`Christian Poveda-46/+14
this simplifies the code inside the `structure.each` closure argument and allows to remove the `vis` field from `FieldInfo`.
2022-05-18remove unnecessary genericsChristian Poveda-4/+4
2022-05-18move misplaced commentChristian Poveda-4/+4
2022-05-18generate `set_arg` code inside `generate_field_attrs_code`Christian Poveda-99/+101
2022-05-17generate code for `subdiagnostic` fields in the second `match`Christian Poveda-18/+56
2022-05-12errors: `set_arg` takes `IntoDiagnosticArg`David Wood-2/+2
Manual implementors of translatable diagnostics will need to call `set_arg`, not just the derive, so make this function a bit more ergonomic by taking `IntoDiagnosticArg` rather than `DiagnosticArgValue`. Signed-off-by: David Wood <david.wood@huawei.com>
2022-05-12macros: spanless subdiagnostics from `()` fieldsDavid Wood-33/+75
Type attributes could previously be used to support spanless subdiagnostics but these couldn't easily be made optional in the same way that spanned subdiagnostics could by using a field attribute on a field with an `Option<Span>` type. Spanless subdiagnostics can now be specified on fields with `()` type or `Option<()>` type. Signed-off-by: David Wood <david.wood@huawei.com>
2022-05-06macros: allow `Vec` fields in diagnostic deriveDavid Wood-39/+85
Diagnostics can have multiple primary spans, or have subdiagnostics repeated at multiple locations, so support `Vec<..>` fields in the diagnostic derive which become loops in the generated code. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29macros: add interop between diagnostic derivesDavid Wood-1/+3
Add `#[subdiagnostic]` field attribute to the diagnostic derive which is applied to fields that have types which use the subdiagnostic derive. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29macros: allow setting applicability in attributeDavid Wood-51/+75
In the initial implementation of the `SessionSubdiagnostic`, the `Applicability` of a suggestion can be set both as a field and as part of the attribute, this commit adds the same support to the original `SessionDiagnostic` derive. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29macros: add more documentation commentsDavid Wood-1/+13
Documentation comments are always good. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29macros: reuse `SetOnce` trait in diagnostic deriveDavid Wood-60/+11
`SetOnce` trait was introduced in the subdiagnostic derive to simplify the code a little bit, re-use it in the diagnostic derive too. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29macros: add helper functions for invalid attrsDavid Wood-229/+149
Remove some duplicated code between both diagnostic derives by introducing helper functions for reporting an error in case of a invalid attribute. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29macros: split diagnostic derives into modulesDavid Wood-1593/+1631
Split `SessionDiagnostic` and `SessionSubdiagnostic` derives and the various helper functions into multiple modules. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29macros: subdiagnostic deriveDavid Wood-132/+772
Add a new derive, `#[derive(SessionSubdiagnostic)]`, which enables deriving structs for labels, notes, helps and suggestions. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-25fix formattingChristian Poveda-3/+1
2022-04-25use `ParseSess` instead of `Session` in `into_diagnostic`Christian Poveda-2/+4
2022-04-21macros: update doc comment for diagnostic deriveDavid Wood-8/+18
The documentation comment for this derive is out-of-date, it should have been updated in #95512. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-13couple of clippy::complexity fixesMatthias Krüger-1/+1
2022-04-05macros: support translatable suggestionsDavid Wood-66/+93
Extends support for generating `DiagnosticMessage::FluentIdentifier` messages from `SessionDiagnostic` derive to `#[suggestion]`. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: note/help in `SessionDiagnostic` deriveDavid Wood-9/+45
Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: support translatable labelsDavid Wood-34/+75
Extends support for generating `DiagnosticMessage::FluentIdentifier` messages from `SessionDiagnostic` derive to `#[label]`. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: optional error codesDavid Wood-23/+19
In an effort to make it easier to port diagnostics to `SessionDiagnostic` (for translation) and since translation slugs could replace error codes, make error codes optional in the `SessionDiagnostic` derive. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: add `#[no_arg]` to skip `set_arg` callDavid Wood-1/+12
A call to `set_arg` is generated for every field of a `SessionDiagnostic` struct without attributes, but not all types support being an argument, so `#[no_arg]` is introduced to skip these fields. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: rename `#[message]` to `#[primary_span]`David Wood-7/+4
Small commit renaming `#[message]` to `#[primary_span]` as this more accurately reflects what it does now. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: translatable struct attrs and warningsDavid Wood-116/+274
Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: update session diagnostic errorsDavid Wood-9/+9
Small commit adding backticks around types and annotations in the error messages from the session diagnostic derive. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: add args for non-subdiagnostic fieldsDavid Wood-24/+54
Non-subdiagnostic fields (i.e. those that don't have `#[label]` attributes or similar and are just additional context) have to be added as arguments for Fluent messages to refer them. This commit extends the `SessionDiagnostic` derive to do this for all fields that do not have attributes and introduces an `IntoDiagnosticArg` trait that is implemented on all types that can be converted to a argument for Fluent. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: move suggestion type handling to fnDavid Wood-68/+61
Move the handling of `Span` or `(Span, Applicability)` types in `#[suggestion]` attributes to its own function. Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05macros: update commentsDavid Wood-27/+34
Various small changes to comments, like wrapping code in backticks, changing comments to doc comments and adding newlines. Signed-off-by: David Wood <david.wood@huawei.com>
2022-03-08add `#[rustc_pass_by_value]` to more typeslcnr-0/+1
2022-03-03Make `Ord`, `PartialOrd` opt-out in `newtype_index`pierwill-24/+46
Also remove `step` impl if `ORD_IMPL = off`
2022-03-02rename ErrorReported -> ErrorGuaranteedmark-1/+1
2022-03-01Rollup merge of #93926 - PatchMixolydic:bugfix/must_use-on-exprs, r=cjgillotDylan DPC-1/+0
Lint against more useless `#[must_use]` attributes This expands the existing `#[must_use]` check in `unused_attributes` to lint against pretty much everything `#[must_use]` doesn't support. Fixes #93906.
2022-02-27Lint against more useless `#[must_use]` attributesRuby Lazuli-1/+0
This expands the existing `#[must_use]` check in `unused_attributes` to lint against pretty much everything `#[must_use]` doesn't support. Fixes #93906.
2022-02-25Auto merge of #93878 - Aaron1011:newtype-macro, r=cjgillotbors-0/+333
Convert `newtype_index` to a proc macro The `macro_rules!` implementation was becomng excessively complicated, and difficult to modify. The new proc macro implementation should make it much easier to add new features (e.g. skipping certain `#[derive]`s)
2022-02-24Address review commentsAaron Hill-24/+10
2022-02-24Convert `newtype_index` to a proc macroAaron Hill-0/+347
The `macro_rules!` implementation was becomng excessively complicated, and difficult to modify. The new proc macro implementation should make it much easier to add new features (e.g. skipping certain `#[derive]`s)
2022-02-23rustc_errors: let `DiagnosticBuilder::emit` return a "guarantee of emission".Eduard-Mihai Burtescu-1/+1
2022-02-20Delete Decoder::read_enum_variantMark Rousskov-8/+4