rust/compiler/rustc_trait_selection/src/solve
bors 196ff446d2 Auto merge of #122493 - lukas-code:sized-constraint, r=lcnr
clean up `Sized` checking

This PR cleans up `sized_constraint` and related functions to make them simpler and faster. This should not make more or less code compile, but it can change error output in some rare cases.

## enums and unions are `Sized`, even if they are not WF

The previous code has some special handling for enums, which made them sized if and only if the last field of each variant is sized. For example given this definition (which is not WF)
```rust
enum E<T1: ?Sized, T2: ?Sized, U1: ?Sized, U2: ?Sized> {
    A(T1, T2),
    B(U1, U2),
}
```
the enum was sized if and only if `T2` and `U2` are sized, while `T1` and `T2` were ignored for `Sized` checking. After this PR this enum will always be sized.

Unsized enums are not a thing in Rust and removing this special case allows us to return an `Option<Ty>` from `sized_constraint`, rather than a `List<Ty>`.

Similarly, the old code made an union defined like this
```rust
union Union<T: ?Sized, U: ?Sized> {
    head: T,
    tail: U,
}
```
sized if and only if `U` is sized, completely ignoring `T`. This just makes no sense at all and now this union is always sized.

## apply the "perf hack" to all (non-error) types, instead of just type parameters

This "perf hack" skips evaluating `sized_constraint(adt): Sized` if `sized_constraint(adt): Sized` exactly matches a predicate defined on `adt`, for example:

```rust
// `Foo<T>: Sized` iff `T: Sized`, but we know `T: Sized` from a predicate of `Foo`
struct Foo<T /*: Sized */>(T);
```

Previously this was only applied to type parameters and now it is applied to every type. This means that for example this type is now always sized:

```rust
// Note that this definition is WF, but the type `S<T>` not WF in the global/empty ParamEnv
struct S<T>([T]) where [T]: Sized;
```

I don't anticipate this to affect compile time of any real-world program, but it makes the code a bit nicer and it also makes error messages a bit more consistent if someone does write such a cursed type.

## tuples are sized if the last type is sized

The old solver already has this behavior and this PR also implements it for the new solver and `is_trivially_sized`. This makes it so that tuples work more like a struct defined like this:

```rust
struct TupleN<T1, T2, /* ... */ Tn: ?Sized>(T1, T2, /* ... */ Tn);
```

This might improve the compile time of programs with large tuples a little, but is mostly also a consistency fix.

## `is_trivially_sized` for more types

This function is used post-typeck code (borrowck, const eval, codegen) to skip evaluating `T: Sized` in some cases. It will now return `true` in more cases, most notably `UnsafeCell<T>` and `ManuallyDrop<T>` where `T.is_trivially_sized`.

I'm anticipating that this change will improve compile time for some real world programs.
2024-03-19 04:21:14 +00:00
..
assembly address nits 2024-03-18 22:28:29 +01:00
eval_ctxt Rollup merge of #122687 - lcnr:normalizes-to-emit-nested-goals, r=compiler-errors 2024-03-18 22:24:39 +01:00
inspect cleanup + review 2024-03-18 18:13:25 +01:00
normalizes_to NormalizesTo return nested goals 2024-03-18 15:53:16 +01:00
alias_relate.rs move normalizes_to_hack to AliasRelate 2024-03-18 12:08:06 +01:00
fulfill.rs avoid collecting into vecs in some places 2024-03-02 14:18:47 +01:00
mod.rs cleanup + review 2024-03-18 18:13:25 +01:00
normalize.rs overflow errors: change source to a concrete enum 2024-02-22 17:43:57 +01:00
project_goals.rs track the source of nested goals 2023-12-18 08:47:29 +01:00
search_graph.rs NormalizesTo return nested goals 2024-03-18 15:53:16 +01:00
trait_goals.rs Rollup merge of #122238 - fee1-dead-contrib:builtin-impl-next-solver-dox, r=lcnr 2024-03-14 15:44:32 +01:00