Skip to content

Commit c188f7b

Browse files
authored
Merge pull request #2579 from rust-lang/tshepang/overlong
reduce overlong physical lines (sembr)
2 parents 5b848af + f112f64 commit c188f7b

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

src/typing_parameter_envs.md

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
## Typing Environments
44

5-
When interacting with the type system there are a few variables to consider that can affect the results of trait solving. The set of in-scope where clauses, and what phase of the compiler type system operations are being performed in (the [`ParamEnv`][penv] and [`TypingMode`][tmode] structs respectively).
5+
When interacting with the type system there are a few variables to consider that can affect the results of trait solving.
6+
The set of in-scope where clauses, and what phase of the compiler type system operations are being performed in (the [`ParamEnv`][penv] and [`TypingMode`][tmode] structs respectively).
67

7-
When an environment to perform type system operations in has not yet been created, the [`TypingEnv`][tenv] can be used to bundle all of the external context required into a single type.
8+
When an environment to perform type system operations in has not yet been created,
9+
the [`TypingEnv`][tenv] can be used to bundle all of the external context required into a single type.
810

9-
Once a context to perform type system operations in has been created (e.g. an [`ObligationCtxt`][ocx] or [`FnCtxt`][fnctxt]) a `TypingEnv` is typically not stored anywhere as only the `TypingMode` is a property of the whole environment, whereas different `ParamEnv`s can be used on a per-goal basis.
11+
Once a context to perform type system operations in has been created (e.g. an [`ObligationCtxt`][ocx] or [`FnCtxt`][fnctxt]) a `TypingEnv` is typically not stored anywhere as only the `TypingMode` is a property of the whole environment,
12+
whereas different `ParamEnv`s can be used on a per-goal basis.
1013

1114
[ocx]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/traits/struct.ObligationCtxt.html
1215
[fnctxt]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/fn_ctxt/struct.FnCtxt.html
@@ -15,9 +18,14 @@ Once a context to perform type system operations in has been created (e.g. an [`
1518

1619
### What is a `ParamEnv`
1720

18-
The [`ParamEnv`][penv] is a list of in-scope where-clauses, it typically corresponds to a specific item's where clauses. Some clauses are not explicitly written but are instead implicitly added in the [`predicates_of`][predicates_of] query, such as `ConstArgHasType` or (some) implied bounds.
21+
The [`ParamEnv`][penv] is a list of in-scope where-clauses,
22+
it typically corresponds to a specific item's where clauses.
23+
Some clauses are not explicitly written but are instead implicitly added in the [`predicates_of`][predicates_of] query,
24+
such as `ConstArgHasType` or (some) implied bounds.
1925

20-
In most cases `ParamEnv`s are initially created via the [`param_env` query][query] which returns a `ParamEnv` derived from the provided item's where clauses. A `ParamEnv` can also be created with arbitrary sets of clauses that are not derived from a specific item, such as in [`compare_method_predicate_entailment`][method_pred_entailment] where we create a hybrid `ParamEnv` consisting of the impl's where clauses and the trait definition's function's where clauses.
26+
In most cases `ParamEnv`s are initially created via the [`param_env` query][query] which returns a `ParamEnv` derived from the provided item's where clauses.
27+
A `ParamEnv` can also be created with arbitrary sets of clauses that are not derived from a specific item,
28+
such as in [`compare_method_predicate_entailment`][method_pred_entailment] where we create a hybrid `ParamEnv` consisting of the impl's where clauses and the trait definition's function's where clauses.
2129

2230
---
2331

@@ -30,7 +38,9 @@ where
3038
<T as Trait>::Assoc: Clone,
3139
{}
3240
```
33-
If we were conceptually inside of `foo` (for example, type-checking or linting it) we would use this `ParamEnv` everywhere that we interact with the type system. This would allow things such as [normalization], evaluating generic constants, and proving where clauses/goals, to rely on `T` being sized, implementing `Trait`, etc.
41+
If we were conceptually inside of `foo` (for example, type-checking or linting it) we would use this `ParamEnv` everywhere that we interact with the type system.
42+
This would allow things such as [normalization], evaluating generic constants,
43+
and proving where clauses/goals, to rely on `T` being sized, implementing `Trait`, etc.
3444

3545
A more concrete example:
3646
```rust
@@ -72,9 +82,13 @@ fn foo2<T>(a: T) {
7282

7383
### Acquiring a `ParamEnv`
7484

75-
Using the wrong [`ParamEnv`][penv] when interacting with the type system can lead to ICEs, illformed programs compiling, or erroring when we shouldn't. See [#82159](https://github.com/rust-lang/rust/pull/82159) and [#82067](https://github.com/rust-lang/rust/pull/82067) as examples of PRs that modified the compiler to use the correct param env and in the process fixed ICEs.
85+
Using the wrong [`ParamEnv`][penv] when interacting with the type system can lead to ICEs,
86+
illformed programs compiling, or erroring when we shouldn't.
87+
See [#82159](https://github.com/rust-lang/rust/pull/82159) and [#82067](https://github.com/rust-lang/rust/pull/82067) as examples of PRs that modified the compiler to use the correct param env and in the process fixed ICEs.
7688

77-
In the large majority of cases, when a `ParamEnv` is required it either already exists somewhere in scope, or above in the call stack and should be passed down. A non exhaustive list of places where you might find an existing `ParamEnv`:
89+
In the large majority of cases, when a `ParamEnv` is required it either already exists somewhere in scope,
90+
or above in the call stack and should be passed down.
91+
A non exhaustive list of places where you might find an existing `ParamEnv`:
7892
- During typeck `FnCtxt` has a [`param_env` field][fnctxt_param_env]
7993
- When writing late lints the `LateContext` has a [`param_env` field][latectxt_param_env]
8094
- During well formedness checking the `WfCheckingCtxt` has a [`param_env` field][wfckctxt_param_env]
@@ -84,14 +98,18 @@ In the large majority of cases, when a `ParamEnv` is required it either already
8498

8599
If you aren't sure if there's a `ParamEnv` in scope somewhere that can be used it can be worth opening a thread in the [`#t-compiler/help`][compiler_help] Zulip channel where someone may be able to point out where a `ParamEnv` can be acquired from.
86100

87-
Manually constructing a `ParamEnv` is typically only needed at the start of some kind of top level analysis (e.g. hir typeck or borrow checking). In such cases there are three ways it can be done:
101+
Manually constructing a `ParamEnv` is typically only needed at the start of some kind of top level analysis (e.g. hir typeck or borrow checking).
102+
In such cases there are three ways it can be done:
88103
- Calling the [`tcx.param_env(def_id)` query][param_env_query] which returns the environment associated with a given definition.
89104
- Creating an empty environment with [`ParamEnv::empty`][env_empty].
90-
- Using [`ParamEnv::new`][param_env_new] to construct an env with an arbitrary set of where clauses. Then calling [`traits::normalize_param_env_or_error`][normalize_env_or_error] to handle normalizing and elaborating all the where clauses in the env.
105+
- Using [`ParamEnv::new`][param_env_new] to construct an env with an arbitrary set of where clauses.
106+
Then calling [`traits::normalize_param_env_or_error`][normalize_env_or_error] to handle normalizing and elaborating all the where clauses in the env.
91107

92108
Using the `param_env` query is by far the most common way to construct a `ParamEnv` as most of the time the compiler is performing an analysis as part of some specific definition.
93109

94-
Creating an empty environment with `ParamEnv::empty` is typically only done either in codegen (indirectly via [`TypingEnv::fully_monomorphized`][tenv_mono]), or as part of some analysis that do not expect to ever encounter generic parameters (e.g. various parts of coherence/orphan check).
110+
Creating an empty environment with `ParamEnv::empty` is typically only done either in codegen (indirectly via [`TypingEnv::fully_monomorphized`][tenv_mono]),
111+
or as part of some analysis that do not expect to ever encounter generic parameters
112+
(e.g. various parts of coherence/orphan check).
95113

96114
Creating an env from an arbitrary set of where clauses is usually unnecessary and should only be done if the environment you need does not correspond to an actual item in the source code (e.g. [`compare_method_predicate_entailment`][method_pred_entailment]).
97115

@@ -113,11 +131,14 @@ Creating an env from an arbitrary set of where clauses is usually unnecessary an
113131

114132
### How are `ParamEnv`s constructed
115133

116-
Creating a [`ParamEnv`][pe] is more complicated than simply using the list of where clauses defined on an item as written by the user. We need to both elaborate supertraits into the env and fully normalize all aliases. This logic is handled by [`traits::normalize_param_env_or_error`][normalize_env_or_error] (even though it does not mention anything about elaboration).
134+
Creating a [`ParamEnv`][pe] is more complicated than simply using the list of where clauses defined on an item as written by the user.
135+
We need to both elaborate supertraits into the env and fully normalize all aliases.
136+
This logic is handled by [`traits::normalize_param_env_or_error`][normalize_env_or_error] (even though it does not mention anything about elaboration).
117137

118138
#### Elaborating supertraits
119139

120-
When we have a function such as `fn foo<T: Copy>()` we would like to be able to prove `T: Clone` inside of the function as the `Copy` trait has a `Clone` supertrait. Constructing a `ParamEnv` looks at all of the trait bounds in the env and explicitly adds new where clauses to the `ParamEnv` for any supertraits found on the traits.
140+
When we have a function such as `fn foo<T: Copy>()` we would like to be able to prove `T: Clone` inside of the function as the `Copy` trait has a `Clone` supertrait.
141+
Constructing a `ParamEnv` looks at all of the trait bounds in the env and explicitly adds new where clauses to the `ParamEnv` for any supertraits found on the traits.
121142

122143
A concrete example would be the following function:
123144
```rust
@@ -133,13 +154,16 @@ fn bar<T: Copy + Trait>(a: T) {
133154
fn requires_impl<T: Clone + SuperSuperTrait>(a: T) {}
134155
```
135156

136-
If we did not elaborate the env then the `requires_impl` call would fail to typecheck as we would not be able to prove `T: Clone` or `T: SuperSuperTrait`. In practice we elaborate the env which means that `bar`'s `ParamEnv` is actually:
157+
If we did not elaborate the env then the `requires_impl` call would fail to typecheck as we would not be able to prove `T: Clone` or `T: SuperSuperTrait`.
158+
In practice we elaborate the env which means that `bar`'s `ParamEnv` is actually:
137159
`[T: Sized, T: Copy, T: Clone, T: Trait, T: SuperTrait, T: SuperSuperTrait]`
138160
This allows us to prove `T: Clone` and `T: SuperSuperTrait` when type checking `bar`.
139161

140162
The `Clone` trait has a `Sized` supertrait however we do not end up with two `T: Sized` bounds in the env (one for the supertrait and one for the implicitly added `T: Sized` bound) as the elaboration process (implemented via [`util::elaborate`][elaborate]) deduplicates where clauses.
141163

142-
A side effect of this is that even if no actual elaboration of supertraits takes place, the existing where clauses in the env are _also_ deduplicated. See the following example:
164+
A side effect of this is that even if no actual elaboration of supertraits takes place,
165+
the existing where clauses in the env are _also_ deduplicated.
166+
See the following example:
143167
```rust
144168
trait Trait {}
145169
// The unelaborated `ParamEnv` would be:
@@ -156,7 +180,8 @@ The [next-gen trait solver][next-gen-solver] also requires this elaboration to t
156180

157181
#### Normalizing all bounds
158182

159-
In the old trait solver the where clauses stored in `ParamEnv` are required to be fully normalized as otherwise the trait solver will not function correctly. A concrete example of needing to normalize the `ParamEnv` is the following:
183+
In the old trait solver the where clauses stored in `ParamEnv` are required to be fully normalized as otherwise the trait solver will not function correctly.
184+
A concrete example of needing to normalize the `ParamEnv` is the following:
160185
```rust
161186
trait Trait<T> {
162187
type Assoc;
@@ -182,11 +207,14 @@ where
182207
fn requires_impl<U: Trait<u32>>(_: U) {}
183208
```
184209

185-
As humans we can tell that `<T as Other>::Bar` is equal to `u32` so the trait bound on `U` is equivalent to `U: Trait<u32>`. In practice trying to prove `U: Trait<u32>` in the old solver in this environment would fail as it is unable to determine that `<T as Other>::Bar` is equal to `u32`.
210+
As humans we can tell that `<T as Other>::Bar` is equal to `u32` so the trait bound on `U` is equivalent to `U: Trait<u32>`.
211+
In practice trying to prove `U: Trait<u32>` in the old solver in this environment would fail as it is unable to determine that `<T as Other>::Bar` is equal to `u32`.
186212

187213
To work around this we normalize `ParamEnv`'s after constructing them so that `foo`'s `ParamEnv` is actually: `[T: Sized, U: Sized, U: Trait<u32>]` which means the trait solver is now able to use the `U: Trait<u32>` in the `ParamEnv` to determine that the trait bound `U: Trait<u32>` holds.
188214

189-
This workaround does not work in all cases as normalizing associated types requires a `ParamEnv` which introduces a bootstrapping problem. We need a normalized `ParamEnv` in order for normalization to give correct results, but we need to normalize to get that `ParamEnv`. Currently we normalize the `ParamEnv` once using the unnormalized param env and it tends to give okay results in practice even though there are some examples where this breaks ([example]).
215+
This workaround does not work in all cases as normalizing associated types requires a `ParamEnv` which introduces a bootstrapping problem.
216+
We need a normalized `ParamEnv` in order for normalization to give correct results, but we need to normalize to get that `ParamEnv`.
217+
Currently we normalize the `ParamEnv` once using the unnormalized param env and it tends to give okay results in practice even though there are some examples where this breaks ([example]).
190218

191219
In the next-gen trait solver the requirement for all where clauses in the `ParamEnv` to be fully normalized is not present and so we do not normalize when constructing `ParamEnv`s.
192220

@@ -196,9 +224,12 @@ In the next-gen trait solver the requirement for all where clauses in the `Param
196224

197225
## Typing Modes
198226

199-
Depending on what context we are performing type system operations in, different behaviour may be required. For example during coherence there are stronger requirements about when we can consider goals to not hold or when we can consider types to be unequal.
227+
Depending on what context we are performing type system operations in,
228+
different behaviour may be required.
229+
For example during coherence there are stronger requirements about when we can consider goals to not hold or when we can consider types to be unequal.
200230

201-
Tracking which "phase" of the compiler type system operations are being performed in is done by the [`TypingMode`][tmode] enum. The documentation on the `TypingMode` enum is quite good so instead of repeating it here verbatim we would recommend reading the API documentation directly.
231+
Tracking which "phase" of the compiler type system operations are being performed in is done by the [`TypingMode`][tmode] enum.
232+
The documentation on the `TypingMode` enum is quite good so instead of repeating it here verbatim we would recommend reading the API documentation directly.
202233

203234
[penv]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamEnv.html
204235
[tenv]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypingEnv.html

0 commit comments

Comments
 (0)