Commit graph

2238 commits

Author SHA1 Message Date
Michael Goulet
28164e3c04 Stop using span hack for contracts feature gating 2025-02-10 19:51:26 +00:00
bjorn3
1fcae03369 Rustfmt 2025-02-08 22:12:13 +00:00
Matthias Krüger
3ce7d9c638
Rollup merge of #136191 - klensy:const_a, r=compiler-errors
compiler: replace few consts arrays with statics to remove const dupes

Locally on `x86_64-pc-windows-msvc` -100kb for `rustc_driver.dll`
2025-02-07 12:01:57 +01:00
bjorn3
b9b2c3affc Stop passing the same resource multiple times when building ParseSess 2025-02-06 17:30:10 +00:00
bors
2f92f050e8 Auto merge of #136471 - safinaskar:parallel, r=SparrowLii
tree-wide: parallel: Fully removed all `Lrc`, replaced with `Arc`

tree-wide: parallel: Fully removed all `Lrc`, replaced with `Arc`

This is continuation of https://github.com/rust-lang/rust/pull/132282 .

I'm pretty sure I did everything right. In particular, I searched all occurrences of `Lrc` in submodules and made sure that they don't need replacement.

There are other possibilities, through.

We can define `enum Lrc<T> { Rc(Rc<T>), Arc(Arc<T>) }`. Or we can make `Lrc` a union and on every clone we can read from special thread-local variable. Or we can add a generic parameter to `Lrc` and, yes, this parameter will be everywhere across all codebase.

So, if you think we should take some alternative approach, then don't merge this PR. But if it is decided to stick with `Arc`, then, please, merge.

cc "Parallel Rustc Front-end" ( https://github.com/rust-lang/rust/issues/113349 )

r? SparrowLii

`@rustbot` label WG-compiler-parallel
2025-02-06 10:50:05 +00:00
Celina G. Val
ddbf54b67d Rename rustc_contract to contract
This has now been approved as a language feature and no longer needs
a `rustc_` prefix.

Also change the `contracts` feature to be marked as incomplete and
`contracts_internals` as internal.
2025-02-03 13:55:15 -08:00
Felix S. Klock II
6a6c6b891b Separate contract feature gates for the internal machinery
The extended syntax for function signature that includes contract clauses
should never be user exposed versus the interface we want to ship
externally eventually.
2025-02-03 13:55:15 -08:00
Celina G. Val
38eff16d0a Express contracts as part of function header and lower it to the contract lang items
includes post-developed commit: do not suggest internal-only keywords as corrections to parse failures.

includes post-developed commit: removed tabs that creeped in into rustfmt tool source code.

includes post-developed commit, placating rustfmt self dogfooding.

includes post-developed commit: add backquotes to prevent markdown checking from trying to treat an attr as a markdown hyperlink/

includes post-developed commit: fix lowering to keep contracts from being erroneously inherited by nested bodies (like closures).

Rebase Conflicts:
 - compiler/rustc_parse/src/parser/diagnostics.rs
 - compiler/rustc_parse/src/parser/item.rs
 - compiler/rustc_span/src/hygiene.rs

Remove contracts keywords from diagnostic messages
2025-02-03 12:54:00 -08:00
Askar Safin
0a21f1d0a2 tree-wide: parallel: Fully removed all Lrc, replaced with Arc 2025-02-03 13:25:57 +03:00
Matthias Krüger
78ded09912
Rollup merge of #135882 - hkBst:master, r=estebank
simplify `similar_tokens` from `Option<Vec<_>>` to `&[_]`

All uses immediately invoke contains, so maybe a further simplification is possible.
2025-01-30 12:45:27 +01:00
klensy
dc62b8fd11 replaces few consts with statics to reduce readonly section 2025-01-28 17:38:22 +03:00
Matthias Krüger
c0005f1560
Rollup merge of #133151 - tyrone-wu:trim-fn-ptr-whitespace, r=compiler-errors
Trim extra whitespace in fn ptr suggestion span

Trim extra whitespace when suggesting removal of invalid qualifiers when parsing function pointer type.

Fixes: #133083

---

I made a comment about the format of the diagnostic error message in https://github.com/rust-lang/rust/issues/133083#issuecomment-2480047875. I think the `.label` may be a little redundant if the diagnostic only highlights the bad qualifier instead of the entire `TyKind::BareFn` span. If it makes sense, I can include it in this PR.
2025-01-28 14:23:20 +01:00
Tyrone Wu
5082fd8b1e
Trim extra whitespace in fn ptr suggestion span
Trim extra whitespace when suggesting removal of invalid qualifiers when
parsing function pointer type.

Fixes: #133083

Signed-off-by: Tyrone Wu <wudevelops@gmail.com>
2025-01-27 17:17:22 +00:00
Michael Goulet
ac1c6c50f4 Use identifiers in diagnostics more often 2025-01-27 01:23:34 +00:00
Yotam Ofek
8b57fd9e43 use fmt::from_fn in more places, instead of using structs that impl formatting traits 2025-01-24 14:45:56 +00:00
Matthias Krüger
ec50812794
Rollup merge of #135855 - cuviper:parser-size, r=wesleywiser
Only assert the `Parser` size on specific arches

The size of this struct depends on the alignment of `u128`, for example
powerpc64le and s390x have align-8 and end up with only 280 bytes. Our
64-bit tier-1 arches are the same though, so let's just assert on those.

r? nnethercote
2025-01-24 00:15:56 +01:00
Marijn Schouten
5f01e12ea8 simplify similar_tokens from Vec<_> to &[_] 2025-01-23 11:45:42 +01:00
Marijn Schouten
ccb967438d simplify similar_tokens from Option<Vec<_>> to Vec<_> 2025-01-23 11:45:42 +01:00
Matthias Krüger
b4266b0bcd
Rollup merge of #135557 - estebank:wtf8, r=fee1-dead
Point at invalid utf-8 span on user's source code

```
error: couldn't read `$DIR/not-utf8-bin-file.rs`: stream did not contain valid UTF-8
  --> $DIR/not-utf8-2.rs:6:5
   |
LL |     include!("not-utf8-bin-file.rs");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: byte `193` is not valid utf-8
  --> $DIR/not-utf8-bin-file.rs:2:14
   |
LL |     let _ = "�|�␂!5�cc␕␂��";
   |              ^
   = note: this error originates in the macro `include` (in Nightly builds, run with -Z macro-backtrace for more info)
```

When we attempt to load a Rust source code file, if there is a OS file failure we try reading the file as bytes. If that succeeds we try to turn it into UTF-8. If *that* fails, we provide additional context about *where* the file has the first invalid UTF-8 character.

Fix #76869.
2025-01-22 20:37:24 +01:00
bors
dee7d0e730 Auto merge of #134478 - compiler-errors:attr-span, r=oli-obk
Properly record metavar spans for other expansions other than TT

This properly records metavar spans for nonterminals other than tokentree. This means that we operations like `span.to(other_span)` work correctly for macros. As you can see, other diagnostics involving metavars have improved as a result.

Fixes #132908
Alternative to #133270

cc `@ehuss`
cc `@petrochenkov`
2025-01-22 14:46:41 +00:00
Josh Stone
aef640a613 Only assert the Parser size on specific arches
The size of this struct depends on the alignment of `u128`, for example
powerpc64le and s390x have align-8 and end up with only 280 bytes. Our
64-bit tier-1 arches are the same though, so let's just assert on those.
2025-01-21 17:24:29 -08:00
Esteban Küber
57dd42d613 Point at invalid utf-8 span on user's source code
```
error: couldn't read `$DIR/not-utf8-bin-file.rs`: stream did not contain valid UTF-8
  --> $DIR/not-utf8-2.rs:6:5
   |
LL |     include!("not-utf8-bin-file.rs");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: `[193]` is not valid utf-8
  --> $DIR/not-utf8-bin-file.rs:2:14
   |
LL |     let _ = "�|�␂!5�cc␕␂��";
   |              ^
   = note: this error originates in the macro `include` (in Nightly builds, run with -Z macro-backtrace for more info)
```

When we attempt to load a Rust source code file, if there is a OS file failure we try reading the file as bytes. If that succeeds we try to turn it into UTF-8. If *that* fails, we provide additional context about *where* the file has the first invalid UTF-8 character.

Fix #76869.
2025-01-22 00:52:27 +00:00
Yotam Ofek
264fa0fc54 Run clippy --fix for unnecessary_map_or lint 2025-01-19 19:15:00 +00:00
Mark Rousskov
6f72f13436 Remove allocations from case-insensitive comparison to keywords 2025-01-11 12:39:44 -05:00
bors
251206c27b Auto merge of #135268 - pietroalbini:pa-bump-stage0, r=Mark-Simulacrum
Master bootstrap update

Part of the release process.

r? `@Mark-Simulacrum`
2025-01-09 13:33:16 +00:00
Matthias Krüger
a1cadeab68
Rollup merge of #135269 - estebank:unneeded-into, r=compiler-errors
Remove some unnecessary `.into()` calls
2025-01-09 09:05:10 +01:00
Esteban Küber
eb917ea24d Remove some unnecessary .into() calls 2025-01-08 21:19:28 +00:00
Pietro Albini
2af3ba9a8a
update cfg(bootstrap) 2025-01-08 21:26:39 +01:00
Oli Scherer
4a8773a3af Rename PatKind::Lit to Expr 2025-01-08 07:34:59 +00:00
Matthias Krüger
344a61e69b
Rollup merge of #134884 - calciumbe:patch1, r=jieyouxu
Fix typos

Hello, I fix some typos in docs and comments. Thank you very much.
2024-12-29 21:18:07 +01:00
calciumbe
4f8bebd6b5
fix: typos
Signed-off-by: calciumbe <192480234+calciumbe@users.noreply.github.com>
2024-12-29 21:35:02 +08:00
David Tolnay
26bb4e6464
Skip parenthesis around tuple struct field calls 2024-12-27 14:33:56 -08:00
Michael Goulet
28a997fa44 Properly record metavar spans for other expansions other than TT 2024-12-21 20:37:27 +00:00
Jacob Pratt
ea8bc3b4be
Rollup merge of #134600 - dtolnay:chainedcomparison, r=oli-obk
Fix parenthesization of chained comparisons by pretty-printer

Example:

```rust
macro_rules! repro {
    () => {
        1 < 2
    };
}

fn main() {
    let _ = repro!() == false;
}
```

Previously `-Zunpretty=expanded` would pretty-print this syntactically invalid output: `fn main() { let _ = 1 < 2 == false; }`

```console
error: comparison operators cannot be chained
 --> <anon>:8:23
  |
8 | fn main() { let _ = 1 < 2 == false; }
  |                       ^   ^^
  |
help: parenthesize the comparison
  |
8 | fn main() { let _ = (1 < 2) == false; }
  |                     +     +
```

With the fix, it will print `fn main() { let _ = (1 < 2) == false; }`.

Making `-Zunpretty=expanded` consistently produce syntactically valid Rust output is important because that is what makes it possible for `cargo expand` to format and perform filtering on the expanded code.

## Review notes

According to `rg '\.fixity\(\)' compiler/` the `fixity` function is called only 3 places:

- 13170cd787/compiler/rustc_ast_pretty/src/pprust/state/expr.rs (L283-L287)

- 13170cd787/compiler/rustc_hir_pretty/src/lib.rs (L1295-L1299)

- 13170cd787/compiler/rustc_parse/src/parser/expr.rs (L282-L289)

The 2 pretty printers definitely want to treat comparisons using `Fixity::None`. That's the whole bug being fixed. Meanwhile, the parser's `Fixity::None` codepath is previously unreachable as indicated by the comment, so as long as `Fixity::None` here behaves exactly the way that `Fixity::Left` used to behave, you can tell that this PR definitely does not constitute any behavior change for the parser.

My guess for why comparison operators were set to `Fixity::Left` instead of `Fixity::None` is that it's a very old workaround for giving a good chained comparisons diagnostic (like what I pasted above). Nowadays that is handled by a different dedicated codepath.
2024-12-21 01:18:43 -05:00
Jacob Pratt
36485acdac
Rollup merge of #133087 - estebank:stmt-misparse, r=chenyukang
Detect missing `.` in method chain in `let` bindings and statements

On parse errors where an ident is found where one wasn't expected, see if the next elements might have been meant as method call or field access.

```
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `map`
  --> $DIR/missing-dot-on-statement-expression.rs:7:29
   |
LL |     let _ = [1, 2, 3].iter()map(|x| x);
   |                             ^^^ expected one of `.`, `;`, `?`, `else`, or an operator
   |
help: you might have meant to write a method call
   |
LL |     let _ = [1, 2, 3].iter().map(|x| x);
   |                             +
```
2024-12-21 01:18:40 -05:00
David Tolnay
fe65e886f3
Change comparison operators to have Fixity::None 2024-12-20 20:12:22 -08:00
Esteban Küber
1549af29c3 Do not suggest foo.Bar 2024-12-21 03:02:07 +00:00
Esteban Küber
cbbc7becc8 Account for missing . in macros to avoid incorrect suggestion 2024-12-21 02:46:33 +00:00
Esteban Küber
1ce0fa98c7 Detect missing . in method chain in let bindings and statements
On parse errors where an ident is found where one wasn't expected, see if the next elements might have been meant as method call or field access.

```
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `map`
  --> $DIR/missing-dot-on-statement-expression.rs:7:29
   |
LL |     let _ = [1, 2, 3].iter()map(|x| x);
   |                             ^^^ expected one of `.`, `;`, `?`, `else`, or an operator
   |
help: you might have meant to write a method call
   |
LL |     let _ = [1, 2, 3].iter().map(|x| x);
   |                             +
```
2024-12-21 02:46:33 +00:00
bjorn3
701e2f708b Reduce the amount of explicit FatalError.raise()
Instead use dcx.abort_if_error() or guar.raise_fatal() instead. These
guarantee that an error actually happened previously and thus we don't
silently abort.
2024-12-20 14:09:25 +00:00
Nicholas Nethercote
0f7dccf784 Fix Parser size assertion on s390x.
For some reason the memory layout is different on s390x.
2024-12-19 20:06:44 +11:00
Nicholas Nethercote
df56c50cee Make TokenType::from_u32 foolproof.
Currently it relies on having the right integer for every variant, and
if you add a variant you need to adjust the integers for all subsequent
variants, which is a pain.

This commit introduces a match guard formulation that takes advantage of
the enum-to-integer conversion to avoid specifying the integer for each
variant. And it does this via a macro to avoid lots of boilerplate.
2024-12-19 16:05:41 +11:00
Nicholas Nethercote
b9bf0b4b10 Speed up Parser::expected_token_types.
The parser pushes a `TokenType` to `Parser::expected_token_types` on
every call to the various `check`/`eat` methods, and clears it on every
call to `bump`. Some of those `TokenType` values are full tokens that
require cloning and dropping. This is a *lot* of work for something
that is only used in error messages and it accounts for a significant
fraction of parsing execution time.

This commit overhauls `TokenType` so that `Parser::expected_token_types`
can be implemented as a bitset. This requires changing `TokenType` to a
C-style parameterless enum, and adding `TokenTypeSet` which uses a
`u128` for the bits. (The new `TokenType` has 105 variants.)

The new types `ExpTokenPair` and `ExpKeywordPair` are now arguments to
the `check`/`eat` methods. This is for maximum speed. The elements in
the pairs are always statically known; e.g. a
`token::BinOp(token::Star)` is always paired with a `TokenType::Star`.
So we now compute `TokenType`s in advance and pass them in to
`check`/`eat` rather than the current approach of constructing them on
insertion into `expected_token_types`.

Values of these pair types can be produced by the new `exp!` macro,
which is used at every `check`/`eat` call site. The macro is for
convenience, allowing any pair to be generated from a single identifier.

The ident/keyword filtering in `expected_one_of_not_found` is no longer
necessary. It was there to account for some sloppiness in
`TokenKind`/`TokenType` comparisons.

The existing `TokenType` is moved to a new file `token_type.rs`, and all
its new infrastructure is added to that file. There is more boilerplate
code than I would like, but I can't see how to make it shorter.
2024-12-19 16:05:41 +11:00
Nicholas Nethercote
d5370d981f Remove bra/ket naming.
This is a naming convention used in a handful of spots in the parser for
delimiters. It confused me when I first saw it a long time ago, and I've
never liked it. A web search says "Bra-ket notation" exists in linear
algebra but the terminology has zero prior use in a programming context,
as far as I can tell.

This commit changes it to `open`/`close`, which is consistent with the
rest of the compiler.
2024-12-19 16:05:41 +11:00
Nicholas Nethercote
fb5ba8a6d4 Tweak some parser check/eat methods.
The most significant is `check_keyword`: it now only pushes to
`expected_token_types` if the keyword check fails, which matches how all
the other `check` methods work.

The remainder are just tweaks to make these methods more consistent with
each other.
2024-12-19 16:05:41 +11:00
Nicholas Nethercote
48f7714819 Rename Parser::expected_tokens as Parser::expected_token_types.
Because the `Token` type is similar to but different to the `TokenType`
type, and the difference is important, so we want to avoid confusion.
2024-12-19 16:05:41 +11:00
bors
4ba4ac612d Auto merge of #134443 - joshtriplett:use-field-init-shorthand, r=lqd,tgross35,nnethercote
Use field init shorthand where possible

Field init shorthand allows writing initializers like `tcx: tcx` as
`tcx`. The compiler already uses it extensively. Fix the last few places
where it isn't yet used.

EDIT: this PR also updates `rustfmt.toml` to set
`use_field_init_shorthand = true`.
2024-12-18 19:16:15 +00:00
许杰友 Jieyou Xu (Joe)
0a2d708c31
Rollup merge of #134253 - nnethercote:overhaul-keywords, r=petrochenkov
Overhaul keyword handling

The compiler's list of keywords has some problems.
- It contains several items that aren't keywords.
- The order isn't quite right in a couple of places.
- Some of the names of predicates relating to keywords are confusing.
- rustdoc and rustfmt have their own (incorrect) versions of the keyword list.
- `AllKeywords` is unnecessarily complex.

r? ```@jieyouxu```
2024-12-18 22:56:53 +08:00
许杰友 Jieyou Xu (Joe)
477f222b02
Rollup merge of #134161 - nnethercote:overhaul-token-cursors, r=spastorino
Overhaul token cursors

Some nice cleanups here.

r? `````@davidtwco`````
2024-12-18 22:56:53 +08:00
Nicholas Nethercote
1564318482 Only have one source of truth for keywords.
`rustc_symbol` is the source of truth for keywords.

rustdoc has its own implicit definition of keywords, via the
`is_doc_keyword`. It (presumably) intends to include all keywords, but
it omits `yeet`.

rustfmt has its own explicit list of Rust keywords. It also (presumably)
intends to include all keywords, but it omits `await`, `builtin`, `gen`,
`macro_rules`, `raw`, `reuse`, `safe`, and `yeet`. Also, it does linear
searches through this list, which is inefficient.

This commit fixes all of the above problems by introducing a new
predicate `is_any_keyword` in rustc and using it in rustdoc and rustfmt.
It documents that it's not the right predicate in most cases.
2024-12-18 20:21:03 +11:00