Skip to content

Commit dfc7727

Browse files
committed
1 parent 2642e55 commit dfc7727

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

text/0000-const-type-inference.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,39 @@ Allowing the naming of function types as in [#3476](https://github.com/rust-lang
106106

107107
`type_alias_impl_trait` may also partially address the problem. In particular, it helps with unnameable types
108108
and macro / code generator output, without the drawbacks of loss of clarity and semvar trouble.
109-
However, there might be cases where we do not want the type to be hidden behind an `impl Trait`.
110-
This also won't help with array lengths or types that do not implement a particular trait.
109+
However, it cannot fully replace inference because
110+
- There are cases where we do not want the type to be hidden behind an `impl Trait`.
111+
- Defining a trait for the const item might be difficult - for example, when the
112+
const item is a function pointer with a variable number of arguments.
113+
- The const item might be generated from a macro, and the macro might not
114+
want to require a separate trait to be defined.
115+
- This also won't help with array lengths or types that do not implement a particular trait.
111116

112117
# Prior art
113118
[prior-art]: #prior-art
114119

115120
In [RFC#1623](https://github.com/rust-lang/rfcs/pull/1623) we added `'static` lifetimes to every reference or generics lifetime value in `static` or `const` declarations.
116121

122+
In [RFC#2010](https://github.com/rust-lang/rfcs/pull/2010) const/static type inference
123+
was proposed, but the RFC was **postponed**. The [reason](https://github.com/rust-lang/rfcs/pull/2010#issuecomment-325827854) can be summarized as follows:
124+
125+
- Things we can do with const/static was quite limited at the time.
126+
Const/static type inference fails to provide value for simple cases such as `const SOMETHING = 32;`
127+
- The team wanted to move forward in other areas (e.g. impl Trait) before moving on to solve this problem.
128+
129+
However, after 7 years it is now time to revisit this topic:
130+
131+
- The things that can be done in a const expression has been greatly expanded since
132+
2017, which means that it is more likely to use a complicated type or an unnameable type on const/static items.
133+
- It is now possible to use impl types in function returns (although having impl type aliases could provide a similar solution for const and statics)
134+
- Const and static aren't necessarily at the top level. It feels weird that the type can be elided on a let statement inside a function, but not on a const or static inside a function
135+
- The original RFC resolution of **postpone** was made at least partially based on
136+
statistics done by @schuster. This RFC was proposed with the motivation of enabling the use
137+
of unnameable types in const/static items. Because this RFC enables new behaviors,
138+
data on current usage isn't very useful for determining how much it might improve language
139+
expressiveness.
140+
141+
117142

118143
# Unresolved questions
119144
[unresolved-questions]: #unresolved-questions

0 commit comments

Comments
 (0)