@@ -8,7 +8,7 @@ use crate::module_name::ModuleName;
8
8
use crate :: module_resolver:: { resolve_module, Module } ;
9
9
use crate :: semantic_index:: ast_ids:: HasScopedExpressionId ;
10
10
use crate :: semantic_index:: semantic_index;
11
- use crate :: types:: { binding_ty , infer_scope_types, Type } ;
11
+ use crate :: types:: { binding_type , infer_scope_types, Type } ;
12
12
use crate :: Db ;
13
13
14
14
pub struct SemanticModel < ' db > {
@@ -40,117 +40,117 @@ impl<'db> SemanticModel<'db> {
40
40
}
41
41
}
42
42
43
- pub trait HasTy {
43
+ pub trait HasType {
44
44
/// Returns the inferred type of `self`.
45
45
///
46
46
/// ## Panics
47
47
/// May panic if `self` is from another file than `model`.
48
- fn ty < ' db > ( & self , model : & SemanticModel < ' db > ) -> Type < ' db > ;
48
+ fn inferred_type < ' db > ( & self , model : & SemanticModel < ' db > ) -> Type < ' db > ;
49
49
}
50
50
51
- impl HasTy for ast:: ExprRef < ' _ > {
52
- fn ty < ' db > ( & self , model : & SemanticModel < ' db > ) -> Type < ' db > {
51
+ impl HasType for ast:: ExprRef < ' _ > {
52
+ fn inferred_type < ' db > ( & self , model : & SemanticModel < ' db > ) -> Type < ' db > {
53
53
let index = semantic_index ( model. db , model. file ) ;
54
54
let file_scope = index. expression_scope_id ( * self ) ;
55
55
let scope = file_scope. to_scope_id ( model. db , model. file ) ;
56
56
57
57
let expression_id = self . scoped_expression_id ( model. db , scope) ;
58
- infer_scope_types ( model. db , scope) . expression_ty ( expression_id)
58
+ infer_scope_types ( model. db , scope) . expression_type ( expression_id)
59
59
}
60
60
}
61
61
62
- macro_rules! impl_expression_has_ty {
62
+ macro_rules! impl_expression_has_type {
63
63
( $ty: ty) => {
64
- impl HasTy for $ty {
64
+ impl HasType for $ty {
65
65
#[ inline]
66
- fn ty <' db>( & self , model: & SemanticModel <' db>) -> Type <' db> {
66
+ fn inferred_type <' db>( & self , model: & SemanticModel <' db>) -> Type <' db> {
67
67
let expression_ref = ExprRef :: from( self ) ;
68
- expression_ref. ty ( model)
68
+ expression_ref. inferred_type ( model)
69
69
}
70
70
}
71
71
} ;
72
72
}
73
73
74
- impl_expression_has_ty ! ( ast:: ExprBoolOp ) ;
75
- impl_expression_has_ty ! ( ast:: ExprNamed ) ;
76
- impl_expression_has_ty ! ( ast:: ExprBinOp ) ;
77
- impl_expression_has_ty ! ( ast:: ExprUnaryOp ) ;
78
- impl_expression_has_ty ! ( ast:: ExprLambda ) ;
79
- impl_expression_has_ty ! ( ast:: ExprIf ) ;
80
- impl_expression_has_ty ! ( ast:: ExprDict ) ;
81
- impl_expression_has_ty ! ( ast:: ExprSet ) ;
82
- impl_expression_has_ty ! ( ast:: ExprListComp ) ;
83
- impl_expression_has_ty ! ( ast:: ExprSetComp ) ;
84
- impl_expression_has_ty ! ( ast:: ExprDictComp ) ;
85
- impl_expression_has_ty ! ( ast:: ExprGenerator ) ;
86
- impl_expression_has_ty ! ( ast:: ExprAwait ) ;
87
- impl_expression_has_ty ! ( ast:: ExprYield ) ;
88
- impl_expression_has_ty ! ( ast:: ExprYieldFrom ) ;
89
- impl_expression_has_ty ! ( ast:: ExprCompare ) ;
90
- impl_expression_has_ty ! ( ast:: ExprCall ) ;
91
- impl_expression_has_ty ! ( ast:: ExprFString ) ;
92
- impl_expression_has_ty ! ( ast:: ExprStringLiteral ) ;
93
- impl_expression_has_ty ! ( ast:: ExprBytesLiteral ) ;
94
- impl_expression_has_ty ! ( ast:: ExprNumberLiteral ) ;
95
- impl_expression_has_ty ! ( ast:: ExprBooleanLiteral ) ;
96
- impl_expression_has_ty ! ( ast:: ExprNoneLiteral ) ;
97
- impl_expression_has_ty ! ( ast:: ExprEllipsisLiteral ) ;
98
- impl_expression_has_ty ! ( ast:: ExprAttribute ) ;
99
- impl_expression_has_ty ! ( ast:: ExprSubscript ) ;
100
- impl_expression_has_ty ! ( ast:: ExprStarred ) ;
101
- impl_expression_has_ty ! ( ast:: ExprName ) ;
102
- impl_expression_has_ty ! ( ast:: ExprList ) ;
103
- impl_expression_has_ty ! ( ast:: ExprTuple ) ;
104
- impl_expression_has_ty ! ( ast:: ExprSlice ) ;
105
- impl_expression_has_ty ! ( ast:: ExprIpyEscapeCommand ) ;
106
-
107
- impl HasTy for ast:: Expr {
108
- fn ty < ' db > ( & self , model : & SemanticModel < ' db > ) -> Type < ' db > {
74
+ impl_expression_has_type ! ( ast:: ExprBoolOp ) ;
75
+ impl_expression_has_type ! ( ast:: ExprNamed ) ;
76
+ impl_expression_has_type ! ( ast:: ExprBinOp ) ;
77
+ impl_expression_has_type ! ( ast:: ExprUnaryOp ) ;
78
+ impl_expression_has_type ! ( ast:: ExprLambda ) ;
79
+ impl_expression_has_type ! ( ast:: ExprIf ) ;
80
+ impl_expression_has_type ! ( ast:: ExprDict ) ;
81
+ impl_expression_has_type ! ( ast:: ExprSet ) ;
82
+ impl_expression_has_type ! ( ast:: ExprListComp ) ;
83
+ impl_expression_has_type ! ( ast:: ExprSetComp ) ;
84
+ impl_expression_has_type ! ( ast:: ExprDictComp ) ;
85
+ impl_expression_has_type ! ( ast:: ExprGenerator ) ;
86
+ impl_expression_has_type ! ( ast:: ExprAwait ) ;
87
+ impl_expression_has_type ! ( ast:: ExprYield ) ;
88
+ impl_expression_has_type ! ( ast:: ExprYieldFrom ) ;
89
+ impl_expression_has_type ! ( ast:: ExprCompare ) ;
90
+ impl_expression_has_type ! ( ast:: ExprCall ) ;
91
+ impl_expression_has_type ! ( ast:: ExprFString ) ;
92
+ impl_expression_has_type ! ( ast:: ExprStringLiteral ) ;
93
+ impl_expression_has_type ! ( ast:: ExprBytesLiteral ) ;
94
+ impl_expression_has_type ! ( ast:: ExprNumberLiteral ) ;
95
+ impl_expression_has_type ! ( ast:: ExprBooleanLiteral ) ;
96
+ impl_expression_has_type ! ( ast:: ExprNoneLiteral ) ;
97
+ impl_expression_has_type ! ( ast:: ExprEllipsisLiteral ) ;
98
+ impl_expression_has_type ! ( ast:: ExprAttribute ) ;
99
+ impl_expression_has_type ! ( ast:: ExprSubscript ) ;
100
+ impl_expression_has_type ! ( ast:: ExprStarred ) ;
101
+ impl_expression_has_type ! ( ast:: ExprName ) ;
102
+ impl_expression_has_type ! ( ast:: ExprList ) ;
103
+ impl_expression_has_type ! ( ast:: ExprTuple ) ;
104
+ impl_expression_has_type ! ( ast:: ExprSlice ) ;
105
+ impl_expression_has_type ! ( ast:: ExprIpyEscapeCommand ) ;
106
+
107
+ impl HasType for ast:: Expr {
108
+ fn inferred_type < ' db > ( & self , model : & SemanticModel < ' db > ) -> Type < ' db > {
109
109
match self {
110
- Expr :: BoolOp ( inner) => inner. ty ( model) ,
111
- Expr :: Named ( inner) => inner. ty ( model) ,
112
- Expr :: BinOp ( inner) => inner. ty ( model) ,
113
- Expr :: UnaryOp ( inner) => inner. ty ( model) ,
114
- Expr :: Lambda ( inner) => inner. ty ( model) ,
115
- Expr :: If ( inner) => inner. ty ( model) ,
116
- Expr :: Dict ( inner) => inner. ty ( model) ,
117
- Expr :: Set ( inner) => inner. ty ( model) ,
118
- Expr :: ListComp ( inner) => inner. ty ( model) ,
119
- Expr :: SetComp ( inner) => inner. ty ( model) ,
120
- Expr :: DictComp ( inner) => inner. ty ( model) ,
121
- Expr :: Generator ( inner) => inner. ty ( model) ,
122
- Expr :: Await ( inner) => inner. ty ( model) ,
123
- Expr :: Yield ( inner) => inner. ty ( model) ,
124
- Expr :: YieldFrom ( inner) => inner. ty ( model) ,
125
- Expr :: Compare ( inner) => inner. ty ( model) ,
126
- Expr :: Call ( inner) => inner. ty ( model) ,
127
- Expr :: FString ( inner) => inner. ty ( model) ,
128
- Expr :: StringLiteral ( inner) => inner. ty ( model) ,
129
- Expr :: BytesLiteral ( inner) => inner. ty ( model) ,
130
- Expr :: NumberLiteral ( inner) => inner. ty ( model) ,
131
- Expr :: BooleanLiteral ( inner) => inner. ty ( model) ,
132
- Expr :: NoneLiteral ( inner) => inner. ty ( model) ,
133
- Expr :: EllipsisLiteral ( inner) => inner. ty ( model) ,
134
- Expr :: Attribute ( inner) => inner. ty ( model) ,
135
- Expr :: Subscript ( inner) => inner. ty ( model) ,
136
- Expr :: Starred ( inner) => inner. ty ( model) ,
137
- Expr :: Name ( inner) => inner. ty ( model) ,
138
- Expr :: List ( inner) => inner. ty ( model) ,
139
- Expr :: Tuple ( inner) => inner. ty ( model) ,
140
- Expr :: Slice ( inner) => inner. ty ( model) ,
141
- Expr :: IpyEscapeCommand ( inner) => inner. ty ( model) ,
110
+ Expr :: BoolOp ( inner) => inner. inferred_type ( model) ,
111
+ Expr :: Named ( inner) => inner. inferred_type ( model) ,
112
+ Expr :: BinOp ( inner) => inner. inferred_type ( model) ,
113
+ Expr :: UnaryOp ( inner) => inner. inferred_type ( model) ,
114
+ Expr :: Lambda ( inner) => inner. inferred_type ( model) ,
115
+ Expr :: If ( inner) => inner. inferred_type ( model) ,
116
+ Expr :: Dict ( inner) => inner. inferred_type ( model) ,
117
+ Expr :: Set ( inner) => inner. inferred_type ( model) ,
118
+ Expr :: ListComp ( inner) => inner. inferred_type ( model) ,
119
+ Expr :: SetComp ( inner) => inner. inferred_type ( model) ,
120
+ Expr :: DictComp ( inner) => inner. inferred_type ( model) ,
121
+ Expr :: Generator ( inner) => inner. inferred_type ( model) ,
122
+ Expr :: Await ( inner) => inner. inferred_type ( model) ,
123
+ Expr :: Yield ( inner) => inner. inferred_type ( model) ,
124
+ Expr :: YieldFrom ( inner) => inner. inferred_type ( model) ,
125
+ Expr :: Compare ( inner) => inner. inferred_type ( model) ,
126
+ Expr :: Call ( inner) => inner. inferred_type ( model) ,
127
+ Expr :: FString ( inner) => inner. inferred_type ( model) ,
128
+ Expr :: StringLiteral ( inner) => inner. inferred_type ( model) ,
129
+ Expr :: BytesLiteral ( inner) => inner. inferred_type ( model) ,
130
+ Expr :: NumberLiteral ( inner) => inner. inferred_type ( model) ,
131
+ Expr :: BooleanLiteral ( inner) => inner. inferred_type ( model) ,
132
+ Expr :: NoneLiteral ( inner) => inner. inferred_type ( model) ,
133
+ Expr :: EllipsisLiteral ( inner) => inner. inferred_type ( model) ,
134
+ Expr :: Attribute ( inner) => inner. inferred_type ( model) ,
135
+ Expr :: Subscript ( inner) => inner. inferred_type ( model) ,
136
+ Expr :: Starred ( inner) => inner. inferred_type ( model) ,
137
+ Expr :: Name ( inner) => inner. inferred_type ( model) ,
138
+ Expr :: List ( inner) => inner. inferred_type ( model) ,
139
+ Expr :: Tuple ( inner) => inner. inferred_type ( model) ,
140
+ Expr :: Slice ( inner) => inner. inferred_type ( model) ,
141
+ Expr :: IpyEscapeCommand ( inner) => inner. inferred_type ( model) ,
142
142
}
143
143
}
144
144
}
145
145
146
146
macro_rules! impl_binding_has_ty {
147
147
( $ty: ty) => {
148
- impl HasTy for $ty {
148
+ impl HasType for $ty {
149
149
#[ inline]
150
- fn ty <' db>( & self , model: & SemanticModel <' db>) -> Type <' db> {
150
+ fn inferred_type <' db>( & self , model: & SemanticModel <' db>) -> Type <' db> {
151
151
let index = semantic_index( model. db, model. file) ;
152
152
let binding = index. definition( self ) ;
153
- binding_ty ( model. db, binding)
153
+ binding_type ( model. db, binding)
154
154
}
155
155
}
156
156
} ;
@@ -168,10 +168,10 @@ mod tests {
168
168
use ruff_db:: parsed:: parsed_module;
169
169
170
170
use crate :: db:: tests:: TestDbBuilder ;
171
- use crate :: { HasTy , SemanticModel } ;
171
+ use crate :: { HasType , SemanticModel } ;
172
172
173
173
#[ test]
174
- fn function_ty ( ) -> anyhow:: Result < ( ) > {
174
+ fn function_type ( ) -> anyhow:: Result < ( ) > {
175
175
let db = TestDbBuilder :: new ( )
176
176
. with_file ( "/src/foo.py" , "def test(): pass" )
177
177
. build ( ) ?;
@@ -182,15 +182,15 @@ mod tests {
182
182
183
183
let function = ast. suite ( ) [ 0 ] . as_function_def_stmt ( ) . unwrap ( ) ;
184
184
let model = SemanticModel :: new ( & db, foo) ;
185
- let ty = function. ty ( & model) ;
185
+ let ty = function. inferred_type ( & model) ;
186
186
187
187
assert ! ( ty. is_function_literal( ) ) ;
188
188
189
189
Ok ( ( ) )
190
190
}
191
191
192
192
#[ test]
193
- fn class_ty ( ) -> anyhow:: Result < ( ) > {
193
+ fn class_type ( ) -> anyhow:: Result < ( ) > {
194
194
let db = TestDbBuilder :: new ( )
195
195
. with_file ( "/src/foo.py" , "class Test: pass" )
196
196
. build ( ) ?;
@@ -201,15 +201,15 @@ mod tests {
201
201
202
202
let class = ast. suite ( ) [ 0 ] . as_class_def_stmt ( ) . unwrap ( ) ;
203
203
let model = SemanticModel :: new ( & db, foo) ;
204
- let ty = class. ty ( & model) ;
204
+ let ty = class. inferred_type ( & model) ;
205
205
206
206
assert ! ( ty. is_class_literal( ) ) ;
207
207
208
208
Ok ( ( ) )
209
209
}
210
210
211
211
#[ test]
212
- fn alias_ty ( ) -> anyhow:: Result < ( ) > {
212
+ fn alias_type ( ) -> anyhow:: Result < ( ) > {
213
213
let db = TestDbBuilder :: new ( )
214
214
. with_file ( "/src/foo.py" , "class Test: pass" )
215
215
. with_file ( "/src/bar.py" , "from foo import Test" )
@@ -222,7 +222,7 @@ mod tests {
222
222
let import = ast. suite ( ) [ 0 ] . as_import_from_stmt ( ) . unwrap ( ) ;
223
223
let alias = & import. names [ 0 ] ;
224
224
let model = SemanticModel :: new ( & db, bar) ;
225
- let ty = alias. ty ( & model) ;
225
+ let ty = alias. inferred_type ( & model) ;
226
226
227
227
assert ! ( ty. is_class_literal( ) ) ;
228
228
0 commit comments