@@ -51,7 +51,7 @@ impl LateLintPass<'_> for MyStructLint {
51
51
fn check_expr (& mut self , cx : & LateContext <'_ >, expr : & Expr <'_ >) {
52
52
// Get type of `expr`
53
53
let ty = cx . typeck_results (). expr_ty (expr );
54
-
54
+
55
55
// Check if the `Ty` of this expression is of character type
56
56
if ty . is_char () {
57
57
println! (" Our expression is a char!" );
@@ -70,18 +70,18 @@ pub fn is_char(self) -> bool {
70
70
}
71
71
```
72
72
73
- Indeed, we just discovered ` Ty ` 's [ ` kind ` method] [ kind ] , which provides us
73
+ Indeed, we just discovered ` Ty ` 's [ ` kind() ` method] [ kind ] , which provides us
74
74
with [ ` TyKind ` ] [ TyKind ] of a ` Ty ` .
75
75
76
76
## ` TyKind `
77
77
78
78
` TyKind ` defines the kinds of types in Rust's type system.
79
79
Peeking into [ ` TyKind ` documentation] [ TyKind ] , we will see that it is an
80
- enum of 27 variants, including items such as ` Bool ` , ` Int ` , ` Ref ` , etc.
80
+ enum of over 25 variants, including items such as ` Bool ` , ` Int ` , ` Ref ` , etc.
81
81
82
82
### ` kind ` Usage
83
83
84
- The ` TyKind ` of ` Ty ` can be returned by calling [ ` Ty.kind ` method] [ kind ] .
84
+ The ` TyKind ` of ` Ty ` can be returned by calling [ ` Ty.kind() ` method] [ kind ] .
85
85
We often use this method to perform pattern matching in Clippy.
86
86
87
87
For instance, if we want to check for a ` struct ` , we could examine if the
@@ -107,15 +107,21 @@ impl LateLintPass<'_> for MyStructLint {
107
107
We've been talking about [ ` ty::Ty ` ] [ middle_ty ] this whole time without addressing [ ` hir::Ty ` ] [ hir_ty ] , but the latter
108
108
is also important to understand.
109
109
110
- ` hir::Ty ` would represent * what* an user wrote, while ` ty::Ty ` would understand the meaning of it (because it has more
111
- information).
110
+ ` hir::Ty ` would represent * what* the user wrote, while ` ty::Ty ` is how the compiler sees the type and has more
111
+ information. Example:
112
112
113
- ** Example: ` fn foo(x: u32) -> u32 { x } ` **
113
+ ``` rust
114
+ fn foo (x : u32 ) -> u32 { x }
115
+ ```
114
116
115
117
Here the HIR sees the types without "thinking" about them, it knows that the function takes an ` u32 ` and returns
116
- an ` u32 ` . But at the ` ty::Ty ` level the compiler understands that they're the same type, in-depth lifetimes, etc...
118
+ an ` u32 ` . As far as ` hir::Ty ` is concerned those might be different types. But at the ` ty::Ty ` level the compiler
119
+ understands that they're the same type, in-depth lifetimes, etc...
120
+
121
+ To get from a ` hir::Ty ` to a ` ty::Ty ` , you can use the [ ` hir_ty_to_ty ` ] [ hir_ty_to_ty ] function outside of bodies or
122
+ outside of bodies the [ ` TypeckResults::node_type() ` ] [ node_type ] method.
117
123
118
- you can use the [ ` hir_ty_to_ty ` ] [ hir_ty_to_ty ] function to convert from a ` hir::Ty ` to a ` ty::Ty `
124
+ > ** Warning ** : Don't use ` hir_ty_to_ty ` inside of bodies, because this can cause ICEs.
119
125
120
126
## Useful Links
121
127
@@ -130,6 +136,7 @@ in this chapter:
130
136
[ Adt ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/enum.TyKind.html#variant.Adt
131
137
[ AdtDef ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/adt/struct.AdtDef.html
132
138
[ expr_ty ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckResults.html#method.expr_ty
139
+ [ node_type ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TypeckResults.html#method.node_type
133
140
[ is_char ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.is_char
134
141
[ is_char_source ] : https://doc.rust-lang.org/nightly/nightly-rustc/src/rustc_middle/ty/sty.rs.html#1831-1834
135
142
[ kind ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.Ty.html#method.kind
0 commit comments