@@ -15,87 +15,11 @@ use super::{HirPlaceholderCollector, ItemCtxt, bad_placeholder};
1515use crate :: check:: wfcheck:: check_static_item;
1616use crate :: hir_ty_lowering:: HirTyLowerer ;
1717
18- mod opaque;
19-
20- fn anon_const_type_of < ' tcx > ( icx : & ItemCtxt < ' tcx > , def_id : LocalDefId ) -> Ty < ' tcx > {
21- use hir:: * ;
22- use rustc_middle:: ty:: Ty ;
23- let tcx = icx. tcx ;
24- let hir_id = tcx. local_def_id_to_hir_id ( def_id) ;
25-
26- let node = tcx. hir_node ( hir_id) ;
27- let Node :: AnonConst ( & AnonConst { span, .. } ) = node else {
28- span_bug ! (
29- tcx. def_span( def_id) ,
30- "expected anon const in `anon_const_type_of`, got {node:?}"
31- ) ;
32- } ;
33-
34- let parent_node_id = tcx. parent_hir_id ( hir_id) ;
35- let parent_node = tcx. hir_node ( parent_node_id) ;
36-
37- match parent_node {
38- // Anon consts "inside" the type system.
39- Node :: ConstArg ( & ConstArg {
40- hir_id : arg_hir_id,
41- kind : ConstArgKind :: Anon ( & AnonConst { hir_id : anon_hir_id, .. } ) ,
42- ..
43- } ) if anon_hir_id == hir_id => const_arg_anon_type_of ( icx, arg_hir_id, span) ,
44-
45- Node :: Variant ( Variant { disr_expr : Some ( e) , .. } ) if e. hir_id == hir_id => {
46- tcx. adt_def ( tcx. hir_get_parent_item ( hir_id) ) . repr ( ) . discr_type ( ) . to_ty ( tcx)
47- }
48-
49- Node :: Field ( & hir:: FieldDef { default : Some ( c) , def_id : field_def_id, .. } )
50- if c. hir_id == hir_id =>
51- {
52- tcx. type_of ( field_def_id) . instantiate_identity ( )
53- }
54-
55- _ => Ty :: new_error_with_message (
56- tcx,
57- span,
58- format ! ( "unexpected anon const parent in type_of(): {parent_node:?}" ) ,
59- ) ,
60- }
61- }
62-
63- fn const_arg_anon_type_of < ' tcx > ( icx : & ItemCtxt < ' tcx > , arg_hir_id : HirId , span : Span ) -> Ty < ' tcx > {
64- use hir:: * ;
65- use rustc_middle:: ty:: Ty ;
66-
67- let tcx = icx. tcx ;
68-
69- match tcx. parent_hir_node ( arg_hir_id) {
70- // Array length const arguments do not have `type_of` fed as there is never a corresponding
71- // generic parameter definition.
72- Node :: Ty ( & hir:: Ty { kind : TyKind :: Array ( _, ref constant) , .. } )
73- | Node :: Expr ( & Expr { kind : ExprKind :: Repeat ( _, ref constant) , .. } )
74- if constant. hir_id == arg_hir_id =>
75- {
76- tcx. types . usize
77- }
18+ use tracing:: instrument;
7819
79- Node :: TyPat ( pat) => {
80- let node = match tcx. parent_hir_node ( pat. hir_id ) {
81- // Or patterns can be nested one level deep
82- Node :: TyPat ( p) => tcx. parent_hir_node ( p. hir_id ) ,
83- other => other,
84- } ;
85- let hir:: TyKind :: Pat ( ty, _) = node. expect_ty ( ) . kind else { bug ! ( ) } ;
86- icx. lower_ty ( ty)
87- }
88-
89- // This is not a `bug!` as const arguments in path segments that did not resolve to anything
90- // will result in `type_of` never being fed.
91- _ => Ty :: new_error_with_message (
92- tcx,
93- span,
94- "`type_of` called on const argument's anon const before the const argument was lowered" ,
95- ) ,
96- }
97- }
20+ mod opaque;
9821
22+ #[ instrument( level = "debug" , skip( tcx) , ret) ]
9923pub ( super ) fn type_of ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> ty:: EarlyBinder < ' _ , Ty < ' _ > > {
10024 use rustc_hir:: * ;
10125 use rustc_middle:: ty:: Ty ;
@@ -408,6 +332,85 @@ pub(super) fn type_of_opaque_hir_typeck(
408332 }
409333}
410334
335+ fn anon_const_type_of < ' tcx > ( icx : & ItemCtxt < ' tcx > , def_id : LocalDefId ) -> Ty < ' tcx > {
336+ use hir:: * ;
337+ use rustc_middle:: ty:: Ty ;
338+ let tcx = icx. tcx ;
339+ let hir_id = tcx. local_def_id_to_hir_id ( def_id) ;
340+
341+ let node = tcx. hir_node ( hir_id) ;
342+ let Node :: AnonConst ( & AnonConst { span, .. } ) = node else {
343+ span_bug ! (
344+ tcx. def_span( def_id) ,
345+ "expected anon const in `anon_const_type_of`, got {node:?}"
346+ ) ;
347+ } ;
348+
349+ let parent_node_id = tcx. parent_hir_id ( hir_id) ;
350+ let parent_node = tcx. hir_node ( parent_node_id) ;
351+
352+ match parent_node {
353+ // Anon consts "inside" the type system.
354+ Node :: ConstArg ( & ConstArg {
355+ hir_id : arg_hir_id,
356+ kind : ConstArgKind :: Anon ( & AnonConst { hir_id : anon_hir_id, .. } ) ,
357+ ..
358+ } ) if anon_hir_id == hir_id => const_arg_anon_type_of ( icx, arg_hir_id, span) ,
359+
360+ Node :: Variant ( Variant { disr_expr : Some ( e) , .. } ) if e. hir_id == hir_id => {
361+ tcx. adt_def ( tcx. hir_get_parent_item ( hir_id) ) . repr ( ) . discr_type ( ) . to_ty ( tcx)
362+ }
363+
364+ Node :: Field ( & hir:: FieldDef { default : Some ( c) , def_id : field_def_id, .. } )
365+ if c. hir_id == hir_id =>
366+ {
367+ tcx. type_of ( field_def_id) . instantiate_identity ( )
368+ }
369+
370+ _ => Ty :: new_error_with_message (
371+ tcx,
372+ span,
373+ format ! ( "unexpected anon const parent in type_of(): {parent_node:?}" ) ,
374+ ) ,
375+ }
376+ }
377+
378+ fn const_arg_anon_type_of < ' tcx > ( icx : & ItemCtxt < ' tcx > , arg_hir_id : HirId , span : Span ) -> Ty < ' tcx > {
379+ use hir:: * ;
380+ use rustc_middle:: ty:: Ty ;
381+
382+ let tcx = icx. tcx ;
383+
384+ match tcx. parent_hir_node ( arg_hir_id) {
385+ // Array length const arguments do not have `type_of` fed as there is never a corresponding
386+ // generic parameter definition.
387+ Node :: Ty ( & hir:: Ty { kind : TyKind :: Array ( _, ref constant) , .. } )
388+ | Node :: Expr ( & Expr { kind : ExprKind :: Repeat ( _, ref constant) , .. } )
389+ if constant. hir_id == arg_hir_id =>
390+ {
391+ tcx. types . usize
392+ }
393+
394+ Node :: TyPat ( pat) => {
395+ let node = match tcx. parent_hir_node ( pat. hir_id ) {
396+ // Or patterns can be nested one level deep
397+ Node :: TyPat ( p) => tcx. parent_hir_node ( p. hir_id ) ,
398+ other => other,
399+ } ;
400+ let hir:: TyKind :: Pat ( ty, _) = node. expect_ty ( ) . kind else { bug ! ( ) } ;
401+ icx. lower_ty ( ty)
402+ }
403+
404+ // This is not a `bug!` as const arguments in path segments that did not resolve to anything
405+ // will result in `type_of` never being fed.
406+ _ => Ty :: new_error_with_message (
407+ tcx,
408+ span,
409+ "`type_of` called on const argument's anon const before the const argument was lowered" ,
410+ ) ,
411+ }
412+ }
413+
411414fn infer_placeholder_type < ' tcx > (
412415 cx : & dyn HirTyLowerer < ' tcx > ,
413416 def_id : LocalDefId ,
0 commit comments