Commit graph

1042 commits

Author SHA1 Message Date
Jacob Pratt b1f43974c4
Rollup merge of #126928 - nnethercote:124141-pre, r=oli-obk
Some `Nonterminal` removal precursors

Small things to prepare for #124141, more or less.

r? ```@oli-obk```
2024-06-27 02:06:19 -04:00
Nicholas Nethercote 2e4d547d4a Extra panic cases.
Just some extra sanity checking, making explicit some values not
possible in code working with token trees -- we shouldn't be seeing
explicit delimiter tokens, because they should be represented as
`TokenTree::Delimited`.
2024-06-25 14:29:25 +10:00
Matthias Krüger 9a591ea1ce
Rollup merge of #126177 - carbotaniuman:unsafe_attr_errors, r=jieyouxu
Add hard error and migration lint for unsafe attrs

More implementation work for https://github.com/rust-lang/rust/issues/123757

This adds the migration lint for unsafe attributes, as well as making it a hard error in Rust 2024.
2024-06-24 06:27:12 +02:00
carbotaniuman a23917cfd0 Add hard error and migration lint for unsafe attrs 2024-06-23 19:02:14 -05:00
Matthias Krüger 0149bc47d8
Rollup merge of #126851 - nnethercote:NtExprKind-NtPatKind, r=compiler-errors
Rework pattern and expression nonterminal kinds.

Some tweaks to `NonterminalKind` that will assist with #124141. Details in the individual commits.

r? compiler-errors
cc ```@eholk```
2024-06-23 22:39:01 +02:00
Nicholas Nethercote e2aa38e6ab Rework pattern and expression nonterminal kinds.
Merge `PatParam`/`PatWithOr`, and `Expr`/`Expr2021`, for a few reasons.

- It's conceptually nice, because the two pattern kinds and the two
  expression kinds are very similar.

- With expressions in particular, there are several places where both
  expression kinds get the same treatment.

- It removes one unreachable match arm.

- Most importantly, for #124141 I will need to introduce a new type
  `MetaVarKind` that is very similar to `NonterminalKind`, but records a
  couple of extra fields for expression metavars. It's nicer to have a
  single `MetaVarKind::Expr` expression variant to hold those extra
  fields instead of duplicating them across two variants
  `MetaVarKind::{Expr,Expr2021}`. And then it makes sense for patterns
  to be treated the same way, and for `NonterminalKind` to also be
  treated the same way.

I also clarified the comments, because I have long found them a little
hard to understand.
2024-06-23 15:57:24 +10:00
Nicholas Nethercote 70fa67c0b2 Tweak some ugly formatting. 2024-06-23 08:13:41 +10:00
Nicholas Nethercote 470b0e9c3c Import NonterminalKind in compiler/rustc_expand/src/mbe/quoted.rs.
So we can omit the `token::` qualifier, which gives more space to some
cramped code.
2024-06-23 08:11:54 +10:00
Vadim Petrochenkov 0a265957dd delegation: Do not crash on qpaths without a trait 2024-06-22 19:57:19 +03:00
Matthias Krüger 3bd84f18bc
Rollup merge of #126700 - compiler-errors:fragment, r=fmease
Make edition dependent `:expr` macro fragment act like the edition-dependent `:pat` fragment does

Parse the `:expr` fragment as `:expr_2021` in editions <=2021, and as `:expr` in edition 2024. This is similar to how we parse `:pat` as `:pat_param` in edition <=2018 and `:pat_with_or` in >=2021, and means we can get rid of a span dependency from `nonterminal_may_begin_with`.

Specifically, this fixes a theoretical regression since the `expr_2021` macro fragment previously would allow `const {}` if the *caller* is edition 2024. This is inconsistent with the way that the `pat` macro fragment was upgraded, and also leads to surprising behavior when a macro *caller* crate upgrades to edtion 2024, since they may have parsing changes that they never asked for (with no way of opting out of it).

This PR also allows using `expr_2021` in all editions. Why was this was disallowed in the first place? It's purely additive, and also it's still feature gated?

r? ```@fmease``` ```@eholk``` cc ```@vincenzopalazzo```
cc #123865

Tracking:

- https://github.com/rust-lang/rust/issues/123742
2024-06-21 09:12:36 +02:00
Nicholas Nethercote 665821cb60 Add blank lines after module-level //! comments.
Most modules have such a blank line, but some don't. Inserting the blank
line makes it clearer that the `//!` comments are describing the entire
module, rather than the `use` declaration(s) that immediately follows.
2024-06-20 09:23:20 +10:00
Michael Goulet 3e8898a4e1 Allow naming expr_2021 in all editions 2024-06-19 12:37:49 -04:00
许杰友 Jieyou Xu (Joe) f8ce1cfbf5
Rollup merge of #124135 - petrochenkov:deleglob, r=fmease
delegation: Implement glob delegation

Support delegating to all trait methods in one go.
Overriding globs with explicit definitions is also supported.

The implementation is generally based on the design from https://github.com/rust-lang/rfcs/pull/3530#issuecomment-2020869823, but unlike with list delegation in https://github.com/rust-lang/rust/pull/123413 we cannot expand glob delegation eagerly.
We have to enqueue it into the queue of unexpanded macros (most other macros are processed this way too), and then a glob delegation waits in that queue until its trait path is resolved, and enough code expands to generate the identifier list produced from the glob.

Glob delegation is only allowed in impls, and can only point to traits.
Supporting it in other places gives very little practical benefit, but significantly raises the implementation complexity.

Part of https://github.com/rust-lang/rust/issues/118212.
2024-06-19 01:51:36 +01:00
Oli Scherer 3f34196839 Remove redundant argument from subdiagnostic method 2024-06-18 15:42:11 +00:00
Oli Scherer 7ba82d61eb Use a dedicated type instead of a reference for the diagnostic context
This paves the way for tracking more state (e.g. error tainting) in the diagnostic context handle
2024-06-18 15:42:11 +00:00
Oli Scherer c91edc3888 Prefer dcx methods over fields or fields' methods 2024-06-18 13:45:08 +00:00
Matthias Krüger 5fac57284e
Rollup merge of #125829 - petrochenkov:upctxt2, r=michaelwoerister
rustc_span: Add conveniences for working with span formats

This is the refactoring part of https://github.com/rust-lang/rust/pull/125017.
2024-06-15 14:40:47 +02:00
Vadim Petrochenkov 22d0b1ee18 delegation: Implement glob delegation 2024-06-14 19:27:51 +03:00
Caio 4b82afb40c Add a new concat metavar expr 2024-06-13 22:12:26 -03:00
Vadim Petrochenkov 6fea953267 rustc_span: By-value interface for ctxt update 2024-06-13 19:29:24 +03:00
许杰友 Jieyou Xu (Joe) 81ff9b5770
Rollup merge of #125913 - fmease:early-lints-spruce-up-some-diags, r=Nadrieril
Spruce up the diagnostics of some early lints

Implement the various "*(note to myself) in a follow-up PR we should turn parts of this message into a subdiagnostic (help msg or even struct sugg)*" drive-by comments I left in #124417 during my review.

For context, before #124417, only a few early lints touched/decorated/customized their diagnostic because the former API made it a bit awkward. Likely because of that, things that should've been subdiagnostics were just crammed into the primary message. This PR rectifies this.
2024-06-11 09:14:34 +01:00
Vadim Petrochenkov d5dd2d8284 rustc_span: Optimize syntax context updates in spans 2024-06-10 02:20:16 +03:00
carbotaniuman 15f8ffe371 Fix formatting 2024-06-06 20:27:25 -05:00
carbotaniuman 87be1bae73 Fix build 2024-06-06 20:27:25 -05:00
carbotaniuman c4de986afa Disallow unsafe in derive 2024-06-06 20:26:27 -05:00
carbotaniuman 67f5dd1ef1 Parse unsafe attributes 2024-06-06 20:26:27 -05:00
bors 2d28b6384e Auto merge of #124482 - spastorino:unsafe-extern-blocks, r=oli-obk
Unsafe extern blocks

This implements RFC 3484.

Tracking issue #123743 and RFC https://github.com/rust-lang/rfcs/pull/3484

This is better reviewed commit by commit.
2024-06-06 08:14:58 +00:00
Nicholas Nethercote b9037339cb Make top-level rustc_parse functions fallible.
Currently we have an awkward mix of fallible and infallible functions:
```
       new_parser_from_source_str
 maybe_new_parser_from_source_str
       new_parser_from_file
(maybe_new_parser_from_file)        // missing
      (new_parser_from_source_file) // missing
 maybe_new_parser_from_source_file
       source_str_to_stream
 maybe_source_file_to_stream
```
We could add the two missing functions, but instead this commit removes
of all the infallible ones and renames the fallible ones leaving us with
these which are all fallible:
```
new_parser_from_source_str
new_parser_from_file
new_parser_from_source_file
source_str_to_stream
source_file_to_stream
```
This requires making `unwrap_or_emit_fatal` public so callers of
formerly infallible functions can still work.

This does make some of the call sites slightly more verbose, but I think
it's worth it for the simpler API. Also, there are two `catch_unwind`
calls and one `catch_fatal_errors` call in this diff that become
removable thanks this change. (I will do that in a follow-up PR.)
2024-06-05 10:38:03 +10:00
Nicholas Nethercote ab192a0c97 Reorder source_str_to_stream arguments.
It's the only one of these functions where `psess` isn't the first
argument.
2024-06-05 10:38:02 +10:00
Nicholas Nethercote 3c321b9ea8 Remove stream_to_parser.
It's a zero-value wrapper of `Parser::new`.
2024-06-05 10:37:59 +10:00
Nicholas Nethercote d1215da26e Don't use the word "parse" for lexing operations.
Lexing converts source text into a token stream. Parsing converts a
token stream into AST fragments. This commit renames several lexing
operations that have "parse" in the name. I think these names have been
subtly confusing me for years.

This is just a `s/parse/lex/` on function names, with one exception:
`parse_stream_from_source_str` becomes `source_str_to_stream`, to make
it consistent with the existing `source_file_to_stream`. The commit also
moves that function's location in the file to be just above
`source_file_to_stream`.

The commit also cleans up a few comments along the way.
2024-06-05 10:29:16 +10:00
Santiago Pastorino bac72cf7cf
Add safe/unsafe to static inside extern blocks 2024-06-04 14:19:43 -03:00
León Orell Valerian Liehr b2949ff911
Spruce up the diagnostics of some early lints 2024-06-03 07:25:32 +02:00
Nicholas Nethercote cf0c2c7333 Convert proc_macro_back_compat lint to an unconditional error.
We still check for the `rental`/`allsorts-rental` crates. But now if
they are detected we just emit a fatal error, instead of emitting a
warning and providing alternative behaviour.

The original "hack" implementing alternative behaviour was added
in #73345.

The lint was added in #83127.

The tracking issue is #83125.

The direct motivation for the change is that providing the alternative
behaviour is interfering with #125174 and follow-on work.
2024-05-28 08:15:15 +10:00
Nicholas Nethercote 3607cee3e7 Use let chains in pretty_printing_compatibility_hack.
To reduce indentation and improve readability.
2024-05-28 08:14:20 +10:00
Nicholas Nethercote d6d2ff055e Remove a stray comment that shouldn't be here. 2024-05-28 08:14:20 +10:00
Guillaume Gomez f50b4f5034
Rollup merge of #125530 - SparrowLii:expand2, r=petrochenkov
cleanup dependence of `ExtCtxt` in transcribe when macro expansion

part of #125356
We can remove `transcribe`’s dependence on `ExtCtxt` to facilitate subsequent work (such as moving macro expansion into the incremental compilation system)

r? ```@petrochenkov```
Thanks for the reviewing!
2024-05-27 13:10:35 +02:00
SparrowLii 278212342e cleanup dependence of ExtCtxt in transcribe when macro expansion 2024-05-25 10:38:18 +08:00
Matthias Krüger 3c79f0cd69
Rollup merge of #125316 - nnethercote:tweak-Spacing, r=petrochenkov
Tweak `Spacing` use

Some clean-up precursors to #125174.

r? ``@petrochenkov``
2024-05-23 07:41:18 +02:00
Nicholas Nethercote 4d513cb4bf Add some comments. 2024-05-23 06:03:17 +10:00
Nicholas Nethercote b6de782198 Clarify a comment. 2024-05-23 06:03:17 +10:00
Nicholas Nethercote 3fc8f8998c Clarify parse a little.
- Name the colon span as `colon_span` to distinguish it from the other
  `span` local variable.
- Just use basic pattern matching, which is easier to read than `map_or`.
2024-05-23 06:00:57 +10:00
Nicholas Nethercote c8844dfdc0 Clarify the meaning of the span within mbe::TokenTree::MetaVar. 2024-05-23 06:00:28 +10:00
Xiretza 98dd6c7e8f Rename buffer_lint_with_diagnostic to buffer_lint 2024-05-21 20:16:39 +00:00
Xiretza 8004e6a379 Make early lints translatable 2024-05-21 20:16:39 +00:00
Xiretza b7abf014ec Convert uses of BuiltinLintDiag::Normal to custom variants
This ensures all diagnostic messages are created at diagnostic emission
time, making them translatable.
2024-05-21 20:16:39 +00:00
Xiretza c227f35a9c Generate lint diagnostic message from BuiltinLintDiag
Translation of the lint message happens when the actual diagnostic is
created, not when the lint is buffered. Generating the message from
BuiltinLintDiag ensures that all required data to construct the message
is preserved in the LintBuffer, eventually allowing the messages to be
moved to fluent.

Remove the `msg` field from BufferedEarlyLint, it is either generated
from the data in the BuiltinLintDiag or stored inside
BuiltinLintDiag::Normal.
2024-05-21 20:16:39 +00:00
bors 685a80f7a0 Auto merge of #125180 - mu001999-contrib:improve/macro-diag, r=fee1-dead
Improve error message: missing `;` in macro_rules

Fixes #124968
2024-05-18 13:02:48 +00:00
r0cky c2be1342b7 Improve error message: missing ; in macro_rules 2024-05-18 18:56:12 +08:00
bors 9b75a43881 Auto merge of #123865 - eholk:expr_2021, r=fmease
Update `expr` matcher for Edition 2024 and add `expr_2021` nonterminal

This commit adds a new nonterminal `expr_2021` in macro patterns, and `expr_fragment_specifier_2024` feature flag.

This change also updates `expr` so that on Edition 2024 it will also match `const { ... }` blocks, while `expr_2021` preserves the current behavior of `expr`, matching expressions without `const` blocks.

Joint work with `@vincenzopalazzo.`

Issue #123742
2024-05-17 21:54:14 +00:00