Commit Graph

1918 Commits

Author SHA1 Message Date
Nicholas Nethercote
100f3fd133 Add a new special case to Parser::look_ahead.
This new special case is simpler than the old special case because it
only is used when `dist == 1`. But that's still enough to cover ~98% of
cases. This results in equivalent performance to the old special case,
and identical behaviour as the general case.
2024-07-12 13:35:24 +10:00
Nicholas Nethercote
ebe1305b1e Remove the bogus special case from Parser::look_ahead.
The general case at the bottom of `look_ahead` is slow, because it
clones the token cursor. Above it there is a special case for
performance that is hit most of the time and avoids the cloning.
Unfortunately, its behaviour differs from the general case in two ways.

- When within a pair of delimiters, if you look any distance past the
  closing delimiter you get the closing delimiter instead of what comes
  after the closing delimiter.

- It uses `tree_cursor.look_ahead(dist - 1)` which totally confuses
  tokens with token trees. This means that only the first token in a
  token tree will be seen. E.g. in a sequence like `{ a }` the `a` and
  `}` will be skipped over. Bad!

It's likely that these differences weren't noticed before now because
the use of `look_ahead` in the parser is limited to small distances and
relatively few contexts.

Removing the special case causes slowdowns up of to 2% on a range of
benchmarks. The next commit will add a new, correct special case to
regain that lost performance.
2024-07-12 13:33:38 +10:00
Nicholas Nethercote
dad95578b0 Add unit tests for Parser::look_ahead.
It's currently buggy, so some of the test results are surprising, as
described in the `FIXME` comments. The bugs will be fixed in subsequent
commits.
2024-07-12 13:30:00 +10:00
Nicholas Nethercote
022582ca46 Remove Clone derive from LazyAttrTokenStreamImpl. 2024-07-07 16:24:51 +10:00
Nicholas Nethercote
3a5c4b6e4e Rename some attribute types for consistency.
- `AttributesData` -> `AttrsTarget`
- `AttrTokenTree::Attributes` -> `AttrTokenTree::AttrsTarget`
- `FlatToken::AttrTarget` -> `FlatToken::AttrsTarget`
2024-07-07 16:14:30 +10:00
Nicholas Nethercote
9d33a8fe51 Simplify ReplaceRange.
Currently the second element is a `Vec<(FlatToken, Spacing)>`. But the
vector always has zero or one elements, and the `FlatToken` is always
`FlatToken::AttrTarget` (which contains an `AttributesData`), and the
spacing is always `Alone`. So we can simplify it to
`Option<AttributesData>`.

An assertion in `to_attr_token_stream` can can also be removed, because
`new_tokens.len()` was always 0 or 1, which means than `range.len()`
is always greater than or equal to it, because `range.is_empty()` is
always false (as per the earlier assertion).
2024-07-07 15:58:36 +10:00
Nicholas Nethercote
dd790ab8ef Remove some unnecessary integer conversions.
These should have been removed in #127233 when the positions were
changed from `usize` to `u32`.
2024-07-05 08:27:17 +10:00
Matthias Krüger
33e9f25e91
Rollup merge of #127092 - compiler-errors:rtn-dots-redux, r=estebank
Change return-type-notation to use `(..)`

Aligns the syntax with the current wording of [RFC 3654](https://github.com/rust-lang/rfcs/pull/3654). Also implements rustfmt support (along with making a match exhaustive).

Tracking:
* https://github.com/rust-lang/rust/issues/109417
2024-07-03 23:30:07 +02:00
Nicholas Nethercote
edeebe675b Import std::{iter,mem}. 2024-07-02 20:29:01 +10:00
Nicholas Nethercote
6f6015679f Rename make_token_stream.
And update the comment. Clearly the return type of this function was
changed at some point in the past, but its name and comment weren't
updated to match.
2024-07-02 17:38:43 +10:00
Nicholas Nethercote
3d750e2702 Shrink parser positions from usize to u32.
The number of source code bytes can't exceed a `u32`'s range, so a token
position also can't. This reduces the size of `Parser` and
`LazyAttrTokenStreamImpl` by eight bytes each.
2024-07-02 17:03:53 +10:00
Nicholas Nethercote
f5b28968db Move more things around in collect_tokens_trailing_token.
To make things a little clearer, and to avoid some `mut` variables.
2024-07-02 10:46:44 +10:00
Nicholas Nethercote
8b5a7eb7f4 Move things around in collect_tokens_trailing_token.
So that the `capturing` state is adjusted immediately before and after
the call to `f`.
2024-07-02 10:46:44 +10:00
Nicholas Nethercote
2342770f49 Flip an if/else in AttrTokenStream::to_attr_token_stream.
To put the simple case first.
2024-07-02 10:46:44 +10:00
Nicholas Nethercote
36c30a968b Fix comment.
Both the indenting, and the missing `)`.
2024-07-02 10:46:44 +10:00
Nicholas Nethercote
d6c0b8117e Fix a typo in a comment. 2024-07-02 10:46:43 +10:00
Matthias Krüger
a4e92bfef0
Rollup merge of #127103 - compiler-errors:tighten-trait-bound-parsing, r=fmease
Move binder and polarity parsing into `parse_generic_ty_bound`

Let's pull out the parts of #127054 which just:
1. Make the parsing code less confusing
2. Fix `?use<>` (to correctly be denied)
3. Improve `T: for<'a> 'a` diagnostics

This should have no user-facing effects on stable parsing.

r? fmease
2024-06-29 09:14:59 +02:00
Michael Goulet
3bc3247200 Move binder and polarity parsing into parse_generic_ty_bound 2024-06-28 19:40:31 -04:00
Michael Goulet
b1a0c0b123 Change RTN to use .. again 2024-06-28 14:20:43 -04:00
Michael Goulet
789ee88bd0 Tighten spans for async blocks 2024-06-27 15:19:08 -04:00
Jacob Pratt
5ec93b8e36
Rollup merge of #126571 - nnethercote:less-maybe_whole-expr-2, r=petrochenkov
Less `maybe_whole_expr`, take 2

I first tried this in #107550. I now think it's worth doing again, as a precursor to #124141.

r? ```@petrochenkov```
2024-06-27 02:06:18 -04:00
Nicholas Nethercote
cf0251d92c Fix a span in parse_ty_bare_fn.
It currently goes one token too far.

Example: line 259 of `tests/ui/abi/compatibility.rs`:
```
test_abi_compatible!(fn_fn, fn(), fn(i32) -> i32);
```
This commit changes the span for the second element from `fn(),` to
`fn()`, i.e. removes the extraneous comma.
2024-06-26 08:23:57 +10:00
Nicholas Nethercote
379b761462 Inline and remove maybe_whole_expr!.
And remove the `NtPath` and `NtBlock` cases in
`parse_literal_maybe_minus`, because they are unnecessary.
2024-06-25 14:57:56 +10:00
Michael Goulet
9ce2a070b3
Rollup merge of #126682 - Zalathar:coverage-attr, r=lcnr
coverage: Overhaul validation of the `#[coverage(..)]` attribute

This PR makes sweeping changes to how the (currently-unstable) coverage attribute is validated:
- Multiple coverage attributes on the same item/expression are now treated as an error.
- The attribute must always be `#[coverage(off)]` or `#[coverage(on)]`, and the error messages for this are more consistent.
  -  A trailing comma is still allowed after off/on, since that's part of the normal attribute syntax.
- Some places that silently ignored a coverage attribute now produce an error instead.
  - These cases were all clearly bugs.
- Some places that ignored a coverage attribute (with a warning) now produce an error instead.
  - These were originally added as lints, but I don't think it makes much sense to knowingly allow new attributes to be used in meaningless places.
  - Some of these errors might soon disappear, if it's easy to extend recursive coverage attributes to things like modules and impl blocks.

---

One of the goals of this PR is to lay a more solid foundation for making the coverage attribute recursive, so that it applies to all nested functions/closures instead of just the one it is directly attached to.

Fixes #126658.

This PR incorporates #126659, which adds more tests for validation of the coverage attribute.

`@rustbot` label +A-code-coverage
2024-06-24 15:51:03 -04:00
Matthias Krüger
a80ee9159b
Rollup merge of #126882 - estebank:multiline-order, r=WaffleLapkin
Special case when a code line only has multiline span starts

Minimize multline span overlap when there are multiple of them starting on the same line:

```
3 |       X0 Y0 Z0
  |  _____^  -  -
  | | _______|  |
  | || _________|
4 | |||   X1 Y1 Z1
5 | |||   X2 Y2 Z2
  | |||____^__-__- `Z` label
  | ||_____|__|
  | |______|  `Y` is a good letter too
  |        `X` is a good letter
```
2024-06-24 15:06:23 +02:00
Zalathar
a000fa8b54 coverage: Tighten validation of #[coverage(off)] and #[coverage(on)] 2024-06-24 20:15:01 +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
Esteban Küber
284437d434 Special case when a code line only has multiline span starts
```
3 |       X0 Y0 Z0
  |  _____^  -  -
  | | _______|  |
  | || _________|
4 | |||   X1 Y1 Z1
5 | |||   X2 Y2 Z2
  | |||____^__-__- `Z` label
  | ||_____|__|
  | |______|  `Y` is a good letter too
  |        `X` is a good letter
```
2024-06-23 22:00:52 +00: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
Matthias Krüger
f577d808b7
Rollup merge of #126767 - compiler-errors:static-foreign-item, r=spastorino
`StaticForeignItem` and `StaticItem` are the same

The struct `StaticItem` and `StaticForeignItem` are the same, so remove `StaticForeignItem`. Having them be separate is unique to `static` items -- unlike `ForeignItemKind::{Fn,TyAlias}`, which use the normal AST item.

r? ``@spastorino`` or ``@oli-obk``
2024-06-21 09:12:37 +02: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
Matthias Krüger
73cc4eca56
Rollup merge of #126125 - dev-ardi:conflict-markers, r=estebank
Improve conflict marker recovery

<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r​? <reviewer name>
-->
closes #113826
r? ```@estebank``` since you reviewed #115413
cc: ```@rben01``` since you opened up the issue in the first place
2024-06-21 09:12:34 +02:00
bors
4e6de37349 Auto merge of #126757 - compiler-errors:safe, r=spastorino
Properly gate `safe` keyword in pre-expansion

This PR gates `safe` keyword in pre-expansion contexts. Should mitigate the fallout of https://github.com/rust-lang/rust/issues/126755, which is that `safe` is now usable on beta lol.

r? `@spastorino` or `@oli-obk`

cc #124482 tracking #123743
2024-06-21 04:22:02 +00:00
Michael Goulet
3e59f0c3c5 StaticForeignItem and StaticItem are the same 2024-06-20 19:51:09 -04:00
Michael Goulet
108b3f214a Properly gate safe keyword in pre-expansion 2024-06-20 14:14:49 -04:00
Matthias Krüger
ef2e8bfcbf
Rollup merge of #126717 - nnethercote:rustfmt-use-pre-cleanups, r=jieyouxu
Clean up some comments near `use` declarations

#125443 will reformat all `use` declarations in the repository. There are a few edge cases involving comments on `use` declarations that require care. This PR cleans up some clumsy comment cases, taking us a step closer to #125443 being able to merge.

r? ``@lqd``
2024-06-20 14:07:04 +02:00
Nicholas Nethercote
b104fbec85 Add blank lines after module-level // comments.
Similar to the previous commit.
2024-06-20 09:23:20 +10:00
Nicholas Nethercote
c6f78270b6 Introduce can_begin_string_literal.
We currently use `can_begin_literal_maybe_minus` in a couple of places
where only string literals are allowed. This commit introduces a
more specific function, which makes things clearer. It doesn't change
behaviour because the two functions affected (`is_unsafe_foreign_mod`
and `check_keyword_case`) are always followed by a call to `parse_abi`,
which checks again for a string literal.
2024-06-20 04:50:40 +10:00
Nicholas Nethercote
7d9a92ba31 Inline can_begin_literal_maybe_minus call into two places.
It's clearer this way, because the `Interpolated` cases in
`can_begin_const_arg` and `is_pat_range_end_start` are more permissive
than the `Interpolated` cases in `can_begin_literal_maybe_minus`.
2024-06-20 04:50:38 +10:00
Michael Goulet
3e8898a4e1 Allow naming expr_2021 in all editions 2024-06-19 12:37:49 -04:00
bors
894f7a4ba6 Auto merge of #126678 - nnethercote:fix-duplicated-attrs-on-nt-expr, r=petrochenkov
Fix duplicated attributes on nonterminal expressions

This PR fixes a long-standing bug (#86055) whereby expression attributes can be duplicated when expanded through declarative macros.

First, consider how items are parsed in declarative macros:
```
Items:
- parse_nonterminal
  - parse_item(ForceCollect::Yes)
    - parse_item_
      - attrs = parse_outer_attributes
      - parse_item_common(attrs)
        - maybe_whole!
        - collect_tokens_trailing_token
```
The important thing is that the parsing of outer attributes is outside token collection, so the item's tokens don't include the attributes. This is how it's supposed to be.

Now consider how expression are parsed in declarative macros:
```
Exprs:
- parse_nonterminal
  - parse_expr_force_collect
    - collect_tokens_no_attrs
      - collect_tokens_trailing_token
        - parse_expr
          - parse_expr_res(None)
            - parse_expr_assoc_with
              - parse_expr_prefix
                - parse_or_use_outer_attributes
                - parse_expr_dot_or_call
```
The important thing is that the parsing of outer attributes is inside token collection, so the the expr's tokens do include the attributes, i.e. in `AttributesData::tokens`.

This PR fixes the bug by rearranging expression parsing to that outer attribute parsing happens outside of token collection. This requires a number of small refactorings because expression parsing is somewhat complicated. While doing so the PR makes the code a bit cleaner and simpler, by eliminating `parse_or_use_outer_attributes` and `Option<AttrWrapper>` arguments (in favour of the simpler `parse_outer_attributes` and `AttrWrapper` arguments), and simplifying `LhsExpr`.

r? `@petrochenkov`
2024-06-19 13:58:21 +00:00
Nicholas Nethercote
64c2e9ed3b Change how parse_expr_force_collect works.
It now parses outer attributes before collecting tokens. This avoids the
problem where the outer attribute tokens were being stored twice -- for
the attribute tokesn, and also for the expression tokens.

Fixes #86055.
2024-06-19 19:15:06 +10:00
Nicholas Nethercote
8170acb197 Refactor parse_expr_res.
This removes the final `Option<AttrWrapper>` argument.
2024-06-19 19:12:02 +10:00
Nicholas Nethercote
43eae4cef4 Simplify LhsExpr::Unparsed.
By making the `AttrWrapper` non-optional.
2024-06-19 19:12:02 +10:00
Nicholas Nethercote
aaa220e875 Move parse_or_use_outer_attributes out of parse_expr_prefix_range.
This eliminates another `Option<AttrWrapper>` argument and changes one
obscure error message.
2024-06-19 19:12:00 +10:00
Nicholas Nethercote
802779f77d Move parse_or_use_outer_attributes out of parse_expr_prefix.
This eliminates one `Option<AttrWrapper>` argument.
2024-06-19 18:53:25 +10:00
Nicholas Nethercote
ead0a45202 Inline and remove parse_expr_assoc.
It has a single call site.
2024-06-19 18:53:25 +10:00
Nicholas Nethercote
25523ba382 Refactor LhsExpr.
Combine `NotYetParsed` and `AttributesParsed` into a single variant,
because (a) that reflects the structure of the code that consumes
`LhsExpr`, and (b) because that variant will have the `Option` removed
in a later commit.
2024-06-19 18:53:25 +10:00
Nicholas Nethercote
42e47dfe82 Remove From impls for LhsExpr.
The `Option<AttrWrapper>` one maps to the first two variants, and the
`P<Expr>` one maps to the third. Weird. The code is shorter and clearer
without them.
2024-06-19 18:53:25 +10:00