Skip to content

Commit 4a96ed5

Browse files
authored
small fixes to ty chapter (#1390)
1 parent f92263e commit 4a96ed5

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

Diff for: src/ty.md

+21-11
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,21 @@ In contrast, `ty::Ty` represents the semantics of a type, that is, the *meaning*
3131
wrote. For example, `rustc_hir::Ty` would record the fact that a user used the name `u32` twice
3232
in their program, but the `ty::Ty` would record the fact that both usages refer to the same type.
3333

34-
**Example: `fn foo(x: u32) → u32 { x }`** In this function we see that `u32` appears twice. We know
35-
that that is the same type, i.e. the function takes an argument and returns an argument of the same
36-
type, but from the point of view of the HIR there would be two distinct type instances because these
37-
are occurring in two different places in the program. That is, they have two
38-
different [`Span`s][span] (locations).
34+
**Example: `fn foo(x: u32) → u32 { x }`**
35+
36+
In this function, we see that `u32` appears twice. We know
37+
that that is the same type,
38+
i.e. the function takes an argument and returns an argument of the same type,
39+
but from the point of view of the HIR,
40+
there would be two distinct type instances because these
41+
are occurring in two different places in the program.
42+
That is, they have two different [`Span`s][span] (locations).
3943

4044
[span]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html
4145

42-
**Example: `fn foo(x: &u32) -> &u32`** In addition, HIR might have information left out. This type
46+
**Example: `fn foo(x: &u32) -> &u32`**
47+
48+
In addition, HIR might have information left out. This type
4349
`&u32` is incomplete, since in the full Rust type there is actually a lifetime, but we didn’t need
4450
to write those lifetimes. There are also some elision rules that insert information. The result may
4551
look like `fn foo<'a>(x: &'a u32) -> &'a u32`.
@@ -56,23 +62,27 @@ Here is a summary:
5662
| Describe the *syntax* of a type: what the user wrote (with some desugaring). | Describe the *semantics* of a type: the meaning of what the user wrote. |
5763
| Each `rustc_hir::Ty` has its own spans corresponding to the appropriate place in the program. | Doesn’t correspond to a single place in the user’s program. |
5864
| `rustc_hir::Ty` has generics and lifetimes; however, some of those lifetimes are special markers like [`LifetimeName::Implicit`][implicit]. | `ty::Ty` has the full type, including generics and lifetimes, even if the user left them out |
59-
| `fn foo(x: u32) → u32 { }` - Two `rustc_hir::Ty` representing each usage of `u32`. Each has its own `Span`s, etc.- `rustc_hir::Ty` doesn’t tell us that both are the same type | `fn foo(x: u32) → u32 { }` - One `ty::Ty` for all instances of `u32` throughout the program.- `ty::Ty` tells us that both usages of `u32` mean the same type. |
60-
| `fn foo(x: &u32) -> &u32)`- Two `rustc_hir::Ty` again.- Lifetimes for the references show up in the `rustc_hir::Ty`s using a special marker, [`LifetimeName::Implicit`][implicit]. | `fn foo(x: &u32) -> &u32)`- A single `ty::Ty`.- The `ty::Ty` has the hidden lifetime param |
65+
| `fn foo(x: u32) → u32 { }` - Two `rustc_hir::Ty` representing each usage of `u32`, each has its own `Span`s, and `rustc_hir::Ty` doesn’t tell us that both are the same type | `fn foo(x: u32) → u32 { }` - One `ty::Ty` for all instances of `u32` throughout the program, and `ty::Ty` tells us that both usages of `u32` mean the same type. |
66+
| `fn foo(x: &u32) -> &u32)` - Two `rustc_hir::Ty` again. Lifetimes for the references show up in the `rustc_hir::Ty`s using a special marker, [`LifetimeName::Implicit`][implicit]. | `fn foo(x: &u32) -> &u32)`- A single `ty::Ty`. The `ty::Ty` has the hidden lifetime param. |
6167

6268
[implicit]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/enum.LifetimeName.html#variant.Implicit
6369

64-
**Order** HIR is built directly from the AST, so it happens before any `ty::Ty` is produced. After
70+
**Order**
71+
72+
HIR is built directly from the AST, so it happens before any `ty::Ty` is produced. After
6573
HIR is built, some basic type inference and type checking is done. During the type inference, we
6674
figure out what the `ty::Ty` of everything is and we also check if the type of something is
67-
ambiguous. The `ty::Ty` then, is used for type checking while making sure everything has the
75+
ambiguous. The `ty::Ty` is then used for type checking while making sure everything has the
6876
expected type. The [`astconv` module][astconv] is where the code responsible for converting a
6977
`rustc_hir::Ty` into a `ty::Ty` is located. This occurs during the type-checking phase,
7078
but also in other parts of the compiler that want to ask questions like "what argument types does
7179
this function expect?"
7280

7381
[astconv]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_typeck/astconv/index.html
7482

75-
**How semantics drive the two instances of `Ty`** You can think of HIR as the perspective
83+
**How semantics drive the two instances of `Ty`**
84+
85+
You can think of HIR as the perspective
7686
of the type information that assumes the least. We assume two things are distinct until they are
7787
proven to be the same thing. In other words, we know less about them, so we should assume less about
7888
them.

0 commit comments

Comments
 (0)