rust/compiler
Matthias Krüger 4e510daed7
Rollup merge of #130866 - compiler-errors:dyn-instantiate-binder, r=lcnr
Allow instantiating object trait binder when upcasting

This PR fixes two bugs (that probably need an FCP).

### We use equality rather than subtyping for upcasting dyn conversions

This code should be valid:

```rust
#![feature(trait_upcasting)]

trait Foo: for<'h> Bar<'h> {}
trait Bar<'a> {}

fn foo(x: &dyn Foo) {
    let y: &dyn Bar<'static> = x;
}
```
But instead:

```
error[E0308]: mismatched types
 --> src/lib.rs:7:32
  |
7 |     let y: &dyn Bar<'static> = x;
  |                                ^ one type is more general than the other
  |
  = note: expected existential trait ref `for<'h> Bar<'h>`
             found existential trait ref `Bar<'_>`
```

And so should this:

```rust
#![feature(trait_upcasting)]

fn foo(x: &dyn for<'h> Fn(&'h ())) {
    let y: &dyn FnOnce(&'static ()) = x;
}
```

But instead:

```
error[E0308]: mismatched types
 --> src/lib.rs:4:39
  |
4 |     let y: &dyn FnOnce(&'static ()) = x;
  |                                       ^ one type is more general than the other
  |
  = note: expected existential trait ref `for<'h> FnOnce<(&'h (),)>`
             found existential trait ref `FnOnce<(&(),)>`
```

Specifically, both of these fail because we use *equality* when comparing the supertrait to the *target* of the unsize goal. For the first example, since our supertrait is `for<'h> Bar<'h>` but our target is `Bar<'static>`, there's a higher-ranked type mismatch even though we *should* be able to instantiate that supertrait binder when upcasting. Similarly for the second example.

### New solver uses equality rather than subtyping for no-op (i.e. non-upcasting) dyn conversions

This code should be valid in the new solver, like it is with the old solver:

```rust
// -Znext-solver

fn foo<'a>(x: &mut for<'h> dyn Fn(&'h ())) {
   let _: &mut dyn Fn(&'a ()) = x;
}
```

But instead:

```
error: lifetime may not live long enough
 --> <source>:2:11
  |
1 | fn foo<'a>(x: &mut dyn for<'h> Fn(&'h ())) {
  |        -- lifetime `'a` defined here
2 |    let _: &mut dyn Fn(&'a ()) = x;
  |           ^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
  |
  = note: requirement occurs because of a mutable reference to `dyn Fn(&())`
```

Specifically, this fails because we try to coerce `&mut dyn for<'h> Fn(&'h ())` to `&mut dyn Fn(&'a ())`, which registers an `dyn for<'h> Fn(&'h ()): dyn Fn(&'a ())` goal. This fails because the new solver uses *equating* rather than *subtyping* in `Unsize` goals.

This is *mostly* not a problem... You may wonder why the same code passes on the new solver for immutable references:

```
// -Znext-solver

fn foo<'a>(x: &dyn Fn(&())) {
   let _: &dyn Fn(&'a ()) = x; // works
}
```

That's because in this case, we first try to coerce via `Unsize`, but due to the leak check the goal fails. Then, later in coercion, we fall back to a simple subtyping operation, which *does* work.

Since `&T` is covariant over `T`, but `&mut T` is invariant, that's where the discrepancy between these two examples crops up.

---

r? lcnr or reassign :D
2024-09-28 09:35:09 +02:00
..
rustc disable size asserts in the compiler when randomizing layouts 2024-08-31 23:56:45 +02:00
rustc_abi Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_arena Remove unnecessary lifetimes from Arena. 2024-09-13 15:33:19 +10:00
rustc_ast Pin memchr to 2.5.0 in the library rather than rustc_ast 2024-09-24 18:09:43 +02:00
rustc_ast_ir Add warn(unreachable_pub) to several crates. 2024-08-16 08:46:13 +10:00
rustc_ast_lowering Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_ast_passes Ban combination of GCE and new solver 2024-09-24 10:53:32 -04:00
rustc_ast_pretty Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_attr rustdoc: inherit parent's stability where applicable 2024-09-24 20:18:36 +02:00
rustc_baked_icu_data Don't add warn(unreachable_pub) to rustc_baked_icu. 2024-08-16 08:46:52 +10:00
rustc_borrowck Auto merge of #130803 - cuviper:file-buffered, r=joshtriplett 2024-09-25 04:57:12 +00:00
rustc_builtin_macros Rollup merge of #130917 - gurry:129503-ice-wrong-span-in-macros, r=chenyukang 2024-09-27 19:08:01 +02:00
rustc_codegen_cranelift Add a debug assertion in codegen that unsize casts of the same principal trait def id are truly NOPs 2024-09-25 11:13:59 -04:00
rustc_codegen_gcc Fmt 2024-09-27 22:09:18 +02:00
rustc_codegen_llvm Use &raw in the compiler 2024-09-26 20:33:26 -07:00
rustc_codegen_ssa Move Apple linker args from rustc_target to rustc_codegen_ssa 2024-09-26 16:40:25 +02:00
rustc_const_eval Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_data_structures Use &raw in the compiler 2024-09-26 20:33:26 -07:00
rustc_driver
rustc_driver_impl Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_error_codes Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_error_messages Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_errors Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_expand Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_feature Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_fluent_macro Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_fs_util Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_graphviz Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_hir Rollup merge of #130714 - compiler-errors:try-structurally-resolve-const, r=BoxyUwU 2024-09-23 06:45:36 +02:00
rustc_hir_analysis Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_hir_pretty Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_hir_typeck Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_incremental Dogfood feature(file_buffered) 2024-09-24 14:25:16 -07:00
rustc_index Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_index_macros Remove usage of specialization from newtype_index! 2024-06-30 16:42:53 +00:00
rustc_infer Get rid of a_is_expected from ToTrace 2024-09-27 15:43:18 -04:00
rustc_interface Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_lexer Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_lint Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_lint_defs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_llvm Pass Module Analysis Manager to Standard Instrumentations 2024-09-25 22:57:32 +03:00
rustc_log Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_macros Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_metadata Auto merge of #130803 - cuviper:file-buffered, r=joshtriplett 2024-09-25 04:57:12 +00:00
rustc_middle Auto merge of #130946 - matthiaskrgr:rollup-ia4mf0y, r=matthiaskrgr 2024-09-27 21:23:29 +00:00
rustc_mir_build be even more precise about "cast" vs "coercion" 2024-09-24 23:12:02 +02:00
rustc_mir_dataflow Dogfood feature(file_buffered) 2024-09-24 14:25:16 -07:00
rustc_mir_transform Add a debug assertion in codegen that unsize casts of the same principal trait def id are truly NOPs 2024-09-25 11:13:59 -04:00
rustc_monomorphize Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_next_trait_solver Rollup merge of #130866 - compiler-errors:dyn-instantiate-binder, r=lcnr 2024-09-28 09:35:09 +02:00
rustc_parse Rollup merge of #130551 - nnethercote:fix-break-last-token, r=petrochenkov 2024-09-23 07:54:44 -07:00
rustc_parse_format Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_passes Check vtable projections for validity in miri 2024-09-23 19:38:26 -04:00
rustc_pattern_analysis Rollup merge of #130715 - compiler-errors:mir-build-const-eval, r=BoxyUwU 2024-09-23 06:45:36 +02:00
rustc_privacy Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_query_impl Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_query_system Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_resolve Separate collection of crate-local inherent impls from error reporting 2024-09-24 10:12:05 -04:00
rustc_sanitizers Compiler: Rename "object safe" to "dyn compatible" 2024-09-25 13:26:48 +02:00
rustc_serialize Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_session Rollup merge of #129087 - slanterns:option_get_or_insert_default, r=dtolnay 2024-09-27 19:07:58 +02:00
rustc_smir Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, r=compiler-errors 2024-09-27 21:35:08 +02:00
rustc_span add unqualified_local_imports lint 2024-09-23 11:57:28 +02:00
rustc_symbol_mangling Rollup merge of #130344 - Jaic1:fix-116306, r=BoxyUwU 2024-09-23 06:45:33 +02:00
rustc_target Rollup merge of #130873 - taiki-e:ppc64-atomic, r=Amanieu 2024-09-27 19:08:00 +02:00
rustc_trait_selection Rollup merge of #130866 - compiler-errors:dyn-instantiate-binder, r=lcnr 2024-09-28 09:35:09 +02:00
rustc_traits Compiler: Rename "object safe" to "dyn compatible" 2024-09-25 13:26:48 +02:00
rustc_transmute Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_ty_utils Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
rustc_type_ir Auto merge of #130946 - matthiaskrgr:rollup-ia4mf0y, r=matthiaskrgr 2024-09-27 21:23:29 +00:00
rustc_type_ir_macros Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
stable_mir Auto merge of #130946 - matthiaskrgr:rollup-ia4mf0y, r=matthiaskrgr 2024-09-27 21:23:29 +00:00