1+ use std:: assert_matches:: assert_matches;
2+
13use rustc_hir as hir;
4+ use rustc_hir:: def:: DefKind ;
25use rustc_middle:: bug;
3- use rustc_middle:: ty:: { self , CanonicalUserType } ;
6+ use rustc_middle:: ty:: { self , CanonicalUserType , TyCtxt } ;
47use tracing:: debug;
58
69/// Looks up the type associated with this hir-id and applies the
710/// user-given generic parameters; the hir-id must map to a suitable
811/// type.
912pub ( crate ) fn user_args_applied_to_ty_of_hir_id < ' tcx > (
13+ tcx : TyCtxt < ' tcx > ,
1014 typeck_results : & ty:: TypeckResults < ' tcx > ,
1115 hir_id : hir:: HirId ,
1216) -> Option < CanonicalUserType < ' tcx > > {
@@ -16,7 +20,23 @@ pub(crate) fn user_args_applied_to_ty_of_hir_id<'tcx>(
1620 let ty = typeck_results. node_type ( hir_id) ;
1721 match ty. kind ( ) {
1822 ty:: Adt ( adt_def, ..) => {
23+ // This "fixes" user type annotations for tupled ctor patterns for ADTs.
24+ // That's because `type_of(ctor_did)` returns a FnDef, but we actually
25+ // want to be annotating the type of the ADT itself. It's a bit goofy,
26+ // but it's easier to adjust this here rather than in the path lowering
27+ // code for patterns in HIR.
1928 if let ty:: UserTypeKind :: TypeOf ( did, _) = & mut user_ty. value . kind {
29+ // This is either already set up correctly (struct, union, enum, or variant),
30+ // or needs adjusting (ctor). Make sure we don't start adjusting other
31+ // user annotations like consts or fn calls.
32+ assert_matches ! (
33+ tcx. def_kind( * did) ,
34+ DefKind :: Ctor ( ..)
35+ | DefKind :: Struct
36+ | DefKind :: Enum
37+ | DefKind :: Union
38+ | DefKind :: Variant
39+ ) ;
2040 * did = adt_def. did ( ) ;
2141 }
2242 Some ( user_ty)
0 commit comments