Skip to content

Commit fa53bf8

Browse files
authored
Merge pull request #2700 from bstrie/intconst
Deprecate stdlib modules dedicated to numeric constants and move those constants to associated consts
2 parents 224bd17 + 2e1ed05 commit fa53bf8

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
- Feature Name: `assoc_int_consts`
2+
- Start Date: 2019-05-13
3+
- RFC PR: [rust-lang/rfcs#2700](https://github.com/rust-lang/rfcs/pull/2700)
4+
- Rust Issue: [rust-lang/rust#68490](https://github.com/rust-lang/rust/issues/68490)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Add the relevant associated constants to the numeric types in the standard library, and consider a
10+
timeline for the deprecation of the corresponding (and originally intended to be temporary)
11+
primitive numeric modules and associated functions.
12+
13+
# Motivation
14+
[motivation]: #motivation
15+
16+
All programming languages with bounded integers provide numeric constants for their maximum and
17+
minimum extents. In Rust, [these constants were
18+
stabilized](https://github.com/rust-lang/rust/pull/23549) in the eleventh hour before Rust 1.0
19+
(literally the day before the branching of 1.0-beta on April 1, 2015), with some
20+
known-to-be-undesirable properties. In particular, associated consts were yet to be implemented
21+
(these landed, amusingly, one month after 1.0-beta and two weeks before 1.0-stable), and so each of
22+
the twelve numeric types were given their own top-level modules in the standard library, whose
23+
contents are exclusively these constants (all related non-constants being defined in inherent impls
24+
directly on each type). However, in the even-eleventh-er hour before 1.0-beta, it was realized that
25+
this solution did not work for anyone seeking to reference these constants when working with types
26+
such as `c_int`, which are defined as type aliases and can thus access inherent impls but not
27+
modules that merely happen to be named the same as the original type; as a result, [an emergency
28+
PR](https://github.com/rust-lang/rust/pull/23947) also added redundant `max_value` and `min_value`
29+
inherent functions as a last-second workaround. The PR itself notes how distasteful this remedy is:
30+
31+
> It's unfortunate to freeze these as methods, but when we can provide inherent associated constants
32+
> these methods can be deprecated. [aturon, Apr 1, 2015]
33+
34+
Meanwhile, the author of the associated consts patch
35+
[despairs](https://github.com/rust-lang/rust/pull/23606#issuecomment-88541583) of just barely
36+
missing the deadline:
37+
38+
> @nikomatsakis The original motivation for trying to get this in before the beta was to get rid of
39+
> all the functions that deal with constants in Int/Float, and then to get rid of all the modules
40+
> like std::i64 that just hold constants as well. We could have dodged most of the issues (ICEs and
41+
> generic code design) by using inherent impls instead of associating the constants with traits. But
42+
> since [#23549](https://github.com/rust-lang/rust/pull/23549) came in a bit earlier and stabilized
43+
> a bunch more of those constants before the beta, whereas this hasn't landed yet, blegh.
44+
> [quantheory, Apr 1, 2015]
45+
46+
Anticipating the situation, an [issue](https://github.com/rust-lang/rfcs/issues/1099) was filed in
47+
the RFCs repo regarding moving the contents of these modules into associated consts:
48+
49+
> I think it's a minor enough breaking change to move the constants and deprecate the modules u8,
50+
> u16, etc. Not so sure about removing these modules entirely, I'd appreciate that, but it'll break
51+
> all the code use-ing them. [petrochenkov, Apr 29, 2015]
52+
53+
Finally, so obvious was this solution that [the original RFC for associated
54+
items](https://github.com/nox/rust-rfcs/blob/master/text/0195-associated-items.md#expressiveness)
55+
used the numeric constants as the only motivating example for the feature of associated consts:
56+
57+
> For example, today's Rust includes a variety of numeric traits, including Float, which must
58+
> currently expose constants as static functions [...] Associated constants would allow the consts
59+
> to live directly on the traits
60+
61+
Despite the obvious intent, 1.0 came and went and there were plenty of other things to occupy
62+
everyone's attention. Now, two days shy of Rust's fourth anniversary, let's re-examine the
63+
situation. We propose to deprecate all of the aforementioned functions and constants in favor of
64+
associated constants defined on the appropriate types, and to additionally deprecate all constants
65+
living directly in the `i8`, `i16`, `i32`, `i64`, `i128`, `isize`, `u8`, `u16`, `u32`, `u64`,
66+
`u128`, `usize`, `f32` and `f64` modules in `std`. Advantages of this:
67+
68+
1. Consistency with the rest of the language. As demonstrated by the above quotes, associated consts
69+
have been the natural way to express these concepts in Rust since before associated consts were even
70+
implemented; this approach satisfies the principle of least surprise.
71+
72+
2. Documentation. On the front page of the [standard library API
73+
docs](https://doc.rust-lang.org/std/index.html), 12 of the 60 modules in the standard library (20%)
74+
are the aforementioned numeric modules which exist only to namespace two constants each. This
75+
number will increase as new numeric primitives are added to Rust, as already seen with
76+
`i128` and `u128`. Although deprecated modules cannot be easily removed from std, they can be
77+
removed from the documentation, making the stdlib API docs less cluttered and easier to navigate.
78+
79+
3. Beginner ease. For a beginner, finding two identical ways to achieve something immediately raises
80+
the question of "why", to which the answer here is ultimately uninteresting (and mildly
81+
embarrassing). Even then the question of "which one to use" remains unanswered; neither current
82+
approach is more idiomatic than the other. As noted, deprecated items can be removed from the
83+
documentation, thereby decreasing the likelihood of head-scratching and incredulous sidelong
84+
glances from people new to Rust.
85+
86+
4. Removal of ambiguity between primitive types and their identically-named modules. Currently
87+
if you import an integer module and access constants in the module and methods on the type,
88+
one has no apparent indication as to what comes from where:
89+
```rust
90+
use std::u32;
91+
assert_eq!(u32::MAX, u32::max_value());
92+
```
93+
The fact that this sort of shadowing of primitive types works in the first place is surprising
94+
even to experienced Rust programmers; the fact that such a pattern is seemingly encouraged by
95+
the standard library is even more of a surprise. By making this change we would be able to
96+
remove all modules in the standard library whose names shadow integral types.
97+
98+
5. Removal of a frustrating papercut. Even experienced Rust programmers are prone to trip over
99+
this and curse at having to be reminded of a bizarre and jarring artifact of Rust 1.0.
100+
By removing these artifacts we can make the experience of using Rust more universally pleasant.
101+
102+
# Guide-level explanation
103+
[guide-level-explanation]: #guide-level-explanation
104+
105+
1. Add the following associated constants to the relevant types in standard library, with their definitions taken from the corresponding legacy module-level constants:
106+
- i8::{MAX, MIN}
107+
- i16::{MAX, MIN}
108+
- i32::{MAX, MIN}
109+
- i64::{MAX, MIN}
110+
- i128::{MAX, MIN}
111+
- isize::{MAX, MIN}
112+
- u8::{MAX, MIN}
113+
- u16::{MAX, MIN}
114+
- u32::{MAX, MIN}
115+
- u64::{MAX, MIN}
116+
- u128::{MAX, MIN}
117+
- usize::{MAX, MIN}
118+
- f32::{DIGITS, EPSILON, INFINITY, MANTISSA_DIGITS, MAX, MAX_10_EXP, MAX_EXP, MIN, MIN_10_EXP, MIN_EXP, MIN_POSITIVE, NAN, NEG_INFINITY, RADIX}
119+
- f64::{DIGITS, EPSILON, INFINITY, MANTISSA_DIGITS, MAX, MAX_10_EXP, MAX_EXP, MIN, MIN_10_EXP, MIN_EXP, MIN_POSITIVE, NAN, NEG_INFINITY, RADIX}
120+
121+
2. Redefine the following module-level constants in terms of the associated constants added in step 1:
122+
- std::i8::{[MIN](https://doc.rust-lang.org/std/i8/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/i8/constant.MIN.html)}
123+
- std::i16::{[MIN](https://doc.rust-lang.org/std/i16/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/i16/constant.MIN.html)}
124+
- std::i32::{[MIN](https://doc.rust-lang.org/std/i32/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/i32/constant.MIN.html)}
125+
- std::i64::{[MIN](https://doc.rust-lang.org/std/i64/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/i64/constant.MIN.html)}
126+
- std::i128::{[MIN](https://doc.rust-lang.org/std/i128/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/i128/constant.MIN.html)}
127+
- std::isize::{[MIN](https://doc.rust-lang.org/std/isize/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/isize/constant.MIN.html)}
128+
- std::u8::{[MIN](https://doc.rust-lang.org/std/u8/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/u8/constant.MIN.html)}
129+
- std::u16::{[MIN](https://doc.rust-lang.org/std/u16/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/u16/constant.MIN.html)}
130+
- std::u32::{[MIN](https://doc.rust-lang.org/std/u32/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/u32/constant.MIN.html)}
131+
- std::u64::{[MIN](https://doc.rust-lang.org/std/u64/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/u64/constant.MIN.html)}
132+
- std::u128::{[MIN](https://doc.rust-lang.org/std/u128/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/u128/constant.MIN.html)}
133+
- std::usize::{[MIN](https://doc.rust-lang.org/std/usize/constant.MIN.html), [MAX](https://doc.rust-lang.org/std/usize/constant.MIN.html)}
134+
- std::f32::{[DIGITS](https://doc.rust-lang.org/std/f32/constant.DIGITS.html), [EPSILON](https://doc.rust-lang.org/std/f32/constant.EPSILON.html), [INFINITY](https://doc.rust-lang.org/std/f32/constant.INFINITY.html), [MANTISSA_DIGITS](https://doc.rust-lang.org/std/f32/constant.MANTISSA_DIGITS.html), [MAX](https://doc.rust-lang.org/std/f32/constant.MAX.html), [MAX_10_EXP](https://doc.rust-lang.org/std/f32/constant.MAX_10_EXP.html), [MAX_EXP](https://doc.rust-lang.org/std/f32/constant.MAX_EXP.html), [MIN](https://doc.rust-lang.org/std/f32/constant.MIN.html), [MIN_10_EXP](https://doc.rust-lang.org/std/f32/constant.MIN_10_EXP.html), [MIN_EXP](https://doc.rust-lang.org/std/f32/constant.MIN_EXP.html), [MIN_POSITIVE](https://doc.rust-lang.org/std/f32/constant.MIN_POSITIVE.html), [NAN](https://doc.rust-lang.org/std/f32/constant.NAN.html), [NEG_INFINITY](https://doc.rust-lang.org/std/f32/constant.NEG_INFINITY.html), [RADIX](https://doc.rust-lang.org/std/f32/constant.RADIX.html)}
135+
- std::f64::{[DIGITS](https://doc.rust-lang.org/std/f64/constant.DIGITS.html), [EPSILON](https://doc.rust-lang.org/std/f64/constant.EPSILON.html), [INFINITY](https://doc.rust-lang.org/std/f64/constant.INFINITY.html), [MANTISSA_DIGITS](https://doc.rust-lang.org/std/f64/constant.MANTISSA_DIGITS.html), [MAX](https://doc.rust-lang.org/std/f64/constant.MAX.html), [MAX_10_EXP](https://doc.rust-lang.org/std/f64/constant.MAX_10_EXP.html), [MAX_EXP](https://doc.rust-lang.org/std/f64/constant.MAX_EXP.html), [MIN](https://doc.rust-lang.org/std/f64/constant.MIN.html), [MIN_10_EXP](https://doc.rust-lang.org/std/f64/constant.MIN_10_EXP.html), [MIN_EXP](https://doc.rust-lang.org/std/f64/constant.MIN_EXP.html), [MIN_POSITIVE](https://doc.rust-lang.org/std/f64/constant.MIN_POSITIVE.html), [NAN](https://doc.rust-lang.org/std/f64/constant.NAN.html), [NEG_INFINITY](https://doc.rust-lang.org/std/f64/constant.NEG_INFINITY.html), [RADIX](https://doc.rust-lang.org/std/f64/constant.RADIX.html)}
136+
137+
3. At a future point to be determined (see "Unresolved questions" below), deprecate the items listed in step 2. Additionally, deprecate the following associated functions:
138+
- i8::{[min_value](https://doc.rust-lang.org/std/primitive.i8.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.i8.html#method.max_value)}
139+
- i16::{[min_value](https://doc.rust-lang.org/std/primitive.i16.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.i16.html#method.max_value)}
140+
- i32::{[min_value](https://doc.rust-lang.org/std/primitive.i32.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.i32.html#method.max_value)}
141+
- i64::{[min_value](https://doc.rust-lang.org/std/primitive.i64.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.i64.html#method.max_value)}
142+
- i128::{[min_value](https://doc.rust-lang.org/std/primitive.i128.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.i128.html#method.max_value)}
143+
- isize::{[min_value](https://doc.rust-lang.org/std/primitive.isize.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.isize.html#method.max_value)}
144+
- u8::{[min_value](https://doc.rust-lang.org/std/primitive.u8.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.u8.html#method.max_value)}
145+
- u16::{[min_value](https://doc.rust-lang.org/std/primitive.u16.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.u16.html#method.max_value)}
146+
- u32::{[min_value](https://doc.rust-lang.org/std/primitive.u32.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.u32.html#method.max_value)}
147+
- u64::{[min_value](https://doc.rust-lang.org/std/primitive.u64.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.u64.html#method.max_value)}
148+
- u128::{[min_value](https://doc.rust-lang.org/std/primitive.u128.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.u128.html#method.max_value)}
149+
- usize::{[min_value](https://doc.rust-lang.org/std/primitive.usize.html#method.min_value), [max_value](https://doc.rust-lang.org/std/primitive.usize.html#method.max_value)}
150+
151+
4. Following step 3, the following modules will be made hidden from the front page of the stdlib documentation, as they no longer contain any non-deprecated items: `std::{i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize}` (note that this does not apply to either of `std::{f32, f64}`; see the Alternatives section below)
152+
153+
# Drawbacks
154+
[drawbacks]: #drawbacks
155+
156+
1. Deprecation warnings, although these can be easily addressed.
157+
2. Because associated items cannot be directly imported, code of the form `use std::i32::MAX; foo(MAX, MAX);`
158+
will most likely be changed to `foo(i32::MAX, i32::MAX)`, which may be marginally more verbose.
159+
However, given how many `MAX` and `MIN` constants there are in the stdlib,
160+
it is easy to argue that such unprefixed constants in the wild would be confusing,
161+
and ought to be avoided in the first place. In any case, users desperate for such behavior
162+
will be trivially capable of replacing `use std::i32::MAX;` with `const MAX: i32 = i32::MAX;`.
163+
164+
# Unresolved questions
165+
166+
How long should we go before issuing a deprecation warning? At the extreme end of the scale we could wait until the next edition of Rust is released, and have the legacy items only issue deprecation warnings when opting in to the new edition; this would limit disruption only to people opting in to a new edition (and, being merely an trivially-addressed deprecation, would constitute far less of a disruption than any ordinary edition-related change; any impact of the deprecation would be mere noise in light of the broader edition-related impacts). However long it takes, it is the opinion of the author that deprecation should happen *eventually*, as we should not give the impression that it is the ideal state of things that there should exist three ways of finding the maximum value of an integer type; we expect experienced users to intuitively reach for the new way proposed in this RFC as the "natural" way these constants ought to be implemented, but for the sake of new users it would be a pedagogical wart to allow all three to exist without explicitly calling out the preferred one.
167+
168+
# Alternatives
169+
[alternatives]: #alternatives
170+
171+
- Unlike the twelve integral modules, the two floating-point modules would not themselves be
172+
entirely deprecated by the changes proposed here. This is because the `std::f32` and `std::f64`
173+
modules each contain a `consts` submodule, in which reside constants of a more mathematical bent
174+
(the sort of things other languages might put in a `std::math` module).
175+
It is the author's opinion that special treatment for such "math-oriented constants" (as opposed to
176+
the "machine-oriented constants" addressed by this RFC) is not particularly precedented; e.g. this
177+
separation is not consistent with the existing set of associated functions implemented on `f32`
178+
and `f64`, which consist of a mix of both functions concerned with mathematical operations
179+
(e.g. `f32::atanh`) and functions concerned with machine representation (e.g.
180+
`f32::is_sign_negative`). However, although earlier versions of this RFC proposed deprecating
181+
`std::{f32, f64}::consts` (and thereby `std::{f32, f64}` as well), the current version does not do
182+
so, as this was met with mild resistance (and, in any case, the greatest gains from this RFC will
183+
be its impact on the integral modules).
184+
Ultimately, there is no reason that such a change could not be left to a future RFC if desired.
185+
However, one alternative design would be to turn all the constants in `{f32, f64}` into associated
186+
consts as well, which would leave no more modules in the standard library that shadow primitive
187+
types. A different alternative would be to restrict this RFC only to the integral modules, leaving
188+
f32 and f64 for a future RFC, since the integral modules are the most important aspect of this
189+
RFC and it would be a shame for them to get bogged down by the unrelated concerns of the
190+
floating-point modules.
191+
192+
- Rather than immediately deprecating the existing items in the standard library, we could add
193+
the new associated consts without any corresponding deprecations. The downside of this idea is
194+
that we now have *three* ways of doing the exact same thing, and without deprecation warnings
195+
(and their associated notes) there is little enough to guide users as to which is solution
196+
is the idiomatic one. It is the author's opinion that there is no downside to deprecation
197+
warnings in this case, especially since mitigation of the warning is trivial (as discussed in
198+
the Drawbacks section above).

0 commit comments

Comments
 (0)