Commit Graph

32266 Commits

Author SHA1 Message Date
Guillaume Gomez f99b510429
Rollup merge of #120488 - nnethercote:diag-lifetimes, r=oli-obk
Diagnostic lifetimes cleanups

Some diagnostic simplifications.

r? `@oli-obk`
2024-01-30 11:19:20 +01:00
Guillaume Gomez c00192ae2a
Rollup merge of #120460 - nnethercote:fix-120397, r=compiler-errors
Be more careful about interpreting a label/lifetime as a mistyped char literal.

Currently the parser interprets any label/lifetime in certain positions as a mistyped char literal, on the assumption that the trailing single quote was accidentally omitted. In such cases it gives an error with a suggestion to add the trailing single quote, and then puts the appropriate char literal into the AST. This behaviour was introduced in #101293.

This is reasonable for a case like this:
```
let c = 'a;
```
because `'a'` is a valid char literal. It's less reasonable for a case like this:
```
let c = 'abc;
```
because `'abc'` is not a valid char literal.

Prior to #120329 this could result in some sub-optimal suggestions in error messages, but nothing else. But #120329 changed `LitKind::from_token_lit` to assume that the char/byte/string literals it receives are valid, and to assert if not. This is reasonable because the lexer does not produce invalid char/byte/string literals in general. But in this "interpret label/lifetime as unclosed char literal" case the parser can produce an invalid char literal with contents such as `abc`, which triggers an assertion failure.

This PR changes the parser so it's more cautious about interpreting labels/lifetimes as unclosed char literals.

Fixes #120397.

r? `@compiler-errors`
2024-01-30 11:19:19 +01:00
Guillaume Gomez f35504dbf2
Rollup merge of #120443 - GuillaumeGomez:footnote-def-improvement, r=fmease
Fixes footnote handling in rustdoc

Fixes #100638.

You can now declare footnotes like this:

```rust
//! Reference to footnotes A[^1], B[^2] and C[^3].
//!
//! [^1]: Footnote A.
//! [^2]: Footnote B.
//! [^3]: Footnote C.
```

r? `@notriddle`
2024-01-30 11:19:18 +01:00
Guillaume Gomez 8c2571ceca
Rollup merge of #120425 - DaniPopes:query-default-return, r=Nilstrieb
Remove unnecessary unit returns in query declarations

For consistency with normal functions.
2024-01-30 11:19:17 +01:00
Guillaume Gomez a44b134770
Rollup merge of #120402 - compiler-errors:async-closure-def-tree, r=cjgillot
Make the coroutine def id of an async closure the child of the closure def id

Adjust def collection to make the (inner) coroutine returned by an async closure be a def id child of the (outer) closure. This makes it easy to map from coroutine -> closure by using `tcx.parent`, since currently it's not trivial to do this.
2024-01-30 11:19:15 +01:00
Guillaume Gomez f7043a41ce
Rollup merge of #120400 - estebank:bound-error-cleanup, r=oli-obk
Bound errors span label cleanup

Consolidate span labels for "this type doesn't satisfy a bound" for more compact diagnostic output.
2024-01-30 11:19:15 +01:00
Guillaume Gomez e0e96a1829
Rollup merge of #120387 - RalfJung:large-array-followup, r=oli-obk
interpret/memory: fix safety comment for large array memset optimization

Also fix the doc comment for `check_and_deref_ptr`.
2024-01-30 11:19:14 +01:00
Nicholas Nethercote 514a5d8d55 Remove the second lifetime from `DiagnosticArg`.
Because it's always static.

I'm surprised the compiler allowed this unused lifetime without any
complaint.
2024-01-30 18:46:08 +11:00
Nicholas Nethercote f0426b77fc Remove the lifetime from `DiagnosticArgName`.
Because it's always 'static.
2024-01-30 18:46:08 +11:00
Nicholas Nethercote 06aa381adb Remove `DiagnosticArgName` from `rustc_codegen_ssa`.
It's identical to the one in `rustc_errors`; use that instead.

Also remove some `rustc_errors::` qualifiers.
2024-01-30 18:46:08 +11:00
Nicholas Nethercote 45dc19785d Remove lifetimes from some diagnostics.
Because the `&'a str` fields can be trivially converted to `String`
without causing any extra allocations.
2024-01-30 18:46:08 +11:00
Nicholas Nethercote 5350edb9e8 Remove the lifetime from `DiagnosticArgValue`.
Because it's almost always static.

This makes `impl IntoDiagnosticArg for DiagnosticArgValue` trivial,
which is nice.

There are a few diagnostics constructed in
`compiler/rustc_mir_build/src/check_unsafety.rs` and
`compiler/rustc_mir_transform/src/errors.rs` that now need symbols
converted to `String` with `to_string` instead of `&str` with `as_str`,
but that' no big deal, and worth it for the simplifications elsewhere.
2024-01-30 18:46:06 +11:00
bors c401f09979 Auto merge of #119744 - lcnr:assemble-only-rigid, r=compiler-errors
only assemble alias bound candidates for rigid aliases

fixes https://github.com/rust-lang/trait-system-refactor-initiative/issues/77

This also causes `<Wrapper<?0> as Trait>::Unwrap: Trait` to always be ambig, as we now normalize the self type before checking whether it is an inference variable.

I cannot think of an approach to the underlying issues here which does not require the "may-define means must-define" restriction for opaque types. Going to go ahead with this and added this restriction to the tracking issue for the new solver to make sure we don't stabilize it without getting types + lang signoff here.

r? `@compiler-errors`
2024-01-30 07:11:24 +00:00
Esteban Küber fc964fb439 review comments 2024-01-29 23:33:02 +00:00
bors 5518eaa946 Auto merge of #120466 - Dylan-DPC:rollup-v0or19a, r=Dylan-DPC
Rollup of 9 pull requests

Successful merges:

 - #116677 (References refer to allocated objects)
 - #118625 (Improve handling of expressions in patterns)
 - #120266 (Improve documentation for [A]Rc::into_inner)
 - #120373 (Adjust Behaviour of `read_dir` and `ReadDir` in Windows Implementation: Check Whether Path to Search In Exists)
 - #120390 (Borrow check inline const patterns)
 - #120420 (Stop using derivative in rustc_pattern_analysis)
 - #120428 (hir: Two preparatory changes for #120206)
 - #120453 (Fix incorrect comment in normalize_newlines)
 - #120462 (Clean dead code)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-01-29 21:53:39 +00:00
Esteban Küber 09f16b596d Avoid ICE in trait without `dyn` lint
Do not attempt to provide an accurate suggestion for `impl Trait`
in bare trait types when linting. Instead, only do the object
safety check when an E0782 is already going to be emitted in the
2021 edition.

Fix #120241.
2024-01-29 18:33:52 +00:00
lcnr ea4e5b8458 merge builtin unsize candidates again 2024-01-29 14:32:06 +01:00
lcnr 0d71860368 bye bye `assemble_candidates_via_self_ty` 2024-01-29 14:32:06 +01:00
Guillaume Gomez 1bdeeef0d5 Update pulldown-cmark version to 0.9.5 2024-01-29 14:14:03 +01:00
Dylan DPC 15e8b903b2
Rollup merge of #120453 - mattheww:2024-01_normalize_newlines, r=oli-obk
Fix incorrect comment in normalize_newlines

The incorrect comment seems to be left over from sometime before this function was first merged.
2024-01-29 12:56:54 +00:00
Dylan DPC eaa100204e
Rollup merge of #120428 - petrochenkov:somehir2, r=compiler-errors
hir: Two preparatory changes for #120206

cc https://github.com/rust-lang/rust/pull/120206
r? ```@compiler-errors```
2024-01-29 12:56:54 +00:00
Dylan DPC 5de94a3c80
Rollup merge of #120420 - lnicola:rm-pattern-analysis-derivative, r=Nilstrieb
Stop using derivative in rustc_pattern_analysis

CC #109302, https://github.com/rust-lang/rust-analyzer/pull/16420#discussion_r1464357157

r? ````@Nadrieril````
2024-01-29 12:56:53 +00:00
Dylan DPC 549eeb077d
Rollup merge of #120390 - matthewjasper:inline-constant-pat-mir, r=davidtwco
Borrow check inline const patterns

Add type annotations to MIR so that borrowck can pass constraints from inline constants in patterns to the containing function.
Also enables some inline constant pattern tests that were fixed by the THIR unsafeck stabilization.

cc #76001
2024-01-29 12:56:53 +00:00
Dylan DPC 0138151c21
Rollup merge of #118625 - ShE3py:expr-in-pats, r=WaffleLapkin
Improve handling of expressions in patterns

Closes #112593.

Methodcalls' dots in patterns are silently recovered as commas (e.g. `Foo("".len())` -> `Foo("", len())`) so extra diagnostics are emitted:
```rs
struct Foo(u8, String, u8);

fn bar(foo: Foo) -> bool {
    match foo {
        Foo(4, "yippee".yeet(), 7) => true,
        _ => false
    }
}
```
```
error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found `.`
 --> main.rs:5:24
  |
5 |         Foo(4, "yippee".yeet(), 7) => true,
  |                        ^
  |                        |
  |                        expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
  |                        help: missing `,`

error[E0531]: cannot find tuple struct or tuple variant `yeet` in this scope
 --> main.rs:5:25
  |
5 |         Foo(4, "yippee".yeet(), 7) => true,
  |                         ^^^^ not found in this scope

error[E0023]: this pattern has 4 fields, but the corresponding tuple struct has 3 fields
 --> main.rs:5:13
  |
1 | struct Foo(u8, String, u8);
  |            --  ------  -- tuple struct has 3 fields
...
5 |         Foo(4, "yippee".yeet(), 7) => true,
  |             ^  ^^^^^^^^ ^^^^^^  ^ expected 3 fields, found 4

error: aborting due to 3 previous errors
```

This PR checks for patterns that ends with a dot and a lowercase ident (as structs/variants should be uppercase):
```
error: expected a pattern, found a method call
 --> main.rs:5:16
  |
5 |         Foo(4, "yippee".yeet(), 7) => true,
  |                ^^^^^^^^^^^^^^^ method calls are not allowed in patterns

error: aborting due to 1 previous error
```

Also check for expressions:
```rs
fn is_idempotent(x: f32) -> bool {
    match x {
        x * x => true,
        _ => false,
    }
}

fn main() {
    let mut t: [i32; 5];
    let t[0] = 1;
}
```
```
error: expected a pattern, found an expression
 --> main.rs:3:9
  |
3 |         x * x => true,
  |         ^^^^^ arbitrary expressions are not allowed in patterns

error: expected a pattern, found an expression
  --> main.rs:10:9
   |
10 |     let t[0] = 1;
   |         ^^^^ arbitrary expressions are not allowed in patterns
```

Would be cool if the compiler could suggest adding a guard for `match`es, but I've no idea how to do it.

---
`@rustbot` label +A-diagnostics +A-parser +A-patterns +C-enhancement
2024-01-29 12:56:51 +00:00
Matthew Jasper 83fa46fe5b Borrow check inline const patterns
Add type annotations to MIR so that borrowck can pass constraints from
inline constants in patterns to the containing function.
2024-01-29 09:17:00 +00:00
bors fb4bca04fa Auto merge of #120165 - reitermarkus:nonzero-switch-alias-direction, r=dtolnay
Switch `NonZero` alias direction.

Step 4 mentioned in https://github.com/rust-lang/rust/pull/100428#pullrequestreview-1767139731.

Depends on https://github.com/rust-lang/rust/pull/120160.

r? `@dtolnay`
2024-01-29 08:52:13 +00:00
Nicholas Nethercote 306612ea60 Be more careful about interpreting a label/lifetime as a mistyped char literal.
Currently the parser will interpret any label/lifetime in certain
positions as a mistyped char literal, on the assumption that the
trailing single quote was accidentally omitted. This is reasonable for a
something like 'a (because 'a' would be valid) but not reasonable for a
something like 'abc (because 'abc' is not valid).

This commit restricts this behaviour only to labels/lifetimes that would
be valid char literals, via the new `could_be_unclosed_char_literal`
function. The commit also augments the `label-is-actually-char.rs` test
in a couple of ways:
- Adds testing of labels/lifetimes with identifiers longer than one
  char, e.g. 'abc.
- Adds a new match with simpler patterns, because the
  `recover_unclosed_char` call in `parse_pat_with_range_pat` was not
  being exercised (in this test or any other ui tests).

Fixes #120397, an assertion failure, which was caused by this behaviour
in the parser interacting with some new stricter char literal checking
added in #120329.
2024-01-29 11:25:09 +11:00
Nicholas Nethercote 5bda589ff3 Tweak comment and naming for `recover_unclosed_char`.
Because it can be used for a lifetime or a label.
2024-01-29 09:33:49 +11:00
Nicholas Nethercote 5d9dfbd08f Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!

This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.

With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123)  // macro call

struct_span_code_err!(dcx, span, E0123, "msg");  // bare ident arg to macro call

\#[diag(name, code = "E0123")]  // string
struct Diag;
```

With the new code, they all use the `E0123` constant.
```
E0123  // constant

struct_span_code_err!(dcx, span, E0123, "msg");  // constant

\#[diag(name, code = E0123)]  // constant
struct Diag;
```

The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
  used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
  moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
  code constants and the `DIAGNOSTIC_TABLES`. This is in its new
  `codes.rs` file.
2024-01-29 07:41:41 +11:00
Nicholas Nethercote 0321de2778 Remove bogus `{code}` attributes on `TraitImplMismatch`.
This makes no sense, and has no effect. I suspect it's been confused
with a `code = "{code}"` attribute on a subdiagnostic suggestion, where
it is valid (but the "code" there is suggested source code, rather than
an error code.)
2024-01-29 07:40:10 +11:00
Nicholas Nethercote d91d164b00 Sort attributes in `compiler/rustc_errors/src/lib.rs`.
As is already done in `rustc_span` and `rustc_data_structures`.
2024-01-29 07:40:08 +11:00
Matthew Woodcraft 67558055e3 normalize_newlines(): fix incorrect comment 2024-01-28 19:30:41 +00:00
Lieselotte 6f014a81b2
Handle methodcalls & operators in patterns 2024-01-28 16:12:21 +01:00
Vadim Petrochenkov b2b5b91bfb hir: Use `InferArg` in `ArrayLen::Infer` 2024-01-28 02:04:39 +03:00
Vadim Petrochenkov 5f8030dcc9 hir: Remove unnecessary `HirId` from `hir::Let`
It has 1-to-1 correspondence to its expression id.

Also remove mostly useless `visit_let_expr`.
2024-01-28 02:04:39 +03:00
bors 6351247048 Auto merge of #120024 - Mark-Simulacrum:fast-union-merge, r=cjgillot
Merge into larger interval set

This reduces the work done while merging rows. In at least one case (#50450), we have thousands of union([range], [20,000 ranges]), which previously inserted each of the 20,000 ranges one by one. Now we only insert one range into the right hand set after copying the set over.

This cuts the runtime of the test case in #50450 from ~26 seconds to ~6 seconds locally, though it doesn't change the memory usage peak (~9.5GB).
2024-01-27 22:26:37 +00:00
Michael Goulet 5d8c1780fa Make the coroutine def id of an async closure the child of the closure def id 2024-01-27 19:39:02 +00:00
Markus Reiter 117eb9f376
Fix `NonZero` suggestions. 2024-01-27 16:38:57 +01:00
Markus Reiter 554b0f70c3
Add `NonZero` symbol. 2024-01-27 16:38:57 +01:00
DaniPopes cda898b0d9
Remove unnecessary unit returns in query declarations
For consistency with normal functions.
2024-01-27 14:55:17 +01:00
Laurențiu Nicola f5c78955c8 Stop using derivative in rustc_pattern_analysis 2024-01-27 14:21:01 +02:00
bors 8af70c7a18 Auto merge of #120062 - davidtwco:llvm-data-layout-check, r=wesleywiser
llvm: change data layout bug to an error and make it trigger more

Fixes #33446.

Don't skip the inconsistent data layout check for custom LLVMs or non-built-in targets.

With #118708, all targets will have a simple test that would trigger this error if LLVM's data layouts do change - so data layouts would be corrected during the LLVM upgrade. Therefore, with builtin targets, this error won't happen with our LLVM because each target will have been confirmed to work. With non-builtin targets, this error is probably useful to have because you can change the data layout in your target and if it is wrong then that could lead to bugs.

When using a custom LLVM, the same justification makes sense for non-builtin targets as with our LLVM, the user can update their target to match their LLVM and that's probably a good thing to do. However, with a custom LLVM, the user cannot change the builtin target data layouts if they don't match - though given that the compiler's data layout is used for layout computation and a bunch of other things - you could get some bugs because of the mismatch and probably want to know about that. I'm not sure if this is something that people do and is okay, but I doubt it?

`CFG_LLVM_ROOT` was also always set during local development with `download-ci-llvm` so this bug would never trigger locally.

In #33446, two points are raised:

- In the issue itself, changing this from a `bug!` to a proper error is what is suggested, by using `isCompatibleDataLayout` from LLVM, but that function still just does the same thing that we do and check for equality, so I've avoided the additional code necessary to do that FFI call.
- `@Mark-Simulacrum` suggests a different check is necessary to maintain backwards compatibility with old LLVM versions. I don't know how often this comes up, but we can do that with some simple string manipulation + LLVM version checks as happens already for LLVM 17 just above this diff.
2024-01-27 12:19:41 +00:00
Matthias Krüger 0972d4441e
Rollup merge of #120398 - Urgau:into_diag_arg-numbers, r=compiler-errors
Improve handling of numbers in `IntoDiagnosticArg`

While working on https://github.com/rust-lang/rust/pull/120393, I realize that my fluent selectors were not working. So here is an improvement (not a fix unfortunately).
2024-01-27 10:48:48 +01:00
Matthias Krüger 58db961d71
Rollup merge of #120386 - klensy:destruction_scopes, r=compiler-errors
ScopeTree: remove destruction_scopes as unused

last usages removed by https://github.com/rust-lang/rust/pull/116170

Unused, but still presented in memory at `t-gmax` (in DHAT termonology)
2024-01-27 10:48:48 +01:00
Matthias Krüger c6f0a5cfe3
Rollup merge of #119957 - Young-Flash:fix, r=fmease
fix: correct suggestion arg for impl trait

follow up https://github.com/rust-lang/rust/pull/118502, close https://github.com/rust-lang/rust/issues/119775
2024-01-27 10:48:47 +01:00
Matthias Krüger 9a4417659e
Rollup merge of #118182 - estebank:issue-118164, r=davidtwco
Properly recover from trailing attr in body

When encountering an attribute in a body, we try to recover from an attribute on an expression (as opposed to a statement). We need to properly clean up when the attribute is at the end of the body where a tail expression would be.

Fix #118164, fix #118575.
2024-01-27 10:48:46 +01:00
bors 04521fd10e Auto merge of #118636 - h1467792822:dev, r=michaelwoerister
Add the unstable option  to reduce the binary size of dynamic library…

# Motivation

The average length of symbol names in the rust standard library is about 100 bytes, while the average length of symbol names in the C++ standard library is about 65 bytes. In some embedded environments where dynamic library are widely used, rust dynamic library symbol name space hash become one of the key bottlenecks of application, Especially when the existing C/C++ module is reconstructed into the rust module.

The unstable option `-Z symbol_mangling_version=hashed` is added to solve the bottleneck caused by too long dynamic library symbol names.

## Test data

The following is a set of test data on the ubuntu 18.04 LTS environment. With this plug-in, the space saving rate of dynamic libraries can reach about 20%.

The test object is the standard library of rust (built based on Xargo), tokio crate, and hyper crate.

The contents of the Cargo.toml file in the construction project of the three dynamic libraries are as follows:

```txt
# Cargo.toml
[profile.release]
panic = "abort"
opt-leve="z"
codegen-units=1
strip=true
debug=true
```
The built dynamic library also removes the `.rustc` segments that are not needed at run time and then compares the size. The detailed data is as follows:

1. libstd.so
> | symbol_mangling_version | size | saving rate |
> | --- | --- | --- |
> | legacy | 804896 ||
> | hashed | 608288 | 0.244 |
> | v0 | 858144 ||
> | hashed | 608288 | 0.291 |

2. libhyper.so
> | symbol_mangling_version(libhyper.so) | symbol_mangling_version(libstd.so) | size | saving rate |
> | --- | --- | --- | --- |
> | legacy | legacy | 866312 ||
> | hashed | legacy | 645128 |0.255|
> | legacy | hashed | 854024 ||
> | hashed | hashed | 632840 |0.259|
2024-01-27 02:32:30 +00:00
Young-Flash 8b3a681a34 minor: pick a suitable var name 2024-01-27 10:24:45 +08:00
bors b362939be1 Auto merge of #120401 - matthiaskrgr:rollup-7740vrx, r=matthiaskrgr
Rollup of 12 pull requests

Successful merges:

 - #103522 (stabilise array methods)
 - #113489 (impl `From<&[T; N]>` for `Cow<[T]>`)
 - #119342 (Emit suggestion when trying to write exclusive ranges as `..<`)
 - #119562 (Rename `pointer` field on `Pin`)
 - #119800 (Document `rustc_index::vec::IndexVec`)
 - #120205 (std: make `HEAP` initializer never inline)
 - #120277 (Normalize field types before checking validity)
 - #120311 (core: add `From<core::ascii::Char>` implementations)
 - #120366 (mark a doctest with UB as no_run)
 - #120378 (always normalize `LoweredTy` in the new solver)
 - #120382 (Classify closure arguments in refutable pattern in argument error)
 - #120389 (Add fmease to the compiler review rotation)

r? `@ghost`
`@rustbot` modify labels: rollup
2024-01-27 00:22:48 +00:00
Esteban Küber a5d9def321 Properly recover from trailing attr in body
When encountering an attribute in a body, we try to recover from an
attribute on an expression (as opposed to a statement). We need to
properly clean up when the attribute is at the end of the body where a
tail expression would be.

Fix #118164.
2024-01-26 23:11:42 +00:00