Commit graph

2689 commits

Author SHA1 Message Date
Matthias Krüger
3559a48b8e
Rollup merge of #136368 - estebank:listify, r=fee1-dead
Make comma separated lists of anything easier to make for errors

Provide a new function `listify`, meant to be used in cases similar to `pluralize!`. When you have a slice of arbitrary elements that need to be presented to the user, `listify` allows you to turn that into a list of comma separated strings.

This reduces a lot of redundant logic that happens often in diagnostics.
2025-02-02 12:31:57 +01:00
Zalathar
3581512fb8 Use an explicit type when discarding the result of tcx.ensure_ok() 2025-02-01 12:42:41 +11:00
Zalathar
24cdaa146a Rename tcx.ensure() to tcx.ensure_ok() 2025-02-01 12:38:54 +11:00
Esteban Küber
8e9422f94e Make comma separated lists of anything easier to make for errors
Provide a new function `listify`, meant to be used in cases similar to `pluralize!`. When you have a slice of arbitrary elements that need to be presented to the user, `listify` allows you to turn that into a list of comma separated strings.

This reduces a lot of redundant logic that happens often in diagnostics.
2025-01-31 20:36:44 +00:00
bors
854f22563c Auto merge of #136350 - matthiaskrgr:rollup-6eqfyvh, r=matthiaskrgr
Rollup of 9 pull requests

Successful merges:

 - #134531 ([rustdoc] Add `--extract-doctests` command-line flag)
 - #135860 (Compiler: Finalize dyn compatibility renaming)
 - #135992 (Improve documentation when adding a new target)
 - #136194 (Support clobber_abi in BPF inline assembly)
 - #136325 (Delay a bug when indexing unsized slices)
 - #136326 (Replace our `LLVMRustDIBuilderRef` with LLVM-C's `LLVMDIBuilderRef`)
 - #136330 (Remove unnecessary hooks)
 - #136336 (Overhaul `rustc_middle::util`)
 - #136341 (Remove myself from vacation)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-01-31 20:16:46 +00:00
Matthias Krüger
308ea7120b
Rollup merge of #135860 - fmease:compiler-mv-obj-save-dyn-compat-ii, r=jieyouxu
Compiler: Finalize dyn compatibility renaming

Update the Reference link to use the new URL fragment from https://github.com/rust-lang/reference/pull/1666 (this change has finally hit stable). Fixes a FIXME.

Follow-up to #130826.
Part of #130852.

~~Blocking it on #133372.~~ (merged)

r? ghost
2025-01-31 12:28:15 +01:00
bors
7f36543a48 Auto merge of #136332 - jhpratt:rollup-aa69d0e, r=jhpratt
Rollup of 9 pull requests

Successful merges:

 - #132156 (When encountering unexpected closure return type, point at return type/expression)
 - #133429 (Autodiff Upstreaming - rustc_codegen_ssa, rustc_middle)
 - #136281 (`rustc_hir_analysis` cleanups)
 - #136297 (Fix a typo in profile-guided-optimization.md)
 - #136300 (atomic: extend compare_and_swap migration docs)
 - #136310 (normalize `*.long-type.txt` paths for compare-mode tests)
 - #136312 (Disable `overflow_delimited_expr` in edition 2024)
 - #136313 (Filter out RPITITs when suggesting unconstrained assoc type on too many generics)
 - #136323 (Fix a typo in conventions.md)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-01-31 09:42:28 +00:00
bors
25a16572a3 Auto merge of #136331 - jhpratt:rollup-curo1f4, r=jhpratt
Rollup of 8 pull requests

Successful merges:

 - #135414 (Stabilize `const_black_box`)
 - #136150 (ci: use windows 2025 for i686-mingw)
 - #136258 (rustdoc: rename `issue-\d+.rs` tests to have meaningful names (part 11))
 - #136270 (Remove `NamedVarMap`.)
 - #136278 (add constraint graph to polonius MIR dump)
 - #136287 (LLVM changed the nocapture attribute to captures(none))
 - #136291 (some test suite cleanups)
 - #136296 (float::min/max: mention the non-determinism around signed 0)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-01-31 06:55:04 +00:00
Jacob Pratt
e6285b8ff1
Rollup merge of #136313 - compiler-errors:too-many-args, r=lqd
Filter out RPITITs when suggesting unconstrained assoc type on too many generics

Fixes #136233
2025-01-31 00:26:34 -05:00
Jacob Pratt
940b45f27c
Rollup merge of #136281 - nnethercote:rustc_hir_analysis, r=lcnr
`rustc_hir_analysis` cleanups

Just some improvements I found while looking through this code.

r? `@lcnr`
2025-01-31 00:26:31 -05:00
Jacob Pratt
dcf1134386
Rollup merge of #136270 - nnethercote:rm-NamedVarMap, r=jackh726
Remove `NamedVarMap`.

`NamedVarMap` is extremely similar to `ResolveBoundVars`. The former contains two `UnordMap<ItemLocalId, T>` fields (obscured behind `ItemLocalMap` typedefs). The latter contains two
`SortedMap<ItemLocalId, T>` fields. We construct a `NamedVarMap` and then convert it into a `ResolveBoundVars` by sorting the `UnordMap`s, which is unnecessary busywork.

This commit removes `NamedVarMap` and constructs a `ResolveBoundVars` directly. `SortedMap` and `NamedVarMap` have slightly different perf characteristics during construction (e.g. speed of insertion) but this code isn't hot enough for that to matter.

A few details to note.
- A `FIXME` comment is removed.
- The detailed comments on the fields of `NamedVarMap` are copied to `ResolveBoundVars` (which has a single, incorrect comment).
- `BoundVarContext::map` is renamed.
- `ResolveBoundVars` gets a derived `Default` impl.

r? `@jackh726`
2025-01-31 00:25:36 -05:00
Nicholas Nethercote
483307f127 Don't export rustc_hir_analysis::collect.
Instead re-export `rustc_hir_analysis::collect::suggest_impl_trait`,
which is the only thing from the module used in another crate. This
fixes a `FIXME` comment. Also adjust some visibilities to satisfy the
`unreachable_pub` lint.

This changes requires downgrading a link in a comment on `FnCtxt`
because `collect` is no longer public and rustdoc complains otherwise.
This is annoying but I can't see how to avoid it.
2025-01-31 08:36:30 +11:00
Nicholas Nethercote
29317604d1 Remove xform submodule.
It used to be bigger, with an `Xform` trait, but now it has just a
single function.
2025-01-31 08:28:28 +11:00
Nicholas Nethercote
4e2ef694f3 Remove an unnecessary loop label. 2025-01-31 08:28:28 +11:00
Nicholas Nethercote
d8be00fd44 Fix a comment typo. 2025-01-31 08:28:14 +11:00
Nicholas Nethercote
40db88979d Use .and chaining to improve readability. 2025-01-31 08:27:16 +11:00
Nicholas Nethercote
ceb09de256 Remove an unnecessary lifetime from RemapLateParam. 2025-01-31 08:27:15 +11:00
Nicholas Nethercote
c2e37d32d5 Remove an unused arg from the trait method provided_kind. 2025-01-31 08:27:15 +11:00
Nicholas Nethercote
969d9336ca Remove unnecessary builders.
`delegation.rs` has three builders: `GenericsBuilder`,
`PredicatesBuilder`, and `GenericArgsBuilder`. The first two builders
have just two optional parameters, and the third one has zero. Each
builder is used within a single function. The code is over-engineered.

This commit removes the builders, replacing each with with a single
`build_*` function. This makes the code shorter and simpler.
2025-01-31 08:27:15 +11:00
Nicholas Nethercote
7be593e379 Format delegation.rs better.
There is a comment `Delegation to inherent methods is not yet
supported.` that appears three times mid-pattern and somehow inhibits
rustfmt from formatting the enclosing `match` statement. This commit
moves them to the top of the pattern, which enables more formatting.
2025-01-31 08:27:15 +11:00
Nicholas Nethercote
cab629fbd2 Merge two identical match arms.
Note: `inherit_predicates_for_delegation_item` already has these cases
merged.
2025-01-31 08:27:15 +11:00
Nicholas Nethercote
2dbe9fd8a6 Remove an out-of-date FIXME comment.
This comment made sense when this crate was called `rustc_typeck`, but
makes less sense now that it's called `rustc_hir_analysis`. Especially
given that `check_drop_impl` is only called within the crate.
2025-01-31 08:27:15 +11:00
Nicholas Nethercote
f453f930ab Merge two match arms that are identical.
Also rewrite the merged arm slightly to more closely match the arm above
it.
2025-01-31 08:27:15 +11:00
Nicholas Nethercote
b546334f6c Avoid a duplicated error case in fn_sig_suggestion. 2025-01-31 08:27:15 +11:00
Nicholas Nethercote
97d1b0cbcd Clarify a comment.
I was confused here for a bit.
2025-01-31 08:27:15 +11:00
Michael Goulet
88d7ea36e9 Filter out RPITITs when suggesting unconstrained assoc type on too many generics 2025-01-30 18:51:49 +00:00
Esteban Küber
d116767113 review comment: change span argument 2025-01-30 18:38:42 +00:00
Esteban Küber
d3a148fe07 When encountering unexpected closure return type, point at return type/expression
```
error[E0271]: expected `{closure@fallback-closure-wrap.rs:18:40}` to be a closure that returns `()`, but it returns `!`
  --> $DIR/fallback-closure-wrap.rs:19:9
   |
LL |     let error = Closure::wrap(Box::new(move || {
   |                                        -------
LL |         panic!("Can't connect to server.");
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `!`
   |
   = note: expected unit type `()`
                   found type `!`
   = note: required for the cast from `Box<{closure@$DIR/fallback-closure-wrap.rs:18:40: 18:47}>` to `Box<dyn FnMut()>`
```

```
error[E0271]: expected `{closure@dont-ice-for-type-mismatch-in-closure-in-async.rs:6:10}` to be a closure that returns `bool`, but it returns `Option<()>`
  --> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:6:16
   |
LL |     call(|| -> Option<()> {
   |     ---- ------^^^^^^^^^^
   |     |          |
   |     |          expected `bool`, found `Option<()>`
   |     required by a bound introduced by this call
   |
   = note: expected type `bool`
              found enum `Option<()>`
note: required by a bound in `call`
  --> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:3:25
   |
LL | fn call(_: impl Fn() -> bool) {}
   |                         ^^^^ required by this bound in `call`
```

```
error[E0271]: expected `{closure@f670.rs:28:13}` to be a closure that returns `Result<(), _>`, but it returns `!`
    --> f670.rs:28:20
     |
28   |     let c = |e| -> ! {
     |             -------^
     |                    |
     |                    expected `Result<(), _>`, found `!`
...
32   |     f().or_else(c);
     |         ------- required by a bound introduced by this call
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs:1433:28
     |
     = note: expected enum `Result<(), _>`
                found type `!`
note: required by a bound in `Result::<T, E>::or_else`
    --> /home/gh-estebank/rust/library/core/src/result.rs:1406:39
     |
1406 |     pub fn or_else<F, O: FnOnce(E) -> Result<T, F>>(self, op: O) -> Result<T, F> {
     |                                       ^^^^^^^^^^^^ required by this bound in `Result::<T, E>::or_else`
```
2025-01-30 18:38:37 +00:00
Michael Goulet
fdc4bd22b7 Do not treat vtable supertraits as distinct when bound with different bound vars 2025-01-30 15:33:58 +00:00
Michael Goulet
08d7e9dfe5 Rework rustc_dump_vtable 2025-01-30 15:30:04 +00:00
bors
5a45ab9738 Auto merge of #136038 - compiler-errors:outlives, r=lcnr
Simplify and consolidate the way we handle construct `OutlivesEnvironment` for lexical region resolution

This is best reviewed commit-by-commit. I tried to consolidate the API for lexical region resolution *first*, then change the API when it was finally behind a single surface.

r? lcnr or reassign
2025-01-30 11:40:32 +00:00
Nicholas Nethercote
022c0ce947 Remove NamedVarMap.
`NamedVarMap` is extremely similar to `ResolveBoundVars`. The former
contains two `UnordMap<ItemLocalId, T>` fields (obscured behind
`ItemLocalMap` typedefs). The latter contains two
`SortedMap<ItemLocalId, T>` fields. We construct a `NamedVarMap` and
then convert it into a `ResolveBoundVars` by sorting the `UnordMap`s,
which is unnecessary busywork.

This commit removes `NamedVarMap` and constructs a `ResolveBoundVars`
directly. `SortedMap` and `NamedVarMap` have slightly different
perf characteristics during construction (e.g. speed of insertion) but
this code isn't hot enough for that to matter.

A few details to note.
- A `FIXME` comment is removed.
- The detailed comments on the fields of `NamedVarMap` are copied to
  `ResolveBoundVars` (which has a single, incorrect comment).
- `BoundVarContext::map` is renamed.
- `ResolveBoundVars` gets a derived `Default` impl.
2025-01-30 09:39:52 +11:00
Oli Scherer
f47ad71059 Eliminate PatKind::Path 2025-01-29 15:45:13 +00:00
Michael Goulet
009d68740f Make item self/non-self bound naming less whack 2025-01-28 19:08:50 +00:00
Michael Goulet
48b7e38c06 Move outlives env computation into methods 2025-01-28 18:55:03 +00:00
Michael Goulet
2b8930c71c Consolidate OutlivesEnv construction with resolve_regions 2025-01-28 18:55:03 +00:00
Guillaume Gomez
03fdcffa1e
Rollup merge of #136114 - compiler-errors:more-idents, r=jieyouxu
Use identifiers more in diagnostics code

This should make the diagnostics code slightly more correct when rendering idents in mixed crate edition situations. Kinda a no-op, but a cleanup regardless.

r? oli-obk or reassign
2025-01-27 15:38:30 +01:00
Michael Goulet
c08624d8d2 Remove redundant to_ident_string calls 2025-01-27 01:23:34 +00:00
Michael Goulet
ac1c6c50f4 Use identifiers in diagnostics more often 2025-01-27 01:23:34 +00:00
FedericoBruzzone
cef97bce7b Add TooGeneric variant to LayoutError and emit Unknown one
- `check-pass` test for a MRE of #135020
- fail test for #135138
- switch to `TooGeneric` for checking CMSE fn signatures
- switch to `TooGeneric` for compute `SizeSkeleton` (for transmute)
- fix broken tests
2025-01-27 00:37:34 +01:00
León Orell Valerian Liehr
57b5d3af62
Compiler: Finalize dyn compatibility renaming 2025-01-26 21:20:31 +01:00
Jacob Pratt
6cf4204790
Rollup merge of #135951 - yotamofek:use-debug-helpers, r=SparrowLii
Use `fmt::from_fn` in more places in the compiler

Use the unstable functions from #117729 in more places in the compiler, follow up to #135494
2025-01-25 23:27:00 -05:00
Matthias Krüger
9ffe558455
Rollup merge of #135971 - compiler-errors:self-projection, r=fmease
Properly report error when object type param default references self

I accidentally broke this error for cases where a type parameter references `Self` via a projection (i.e. `trait Foo<Arg = Self::Bar> {}`). This PR fixes that, and also makes the error a bit easier to understand.

Fixes #135918
2025-01-25 08:03:33 +01: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
bors
8231e8599e Auto merge of #135272 - BoxyUwU:generic_arg_infer_reliability_2, r=compiler-errors
Forbid usage of `hir` `Infer` const/ty variants in ambiguous contexts

The feature `generic_arg_infer` allows providing `_` as an argument to const generics in order to infer them. This introduces a syntactic ambiguity as to whether generic arguments are type or const arguments. In order to get around this we introduced a fourth `GenericArg` variant, `Infer` used to represent `_` as an argument to generic parameters when we don't know if its a type or a const argument.

This made hir visitors that care about `TyKind::Infer` or `ConstArgKind::Infer` very error prone as checking for `TyKind::Infer`s in  `visit_ty` would find *some* type infer arguments but not *all* of them as they would sometimes be lowered to `GenericArg::Infer` instead.

Additionally the `visit_infer` method would previously only visit `GenericArg::Infer` not *all* infers (e.g. `TyKind::Infer`), this made it very easy to override `visit_infer` and expect it to visit all infers when in reality it would only visit *some* infers.

---

This PR aims to fix those issues by making the `TyKind` and `ConstArgKind` types generic over whether the infer types/consts are represented by `Ty/ConstArgKind::Infer` or out of line (e.g. by a `GenericArg::Infer` or accessible by overiding `visit_infer`). We then make HIR Visitors convert all const args and types to the versions where infer vars are stored out of line and call `visit_infer` in cases where a `Ty`/`Const` would previously have had a `Ty/ConstArgKind::Infer` variant:

API Summary
```rust
enum AmbigArg {}

enum Ty/ConstArgKind<Unambig = ()> {
   ...
   Infer(Unambig),
}

impl Ty/ConstArg {
  fn try_as_ambig_ty/ct(self) -> Option<Ty/ConstArg<AmbigArg>>;
}
impl Ty/ConstArg<AmbigArg> {
  fn as_unambig_ty/ct(self) -> Ty/ConstArg;
}

enum InferKind {
  Ty(Ty),
  Const(ConstArg),
  Ambig(InferArg),
}

trait Visitor {
  ...
  fn visit_ty/const_arg(&mut self, Ty/ConstArg<AmbigArg>) -> Self::Result;
  fn visit_infer(&mut self, id: HirId, sp: Span, kind: InferKind) -> Self::Result;
}

// blanket impl'd, not meant to be overriden
trait VisitorExt {
  fn visit_ty/const_arg_unambig(&mut self, Ty/ConstArg) -> Self::Result;
}

fn walk_unambig_ty/const_arg(&mut V, Ty/ConstArg) -> Self::Result;
fn walk_ty/const_arg(&mut V, Ty/ConstArg<AmbigArg>) -> Self::Result;
```

The end result is that `visit_infer` visits *all* infer args and is also the *only* way to visit an infer arg, `visit_ty` and `visit_const_arg` can now no longer encounter a `Ty/ConstArgKind::Infer`. Representing this in the type system means that it is now very difficult to mess things up, either accessing `TyKind::Infer` "just works" and you won't miss *some* type infers- or it doesn't work and you have to look at `visit_infer` or some `GenericArg::Infer` which forces you to think about the full complexity involved.

Unfortunately there is no lint right now about explicitly matching on uninhabited variants, I can't find the context for why this is the case 🤷‍♀️

I'm not convinced the framing of un/ambig ty/consts is necessarily the right one but I'm not sure what would be better. I somewhat like calling them full/partial types based on the fact that `Ty<Partial>`/`Ty<Full>` directly specifies how many of the type kinds are actually represented compared to `Ty<Ambig>` which which leaves that to the reader to figure out based on the logical consequences of it the type being in an ambiguous position.

---

tool changes have been modified in their own commits for easier reviewing by anyone getting cc'd from subtree changes. I also attempted to split out "bug fixes arising from the refactoring" into their own commit so they arent lumped in with a big general refactor commit

Fixes #112110
2025-01-24 11:12:01 +00:00
Matthias Krüger
efb8084672
Rollup merge of #135865 - zachs18:maybe_report_similar_assoc_fn_more, r=compiler-errors
For E0223, suggest associated functions that are similar to the path, even if the base type has multiple inherent impl blocks.

Currently, the "help: there is an associated function with a similar name `from_utf8`" suggestion for `String::from::utf8` is only given if `String` has exactly one inherent `impl` item. This PR makes the suggestion be emitted even if the base type has multiple inherent `impl` items.

Example:

```rust
struct Foo;
impl Foo {
    fn bar_baz() {}
}
impl Foo {} // load-bearing
fn main() {
    Foo::bar::baz;
}
```

Nightly/stable output:

```rust
error[E0223]: ambiguous associated type
 --> f.rs:7:5
  |
7 |     Foo::bar::baz;
  |     ^^^^^^^^
  |
help: if there were a trait named `Example` with associated type `bar` implemented for `Foo`, you could use the fully-qualified path
  |
7 |     <Foo as Example>::bar::baz;
  |     ~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0223`.
```

Output with this PR, or without the load-bearing empty impl on nightly/stable:

```rust
error[E0223]: ambiguous associated type
 --> f.rs:7:5
  |
7 |     Foo::bar::baz;
  |     ^^^^^^^^
  |
help: there is an associated function with a similar name: `bar_baz`
  |
7 |     Foo::bar_baz;
  |          ~~~~~~~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0223`.
```

Ideally, this suggestion would also work for non-ADT types like ~~`str::char::indices`~~ (edit: latest commit makes this work with primitives)  or `<dyn Any>::downcast::mut_unchecked`, but that seemed to be a harder change.

`@rustbot` label +A-diagnostics
2025-01-24 08:08:08 +01:00
Michael Goulet
ea9a253ff1 Properly report error when object type param default references self 2025-01-24 04:07:10 +00:00
Boxy
2bdeff2fb8 visit_x_unambig 2025-01-23 06:01:36 +00:00
Boxy
7c8c6d2497 Semantic changes from new hir representation
Always lower to `GenericArg::Infer`
Update `PlaceholderCollector`
Update closure lifetime binder infer var visitor
Fallback visitor handle ambig infer args
Ensure type infer args have their type recorded
2025-01-23 06:01:36 +00:00
Boxy
98d80e22d0 Split hir TyKind and ConstArgKind in two and update hir::Visitor 2025-01-23 06:01:36 +00:00