Skip to content

Commit e8cd1d9

Browse files
committed
Reorganise into subchapters and explain Reveal
1 parent 01810f4 commit e8cd1d9

6 files changed

+207
-161
lines changed

src/SUMMARY.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@
118118
- [Generic arguments](./generic_arguments.md)
119119
- [Constants in the type system](./constants.md)
120120
- [Bound vars and Parameters](./bound-vars-and-params.md)
121-
- [Parameter Environments](./param_env.md)
121+
- [Parameter Environments](./param_env/param_env_summary.md)
122+
- [What is it?](./param_env/param_env_what_is_it.md)
123+
- [How are `ParamEnv`'s constructed internally](./param_env/param_env_construction_internals.md)
124+
- [Which `ParamEnv` do I use?](./param_env/param_env_acquisition.md)
122125
- [Type inference](./type-inference.md)
123126
- [Trait solving](./traits/resolution.md)
124127
- [Early and Late Bound Parameter Definitions](./early-late-bound-summary.md)

src/param_env.md

-160
This file was deleted.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
# Which `ParamEnv` do I use?
3+
4+
When needing a [`ParamEnv`][pe] in the compiler there are a few options for obtaining one:
5+
- The correct env is already in scope simply use it (or pass it down the call stack to where you are).
6+
- The [`tcx.param_env(def_id)` query][param_env_query]
7+
- Use [`ParamEnv::new`][param_env_new] to construct an env with an arbitrary set of where clauses. Then call `traits::normalize_param_env_or_error` which will handle normalizing and elaborating all the where clauses in the env for you.
8+
- Creating an empty environment via [`ParamEnv::reveal_all`][env_reveal_all] or [`ParamEnv::empty`][env_empty]
9+
10+
In the large majority of cases a `ParamEnv` when required 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`:
11+
- During typeck `FnCtxt` has a [`param_env` field][fnctxt_param_env]
12+
- When writing late lints the `LateContext` has a [`param_env` field][latectxt_param_env]
13+
- During well formedness checking the `WfCheckingCtxt` has a [`param_env` field][wfckctxt_param_env]
14+
- The `TypeChecker` used by Mir Typeck has a [`param_env` field][mirtypeck_param_env]
15+
- In the next-gen trait solver all `Goal`s have a [`param_env` field][goal_param_env] specifying what environment to prove the goal in
16+
- When editing an existing [`TypeRelation`][typerelation] if it implements `ObligationEmittingRelation` then a [`param_env` method][typerelation_param_env] will be available.
17+
18+
Using the `param_env` query to obtain an env is generally done at the start of some kind of analysis and then passed everywhere that a `ParamEnv` is required. For example the type checker will create a `ParamEnv` for the item it is type checking and then pass it around everywhere.
19+
20+
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 (i.e. [`compare_method_predicate_entailment`][method_pred_entailment] as mentioned earlier).
21+
22+
Creating an empty environment via `ParamEnv::empty` is almost always wrong. There are very few places where we actually know that the environment should be empty. One of the only places where we do actually know this is after monomorphization, however the `ParamEnv` there should be constructed via `ParamEnv::reveal_all` instead as at this point we should be able to determine the hidden type of opaque types. Codegen/Post-mono is one of the only places that should be using `ParamEnv::reveal_all`.
23+
24+
An additional piece of complexity here is specifying the [`Reveal`][reveal] (see linked docs for explanation of what reveal does) used for the `ParamEnv`. When constructing a param env using the `param_env` query it will have `Reveal::UserFacing`, if `Reveal::All` is desired then the [`tcx.param_env_reveal_all_normalized`][env_reveal_all_normalized] query can be used instead.
25+
26+
The `ParamEnv` type has a method [`ParamEnv::with_reveal_all_normalized`][with_reveal_all] which converts an existing `ParamEnv` into one with `Reveal::All` specified. Where possible the previously mentioned query should be preferred as it is more efficient.
27+
28+
[param_env_new]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamEnv.html#method.new
29+
[normalize_env_or_error]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/traits/fn.normalize_param_env_or_error.html
30+
[fnctxt_param_env]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_typeck/struct.FnCtxt.html#structfield.param_env
31+
[latectxt_param_env]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/context/struct.LateContext.html#structfield.param_env
32+
[wfckctxt_param_env]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir_analysis/check/wfcheck/struct.WfCheckingCtxt.html#structfield.param_env
33+
[goal_param_env]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_trait_selection/traits/solve/struct.Goal.html#structfield.param_env
34+
[typerelation_param_env]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/infer/relate/combine/trait.ObligationEmittingRelation.html#tymethod.param_env
35+
[typerelation]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/relate/trait.TypeRelation.html
36+
[mirtypeck_param_env]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_borrowck/type_check/struct.TypeChecker.html#structfield.param_env
37+
[env_reveal_all_normalized]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.param_env_reveal_all_normalized
38+
[with_reveal_all]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamEnv.html#method.with_reveal_all_normalized
39+
[env_reveal_all]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamEnv.html#method.reveal_all
40+
[env_empty]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamEnv.html#method.empty
41+
[reveal]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/traits/enum.Reveal.html
42+
[pe]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamEnv.html
43+
[param_env_query]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/context/struct.TyCtxt.html#method.param_env
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
# How are `ParamEnv`'s constructed internally?
3+
4+
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).
5+
6+
## Elaborating supertraits
7+
8+
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.
9+
10+
A concrete example would be the following function:
11+
```rust
12+
trait Trait: SuperTrait {}
13+
trait SuperTrait: SuperSuperTrait {}
14+
15+
// `bar`'s unelaborated `ParamEnv` would be:
16+
// `[T: Sized, T: Copy, T: Trait]`
17+
fn bar<T: Copy + Trait>(a: T) {
18+
requires_impl(a);
19+
}
20+
21+
fn requires_impl<T: Clone + SuperSuperTrait>(a: T) {}
22+
```
23+
24+
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:
25+
`[T: Sized, T: Copy, T: Clone, T: Trait, T: SuperTrait, T: SuperSuperTrait]`
26+
This allows us to prove `T: Clone` and `T: SuperSuperTrait` when type checking `bar`.
27+
28+
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). This is because the elaboration process (implemented via [`util::elaborate`][elaborate]) deduplicates the where clauses to avoid this.
29+
30+
As a side effect this also means that even if no actual elaboration of supertraits takes place, the existing where clauses in the env are _also_ deduplicated. See the following example:
31+
```rust
32+
trait Trait {}
33+
// The unelaborated `ParamEnv` would be:
34+
// `[T: Sized, T: Trait, T: Trait]`
35+
// but after elaboration it would be:
36+
// `[T: Sized, T: Trait]`
37+
fn foo<T: Trait + Trait>() {}
38+
```
39+
40+
The [next-gen trait solver][next-gen-solver] also requires this elaboration to take place.
41+
42+
[elaborate]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_infer/traits/util/fn.elaborate.html
43+
[next-gen-solver]: https://rustc-dev-guide.rust-lang.org/solve/trait-solving.html
44+
45+
## Normalizing all bounds
46+
47+
In the old trait solver the where clauses stored in `ParamEnv` are required to be fully normalized or else the trait solver will not function correctly. A concrete example of needing to normalize the `ParamEnv` is the following:
48+
```rust
49+
trait Trait<T> {
50+
type Assoc;
51+
}
52+
53+
trait Other {
54+
type Bar;
55+
}
56+
57+
impl<T> Other for T {
58+
type Bar = u32;
59+
}
60+
61+
// `foo`'s unnormalized `ParamEnv` would be:
62+
// `[T: Sized, U: Sized, U: Trait<T::Bar>]`
63+
fn foo<T, U>(a: U)
64+
where
65+
U: Trait<<T as Other>::Bar>,
66+
{
67+
requires_impl(a);
68+
}
69+
70+
fn requires_impl<U: Trait<u32>>(_: U) {}
71+
```
72+
73+
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`.
74+
75+
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.
76+
77+
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]).
78+
79+
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.
80+
81+
[example]: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=e6933265ea3e84eaa47019465739992c
82+
[pe]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.ParamEnv.html

0 commit comments

Comments
 (0)