@@ -22,7 +22,7 @@ use rustc_middle::ty::{self, AdtDef, GenericArgKind, Ty};
2222use rustc_span:: { Span , Symbol } ;
2323
2424mod certainty;
25- use certainty:: { Certainty , Meet , join, meet} ;
25+ use certainty:: { Certainty , Meet , TypeKind , join, meet} ;
2626
2727pub fn expr_type_is_certain ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
2828 expr_type_certainty ( cx, expr, false ) . is_certain ( )
@@ -46,11 +46,12 @@ fn expr_type_certainty(cx: &LateContext<'_>, expr: &Expr<'_>, in_arg: bool) -> C
4646 } else {
4747 Certainty :: Uncertain
4848 } ;
49- lhs. join_clearing_def_ids ( rhs)
49+ lhs. join_clearing_types ( rhs)
5050 } ,
5151
5252 ExprKind :: MethodCall ( method, receiver, args, _) => {
53- let mut receiver_type_certainty = expr_type_certainty ( cx, receiver, false ) ;
53+ let mut receiver_type_certainty = expr_type_certainty ( cx, receiver, false ) ?;
54+
5455 // Even if `receiver_type_certainty` is `Certain(Some(..))`, the `Self` type in the method
5556 // identified by `type_dependent_def_id(..)` can differ. This can happen as a result of a `deref`,
5657 // for example. So update the `DefId` in `receiver_type_certainty` (if any).
@@ -66,7 +67,7 @@ fn expr_type_certainty(cx: &LateContext<'_>, expr: &Expr<'_>, in_arg: bool) -> C
6667 . chain ( args. iter ( ) . map ( |arg| expr_type_certainty ( cx, arg, true ) ) ) ,
6768 )
6869 } else {
69- Certainty :: Uncertain
70+ return Certainty :: Uncertain ;
7071 } ;
7172 lhs. join ( rhs)
7273 } ,
@@ -117,12 +118,13 @@ fn expr_type_certainty(cx: &LateContext<'_>, expr: &Expr<'_>, in_arg: bool) -> C
117118 _ => Certainty :: Uncertain ,
118119 } ;
119120
120- let expr_ty = cx. typeck_results ( ) . expr_ty ( expr) ;
121- if let Some ( def_id) = adt_def_id ( expr_ty) {
122- certainty. with_def_id ( def_id)
123- } else {
124- certainty. clear_def_id ( )
125- }
121+ let result = match cx. typeck_results ( ) . expr_ty ( expr) . kind ( ) {
122+ ty:: Adt ( adt_def, _) => certainty. with_def_id ( adt_def. did ( ) ) ,
123+ ty:: Bool | ty:: Char | ty:: Int ( _) | ty:: Uint ( _) | ty:: Float ( _) => certainty. with_prim_ty ( ) ,
124+ _ => certainty. clear_type ( ) ,
125+ } ;
126+
127+ result
126128}
127129
128130struct CertaintyVisitor < ' cx , ' tcx > {
@@ -209,7 +211,11 @@ fn qpath_certainty(cx: &LateContext<'_>, qpath: &QPath<'_>, resolves_to_type: bo
209211 . map_or ( Certainty :: Uncertain , |def_id| {
210212 let generics = cx. tcx . generics_of ( def_id) ;
211213 if generics. is_empty ( ) {
212- Certainty :: Certain ( if resolves_to_type { Some ( def_id) } else { None } )
214+ Certainty :: Certain ( if resolves_to_type {
215+ Some ( TypeKind :: AdtDef ( def_id) )
216+ } else {
217+ None
218+ } )
213219 } else {
214220 Certainty :: Uncertain
215221 }
@@ -249,7 +255,7 @@ fn path_segment_certainty(
249255 . args
250256 . map_or ( Certainty :: Uncertain , |args| generic_args_certainty ( cx, args) ) ;
251257 // See the comment preceding `qpath_certainty`. `def_id` could refer to a type or a value.
252- let certainty = lhs. join_clearing_def_ids ( rhs) ;
258+ let certainty = lhs. join_clearing_types ( rhs) ;
253259 if resolves_to_type {
254260 if let DefKind :: TyAlias = cx. tcx . def_kind ( def_id) {
255261 adt_def_id ( cx. tcx . type_of ( def_id) . instantiate_identity ( ) )
@@ -265,9 +271,7 @@ fn path_segment_certainty(
265271 }
266272 } ,
267273
268- Res :: PrimTy ( _) | Res :: SelfTyParam { .. } | Res :: SelfTyAlias { .. } | Res :: SelfCtor ( _) => {
269- Certainty :: Certain ( None )
270- } ,
274+ Res :: PrimTy ( _) => Certainty :: Certain ( Some ( TypeKind :: PrimTy ) ) ,
271275
272276 // `get_parent` because `hir_id` refers to a `Pat`, and we're interested in the node containing the `Pat`.
273277 Res :: Local ( hir_id) => match cx. tcx . parent_hir_node ( hir_id) {
@@ -284,13 +288,13 @@ fn path_segment_certainty(
284288 if resolves_to_type {
285289 certainty
286290 } else {
287- certainty. clear_def_id ( )
291+ certainty. clear_type ( )
288292 }
289293 } ,
290294 _ => Certainty :: Uncertain ,
291295 } ,
292296
293- _ => Certainty :: Uncertain ,
297+ _ => Certainty :: Certain ( None ) ,
294298 } ;
295299 debug_assert ! ( resolves_to_type || certainty. to_def_id( ) . is_none( ) ) ;
296300 certainty
0 commit comments