Commit graph

129 commits

Author SHA1 Message Date
David Wood 99bc979403 macros: use typed identifiers in diag derive
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-24 09:08:25 +01:00
bors 1a97162cb2 Auto merge of #94732 - nnethercote:infallible-encoder, r=bjorn3
Make `Encodable` and `Encoder` infallible.

A follow-up to #93066.

r? `@ghost`
2022-06-08 10:24:12 +00:00
Nicholas Nethercote 90db033955 Folding revamp.
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-08 09:24:03 +10:00
Nicholas Nethercote 1acbe7573d Use delayed error handling for Encodable and Encoder infallible.
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-08 07:01:26 +10:00
bjorn3 22e8d5f80e Inline many methods of Encoder
They aren't overridden anyway
2022-06-03 17:01:53 +00:00
bjorn3 5cd29225a5 Remove all names from Encoder
They aren't used anymore now that the json format has been removed
2022-06-03 16:56:17 +00:00
David Wood f669b78ffc errors: simplify referring to fluent attributes
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-30 13:38:19 +01:00
Michael Goulet 4638915940 Make TyCtxt implement Interner, make HashStable generic and move to rustc_type_ir 2022-05-28 12:16:05 -07:00
Wilco Kusee a7015fe816 Move things to rustc_type_ir 2022-05-28 11:38:22 -07:00
David Wood 552eb3295a macros: introduce fluent_messages macro
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-24 16:48:17 +01:00
David Wood 6e85efda22 macros: change code block language
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-24 16:48:17 +01:00
Christian Poveda 8227e69018
formatting 2022-05-19 09:02:50 -05:00
Christian Poveda d839d2ecc2
let generate_field_attrs_code create FieldInfo
this simplifies the code inside the `structure.each` closure argument
and allows to remove the `vis` field from `FieldInfo`.
2022-05-19 00:47:45 -05:00
Christian Poveda 7e9be9240c
remove unnecessary generics 2022-05-18 10:55:35 -05:00
Christian Poveda 952121933e
move misplaced comment 2022-05-18 10:53:48 -05:00
Christian Poveda 19e1f73085
generate set_arg code inside generate_field_attrs_code 2022-05-18 10:50:59 -05:00
Christian Poveda 462c1c846b
generate code for subdiagnostic fields in the second match 2022-05-17 13:10:15 -05:00
David Wood de3e8ca2f3 errors: set_arg takes IntoDiagnosticArg
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-12 07:21:51 +01:00
David Wood 7b7061dd89 macros: spanless subdiagnostics from () fields
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-12 07:21:51 +01:00
David Wood 859079ff12 macros: allow Vec fields in diagnostic derive
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-05-06 03:43:30 +01:00
David Wood dca88612b9 macros: add interop between diagnostic derives
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-29 02:12:10 +01:00
David Wood e5d9371b30 macros: allow setting applicability in attribute
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-29 02:12:10 +01:00
David Wood e8ee0d7a20 macros: add more documentation comments
Documentation comments are always good.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29 02:12:10 +01:00
David Wood 2647a4812c macros: reuse SetOnce trait in diagnostic derive
`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-29 02:12:10 +01:00
David Wood 36a396ce51 macros: add helper functions for invalid attrs
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-29 02:12:10 +01:00
David Wood 071f07274b macros: split diagnostic derives into modules
Split `SessionDiagnostic` and `SessionSubdiagnostic` derives and the
various helper functions into multiple modules.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-29 02:12:08 +01:00
David Wood 49ec909ca7 macros: subdiagnostic derive
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-29 02:05:20 +01:00
Christian Poveda 5874b09806
fix formatting 2022-04-25 23:17:32 +02:00
Christian Poveda eb55cdce4b
use ParseSess instead of Session in into_diagnostic 2022-04-25 22:54:16 +02:00
David Wood f79d5e9458 macros: update doc comment for diagnostic derive
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-21 04:22:18 +01:00
Matthias Krüger 7c2d57e0fa couple of clippy::complexity fixes 2022-04-13 22:51:34 +02:00
David Wood 22685b9607 macros: support translatable suggestions
Extends support for generating `DiagnosticMessage::FluentIdentifier`
messages from `SessionDiagnostic` derive to `#[suggestion]`.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05 07:01:03 +01:00
David Wood b40ee88a28 macros: note/help in SessionDiagnostic derive
Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05 07:01:03 +01:00
David Wood a88717cef0 macros: support translatable labels
Extends support for generating `DiagnosticMessage::FluentIdentifier`
messages from `SessionDiagnostic` derive to `#[label]`.

Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05 07:01:03 +01:00
David Wood 72dec56028 macros: optional error codes
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-05 07:01:03 +01:00
David Wood 70ee0c96fc macros: add #[no_arg] to skip set_arg call
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-05 07:01:03 +01:00
David Wood 8100541d54 macros: rename #[message] to #[primary_span]
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-05 07:01:03 +01:00
David Wood d0fd8d7880 macros: translatable struct attrs and warnings
Signed-off-by: David Wood <david.wood@huawei.com>
2022-04-05 07:01:03 +01:00
David Wood f0de7df204 macros: update session diagnostic errors
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-05 07:01:03 +01:00
David Wood 9956d4f99d macros: add args for non-subdiagnostic fields
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-05 07:01:02 +01:00
David Wood 8677fef192 macros: move suggestion type handling to fn
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-05 07:01:02 +01:00
David Wood 2bf64d6483 macros: update comments
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-04-05 07:01:02 +01:00
lcnr b8135fd5c8 add #[rustc_pass_by_value] to more types 2022-03-08 15:39:52 +01:00
pierwill c08a9a4f1d Make Ord, PartialOrd opt-out in newtype_index
Also remove `step` impl if `ORD_IMPL = off`
2022-03-03 11:52:40 -06:00
mark e489a94dee rename ErrorReported -> ErrorGuaranteed 2022-03-02 09:45:25 -06:00
Dylan DPC daed86445d
Rollup merge of #93926 - PatchMixolydic:bugfix/must_use-on-exprs, r=cjgillot
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-03-01 03:41:49 +01:00
Ruby Lazuli 6dcf5d8fde
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-27 08:52:37 -06:00
bors f6a79936da Auto merge of #93878 - Aaron1011:newtype-macro, r=cjgillot
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-25 03:16:22 +00:00
Aaron Hill 01efe6d5c2
Address review comments 2022-02-24 16:02:07 -05:00
Aaron Hill 339bbebbc1
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-24 16:02:06 -05:00