Commit Graph

1996 Commits

Author SHA1 Message Date
Nicholas Nethercote
84ac80f192 Reformat use declarations.
The previous commit updated `rustfmt.toml` appropriately. This commit is
the outcome of running `x fmt --all` with the new formatting options.
2024-07-29 08:26:52 +10:00
Trevor Gross
9164dbd48c
Rollup merge of #128207 - folkertdev:asm-parser-generalize, r=Amanieu
improve error message when `global_asm!` uses `asm!` options

specifically, what was

    error: expected one of `)`, `att_syntax`, or `raw`, found `preserves_flags`
      --> $DIR/bad-options.rs:45:25
       |
    LL | global_asm!("", options(preserves_flags));
       |                         ^^^^^^^^^^^^^^^ expected one of `)`, `att_syntax`, or `raw`

is now

    error: the `preserves_flags` option cannot be used with `global_asm!`
      --> $DIR/bad-options.rs:45:25
       |
    LL | global_asm!("", options(preserves_flags));
       |                         ^^^^^^^^^^^^^^^ the `preserves_flags` option is not meaningful for global-scoped inline assembly

mirroring the phrasing of the [reference](https://doc.rust-lang.org/reference/inline-assembly.html#options).

This is also a bit of a refactor for a future `naked_asm!` macro (for use in `#[naked]` functions). Currently this sort of error can come up when switching from inline to global asm, or when a user just isn't that experienced with assembly. With  `naked_asm!` added to the mix hitting this error is more likely.
2024-07-27 13:32:56 -04:00
Trevor Gross
7eaf74743b
Rollup merge of #128229 - tdittr:unsafe-extern-abi-error, r=compiler-errors
Improve `extern "<abi>" unsafe fn()` error message

These errors were already reported in #87217, and fixed by #87235 but missed the case of an explicit ABI.

This PR does not cover multiple keywords like `extern "C" pub const unsafe fn()`, but I don't know what a good way to cover this  would be. It also seems rarer than `extern "C" unsafe` which I saw happen a few times in workshops.
2024-07-26 19:03:08 -04:00
Trevor Gross
af52be2cea
Rollup merge of #128224 - nnethercote:fewer-replace_ranges, r=petrochenkov
Remove unnecessary range replacements

This PR removes an unnecessary range replacement in `collect_tokens_trailing_token`, and does a couple of other small cleanups.

r? ````@petrochenkov````
2024-07-26 19:03:06 -04:00
Trevor Gross
553a64f412
Rollup merge of #128223 - nnethercote:refactor-collect_tokens, r=petrochenkov
Refactor complex conditions in `collect_tokens_trailing_token`

More readability improvements for this complicated function.

r? ````@petrochenkov````
2024-07-26 19:03:06 -04:00
Tamme Dittrich
3fdc99193e Improve error message for extern "C" unsafe fn()
This was handled correctly already for `extern unsafe fn()`.

Co-authored-by: Folkert <folkert@folkertdev.nl>
2024-07-26 15:14:05 +02:00
Nicholas Nethercote
55d37ae711 Remove an unnecessary block. 2024-07-26 17:37:03 +10:00
Nicholas Nethercote
6ea2da5a28 Tweak a loop.
A fully imperative style is easier to read than a half-iterator,
half-imperative style. Also, rename `inner_attr` as `attr` because it
might be an outer attribute.
2024-07-26 17:37:03 +10:00
Nicholas Nethercote
6e87858f26 Fix a comment.
Imagine you have replace ranges (2..20,X) and (5..15,Y), and these tokens:
```
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x
```
If we replace (5..15,Y) first, then (2..20,X) we get this sequence
```
a,b,c,d,e,Y,_,_,_,_,_,_,_,_,_,p,q,r,s,t,u,v,w,x
a,b,X,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,u,v,w,x
```
which is what we want.

If we do it in the other order, we get this:
```
a,b,X,_,_,_,_,_,_,_,_,_,_,_,_,p,q,r,s,t,u,v,w,x
a,b,X,_,_,Y,_,_,_,_,_,_,_,_,_,_,_,_,_,_,u,v,w,x
```
which is wrong. So it's true that we need the `.rev()` but the comment
is wrong about why.
2024-07-26 17:37:03 +10:00
Nicholas Nethercote
a560810a69 Don't include inner attribute ranges in CaptureState.
The current code is this:
```
self.capture_state.replace_ranges.push((start_pos..end_pos, Some(target)));
self.capture_state.replace_ranges.extend(inner_attr_replace_ranges);
```
What's not obvious is that every range in `inner_attr_replace_ranges`
must be a strict sub-range of `start_pos..end_pos`. Which means, in
`LazyAttrTokenStreamImpl::to_attr_token_stream`, they will be done
first, and then the `start_pos..end_pos` replacement will just overwrite
them. So they aren't needed.
2024-07-26 14:18:20 +10:00
Nicholas Nethercote
e631b1ebfa Invert the sense of is_complete and rename it as needs_tokens.
I have always found `is_complete` an unhelpful name. The new name (and
inverted sense) fits in better with the conditions at its call sites.
2024-07-26 09:58:34 +10:00
Nicholas Nethercote
3d363c3d99 Move is_complete to the module that uses it.
And make it non-`pub`.
2024-07-26 09:44:39 +10:00
Nicholas Nethercote
4288edb219 Inline and remove AttrWrapper::is_complete.
It has a single call site. This change makes the two `needs_collect`
conditions more similar to each other, and therefore easier to
understand.
2024-07-26 09:44:07 +10:00
Nicholas Nethercote
caee195bdd Invert early exit conditions in collect_tokens_trailing_token.
This has been bugging me for a while. I find complex "if any of these
are true" conditions easier to think about than complex "if all of these
are true" conditions, because you can stop as soon as one is true.
2024-07-26 09:43:41 +10:00
Folkert
d3858f7465
improve error message when global_asm! uses asm! options 2024-07-25 22:33:52 +02:00
surechen
4ac60601d3 Fix a span error when parsing a wrong param of function.
fixes #128042
2024-07-25 22:33:45 +08:00
Matthias Krüger
cfc5f25b3d
Rollup merge of #127054 - compiler-errors:bound-ordering, r=fmease
Reorder trait bound modifiers *after* `for<...>` binder in trait bounds

This PR suggests changing the grammar of trait bounds from:

```
[CONSTNESS] [ASYNCNESS] [?] [BINDER] [TRAIT_PATH]

const async ? for<'a> Sized
```

to

```
([BINDER] [CONSTNESS] [ASYNCNESS] | [?]) [TRAIT_PATH]
```

i.e., either

```
? Sized
```

or

```
for<'a> const async Sized
```

(but not both)

### Why?

I think it's strange that the binder applies "more tightly" than the `?` trait polarity. This becomes even weirder when considering that we (or at least, I) want to have `async` trait bounds expressed like:

```
where T: for<'a> async Fn(&'a ()) -> i32,
```

and not:

```
where T: async for<'a> Fn(&'a ()) -> i32,
```

### Fallout

No crates on crater use this syntax, presumably because it's literally useless. This will require modifying the reference grammar, though.

### Alternatives

If this is not desirable, then we can alternatively keep parsing `for<'a>` after the `?` but deprecate it with either an FCW (or an immediate hard error), and begin parsing `for<'a>` *before* the `?`.
2024-07-25 04:43:18 +02:00
León Orell Valerian Liehr
7da751a108
Apply suggestions from code review 2024-07-25 03:00:04 +02:00
Oli Scherer
8d290058c9 Always pass the visitor as the first argument to walk* functions 2024-07-22 14:01:24 +00:00
Oli Scherer
754bdef793 Sync mut_visit function names with immut visit ones (s/noop_visit/walk/) 2024-07-22 14:01:24 +00:00
bors
3811f40d27 Auto merge of #127957 - matthiaskrgr:rollup-1u5ivck, r=matthiaskrgr
Rollup of 6 pull requests

Successful merges:

 - #127350 (Parser: Suggest Placing the Return Type After Function Parameters)
 - #127621 (Rewrite and rename `issue-22131` and `issue-26006` `run-make` tests to rmake)
 - #127662 (When finding item gated behind a `cfg` flag, point at it)
 - #127903 (`force_collect` improvements)
 - #127932 (rustdoc: fix `current` class on sidebar modnav)
 - #127943 (Don't allow unsafe statics outside of extern blocks)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-07-19 13:39:12 +00:00
Matthias Krüger
9ada89d9a1
Rollup merge of #127903 - nnethercote:force_collect-improvements, r=petrochenkov
`force_collect` improvements

Yet more cleanups relating to `cfg_attr` processing.

r? ````@petrochenkov````
2024-07-19 10:48:05 +02:00
Matthias Krüger
c86e13f330
Rollup merge of #127350 - veera-sivarajan:bugfix-126311, r=lcnr
Parser: Suggest Placing the Return Type After Function Parameters

Fixes #126311

This PR suggests placing the return type after the function parameters when it's misplaced after a `where` clause.

This also tangentially improves diagnostics for cases like [this](86d6f1312a/tests/ui/parser/issues/misplaced-return-type-without-where-issue-126311.rs (L1C1-L1C28)) and adds doc comments for `parser::AllowPlus`.
2024-07-19 10:48:03 +02:00
Nicholas Nethercote
1dd566a6d0 Overhaul comments in collect_tokens_trailing_token.
Adding details, clarifying lots of little things, etc. In particular,
the commit adds details of an example. I find this very helpful, because
it's taken me a long time to understand how this code works.
2024-07-19 15:25:55 +10:00
Nicholas Nethercote
ca6649516f Make Parser::num_bump_calls 0-indexed.
Currently in `collect_tokens_trailing_token`, `start_pos` and `end_pos`
are 1-indexed by `replace_ranges` is 0-indexed, which is really
confusing. Making them both 0-indexed makes debugging much easier.
2024-07-19 15:25:55 +10:00
Nicholas Nethercote
f9c7ca70cb Move inner_attr code downwards.
This puts it just before the `replace_ranges` initialization, which
makes sense because the two variables are closely related.
2024-07-19 15:25:54 +10:00
Nicholas Nethercote
1f67cf9e63 Remove final_attrs local variable.
It's no shorter than `ret.attrs()`, and `ret.attrs()` is used multiple
times earlier in the function.
2024-07-19 15:25:54 +10:00
Nicholas Nethercote
757f73f506 Simplify CaptureState::inner_attr_ranges.
The `Option`s within the `ReplaceRange`s within the hashmap are always
`None`. This PR omits them and inserts them when they are extracted from
the hashmap.
2024-07-19 15:25:54 +10:00
Nicholas Nethercote
4158a1c48f Only check force_collect in collect_tokens_trailing_token.
There are three places where we currently check `force_collect` and call
`collect_tokens_no_attrs` for `ForceCollect::Yes` and a vanilla parsing
function for `ForceCollect::No`.

But we can instead just pass in `force_collect` and let
`collect_tokens_trailing_token` do the appropriate thing.
2024-07-19 08:42:33 +10:00
Nicholas Nethercote
9d908a2877 Use ForceCollect in parse_attr_item.
Instead of a `bool`. Because `ForceCollect` is used in this way
everywhere else.
2024-07-19 08:24:54 +10:00
Nicholas Nethercote
7d7e2a173a Don't always force collect tokens in recover_stmt_local_after_let.
Use a parameter to decide whether to force collect, as is done for the
closely related `parse_local_mk` method.
2024-07-19 08:24:53 +10:00
Nicholas Nethercote
e69ff1c106 Remove an unnecessary ForceCollect::Yes.
No need to collect tokens on this recovery path, because the parsed
statement isn't even looked at.
2024-07-19 08:20:57 +10:00
Veera
4cad705017 Parser: Suggest Placing the Return Type After Function Parameters 2024-07-18 17:56:34 -04:00
Matthias Krüger
50a90e394e
Rollup merge of #127835 - estebank:issue-127823, r=compiler-errors
Fix ICE in suggestion caused by `⩵` being recovered as `==`

The second suggestion shown here would previously incorrectly assume that the span corresponding to `⩵` was 2 bytes wide composed by 2 1 byte wide chars, so a span pointing at `==` could point only at one of the `=` to remove it. Instead, we now replace the whole thing (as we should have the whole time):

```
error: unknown start of token: \u{2a75}
  --> $DIR/unicode-double-equals-recovery.rs:1:16
   |
LL | const A: usize ⩵ 2;
   |                ^
   |
help: Unicode character '⩵' (Two Consecutive Equals Signs) looks like '==' (Double Equals Sign), but it is not
   |
LL | const A: usize == 2;
   |                ~~

error: unexpected `==`
  --> $DIR/unicode-double-equals-recovery.rs:1:16
   |
LL | const A: usize ⩵ 2;
   |                ^
   |
help: try using `=` instead
   |
LL | const A: usize = 2;
   |                ~
```

Fix #127823.
2024-07-18 23:05:21 +02:00
Esteban Küber
67ec1326ee Fix ICE in suggestion caused by being recovered as ==
The second suggestion shown here would previously incorrectly assume that the span corresponding to `⩵` was 2 bytes wide composed by 2 1 byte wide chars, so a span pointing at `==` could point only at one of the `=` to remove it. Instead, we now replace the whole thing (as we should have the whole time):

```
error: unknown start of token: \u{2a75}
  --> $DIR/unicode-double-equals-recovery.rs:1:16
   |
LL | const A: usize ⩵ 2;
   |                ^
   |
help: Unicode character '⩵' (Two Consecutive Equals Signs) looks like '==' (Double Equals Sign), but it is not
   |
LL | const A: usize == 2;
   |                ~~

error: unexpected `==`
  --> $DIR/unicode-double-equals-recovery.rs:1:16
   |
LL | const A: usize ⩵ 2;
   |                ^
   |
help: try using `=` instead
   |
LL | const A: usize = 2;
   |                ~
```
2024-07-18 17:47:31 +00:00
Trevor Gross
e2e0681e3a
Rollup merge of #127842 - nnethercote:rm-TrailingToken, r=petrochenkov
Remove `TrailingToken`.

It's used in `Parser::collect_tokens_trailing_token` to decide whether to capture a trailing token. But the callers actually know whether to capture a trailing token, so it's simpler for them to just pass in a bool.

Also, the `TrailingToken::Gt` case was weird, because it didn't result in a trailing token being captured. It could have been subsumed by the `TrailingToken::MaybeComma` case, and it effectively is in the new code.

r? `@petrochenkov`
2024-07-18 05:14:07 -05:00
Nicholas Nethercote
487802d6c8 Remove TrailingToken.
It's used in `Parser::collect_tokens_trailing_token` to decide whether
to capture a trailing token. But the callers actually know whether to
capture a trailing token, so it's simpler for them to just pass in a
bool.

Also, the `TrailingToken::Gt` case was weird, because it didn't result
in a trailing token being captured. It could have been subsumed by the
`TrailingToken::MaybeComma` case, and it effectively is in the new code.
2024-07-18 17:28:49 +10:00
Matthias Krüger
77e5bbf341
Rollup merge of #127889 - estebank:anon-arg-sugg, r=compiler-errors
More accurate span for anonymous argument suggestion

Use smaller span for suggesting adding `_:` ahead of a type:

```
error: expected one of `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
  --> $DIR/anon-params-denied-2018.rs:12:47
   |
LL |     fn foo_with_qualified_path(<Bar as T>::Baz);
   |                                               ^ expected one of 8 possible tokens
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: explicitly ignore the parameter name
   |
LL |     fn foo_with_qualified_path(_: <Bar as T>::Baz);
   |                                ++
```
2024-07-18 08:09:02 +02:00
Trevor Gross
fa1303662a
Rollup merge of #127806 - nnethercote:parser-improvements, r=spastorino
Some parser improvements

I was looking closely at attribute handling in the parser while debugging some issues relating to #124141, and found a few small improvements.

``@spastorino``
2024-07-17 19:53:27 -05:00
Esteban Küber
f6c4679547 More accurate span for anonymous argument suggestion
Use smaller span for suggesting adding `_:` ahead of a type:

```
error: expected one of `(`, `...`, `..=`, `..`, `::`, `:`, `{`, or `|`, found `)`
  --> $DIR/anon-params-denied-2018.rs:12:47
   |
LL |     fn foo_with_qualified_path(<Bar as T>::Baz);
   |                                               ^ expected one of 8 possible tokens
   |
   = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)
help: explicitly ignore the parameter name
   |
LL |     fn foo_with_qualified_path(_: <Bar as T>::Baz);
   |                                ++
```
2024-07-18 00:19:27 +00:00
Michael Goulet
d0a1851ec2 Deny keyword lifetimes pre-expansion 2024-07-16 12:06:25 -04:00
Nicholas Nethercote
9c4f3dbd06 Remove references to maybe_whole_expr.
It was removed in #126571.
2024-07-16 16:40:35 +10:00
Nicholas Nethercote
8cb6bc3b5a Fix a comment. 2024-07-16 16:39:19 +10:00
Nicholas Nethercote
96b39f1204 Inline and remove Parser::parse_expr_dot_or_call_with_.
It only has two call sites, and it extremely similar to
`Parser::parse_expr_dot_or_call_with`, in both name and behaviour. The
only difference is the latter has an `attrs` argument and an
`ensure_sufficient_stack` call. We can pass in an empty `attrs` as
necessary, as is already done at some `parse_expr_dot_or_call_with` call
sites.
2024-07-16 16:16:38 +10:00
Nicholas Nethercote
96cc9c99b2 Inline and remove Parser::parse_and_disallow_postfix_after_cast.
It has a single call site. Removing it removes the need for an
`ExprKind` check. The commit also clarifies the relevant comment.
2024-07-16 16:09:36 +10:00
Nicholas Nethercote
d247489ac2 Reorder Parser::parse_expr_dot_or_call_with arguments.
Put `attrs` before `e0` because that matches the order in the source
code, where outer attributes appear before expressions.
2024-07-16 15:54:34 +10:00
Nicholas Nethercote
48cdfc388d Inline Parser::parse_item_common_.
It has a single call site, it isn't that big, and its name is
confusingly similar to `Parser::parse_item_common`.
2024-07-16 15:07:42 +10:00
Nicholas Nethercote
2f305ff460 Remove an unnecessary ?. 2024-07-16 15:03:25 +10:00
Matthias Krüger
2b82729b91
Rollup merge of #127407 - estebank:parser-suggestions, r=oli-obk
Make parse error suggestions verbose and fix spans

Go over all structured parser suggestions and make them verbose style.

When suggesting to add or remove delimiters, turn them into multiple suggestion parts.
2024-07-15 21:11:48 +02:00
Matthias Krüger
febe4423c1
Rollup merge of #127273 - nnethercote:fix-DebugParser, r=workingjubilee
Fix `DebugParser`.

I tried using this and it didn't work at all. `prev_token` is never eof, so the accumulator is always false, which means the `then_some` always returns `None`, which means `scan` always returns `None`, and `tokens` always ends up an empty vec. I'm not sure how this code was supposed to work.

(An aside: I find `Iterator::scan` to be a pretty wretched function, that produces code which is very hard to understand. Probably why this is just one of two uses of it in the entire compiler.)

This commit changes it to a simpler imperative style that produces a valid `tokens` vec.

r? `@workingjubilee`
2024-07-14 20:24:58 +02:00