rust/compiler
Matthias Krüger e903b29dc3
Rollup merge of #129021 - compiler-errors:ptr-cast-outlives, r=lcnr
Check WF of source type's signature on fn pointer cast

This PR patches the implied bounds holes slightly for #129005, #25860.

Like most implied bounds related unsoundness fixes, this isn't complete w.r.t. higher-ranked function signatures, but I believe it implements a pretty good heuristic for now.

### What does this do?

This PR makes a partial patch for a soundness hole in a `FnDef` -> `FnPtr` "reifying" pointer cast where we were never checking that the signature we are casting *from* is actually well-formed. Because of this, and because `FnDef` doesn't require its signature to be well-formed (just its predicates must hold), we are essentially allowed to "cast away" implied bounds that are assumed within the body of the `FnDef`:

```
fn foo<'a, 'b, T>(_: &'a &'b (), v: &'b T) -> &'a T { v }

fn bad<'short, T>(x: &'short T) -> &'static T {
    let f: fn(_, &'short T) -> &'static T = foo;
    f(&&(), x)
}
```

In this example, subtyping ends up casting the `_` type (which should be `&'static &'short ()`) to some other type that no longer serves as a "witness" to the lifetime relationship `'short: 'static` which would otherwise be required for this call to be WF. This happens regardless of if `foo`'s lifetimes are early- or late-bound.

This PR implements two checks:
1. We check that the signature of the `FnDef` is well-formed *before* casting it. This ensures that there is at least one point in the MIR where we ensure that the `FnDef`'s implied bounds are actually satisfied by the caller.
2. Implements a special case where if we're casting from a higher-ranked `FnDef` to a non-higher-ranked, we instantiate the binder of the `FnDef` with *infer vars* and ensure that it is a supertype of the target of the cast.

The (2.) is necessary to validate that these pointer casts are valid for higher-ranked `FnDef`. Otherwise, the example above would still pass even if `help`'s `'a` lifetime were late-bound.

### Further work

The WF checks for function calls are scattered all over the MIR. We check the WF of args in call terminators, we check the WF of `FnDef` when we create a `const` operand referencing it, and we check the WF of the return type in #115538, to name a few.

One way to make this a bit cleaner is to simply extend #115538 to always check that the signature is WF for `FnDef` types. I may do this as a follow-up, but I wanted to keep this simple since this leads to some pretty bad NLL diagnostics regressions, and AFAICT this solution is *complete enough*.

### Crater triage

Done here: https://github.com/rust-lang/rust/pull/129021#issuecomment-2297702647

r? lcnr
2024-09-06 07:33:56 +02:00
..
rustc disable size asserts in the compiler when randomizing layouts 2024-08-31 23:56:45 +02:00
rustc_abi inhibit layout randomization for Box 2024-08-31 23:56:45 +02:00
rustc_arena compiler: Remove feature(new_uninit) 2024-08-27 10:17:05 -07:00
rustc_ast Create opaque definitions in resolver. 2024-08-31 20:14:43 +00:00
rustc_ast_ir Add warn(unreachable_pub) to several crates. 2024-08-16 08:46:13 +10:00
rustc_ast_lowering chore: Fix typos in 'compiler' (batch 1) 2024-09-02 07:42:38 +02:00
rustc_ast_passes Create opaque definitions in resolver. 2024-08-31 20:14:43 +00:00
rustc_ast_pretty Add warn(unreachable_pub) to rustc_ast_pretty. 2024-08-16 08:46:46 +10:00
rustc_attr Rollup merge of #126013 - nnethercote:unreachable_pub, r=Urgau 2024-08-27 00:41:57 +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 Rollup merge of #129021 - compiler-errors:ptr-cast-outlives, r=lcnr 2024-09-06 07:33:56 +02:00
rustc_builtin_macros Rollup merge of #123940 - kornelski:remove-derived-debug, r=Urgau 2024-08-29 16:21:46 +02:00
rustc_codegen_cranelift introduce PrettyPrintMirOptions for cosmetic MIR dump options 2024-08-30 07:07:28 +00:00
rustc_codegen_gcc Rollup merge of #128731 - RalfJung:simd-shuffle-vector, r=workingjubilee 2024-08-27 01:46:50 -05:00
rustc_codegen_llvm Auto merge of #121614 - clubby789:no-expect, r=saethlin 2024-09-06 00:42:58 +00:00
rustc_codegen_ssa Don't codegen expect in opt-level=0 2024-09-04 11:49:00 +00:00
rustc_const_eval Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_data_structures Add an internal lint that warns when accessing untracked data 2024-09-03 19:14:19 +02:00
rustc_driver
rustc_driver_impl Rollup merge of #101339 - the8472:ci-randomize-debug, r=Mark-Simulacrum 2024-09-05 03:47:39 +02:00
rustc_error_codes chore: Fix typos in 'compiler' (batch 1) 2024-09-02 07:42:38 +02:00
rustc_error_messages Add warn(unreachable_pub) to rustc_error_messages. 2024-08-27 11:52:08 +10:00
rustc_errors Rollup merge of #129875 - Sajjon:sajjon_fix_typos_batch_1, r=compiler-errors,jieyouxu 2024-09-02 22:35:20 +02:00
rustc_expand Add warn(unreachable_pub) to rustc_expand. 2024-08-27 12:40:38 +10:00
rustc_feature Rollup merge of #128919 - Nadrieril:lint-query-leaks, r=cjgillot 2024-09-05 19:43:46 +02:00
rustc_fluent_macro Add warn(unreachable_pub) to rustc_fluent_macro. 2024-08-27 12:56:54 +10:00
rustc_fs_util
rustc_graphviz Add warn(unreachable_pub) to rustc_graphviz. 2024-08-27 12:58:29 +10:00
rustc_hir Revert "Auto merge of #127537 - veluca93:struct_tf, r=BoxyUwU" 2024-09-01 16:35:53 +02:00
rustc_hir_analysis Rollup merge of #129664 - adetaylor:arbitrary-self-types-pointers-feature-gate, r=wesleywiser 2024-09-05 03:47:42 +02:00
rustc_hir_pretty Add warn(unreachable_pub) to rustc_hir_pretty. 2024-08-27 13:25:40 +10:00
rustc_hir_typeck Rollup merge of #129664 - adetaylor:arbitrary-self-types-pointers-feature-gate, r=wesleywiser 2024-09-05 03:47:42 +02:00
rustc_incremental Add warn(unreachable_pub) to rustc_incremental. 2024-08-27 14:30:20 +10:00
rustc_index disable size asserts in the compiler when randomizing layouts 2024-08-31 23:56:45 +02:00
rustc_index_macros
rustc_infer chore: Fix typos in 'compiler' (batch 1) 2024-09-02 07:42:38 +02:00
rustc_interface chore: Fix typos in 'compiler' (batch 1) 2024-09-02 07:42:38 +02:00
rustc_lexer Add warn(unreachable_pub) to rustc_lexer. 2024-08-27 15:12:46 +10:00
rustc_lint Auto merge of #129999 - matthiaskrgr:rollup-pzr9c8p, r=matthiaskrgr 2024-09-06 03:06:52 +00:00
rustc_lint_defs update docs of missing_abi lint 2024-09-04 14:30:56 +02:00
rustc_llvm Rollup merge of #129749 - krasimirgg:llvm-20-lto, r=nikic 2024-08-31 14:46:09 +02:00
rustc_log
rustc_macros Add warn(unreachable_pub) to rustc_metadata. 2024-08-29 20:13:06 +10:00
rustc_metadata Make supertrait and implied predicates queries defaulted 2024-09-04 06:28:59 -04:00
rustc_middle Auto merge of #129999 - matthiaskrgr:rollup-pzr9c8p, r=matthiaskrgr 2024-09-06 03:06:52 +00:00
rustc_mir_build Revert "Auto merge of #127537 - veluca93:struct_tf, r=BoxyUwU" 2024-09-01 16:35:53 +02:00
rustc_mir_dataflow Adjust SanityCheck. 2024-09-03 15:18:30 +10:00
rustc_mir_transform Rollup merge of #129720 - nnethercote:simplify-dest_prop-mm, r=cjgillot 2024-09-05 18:58:55 +02:00
rustc_monomorphize chore: Fix typos in 'compiler' (batch 2) 2024-09-02 07:50:22 +02:00
rustc_next_trait_solver do not attempt to prove unknowable goals 2024-09-03 08:35:23 +02:00
rustc_parse chore: Fix typos in 'compiler' (batch 2) 2024-09-02 07:50:22 +02:00
rustc_parse_format Add warn(unreachable_pub) to rustc_parse_format. 2024-08-29 20:13:06 +10:00
rustc_passes Rollup merge of #128919 - Nadrieril:lint-query-leaks, r=cjgillot 2024-09-05 19:43:46 +02:00
rustc_pattern_analysis Rollup merge of #128934 - Nadrieril:fix-empty-non-exhaustive, r=compiler-errors 2024-09-03 19:13:24 +02:00
rustc_privacy Add warn(unreachable_pub) to rustc_privacy. 2024-08-29 20:18:44 +10:00
rustc_query_impl Add warn(unreachable_pub) to rustc_query_impl. 2024-08-29 20:18:44 +10:00
rustc_query_system chore: Fix typos in 'compiler' (batch 2) 2024-09-02 07:50:22 +02:00
rustc_resolve Rollup merge of #129877 - Sajjon:sajjon_fix_typos_batch_2, r=fee1-dead 2024-09-02 22:35:21 +02:00
rustc_sanitizers Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_serialize Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_session Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_smir Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_span Rollup merge of #128919 - Nadrieril:lint-query-leaks, r=cjgillot 2024-09-05 19:43:46 +02:00
rustc_symbol_mangling Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_target Rollup merge of #127021 - thesummer:1-add-target-support-for-rtems-arm-xilinx-zedboard, r=tgross35 2024-09-05 03:47:40 +02:00
rustc_trait_selection Rollup merge of #129896 - lcnr:bail-on-unknowable, r=jackh726 2024-09-03 19:13:26 +02:00
rustc_traits Add warn(unreachable_pub) to rustc_traits. 2024-09-03 08:49:54 +10:00
rustc_transmute Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgau 2024-09-03 01:27:20 +00:00
rustc_ty_utils Add warn(unreachable_pub) to rustc_ty_utils. 2024-09-03 08:50:33 +10:00
rustc_type_ir do not attempt to prove unknowable goals 2024-09-03 08:35:23 +02:00
rustc_type_ir_macros
stable_mir chore: Fix typos in 'compiler' (batch 3) 2024-09-02 07:33:41 +02:00