rust/compiler
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
..
rustc switch jemalloc-sys back to tikv-jemalloc-sys, and update to 0.6.0 2024-12-03 08:56:33 +00:00
rustc_abi Auto merge of #135047 - Flakebi:amdgpu-kernel-cc, r=workingjubilee 2025-01-17 04:36:09 +00:00
rustc_arena Add inherent versions of MaybeUninit methods for slices 2025-01-11 23:57:00 -05:00
rustc_ast Handle parenthesised infer args 2025-01-23 06:01:36 +00:00
rustc_ast_ir
rustc_ast_lowering Handle parenthesised infer args 2025-01-23 06:01:36 +00:00
rustc_ast_passes Auto merge of #134299 - RalfJung:remove-start, r=compiler-errors 2025-01-21 19:46:20 +00:00
rustc_ast_pretty Rename PatKind::Lit to Expr 2025-01-08 07:34:59 +00:00
rustc_attr_data_structures allowed_through_unstable_modules: support showing a deprecation message when the unstable module name is used 2025-01-15 09:41:33 +01:00
rustc_attr_parsing Run clippy --fix for unnecessary_map_or lint 2025-01-19 19:15:00 +00:00
rustc_baked_icu_data Delete the cfg(not(parallel)) serial compiler 2024-11-12 13:38:58 +00:00
rustc_borrowck visit_x_unambig 2025-01-23 06:01:36 +00:00
rustc_builtin_macros Rollup merge of #135557 - estebank:wtf8, r=fee1-dead 2025-01-22 20:37:24 +01:00
rustc_codegen_cranelift Auto merge of #134299 - RalfJung:remove-start, r=compiler-errors 2025-01-21 19:46:20 +00:00
rustc_codegen_gcc Auto merge of #134299 - RalfJung:remove-start, r=compiler-errors 2025-01-21 19:46:20 +00:00
rustc_codegen_llvm rustc_codegen_llvm: remove outdated asm-to-obj codegen note 2025-01-22 17:58:50 -05:00
rustc_codegen_ssa Rollup merge of #135648 - folkertdev:naked-asm-wasm, r=bjorn3 2025-01-24 00:15:54 +01:00
rustc_const_eval Rollup merge of #134858 - estebank:issue-81370, r=Noratrieb 2025-01-19 11:48:15 +01:00
rustc_data_structures bumpt compiler and tools to windows 0.59 2025-01-21 16:48:44 +03:00
rustc_driver
rustc_driver_impl Rollup merge of #135880 - bjorn3:misc_driver_refactors, r=oli-obk 2025-01-23 19:54:26 +01:00
rustc_error_codes Refactor dyn-compatibility error and suggestions 2025-01-22 09:20:57 -08:00
rustc_error_messages Convert some Into impls into From impls 2024-12-31 01:56:33 +00:00
rustc_errors bumpt compiler and tools to windows 0.59 2025-01-21 16:48:44 +03:00
rustc_expand Auto merge of #134478 - compiler-errors:attr-span, r=oli-obk 2025-01-22 14:46:41 +00:00
rustc_feature Refactor dyn-compatibility error and suggestions 2025-01-22 09:20:57 -08:00
rustc_fluent_macro use tracked_path in rustc_fluent_macro 2024-10-19 22:32:38 +08:00
rustc_fs_util
rustc_graphviz
rustc_hir Explain visit_ty_unambig naming 2025-01-23 06:01:36 +00:00
rustc_hir_analysis Auto merge of #135272 - BoxyUwU:generic_arg_infer_reliability_2, r=compiler-errors 2025-01-24 11:12:01 +00:00
rustc_hir_pretty Split hir TyKind and ConstArgKind in two and update hir::Visitor 2025-01-23 06:01:36 +00:00
rustc_hir_typeck Auto merge of #135272 - BoxyUwU:generic_arg_infer_reliability_2, r=compiler-errors 2025-01-24 11:12:01 +00:00
rustc_incremental turn hir::ItemKind::Fn into a named-field variant 2025-01-04 11:35:31 +01:00
rustc_index Run clippy --fix for unnecessary_map_or lint 2025-01-19 19:15:00 +00:00
rustc_index_macros update rustc_index_macros feature handling 2024-12-19 20:32:12 +03:00
rustc_infer Remove Copy bound from enter_forall 2025-01-22 11:45:09 +00:00
rustc_interface Rollup merge of #135880 - bjorn3:misc_driver_refactors, r=oli-obk 2025-01-23 19:54:26 +01:00
rustc_lexer Add test to check unicode identifier version 2024-12-09 06:23:59 -08:00
rustc_lint Auto merge of #135272 - BoxyUwU:generic_arg_infer_reliability_2, r=compiler-errors 2025-01-24 11:12:01 +00:00
rustc_lint_defs Rollup merge of #132397 - m-ou-se:warn-missing-abi, r=Nadrieril 2025-01-15 04:08:10 -05:00
rustc_llvm Make our DIFlags match LLVMDIFlags in the LLVM-C API 2025-01-21 14:41:44 +11:00
rustc_log Avoid naming variables str 2025-01-07 14:30:02 +02:00
rustc_macros Avoid naming variables str 2025-01-07 14:30:02 +02:00
rustc_metadata Manual cleanup of some is_{or_none|some_and} usages 2025-01-19 20:50:43 +00:00
rustc_middle Auto merge of #135272 - BoxyUwU:generic_arg_infer_reliability_2, r=compiler-errors 2025-01-24 11:12:01 +00:00
rustc_mir_build Rollup merge of #135409 - Shunpoco:issue-133117-ICE-never-false-edge-start-block, r=Nadrieril 2025-01-22 20:37:24 +01:00
rustc_mir_dataflow Run clippy --fix for unnecessary_map_or lint 2025-01-19 19:15:00 +00:00
rustc_mir_transform Run clippy --fix for unnecessary_map_or lint 2025-01-19 19:15:00 +00:00
rustc_monomorphize Make sure we actually use the right trivial lifetime substs when eagerly monomorphizing drop for structs 2025-01-15 04:20:25 +00:00
rustc_next_trait_solver Rollup merge of #135766 - lcnr:candidate-assembly-3, r=compiler-errors 2025-01-23 19:54:25 +01:00
rustc_parse Rollup merge of #135855 - cuviper:parser-size, r=wesleywiser 2025-01-24 00:15:56 +01:00
rustc_parse_format Rollup merge of #135920 - hkBst:patch-16, r=SparrowLii 2025-01-23 19:54:28 +01:00
rustc_passes visit_x_unambig 2025-01-23 06:01:36 +00:00
rustc_pattern_analysis rename BitSet to DenseBitSet 2025-01-11 11:34:01 +00:00
rustc_privacy Split hir TyKind and ConstArgKind in two and update hir::Visitor 2025-01-23 06:01:36 +00:00
rustc_query_impl don't return an Option from try_find_dep_kind 2025-01-07 21:57:00 +01:00
rustc_query_system Properly note when query stack is being cut off 2025-01-16 19:12:22 +00:00
rustc_resolve rustc_resolve: don't open-code Option::filter 2025-01-21 13:44:53 +00:00
rustc_sanitizers Eliminate an unnecessary Symbol::to_string; use as_str 2025-01-07 14:24:47 +02:00
rustc_serialize Fix explicit_iter_loop in rustc_serialize 2024-10-16 15:44:16 +02:00
rustc_session Remove the need to manually call set_using_internal_features 2025-01-23 09:38:58 +00:00
rustc_smir Remove RunCompiler 2025-01-23 09:38:58 +00:00
rustc_span Auto merge of #134478 - compiler-errors:attr-span, r=oli-obk 2025-01-22 14:46:41 +00:00
rustc_symbol_mangling Fix legacy symbol mangling of closures 2025-01-14 16:33:03 +00:00
rustc_target Auto merge of #135978 - matthiaskrgr:rollup-ni16gqr, r=matthiaskrgr 2025-01-24 08:28:35 +00:00
rustc_trait_selection Auto merge of #135272 - BoxyUwU:generic_arg_infer_reliability_2, r=compiler-errors 2025-01-24 11:12:01 +00:00
rustc_traits Remove query normalize from normalize type op 2025-01-23 05:56:22 +00:00
rustc_transmute add comment explaining why ty_and_layout_field is not used 2024-12-18 11:01:54 +01:00
rustc_ty_utils Split hir TyKind and ConstArgKind in two and update hir::Visitor 2025-01-23 06:01:36 +00:00
rustc_type_ir Auto merge of #133830 - compiler-errors:span-key, r=lcnr 2025-01-21 12:33:33 +00:00
rustc_type_ir_macros do not relate Abi and Safety 2024-10-22 23:13:04 +02:00
stable_mir Add gpu-kernel calling convention 2025-01-16 00:26:55 +01:00