-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Commit 8231e85
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 File tree
119 files changed
+1054
-667
lines changed- compiler
- rustc_ast_lowering/src
- rustc_ast/src
- rustc_borrowck/src/diagnostics
- rustc_hir_analysis/src
- check
- collect
- hir_ty_lowering
- rustc_hir_pretty/src
- rustc_hir_typeck/src
- fn_ctxt
- method
- rustc_hir/src
- hir
- rustc_lint/src
- rustc_middle/src
- ty
- rustc_passes/src
- rustc_privacy/src
- rustc_trait_selection/src
- error_reporting
- infer
- nice_region_error
- traits
- rustc_ty_utils/src
- src
- librustdoc
- clean
- tools/clippy
- clippy_lints/src
- casts
- methods
- operators
- transmute
- types
- unit_types
- clippy_utils/src
- ty/type_certainty
- tests/ui
- closures/binder
- const-generics
- generic_arg_infer
- issues
- did_you_mean
- feature-gates
- generics
- macros
- parser/issues
- span
- type-alias-impl-trait
- typeck
- type/pattern_types
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
119 files changed
+1054
-667
lines changedLines changed: 35 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
28 | 28 |
| |
29 | 29 |
| |
30 | 30 |
| |
31 | + | ||
31 | 32 |
| |
32 | 33 |
| |
33 | 34 |
| |
| |||
287 | 288 |
| |
288 | 289 |
| |
289 | 290 |
| |
291 | + | ||
290 | 292 |
| |
291 | 293 |
| |
292 | 294 |
| |
| |||
2165 | 2167 |
| |
2166 | 2168 |
| |
2167 | 2169 |
| |
2170 | + | ||
2171 | + | ||
2172 | + | ||
2173 | + | ||
2174 | + | ||
2175 | + | ||
2176 | + | ||
2177 | + | ||
2168 | 2178 |
| |
2169 | 2179 |
| |
2170 | 2180 |
| |
| |||
2269 | 2279 |
| |
2270 | 2280 |
| |
2271 | 2281 |
| |
2282 | + | ||
2272 | 2283 |
| |
2273 | - | ||
2274 | - | ||
2275 | - | ||
2284 | + | ||
2285 | + | ||
2286 | + | ||
2287 | + | ||
2288 | + | ||
2289 | + | ||
2290 | + | ||
2291 | + | ||
2292 | + | ||
2293 | + | ||
2294 | + | ||
2295 | + | ||
2296 | + | ||
2297 | + | ||
2298 | + | ||
2299 | + | ||
2300 | + | ||
2301 | + | ||
2302 | + | ||
2303 | + | ||
2304 | + | ||
2305 | + | ||
2306 | + | ||
2307 | + | ||
2276 | 2308 |
| |
2277 | 2309 |
| |
2278 | 2310 |
| |
|
Lines changed: 26 additions & 14 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1 | + | ||
1 | 2 |
| |
2 | 3 |
| |
3 | 4 |
| |
| |||
265 | 266 |
| |
266 | 267 |
| |
267 | 268 |
| |
268 | - | ||
269 | - | ||
270 | - | ||
271 | - | ||
272 | - | ||
273 | - | ||
274 | - | ||
275 | - | ||
276 | 269 |
| |
277 | 270 |
| |
278 | 271 |
| |
| |||
302 | 295 |
| |
303 | 296 |
| |
304 | 297 |
| |
305 | - | ||
306 | - | ||
298 | + | ||
299 | + | ||
307 | 300 |
| |
308 | 301 |
| |
309 | 302 |
| |
310 | 303 |
| |
311 | 304 |
| |
312 | 305 |
| |
313 | - | ||
314 | - | ||
306 | + | ||
307 | + | ||
308 | + | ||
309 | + | ||
310 | + | ||
311 | + | ||
315 | 312 |
| |
316 | - | ||
317 | - | ||
313 | + | ||
314 | + | ||
318 | 315 |
| |
319 | 316 |
| |
320 | 317 |
| |
318 | + | ||
319 | + | ||
320 | + | ||
321 | + | ||
322 | + | ||
323 | + | ||
324 | + | ||
325 | + | ||
326 | + | ||
327 | + | ||
328 | + | ||
329 | + | ||
330 | + | ||
331 | + | ||
332 | + | ||
321 | 333 |
| |
322 | 334 |
| |
323 | 335 |
| |
|
Lines changed: 25 additions & 15 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
48 | 48 |
| |
49 | 49 |
| |
50 | 50 |
| |
51 | + | ||
51 | 52 |
| |
52 | 53 |
| |
53 | 54 |
| |
| |||
1083 | 1084 |
| |
1084 | 1085 |
| |
1085 | 1086 |
| |
1087 | + | ||
1088 | + | ||
1089 | + | ||
1090 | + | ||
1091 | + | ||
1092 | + | ||
1093 | + | ||
1094 | + | ||
1095 | + | ||
1086 | 1096 |
| |
1087 | - | ||
1088 | - | ||
1089 | - | ||
1090 | - | ||
1091 | - | ||
1092 | - | ||
1093 | 1097 |
| |
1094 | 1098 |
| |
1095 | 1099 |
| |
1096 | 1100 |
| |
1101 | + | ||
1102 | + | ||
1097 | 1103 |
| |
1098 | 1104 |
| |
1099 | 1105 |
| |
| |||
1110 | 1116 |
| |
1111 | 1117 |
| |
1112 | 1118 |
| |
1113 | - | ||
1119 | + | ||
1114 | 1120 |
| |
1115 | 1121 |
| |
1116 | 1122 |
| |
1117 | 1123 |
| |
1118 | 1124 |
| |
1119 | - | ||
1125 | + | ||
1126 | + | ||
1127 | + | ||
1128 | + | ||
1120 | 1129 |
| |
1121 | - | ||
1122 | 1130 |
| |
1123 | 1131 |
| |
1124 | 1132 |
| |
| |||
1158 | 1166 |
| |
1159 | 1167 |
| |
1160 | 1168 |
| |
1161 | - | ||
1169 | + | ||
1170 | + | ||
1171 | + | ||
1172 | + | ||
1162 | 1173 |
| |
1163 | 1174 |
| |
1164 | 1175 |
| |
| |||
1185 | 1196 |
| |
1186 | 1197 |
| |
1187 | 1198 |
| |
1188 | - | ||
1199 | + | ||
1189 | 1200 |
| |
1190 | 1201 |
| |
1191 | 1202 |
| |
| |||
1309 | 1320 |
| |
1310 | 1321 |
| |
1311 | 1322 |
| |
1312 | - | ||
1323 | + | ||
1313 | 1324 |
| |
1314 | 1325 |
| |
1315 | 1326 |
| |
| |||
2041 | 2052 |
| |
2042 | 2053 |
| |
2043 | 2054 |
| |
2044 | - | ||
2055 | + | ||
2045 | 2056 |
| |
2046 | 2057 |
| |
2047 | 2058 |
| |
| |||
2365 | 2376 |
| |
2366 | 2377 |
| |
2367 | 2378 |
| |
2368 | - | ||
2369 | - | ||
2379 | + | ||
2370 | 2380 |
| |
2371 | 2381 |
| |
2372 | 2382 |
| |
|
Lines changed: 3 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
525 | 525 |
| |
526 | 526 |
| |
527 | 527 |
| |
528 | - | ||
528 | + | ||
529 | + | ||
530 | + | ||
529 | 531 |
| |
530 | 532 |
| |
531 | 533 |
| |
|
Lines changed: 3 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
8 | 8 |
| |
9 | 9 |
| |
10 | 10 |
| |
11 | - | ||
11 | + | ||
12 | 12 |
| |
13 | 13 |
| |
14 | 14 |
| |
| |||
887 | 887 |
| |
888 | 888 |
| |
889 | 889 |
| |
890 | - | ||
890 | + | ||
891 | 891 |
| |
892 | 892 |
| |
893 | 893 |
| |
| |||
987 | 987 |
| |
988 | 988 |
| |
989 | 989 |
| |
990 | - | ||
990 | + | ||
991 | 991 |
| |
992 | 992 |
| |
993 | 993 |
| |
|
Lines changed: 2 additions & 2 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
432 | 432 |
| |
433 | 433 |
| |
434 | 434 |
| |
435 | - | ||
435 | + | ||
436 | 436 |
| |
437 | 437 |
| |
438 | 438 |
| |
| |||
615 | 615 |
| |
616 | 616 |
| |
617 | 617 |
| |
618 | - | ||
618 | + | ||
619 | 619 |
| |
620 | 620 |
| |
621 | 621 |
| |
|
0 commit comments