Commit Graph

5775 Commits

Author SHA1 Message Date
Nicholas Nethercote
18f51f79f3 Make Emitter::emit_diagnostic consuming.
All the other `emit`/`emit_diagnostic` methods were recently made
consuming (e.g. #119606), but this one wasn't. But it makes sense to.

Much of this is straightforward, and lots of `clone` calls are avoided.
There are a couple of tricky bits.
- `Emitter::primary_span_formatted` no longer takes a `Diagnostic` and
  returns a pair. Instead it takes the two fields from `Diagnostic` that
  it used (`span` and `suggestions`) as `&mut`, and modifies them. This
  is necessary to avoid the cloning of `diag.children` in two emitters.
- `from_errors_diagnostic` is rearranged so various uses of `diag` occur
  before the consuming `emit_diagnostic` call.
2024-02-05 21:27:01 +11:00
Michael Ciraci
ead0fc9529
Consolidating dependencies (#6034)
* Updating dependencies for rustfmt in an attempt to consolidate rustc dependencies
* Updating link for dirs 5.0.1
2024-01-29 08:44:11 -05:00
David Bar-On
7bedb9fb8c
wrap macro line with width off by one char beyond max width (#5582)
Fix issue 3805
2024-01-28 19:07:30 -05:00
Josh Soref
cedb7b5058
Spelling (#5753)
various spelling fixes
---------

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2024-01-25 18:55:23 -05:00
Josh McKinney
7f44a0825d Format diff line to be easily clickable
Instead of {filename} at {line}, use {filename}:{line} as this is a
format that many editors allow to be clicked to navigate directly to the
line.
2024-01-25 10:30:22 -07:00
Michael Goulet
a0958082b3 Actually, just use nonterminal_may_begin_with 2024-01-22 02:19:42 +00:00
Michael Goulet
f8847ff3ec Do not eagerly recover malformed AST in rustfmt 2024-01-22 01:49:33 +00:00
Michael Goulet
b92320c39b Check that a token can begin a nonterminal kind before parsing it as a macro arg in rustfmt 2024-01-22 01:49:33 +00:00
IVIURRAY
bf967319e2
Add config option generated_marker_line_search_limit (#5993)
`generated_marker_line_search_limit` allows users to configure how many
lines rustfmt should search for an `@generated` marker comment when
`format_generated_files=false`

---------

Co-authored-by: Jordan Eldredge <jordan@jordaneldredge.com>
2024-01-20 11:14:50 -05:00
Lieselotte
255d2cf8f1 Add PatKind::Err 2024-01-17 03:14:16 +01:00
Bryanskiy
6078b96b23 Delegation implementation: step 1 2024-01-12 14:11:16 +03:00
Nicholas Nethercote
381ef817b7 Stop using DiagnosticBuilder::buffer in the parser.
One consequence is that errors returned by
`maybe_new_parser_from_source_str` now must be consumed, so a bunch of
places that previously ignored those errors now cancel them. (Most of
them explicitly dropped the errors before. I guess that was to indicate
"we are explicitly ignoring these", though I'm not 100% sure.)
2024-01-11 18:37:56 +11:00
Nicholas Nethercote
192c4a0cf4 Change how force-warn lint diagnostics are recorded.
`is_force_warn` is only possible for diagnostics with `Level::Warning`,
but it is currently stored in `Diagnostic::code`, which every diagnostic
has.

This commit:
- removes the boolean `DiagnosticId::Lint::is_force_warn` field;
- adds a `ForceWarning` variant to `Level`.

Benefits:
- The common `Level::Warning` case now has no arguments, replacing
  lots of `Warning(None)` occurrences.
- `rustc_session::lint::Level` and `rustc_errors::Level` are more
  similar, both having `ForceWarning` and `Warning`.
2024-01-11 07:56:17 +11:00
IVIURRAY
6356fca675
Prevent enum variant attributes from wrapping one character early
Fixes 5801 when using `version=Two`

Previously, doc comments would wrap one character less than the
`comment_width` when using `wrap_comments=true`, and fn-like attributes
would wrap one character before the `attr_fn_like_width`.

Now, when using `version=Two` enum variant attributes won't wrap early
2024-01-08 10:06:25 -05:00
Nicholas Nethercote
141b31a23f Make DiagnosticBuilder::emit consuming.
This works for most of its call sites. This is nice, because `emit` very
much makes sense as a consuming operation -- indeed,
`DiagnosticBuilderState` exists to ensure no diagnostic is emitted
twice, but it uses runtime checks.

For the small number of call sites where a consuming emit doesn't work,
the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will
be removed in subsequent commits.)

Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes
consuming, while `delay_as_bug_without_consuming` is added (which will
also be removed in subsequent commits.)

All this requires significant changes to `DiagnosticBuilder`'s chaining
methods. Currently `DiagnosticBuilder` method chaining uses a
non-consuming `&mut self -> &mut Self` style, which allows chaining to
be used when the chain ends in `emit()`, like so:
```
    struct_err(msg).span(span).emit();
```
But it doesn't work when producing a `DiagnosticBuilder` value,
requiring this:
```
    let mut err = self.struct_err(msg);
    err.span(span);
    err
```
This style of chaining won't work with consuming `emit` though. For
that, we need to use to a `self -> Self` style. That also would allow
`DiagnosticBuilder` production to be chained, e.g.:
```
    self.struct_err(msg).span(span)
```
However, removing the `&mut self -> &mut Self` style would require that
individual modifications of a `DiagnosticBuilder` go from this:
```
    err.span(span);
```
to this:
```
    err = err.span(span);
```
There are *many* such places. I have a high tolerance for tedious
refactorings, but even I gave up after a long time trying to convert
them all.

Instead, this commit has it both ways: the existing `&mut self -> Self`
chaining methods are kept, and new `self -> Self` chaining methods are
added, all of which have a `_mv` suffix (short for "move"). Changes to
the existing `forward!` macro lets this happen with very little
additional boilerplate code. I chose to add the suffix to the new
chaining methods rather than the existing ones, because the number of
changes required is much smaller that way.

This doubled chainging is a bit clumsy, but I think it is worthwhile
because it allows a *lot* of good things to subsequently happen. In this
commit, there are many `mut` qualifiers removed in places where
diagnostics are emitted without being modified. In subsequent commits:
- chaining can be used more, making the code more concise;
- more use of chaining also permits the removal of redundant diagnostic
  APIs like `struct_err_with_code`, which can be replaced easily with
  `struct_err` + `code_mv`;
- `emit_without_diagnostic` can be removed, which simplifies a lot of
  machinery, removing the need for `DiagnosticBuilderState`.
2024-01-08 15:24:49 +11:00
Matthias Krüger
75e3172aaa
fix a couply of clippy findings (#6007)
* clippy: autofix some lint warnings
* fix a couple more clippy warnings
2024-01-06 17:52:26 -05:00
Michael Goulet
250d7e764c Rollup merge of #119601 - nnethercote:Emitter-cleanups, r=oli-obk
`Emitter` cleanups

Some improvements I found while looking at this code.

r? `@oli-obk`
2024-01-05 10:57:24 -05:00
Nicholas Nethercote
840824f3bb Rename EmitterWriter as HumanEmitter.
For consistency with other `Emitter` impls, such as `JsonEmitter`,
`SilentEmitter`, `SharedEmitter`, etc.
2024-01-05 10:02:40 +11:00
Jake Goulding
4a1b4182fe Rename unused_tuple_struct_fields in rustfmt
Otherwise tests fail due to unknown lint and dead code warnings.
2024-01-02 15:34:37 -05:00
Jake Goulding
ae760e695c Address unused tuple struct fields in rustfmt 2024-01-01 17:47:54 -05:00
Caleb Cartwright
85e21fabf4
Merge pull request #5994 from ytmimi/subtree_sync_with_1.77.0_nightly_2023_12_28
Subtree push with 1.77.0 nightly 2023-12-28
2023-12-28 21:18:48 -06:00
Yacin Tmimi
621904f452 chore: bump to nightly 2023_12_27 toolchain 2023-12-28 17:28:01 -05:00
Yacin Tmimi
6cc513f5e5 Merge remote-tracking branch 'origin/master' into subtree_sync_with_1.77.0_nightly_2023_12_27 2023-12-28 16:50:17 -05:00
Yacin Tmimi
fd78575c9c Bump Update itertools to 0.11. 2023-12-28 16:48:54 -05:00
Sam Tay
d86fc1bf64
Make trace! formatting consistent with other log macros (#5989)
Fixes 5987

rustfmt already special cases the formatting for the `debug!`, `info!`,
`warn!`, and `error!`, macros from the `log` crate. However, this
speical case handling did not apply to the `trace!` macro.

Now when using `Version=Two` rustfmt will also special case the
formatting for the `trace!` macro.
2023-12-23 22:39:01 -05:00
trevyn
c926898ff0 Clarify format_macro_bodies description 2023-12-23 17:07:53 -07:00
Matthias Krüger
f002221a53 Rollup merge of #119231 - aDotInTheVoid:PatKind-struct-bool-docs, r=compiler-errors
Clairify `ast::PatKind::Struct` presese of `..` by using an enum instead of a bool

The bool is mainly used for when a `..` is present, but it is also set on recovery to avoid errors. The doc comment not describes both of these cases.

See cee794ee98/compiler/rustc_parse/src/parser/pat.rs (L890-L897) for the only place this is constructed.

r? ``@compiler-errors``
2023-12-23 16:23:54 +01:00
Alona Enraght-Moony
d9ea1027b5 bool->enum for ast::PatKind::Struct presence of ..
See cee794ee98/compiler/rustc_parse/src/parser/pat.rs (L890-L897) for the only place this is constructed.
2023-12-23 02:50:31 +00:00
Nicholas Nethercote
101bc225d8 Improve some names.
Lots of vectors of messages called `message` or `msg`. This commit
pluralizes them.

Note that `emit_message_default` and `emit_messages_default` both
already existed, and both process a vector, so I renamed the former
`emit_messages_default_inner` because it's called by the latter.
2023-12-23 13:23:28 +11:00
bors
b29b02ca5b Auto merge of #118847 - eholk:for-await, r=compiler-errors
Add support for `for await` loops

This adds support for `for await` loops. This includes parsing, desugaring in AST->HIR lowering, and adding some support functions to the library.

Given a loop like:
```rust
for await i in iter {
    ...
}
```
this is desugared to something like:
```rust
let mut iter = iter.into_async_iter();
while let Some(i) = loop {
    match core::pin::Pin::new(&mut iter).poll_next(cx) {
        Poll::Ready(i) => break i,
        Poll::Pending => yield,
    }
} {
    ...
}
```

This PR also adds a basic `IntoAsyncIterator` trait. This is partly for symmetry with the way `Iterator` and `IntoIterator` work. The other reason is that for async iterators it's helpful to have a place apart from the data structure being iterated over to store state. `IntoAsyncIterator` gives us a good place to do this.

I've gated this feature behind `async_for_loop` and opened #118898 as the feature tracking issue.

r? `@compiler-errors`
2023-12-22 14:17:10 +00:00
bors
5085bf51dd Auto merge of #119163 - fmease:refactor-ast-trait-bound-modifiers, r=compiler-errors
Refactor AST trait bound modifiers

Instead of having two types to represent trait bound modifiers in the parser / the AST (`parser::ty::BoundModifiers` & `ast::TraitBoundModifier`), only to map one to the other later, just use `parser::ty::BoundModifiers` (moved & renamed to `ast::TraitBoundModifiers`).

The struct type is more extensible and easier to deal with (see [here](https://github.com/rust-lang/rust/pull/119099/files#r1430749981) and [here](https://github.com/rust-lang/rust/pull/119099/files#r1430752116) for context) since it more closely models what it represents: A compound of two kinds of modifiers, constness and polarity. Modeling this as an enum (the now removed `ast::TraitBoundModifier`) meant one had to add a new variant per *combination* of modifier kind, which simply isn't scalable and which lead to a lot of explicit non-DRY matches.

NB: `hir::TraitBoundModifier` being an enum is fine since HIR doesn't need to worry representing invalid modifier kind combinations as those get rejected during AST validation thereby immensely cutting down the number of possibilities.
2023-12-22 02:00:55 +00:00
León Orell Valerian Liehr
60419aa08a Refactor AST trait bound modifiers 2023-12-20 19:39:46 +01:00
Alona Enraght-Moony
df30a7a2e4 Give VariantData::Struct named fields, to clairfy recovered. 2023-12-20 00:07:34 +00:00
Eric Holk
0315daafee Plumb awaitness of for loops 2023-12-19 12:26:20 -08:00
Nicholas Nethercote
ca2472edd7 Rename many DiagCtxt and EarlyDiagCtxt locals. 2023-12-18 16:06:22 +11:00
Nicholas Nethercote
ef315b3d7f Rename default_handler as default_dcx. 2023-12-18 16:06:22 +11:00
Nicholas Nethercote
cce3961f9a Rename ParseSess::with_span_handler as ParseSess::with_dcx. 2023-12-18 16:06:21 +11:00
Nicholas Nethercote
7738d69007 Rename ParseSess::span_diagnostic as ParseSess::dcx. 2023-12-18 16:06:21 +11:00
Nicholas Nethercote
c7992aff25 Rename Handler as DiagCtxt. 2023-12-18 16:06:19 +11:00
Aleksey Kononov
d739d93787
rename hide_parse_errors as show_parse_errors
Closes 3390

`hide_parse_errors` is now deprecated, and was renamed
`show_parse_errors` to avoid confusion around the double negative
default of `hide_parse_errors=false`.
2023-12-16 11:43:26 -05:00
Nicholas Nethercote
7045cad330 Split Handler::emit_diagnostic in two.
Currently, `emit_diagnostic` takes `&mut self`.

This commit changes it so `emit_diagnostic` takes `self` and the new
`emit_diagnostic_without_consuming` function takes `&mut self`.

I find the distinction useful. The former case is much more common, and
avoids a bunch of `mut` and `&mut` occurrences. We can also restrict the
latter with `pub(crate)` which is nice.
2023-12-15 10:13:12 +11:00
Caleb Cartwright
20196767d4
Merge pull request #5980 from ytmimi/subtree_push_2023_12_12
Subtree push 2023-12-12
2023-12-13 14:18:07 -06:00
Yacin Tmimi
03510f3515 chore: bump to the nightly 2023-12-12 toolchain 2023-12-12 11:46:59 -05:00
Yacin Tmimi
227e361187 Merge remote-tracking branch 'upstream/master' into subtree_push_2023_12_12 2023-12-12 11:45:27 -05:00
Guillaume Gomez
948c9047d5 Rollup merge of #118802 - ehuss:remove-edition-preview, r=TaKO8Ki
Remove edition umbrella features.

In the 2018 edition, there was an "umbrella" feature `#[feature(rust_2018_preview)]` which was used to enable several other features at once. This umbrella mechanism was not used in the 2021 edition and likely will not be used in 2024 either. During 2018 users reported that setting the feature was awkward, especially since they already needed to opt-in via the edition mechanism.

This PR removes this mechanism because I believe it will not be used (and will clean up and simplify the code). I believe that there are better ways to handle features and editions. In short:

- For highly experimental features, that may or may not be involved in an edition, they can implement regular feature gates like `tcx.features().my_feature`.
- For experimental features that *might* be involved in an edition, they should implement gates with `tcx.features().my_feature && span.at_least_rust_20xx()`. This requires the user to still specify `#![feature(my_feature)]`, to avoid disrupting testing of other edition features which are ready and have been accepted within the edition.
- For experimental features that have graduated to definitely be part of an edition, they should implement gates with `tcx.features().my_feature || span.at_least_rust_20xx()`, or just remove the feature check altogether and just check `span.at_least_rust_20xx()`.
- For relatively simple changes, they can skip the whole feature gating thing and just check `span.at_least_rust_20xx()`, and rely on the instability of the edition itself (which requires `-Zunstable-options`) to gate it.

I am working on documenting all of this in the rustc-dev-guide.
2023-12-11 11:40:36 +01:00
Nicholas Nethercote
1cb804b520 Add spacing information to delimiters.
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-11 09:36:40 +11:00
Eric Huss
b9ad02421a Remove edition umbrella features. 2023-12-10 13:03:28 -08:00
surechen
1b9bf8adf3 remove redundant imports
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-10 10:56:22 +08:00
Yacin Tmimi
2174e6052d Add StyleEdition enum and StyleEditionDefault trait
**Note** This does not add the `style_edition` config option to rustfmt.
The `StyleEdition` enum will eventually be used to allow users to
configure the `style_edition`, but for now it's added so we can
introduce the the `StyleEditionDefault` trait.
2023-12-09 13:05:47 -06:00
bors
9c809ce8de Auto merge of #118420 - compiler-errors:async-gen, r=eholk
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-08 19:13:57 +00:00