Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 639fa85

Browse filesBrowse files
committed
Reviews
1 parent c963b4a commit 639fa85
Copy full SHA for 639fa85

File tree

Expand file treeCollapse file tree

1 file changed

+11
-11
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+11
-11
lines changed

‎src/hir/ambig-unambig-ty-and-consts.md

Copy file name to clipboardExpand all lines: src/hir/ambig-unambig-ty-and-consts.md
+11-11Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Ambig/Unambig Types and Consts
22

3-
Types and Consts args in the HIR can be in two kinds of positions "ambig" or "unambig". Ambig positions are where
3+
Types and Consts args in the HIR can be in two kinds of positions ambiguous (ambig) or unambiguous (unambig). Ambig positions are where
44
it would be valid to parse either a type or a const, unambig positions are where only one kind would be valid to
55
parse.
66

77
```rust
8-
fn func<T, const N: usize,>(arg: T) {
8+
fn func<T, const N: usize>(arg: T) {
99
// ^ Unambig type position
1010
let a: _ = arg;
1111
// ^ Unambig type position
@@ -21,19 +21,19 @@ fn func<T, const N: usize,>(arg: T) {
2121

2222
```
2323

24-
Most types/consts in ambig positions are able to be disambiguated as either a type or const during either parsing or ast-lowering.
25-
Currently the only exception to this is inferred generic arguments in path segments. In `Foo<_>` it is not clear whether the `_` argument is an
26-
inferred type argument, or an inferred const argument.
24+
Most types/consts in ambig positions are able to be disambiguated as either a type or const during parsing. Single segment paths are always represented as types in the AST but may get resolved to a const parameter during name resolution, then lowered to a const argument during ast-lowering. The only generic arguments which remain ambiguous after lowering are inferred generic arguments (`_`) in path segments. For example, in `Foo<_>` it is not clear whether the `_` argument is an inferred type argument, or an inferred const argument.
2725

2826
In unambig positions, inferred arguments are represented with [`hir::TyKind::Infer`][ty_infer] or [`hir::ConstArgKind::Infer`][const_infer] depending on whether it is a type or const position respectively.
2927
In ambig positions, inferred arguments are represented with `hir::GenericArg::Infer`.
3028

3129
A naive implementation of this structure would result in there being potentially 5 places where an inferred type/const could be found in the HIR if you just looked at the types:
32-
- In unambig type position as a `hir::TyKind::Infer`
33-
- In unambig const arg position as a `hir::ConstArgKind::Infer`
34-
- In an ambig position as a [`GenericArg::Type(TyKind::Infer)`][generic_arg_ty]
35-
- In an ambig position as a [`GenericArg::Const(ConstArgKind::Infer)`][generic_arg_const]
36-
- In an ambig position as a [`GenericArg::Infer`][generic_arg_infer]
30+
1. In unambig type position as a `hir::TyKind::Infer`
31+
2. In unambig const arg position as a `hir::ConstArgKind::Infer`
32+
3. In an ambig position as a [`GenericArg::Type(TyKind::Infer)`][generic_arg_ty]
33+
4. In an ambig position as a [`GenericArg::Const(ConstArgKind::Infer)`][generic_arg_const]
34+
5. In an ambig position as a [`GenericArg::Infer`][generic_arg_infer]
35+
36+
Note that places 3 and 4 would never actually be possible to encounter as we always lower to `GenericArg::Infer` in generic arg position.
3737

3838
This has a few failure modes:
3939
- People may write visitors which check for `GenericArg::Infer` but forget to check for `hir::TyKind/ConstArgKind::Infer`, only handling infers in ambig positions by accident.
@@ -45,7 +45,7 @@ To make writing HIR visitors less error prone when caring about inferred types/c
4545

4646
1. We have different types in the compiler for when a type or const is in an unambig or ambig position, `hir::Ty<AmbigArg>` and `hir::Ty<()>`. [`AmbigArg`][ambig_arg] is an uninhabited type which we use in the `Infer` variant of `TyKind` and `ConstArgKind` to selectively "disable" it if we are in an ambig position.
4747

48-
2. The [`visit_ty`][visit_infer] and [`visit_const_arg`][visit_const_arg] methods on HIR visitors only accept the ambig position versions of types/consts. Unambig types/consts are implicitly converted to ambig types/consts during the visiting process, with the `Infer` variant handled by a dedicated [`visit_infer`][visit_infer] method.
48+
2. The [`visit_ty`][visit_ty] and [`visit_const_arg`][visit_const_arg] methods on HIR visitors only accept the ambig position versions of types/consts. Unambig types/consts are implicitly converted to ambig types/consts during the visiting process, with the `Infer` variant handled by a dedicated [`visit_infer`][visit_infer] method.
4949

5050
This has a number of benefits:
5151
- It's clear that `GenericArg::Type/Const` cannot represent inferred type/const arguments

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.