@@ -87,10 +87,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
8787 TraitItemKind :: Const ( ty, rhs) => rhs
8888 . and_then ( |rhs| {
8989 ty. is_suggestable_infer_ty ( ) . then ( || {
90+ let hir_body_id = match rhs {
91+ ConstItemRhs :: Body ( body) => Some ( body. hir_id ) ,
92+ ConstItemRhs :: Direct ( _) => None ,
93+ } ;
9094 infer_placeholder_type (
9195 icx. lowerer ( ) ,
9296 def_id,
93- rhs . hir_id ( ) ,
97+ hir_body_id ,
9498 ty. span ,
9599 rhs. span ( tcx) ,
96100 item. ident ,
@@ -109,10 +113,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
109113 ImplItemKind :: Fn ( _, _) => new_bound_fn_def ( item. hir_id ( ) , def_id. to_def_id ( ) ) ,
110114 ImplItemKind :: Const ( ty, rhs) => {
111115 if ty. is_suggestable_infer_ty ( ) {
116+ let hir_body_id = match rhs {
117+ ConstItemRhs :: Body ( body) => Some ( body. hir_id ) ,
118+ ConstItemRhs :: Direct ( _) => None ,
119+ } ;
112120 infer_placeholder_type (
113121 icx. lowerer ( ) ,
114122 def_id,
115- rhs . hir_id ( ) ,
123+ hir_body_id ,
116124 ty. span ,
117125 rhs. span ( tcx) ,
118126 item. ident ,
@@ -137,7 +145,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
137145 infer_placeholder_type (
138146 icx. lowerer ( ) ,
139147 def_id,
140- body_id. hir_id ,
148+ Some ( body_id. hir_id ) ,
141149 ty. span ,
142150 tcx. hir_body ( body_id) . value . span ,
143151 ident,
@@ -157,10 +165,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
157165 }
158166 ItemKind :: Const ( ident, _, ty, rhs) => {
159167 if ty. is_suggestable_infer_ty ( ) {
168+ let hir_body_id = match rhs {
169+ ConstItemRhs :: Body ( body) => Some ( body. hir_id ) ,
170+ ConstItemRhs :: Direct ( _) => None ,
171+ } ;
160172 infer_placeholder_type (
161173 icx. lowerer ( ) ,
162174 def_id,
163- rhs . hir_id ( ) ,
175+ hir_body_id ,
164176 ty. span ,
165177 rhs. span ( tcx) ,
166178 ident,
@@ -431,28 +443,28 @@ fn const_arg_anon_type_of<'tcx>(icx: &ItemCtxt<'tcx>, arg_hir_id: HirId, span: S
431443fn infer_placeholder_type < ' tcx > (
432444 cx : & dyn HirTyLowerer < ' tcx > ,
433445 def_id : LocalDefId ,
434- hir_id : HirId ,
446+ hir_body_id : Option < HirId > ,
435447 ty_span : Span ,
436448 body_span : Span ,
437449 item_ident : Ident ,
438450 kind : & ' static str ,
439451) -> Ty < ' tcx > {
440452 let tcx = cx. tcx ( ) ;
441- // If the type is omitted on a `type const` we can't run
442- // type check on since that requires the const have a body
443- // which `type const`s don't.
444- let ty = if tcx. is_type_const ( def_id. to_def_id ( ) ) {
445- if let Some ( trait_item_def_id) = tcx. trait_item_of ( def_id. to_def_id ( ) ) {
446- tcx. type_of ( trait_item_def_id) . instantiate_identity ( ) . skip_norm_wip ( )
447- } else {
448- Ty :: new_error_with_message (
449- tcx,
450- ty_span,
451- "constant with `type const` requires an explicit type" ,
452- )
453+ // If the type is omitted on const with `ConstItemRhs::Direct`, we can't run type check on it,
454+ // since that requires the const have a body, i.e. `ConstItemRhs::Body`.
455+ let ty = match hir_body_id {
456+ Some ( hir_id) => tcx. typeck ( def_id) . node_type ( hir_id) ,
457+ None => {
458+ if let Some ( trait_item_def_id) = tcx. trait_item_of ( def_id. to_def_id ( ) ) {
459+ tcx. type_of ( trait_item_def_id) . instantiate_identity ( ) . skip_norm_wip ( )
460+ } else {
461+ Ty :: new_error_with_message (
462+ tcx,
463+ ty_span,
464+ "directly represented const requires an explicit type" ,
465+ )
466+ }
453467 }
454- } else {
455- tcx. typeck ( def_id) . node_type ( hir_id)
456468 } ;
457469
458470 // If this came from a free `const` or `static mut?` item,
0 commit comments