6
6
# Summary
7
7
[ summary ] : #summary
8
8
9
- Allow ` const impl ` s for trait impls where all method impls are checked as const fn.
9
+ Allow ` impl const Trait ` for trait impls where all method impls are checked as const fn.
10
10
11
11
Make it legal to declare trait bounds on generic parameters of const functions and allow
12
12
the body of the const fn to call methods on the generic parameters that have a ` const ` modifier
@@ -39,7 +39,7 @@ The obligation is passed to the caller of your `triple_add` function to supply a
39
39
40
40
``` rust
41
41
struct MyInt (i8 );
42
- const impl Add for MyInt {
42
+ impl const Add for MyInt {
43
43
fn add (self , other : Self ) -> Self {
44
44
MyInt (self . 0 + other . 0 )
45
45
}
@@ -51,7 +51,7 @@ so in the following `H` is required to have a const impl of `Hasher`, so that
51
51
methods on ` state ` are callable.
52
52
53
53
``` rust
54
- const impl Hash for MyInt {
54
+ impl const Hash for MyInt {
55
55
fn hash <H >(
56
56
& self ,
57
57
state : & mut H ,
@@ -65,11 +65,11 @@ const impl Hash for MyInt {
65
65
66
66
## Drop
67
67
68
- A notable use case of ` const impl` is defining ` Drop ` impls. If you write
68
+ A notable use case of ` impl const ` is defining ` Drop ` impls. If you write
69
69
70
70
``` rust
71
71
struct SomeDropType <'a >(& 'a Cell <u32 >);
72
- const impl Drop for SomeDropType {
72
+ impl const Drop for SomeDropType {
73
73
fn drop (& mut self ) {
74
74
self . 0. set (self . 0. get () - 1 );
75
75
}
@@ -89,7 +89,7 @@ and ensuring that lowering to HIR and MIR keeps track of that.
89
89
The miri engine already fully supports calling methods on generic
90
90
bounds, there's just no way of declaring them. Checking methods for constness is already implemented
91
91
for inherent methods. The implementation will have to extend those checks to also run on methods
92
- of ` const impl` items.
92
+ of ` impl const ` items.
93
93
94
94
# Drawbacks
95
95
[ drawbacks ] : #drawbacks
@@ -150,21 +150,21 @@ It might also be desirable to make the automatic `Fn*` impls on function types a
150
150
This change should probably go in hand with allowing ` const fn ` pointers on const functions
151
151
that support being called (in contrast to regular function pointers).
152
152
153
- ## Deriving ` const impl` s
153
+ ## Deriving ` impl const `
154
154
155
155
``` rust
156
156
#[derive(Clone )]
157
157
pub struct Foo (Bar );
158
158
159
159
struct Bar ;
160
160
161
- const impl Clone for Bar {
161
+ impl const Clone for Bar {
162
162
fn clone (& self ) -> Self { Bar }
163
163
}
164
164
```
165
165
166
166
could theoretically have a scheme inferring ` Foo ` 's ` Clone ` impl to be ` const ` . If some time
167
- later the ` const impl Clone for Bar` (a private type) is changed to just ` impl ` , ` Foo ` 's ` Clone `
167
+ later the ` impl const Clone for Bar ` (a private type) is changed to just ` impl ` , ` Foo ` 's ` Clone `
168
168
impl would suddenly stop being ` const ` , without any visible change to the API. This should not
169
169
be allowed for the same reason as why we're not inferring ` const ` on functions: changes to private
170
170
things should not affect the constness of public things, because that is not compatible with semver.
@@ -177,13 +177,13 @@ pub struct Foo(Bar);
177
177
178
178
struct Bar ;
179
179
180
- const impl Clone for Bar {
180
+ impl const Clone for Bar {
181
181
fn clone (& self ) -> Self { Bar }
182
182
}
183
183
```
184
184
185
- which would generate a ` const impl Clone for Foo` block which would fail to compile if any of ` Foo ` 's
186
- fields (so just ` Bar ` in this example) are not implementing ` Clone ` via ` const impl` . The obligation is
185
+ which would generate a ` impl const Clone for Foo ` block which would fail to compile if any of ` Foo ` 's
186
+ fields (so just ` Bar ` in this example) are not implementing ` Clone ` via ` impl const ` . The obligation is
187
187
now on the crate author to keep the public API semver compatible, but they can't accidentally fail to
188
188
uphold that obligation by changing private things.
189
189
@@ -200,7 +200,7 @@ const context. It seems like a natural extension to this RFC to allow
200
200
const fn foo () -> impl const Bar { /* code here */ }
201
201
```
202
202
203
- which requires that the function only returns types with ` const impl Bar` blocks.
203
+ which requires that the function only returns types with ` impl const Bar ` blocks.
204
204
205
205
## Specialization
206
206
@@ -211,13 +211,13 @@ and `const` modifiers on `impl` blocks.
211
211
# Unresolved questions
212
212
[ unresolved-questions ] : #unresolved-questions
213
213
214
- Should ` const impl` blocks additionally generate impls that are not const if any generic
214
+ Should ` impl const ` blocks additionally generate impls that are not const if any generic
215
215
parameters are not const?
216
216
217
217
E.g.
218
218
219
219
``` rust
220
- const impl <T : Add > Add for Foo <T > {
220
+ impl <T : Add > const Add for Foo <T > {
221
221
fn add (self , other : Self ) -> Self {
222
222
Foo (self . 0 + other . 0 )
223
223
}
@@ -226,21 +226,21 @@ const impl<T: Add> Add for Foo<T> {
226
226
227
227
would allow calling ` Foo(String::new()) + Foo(String::new()) ` even though that is (at the time
228
228
of writing this RFC) most definitely not const, because ` String ` only has an ` impl Add for String `
229
- and not a ` const impl Add for String` .
229
+ and not an ` impl const Add for String ` .
230
230
231
231
This would go in hand with the current scheme for const functions, which may also be called
232
232
at runtime with runtime arguments, but are checked for soundness as if they were called in
233
233
a const context.
234
234
235
- ## Require ` const ` bounds on everything inside a ` const impl` block?
235
+ ## Require ` const ` bounds on everything inside an ` impl const ` block?
236
236
237
- Instead of inferring ` const ` ness on all bounds and functions inside a ` const impl` block,
237
+ Instead of inferring ` const ` ness on all bounds and functions inside a ` impl const ` block,
238
238
we force the user to supply these bounds. This is more consistent with not inferring ` const `
239
239
on ` const ` function argument types and generic bounds. The ` Hash ` example from above would
240
240
then look like
241
241
242
242
``` rust
243
- const impl Hash for MyInt {
243
+ impl const Hash for MyInt {
244
244
const fn hash <H >(
245
245
& self ,
246
246
state : & mut H ,
0 commit comments