Skip to content

Commit 3e64409

Browse files
authored
Basic const generics docs
2 parents 3a90eeb + a10e16b commit 3e64409

File tree

5 files changed

+340
-64
lines changed

5 files changed

+340
-64
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@
126126
- [Lang Items](./lang-items.md)
127127
- [The HIR (High-level IR)](./hir.md)
128128
- [Lowering AST to HIR](./hir/lowering.md)
129-
- [Ambig/Unambig Types and Consts](./hir/ambig-unambig-ty-and-consts.md)
130129
- [Debugging](./hir/debugging.md)
130+
- [Ambig/Unambig Types and Consts](./ambig-unambig-ty-and-consts.md)
131131
- [The THIR (Typed High-level IR)](./thir.md)
132132
- [The MIR (Mid-level IR)](./mir/index.md)
133133
- [MIR construction](./mir/construction.md)
@@ -192,6 +192,7 @@
192192
- [HIR Type checking](./hir-typeck/summary.md)
193193
- [Coercions](./hir-typeck/coercions.md)
194194
- [Method lookup](./hir-typeck/method-lookup.md)
195+
- [Const Generics](./const-generics.md)
195196
- [Opaque types](./opaque-types-type-alias-impl-trait.md)
196197
- [Inference details](./opaque-types-impl-trait-inference.md)
197198
- [Return Position Impl Trait In Trait](./return-position-impl-trait-in-trait.md)

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Ambig/Unambig Types and Consts
2+
3+
Types and Consts args in the AST/HIR can be in two kinds of positions ambiguous (ambig) or unambiguous (unambig). Ambig positions are where
4+
it would be valid to parse either a type or a const, unambig positions are where only one kind would be valid to
5+
parse.
6+
7+
```rust
8+
fn func<T, const N: usize>(arg: T) {
9+
// ^ Unambig type position
10+
let a: _ = arg;
11+
// ^ Unambig type position
12+
13+
func::<T, N>(arg);
14+
// ^ ^
15+
// ^^^^ Ambig position
16+
17+
let _: [u8; 10];
18+
// ^^ ^^ Unambig const position
19+
// ^^ Unambig type position
20+
}
21+
22+
```
23+
24+
Most types/consts in ambig positions are able to be disambiguated as either a type or const during parsing. The only exceptions to this are paths and inferred generic arguments.
25+
26+
## Paths
27+
28+
```rust
29+
struct Foo<const N: usize>;
30+
31+
fn foo<const N: usize>(_: Foo<N>) {}
32+
```
33+
34+
At parse time we parse all unbraced generic arguments as *types* (ie they wind up as [`ast::GenericArg::Ty`]). In the above example this means we would parse the generic argument to `Foo` as an `ast::GenericArg::Ty` wrapping a [`ast::Ty::Path(N)`].
35+
36+
Then during name resolution:
37+
- When encountering a single segment path with no generic arguments in generic argument position, we will first try to resolve it in the type namespace and if that fails we then attempt to resolve in the value namespace.
38+
- All other kinds of paths we only try to resolve in the type namespace
39+
40+
See [`LateResolutionVisitor::visit_generic_arg`] for where this is implemented.
41+
42+
Finally during AST lowering when we attempt to lower a type argument, we first check if it is a `Ty::Path` and if it resolved to something in the value namespace. If it did then we create an *anon const* and lower to a const argument instead of a type argument.
43+
44+
See [`LoweringContext::lower_generic_arg`] for where this is implemented.
45+
46+
Note that the ambiguity for paths is not propgated into the HIR; there's no `hir::GenericArg::Path` which is turned into either a `Ty` or `Const` during HIR ty lowering (though we could do such a thing).
47+
48+
## Inferred arguments (`_`)
49+
50+
```rust
51+
struct Foo<const N: usize>;
52+
53+
fn foo() {
54+
let _unused: Foo<_>;
55+
}
56+
```
57+
58+
The only generic arguments which remain ambiguous after lowering are inferred generic arguments (`_`) in path segments. In the above example it is not clear at parse time whether the `_` argument to `Foo` is an inferred type argument, or an inferred const argument.
59+
60+
In ambig AST positions, inferred argumentsd are parsed as an [`ast::GenericArg::Ty`] wrapping a [`ast::Ty::Infer`]. Then during AST lowering when lowering an `ast::GenericArg::Ty` we check if it is an inferred type and if so lower to a [`hir::GenericArg::Infer`].
61+
62+
In unambig AST positions, inferred arguments are parsed as either `ast::Ty::Infer` or [`ast::AnonConst`]. The `AnonConst` case is quite strange, we use [`ast::ExprKind::Underscore`] to represent the "body" of the "anon const" although in reality we do not actually lower this to an anon const in the HIR.
63+
64+
It may be worth seeing if we can refactor the AST to have `ast::GenericArg::Infer` and then get rid of this overloaded meaning of `AnonConst`, as well as the reuse of `ast::Ty::Infer` in ambig positions.
65+
66+
In unambig AST positions, during AST lowering we lower inferred arguments to [`hir::TyKind::Infer`][ty_infer] or [`hir::ConstArgKind::Infer`][const_infer] depending on whether it is a type or const position respectively.
67+
In ambig AST positions, during AST lowering we lower inferred arguments to [`hir::GenericArg::Infer`][generic_arg_infer]. See [`LoweringContext::lower_generic_arg`] for where this is implemented.
68+
69+
A naive implementation of this would result in there being potentially 5 places where you might think an inferred type/const could be found in the HIR from looking at the structure of the HIR:
70+
1. In unambig type position as a `TyKind::Infer`
71+
2. In unambig const arg position as a `ConstArgKind::Infer`
72+
3. In an ambig position as a [`GenericArg::Type(TyKind::Infer)`][generic_arg_ty]
73+
4. In an ambig position as a [`GenericArg::Const(ConstArgKind::Infer)`][generic_arg_const]
74+
5. In an ambig position as a `GenericArg::Infer`
75+
76+
Note that places 3 and 4 would never actually be possible to encounter as we always lower to `GenericArg::Infer` in generic arg position.
77+
78+
This has a few failure modes:
79+
- 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.
80+
- People may write visitors which check for `TyKind/ConstArgKind::Infer` but forget to check for `GenericArg::Infer`, only handling infers in unambig positions by accident.
81+
- People may write visitors which check for `GenericArg::Type/Const(TyKind/ConstArgKind::Infer)` and `GenericArg::Infer`, not realising that we never represent inferred types/consts in ambig positions as a `GenericArg::Type/Const`.
82+
- People may write visitors which check for *only* `TyKind::Infer` and not `ConstArgKind::Infer` forgetting that there are also inferred const arguments (and vice versa).
83+
84+
To make writing HIR visitors less error prone when caring about inferred types/consts we have a relatively complex system:
85+
86+
1. We have different types in the compiler for when a type or const is in an unambig or ambig position, `Ty<AmbigArg>` and `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.
87+
88+
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.
89+
90+
This has a number of benefits:
91+
- It's clear that `GenericArg::Type/Const` cannot represent inferred type/const arguments
92+
- Implementors of `visit_ty` and `visit_const_arg` will never encounter inferred types/consts making it impossible to write a visitor that seems to work right but handles edge cases wrong
93+
- The `visit_infer` method handles *all* cases of inferred type/consts in the HIR making it easy for visitors to handle inferred type/consts in one dedicated place and not forget cases
94+
95+
[ty_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.TyKind.html#variant.Infer
96+
[const_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.ConstArgKind.html#variant.Infer
97+
[generic_arg_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Type
98+
[generic_arg_const]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Const
99+
[generic_arg_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Infer
100+
[ambig_arg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.AmbigArg.html
101+
[visit_ty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_ty
102+
[visit_const_arg]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_const_arg
103+
[visit_infer]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/intravisit/trait.Visitor.html#method.visit_infer
104+
[`LateResolutionVisitor::visit_generic_arg`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_resolve/late/struct.LateResolutionVisitor.html#method.visit_generic_arg
105+
[`LoweringContext::lower_generic_arg`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_lowering/struct.LoweringContext.html#method.lower_generic_arg
106+
[`ast::GenericArg::Ty`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.GenericArg.html#variant.Type
107+
[`ast::Ty::Infer`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.TyKind.html#variant.Infer
108+
[`ast::AnonConst`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/struct.AnonConst.html
109+
[`hir::GenericArg::Infer`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/hir/enum.GenericArg.html#variant.Infer
110+
[`ast::ExprKind::Underscore`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.ExprKind.html#variant.Underscore
111+
[`ast::Ty::Path(N)`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast/ast/enum.TyKind.html#variant.Path

0 commit comments

Comments
 (0)