Commit Graph

5794 Commits

Author SHA1 Message Date
Matthias Krüger
c18226de7d Rollup merge of #121615 - nnethercote:fix-121517, r=oli-obk
Move `emit_stashed_diagnostic` call in rustfmt.

This call was added to `parse_crate_mod` in #121487, to fix a case where a stashed diagnostic wasn't emitted. But there is another path where a stashed diagnostic might fail to be emitted if there's a parse error, if the `build` call in `parse_crate_inner` fails before `parse_crate_mod` is reached.

So this commit moves the `emit_stashed_diagnostic` call outwards, from `parse_crate_mod` to `format_project`, just after the `Parser::parse_crate` call. This should be far out enough to catch any parsing errors.

Fixes #121517.

r? `@oli-obk`
cc `@ytmimi`
2024-02-26 16:06:03 +01:00
bors
50987bb36d Auto merge of #120586 - ShE3py:exprkind-err, r=fmease
Add `ErrorGuaranteed` to `ast::ExprKind::Err`

See #119967 for context
```
      \
       \
          _~^~^~_
      \) /  o o  \ (/
        '_   -   _'
        / '-----' \
```

r? fmease
2024-02-26 14:43:15 +00:00
Nicholas Nethercote
90af751966 Move emit_stashed_diagnostic call in rustfmt.
This call was added to `parse_crate_mod` in #121487, to fix a case where
a stashed diagnostic wasn't emitted. But there is another path where a
stashed diagnostic might fail to be emitted if there's a parse error, if
the `build` call in `parse_crate_inner` fails before `parse_crate_mod`
is reached.

So this commit moves the `emit_stashed_diagnostic` call outwards, from
`parse_crate_mod` to `format_project`, just after the
`Parser::parse_crate` call. This should be far out enough to catch any
parsing errors.

Fixes #121517.
2024-02-26 10:45:45 +11:00
Lieselotte
1bbfb76993 Add ErrorGuaranteed to ast::ExprKind::Err 2024-02-25 22:24:31 +01:00
Lieselotte
7d82dd0d31 Add ast::ExprKind::Dummy 2024-02-25 22:22:09 +01:00
bors
8c337451c1 Auto merge of #120393 - Urgau:rfc3373-non-local-defs, r=WaffleLapkin
Implement RFC 3373: Avoid non-local definitions in functions

This PR implements [RFC 3373: Avoid non-local definitions in functions](https://github.com/rust-lang/rust/issues/120363).
2024-02-25 19:11:06 +00:00
cui fliter
21f353a637
Spelling fixes(#6088)
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-02-23 09:57:24 -05:00
Nicholas Nethercote
ce71137b96 Explicitly call emit_stashed_diagnostics.
Commit 72b172b in #121206 changed things so that
`emit_stashed_diagnostics` is only called from `run_compiler`. But
rustfmt doesn't use `run_compiler`, so it needs to call
`emit_stashed_diagnostics` itself to avoid an abort in
`DiagCtxtInner::drop` when stashed diagnostics occur.

Fixes #121450.
2024-02-23 16:09:51 +11:00
Matthias Krüger
163c3ebf52 Rollup merge of #121085 - davidtwco:always-eager-diagnostics, r=nnethercote
errors: only eagerly translate subdiagnostics

Subdiagnostics don't need to be lazily translated, they can always be eagerly translated. Eager translation is slightly more complex as we need to have a `DiagCtxt` available to perform the translation, which involves slightly more threading of that context.

This slight increase in complexity should enable later simplifications - like passing `DiagCtxt` into `AddToDiagnostic` and moving Fluent messages into the diagnostic structs rather than having them in separate files (working on that was what led to this change).

r? ```@nnethercote```
2024-02-17 18:47:40 +01:00
rzhb
b5dcc6ff0e doc: simplify environment variable setting 2024-02-17 10:24:39 -07:00
Urgau
263910118c Allow newly added non_local_definitions in rustfmt 2024-02-17 13:59:46 +01:00
David Wood
aba5f546de errors: only eagerly translate subdiagnostics
Subdiagnostics don't need to be lazily translated, they can always be
eagerly translated. Eager translation is slightly more complex as we need
to have a `DiagCtxt` available to perform the translation, which involves
slightly more threading of that context.

This slight increase in complexity should enable later simplifications -
like passing `DiagCtxt` into `AddToDiagnostic` and moving Fluent messages
into the diagnostic structs rather than having them in separate files
(working on that was what led to this change).

Signed-off-by: David Wood <david@davidtw.co>
2024-02-15 10:34:41 +00:00
Nicholas Nethercote
6674be9657 Add an ErrorGuaranteed to ast::TyKind::Err.
This makes it more like `hir::TyKind::Err`, and avoids a
`span_delayed_bug` call in `LoweringContext::lower_ty_direct`.

It also requires adding `ast::TyKind::Dummy`, now that
`ast::TyKind::Err` can't be used for that purpose in the absence of an
error emission.

There are a couple of cases that aren't as neat as I would have liked,
marked with `FIXME` comments.
2024-02-15 09:35:11 +11:00
Jules Bertholet
5805040d28
Search span using *unnormalized* ident (#6073)
Fixes 6069

Calling `item.ident.as_str()` returns an NFC normalized ident, which
might not be what's written in the source code. To avoid panics when
calling `snippet_provider.span_after` use the ident from the source.
2024-02-14 14:40:55 -05:00
Guillaume Gomez
e504c64a41 Rollup merge of #121035 - compiler-errors:rustfmt-asyncness, r=calebcartwright
Format `async` trait bounds in rustfmt

r? `@ytmimi` or `@calebcartwright`

This PR opts to do formatting in the rust-lang/rust tree because otherwise we'd have to wait until a full sync, and rustfmt is currently totally removing the `async` keyword.

cc https://github.com/rust-lang/rustfmt/issues/6070
2024-02-14 15:41:27 +01:00
Michael Goulet
bed388348b Format async bounds in rustfmt 2024-02-13 16:05:41 +00:00
Frank King
16250ea7ba Add rustfmt test from #117942 2024-02-12 12:47:30 +08:00
Frank King
88c5838c06 Lower anonymous structs or unions to HIR 2024-02-12 12:47:23 +08:00
Wim Looman
8486837d55 Bless the target file instead of the source file 2024-02-09 06:53:07 -07:00
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