@@ -253,35 +253,6 @@ impl AssocItemQSelf {
253253 }
254254}
255255
256- /// In some cases, [`hir::ConstArg`]s that are being used in the type system
257- /// through const generics need to have their type "fed" to them
258- /// using the query system.
259- ///
260- /// Use this enum with `<dyn HirTyLowerer>::lower_const_arg` to instruct it with the
261- /// desired behavior.
262- #[ derive( Debug , Clone , Copy ) ]
263- pub enum FeedConstTy < ' tcx > {
264- /// Feed the type to the (anno) const arg.
265- WithTy ( Ty < ' tcx > ) ,
266- /// Don't feed the type.
267- No ,
268- }
269-
270- impl < ' tcx > FeedConstTy < ' tcx > {
271- /// The `DefId` belongs to the const param that we are supplying
272- /// this (anon) const arg to.
273- ///
274- /// The list of generic args is used to instantiate the parameters
275- /// used by the type of the const param specified by `DefId`.
276- pub fn with_type_of (
277- tcx : TyCtxt < ' tcx > ,
278- def_id : DefId ,
279- generic_args : & [ ty:: GenericArg < ' tcx > ] ,
280- ) -> Self {
281- Self :: WithTy ( tcx. type_of ( def_id) . instantiate ( tcx, generic_args) )
282- }
283- }
284-
285256#[ derive( Debug , Clone , Copy ) ]
286257enum LowerTypeRelativePathMode {
287258 Type ( PermitVariants ) ,
@@ -733,7 +704,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
733704 // Ambig portions of `ConstArg` are handled in the match arm below
734705 . lower_const_arg (
735706 ct. as_unambig_ct ( ) ,
736- FeedConstTy :: with_type_of ( tcx, param. def_id , preceding_args) ,
707+ tcx. type_of ( param. def_id ) . instantiate ( tcx , preceding_args) ,
737708 )
738709 . into ( ) ,
739710 ( & GenericParamDefKind :: Const { .. } , GenericArg :: Infer ( inf) ) => {
@@ -1322,7 +1293,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13221293 constraint. hir_id ,
13231294 ) ;
13241295
1325- self . lower_const_arg ( ct, FeedConstTy :: WithTy ( ty ) ) . into ( )
1296+ self . lower_const_arg ( ct, ty ) . into ( )
13261297 }
13271298 } ;
13281299 if term. references_error ( ) {
@@ -2347,16 +2318,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23472318
23482319 /// Lower a [`hir::ConstArg`] to a (type-level) [`ty::Const`](Const).
23492320 #[ instrument( skip( self ) , level = "debug" ) ]
2350- pub fn lower_const_arg (
2351- & self ,
2352- const_arg : & hir:: ConstArg < ' tcx > ,
2353- feed : FeedConstTy < ' tcx > ,
2354- ) -> Const < ' tcx > {
2321+ pub fn lower_const_arg ( & self , const_arg : & hir:: ConstArg < ' tcx > , ty : Ty < ' tcx > ) -> Const < ' tcx > {
23552322 let tcx = self . tcx ( ) ;
23562323
2357- if let FeedConstTy :: WithTy ( anon_const_type) = feed
2358- && let hir:: ConstArgKind :: Anon ( anon) = & const_arg. kind
2359- {
2324+ if let hir:: ConstArgKind :: Anon ( anon) = & const_arg. kind {
23602325 // FIXME(generic_const_parameter_types): Ideally we remove these errors below when
23612326 // we have the ability to intermix typeck of anon const const args with the parent
23622327 // bodies typeck.
@@ -2366,7 +2331,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23662331 // hir typeck was using equality but mir borrowck wound up using subtyping as that could
23672332 // result in a non-infer in hir typeck but a region variable in borrowck.
23682333 if tcx. features ( ) . generic_const_parameter_types ( )
2369- && ( anon_const_type . has_free_regions ( ) || anon_const_type . has_erased_regions ( ) )
2334+ && ( ty . has_free_regions ( ) || ty . has_erased_regions ( ) )
23702335 {
23712336 let e = self . dcx ( ) . span_err (
23722337 const_arg. span ,
@@ -2378,7 +2343,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23782343 // We must error if the instantiated type has any inference variables as we will
23792344 // use this type to feed the `type_of` and query results must not contain inference
23802345 // variables otherwise we will ICE.
2381- if anon_const_type . has_non_region_infer ( ) {
2346+ if ty . has_non_region_infer ( ) {
23822347 let e = self . dcx ( ) . span_err (
23832348 const_arg. span ,
23842349 "anonymous constants with inferred types are not yet supported" ,
@@ -2388,7 +2353,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23882353 }
23892354 // We error when the type contains unsubstituted generics since we do not currently
23902355 // give the anon const any of the generics from the parent.
2391- if anon_const_type . has_non_region_param ( ) {
2356+ if ty . has_non_region_param ( ) {
23922357 let e = self . dcx ( ) . span_err (
23932358 const_arg. span ,
23942359 "anonymous constants referencing generics are not yet supported" ,
@@ -2397,12 +2362,12 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23972362 return ty:: Const :: new_error ( tcx, e) ;
23982363 }
23992364
2400- tcx. feed_anon_const_type ( anon. def_id , ty:: EarlyBinder :: bind ( anon_const_type ) ) ;
2365+ tcx. feed_anon_const_type ( anon. def_id , ty:: EarlyBinder :: bind ( ty ) ) ;
24012366 }
24022367
24032368 let hir_id = const_arg. hir_id ;
24042369 match const_arg. kind {
2405- hir:: ConstArgKind :: Tup ( exprs) => self . lower_const_arg_tup ( exprs, feed , const_arg. span ) ,
2370+ hir:: ConstArgKind :: Tup ( exprs) => self . lower_const_arg_tup ( exprs, ty , const_arg. span ) ,
24062371 hir:: ConstArgKind :: Path ( hir:: QPath :: Resolved ( maybe_qself, path) ) => {
24072372 debug ! ( ?maybe_qself, ?path) ;
24082373 let opt_self_ty = maybe_qself. as_ref ( ) . map ( |qself| self . lower_ty ( qself) ) ;
@@ -2429,12 +2394,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24292394 hir:: ConstArgKind :: Anon ( anon) => self . lower_const_arg_anon ( anon) ,
24302395 hir:: ConstArgKind :: Infer ( ( ) ) => self . ct_infer ( None , const_arg. span ) ,
24312396 hir:: ConstArgKind :: Error ( e) => ty:: Const :: new_error ( tcx, e) ,
2432- hir:: ConstArgKind :: Literal ( kind) if let FeedConstTy :: WithTy ( anon_const_type) = feed => {
2433- self . lower_const_arg_literal ( & kind, anon_const_type, const_arg. span )
2434- }
2435- hir:: ConstArgKind :: Literal ( ..) => {
2436- let e = self . dcx ( ) . span_err ( const_arg. span , "literal of unknown type" ) ;
2437- ty:: Const :: new_error ( tcx, e)
2397+ hir:: ConstArgKind :: Literal ( kind) => {
2398+ self . lower_const_arg_literal ( & kind, ty, const_arg. span )
24382399 }
24392400 }
24402401 }
@@ -2514,7 +2475,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25142475 . iter ( )
25152476 . zip ( args)
25162477 . map ( |( field_def, arg) | {
2517- self . lower_const_arg ( arg, FeedConstTy :: with_type_of ( tcx, field_def. did , adt_args) )
2478+ self . lower_const_arg ( arg, tcx. type_of ( field_def. did ) . instantiate ( tcx , adt_args) )
25182479 } )
25192480 . collect :: < Vec < _ > > ( ) ;
25202481
@@ -2533,23 +2494,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25332494 fn lower_const_arg_tup (
25342495 & self ,
25352496 exprs : & ' tcx [ & ' tcx hir:: ConstArg < ' tcx > ] ,
2536- feed : FeedConstTy < ' tcx > ,
2497+ ty : Ty < ' tcx > ,
25372498 span : Span ,
25382499 ) -> Const < ' tcx > {
25392500 let tcx = self . tcx ( ) ;
25402501
2541- let FeedConstTy :: WithTy ( ty) = feed else {
2542- return Const :: new_error_with_message ( tcx, span, "unsupported const tuple" ) ;
2543- } ;
2544-
25452502 let ty:: Tuple ( tys) = ty. kind ( ) else {
25462503 return Const :: new_error_with_message ( tcx, span, "const tuple must have a tuple type" ) ;
25472504 } ;
25482505
25492506 let exprs = exprs
25502507 . iter ( )
25512508 . zip ( tys. iter ( ) )
2552- . map ( |( expr, ty) | self . lower_const_arg ( expr, FeedConstTy :: WithTy ( ty ) ) )
2509+ . map ( |( expr, ty) | self . lower_const_arg ( expr, ty ) )
25532510 . collect :: < Vec < _ > > ( ) ;
25542511
25552512 let valtree = ty:: ValTree :: from_branches ( tcx, exprs) ;
@@ -2636,7 +2593,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26362593
26372594 self . lower_const_arg (
26382595 expr. expr ,
2639- FeedConstTy :: with_type_of ( tcx, field_def. did , adt_args) ,
2596+ tcx. type_of ( field_def. did ) . instantiate ( tcx , adt_args) ,
26402597 )
26412598 }
26422599 None => {
@@ -2998,7 +2955,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
29982955 . unwrap_or_else ( |guar| Ty :: new_error ( tcx, guar) )
29992956 }
30002957 hir:: TyKind :: Array ( ty, length) => {
3001- let length = self . lower_const_arg ( length, FeedConstTy :: WithTy ( tcx. types . usize ) ) ;
2958+ let length = self . lower_const_arg ( length, tcx. types . usize ) ;
30022959 Ty :: new_array_with_const_len ( tcx, self . lower_ty ( ty) , length)
30032960 }
30042961 hir:: TyKind :: Infer ( ( ) ) => {
@@ -3038,8 +2995,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
30382995 // Keep this list of types in sync with the list of types that
30392996 // the `RangePattern` trait is implemented for.
30402997 ty:: Int ( _) | ty:: Uint ( _) | ty:: Char => {
3041- let start = self . lower_const_arg ( start, FeedConstTy :: WithTy ( ty ) ) ;
3042- let end = self . lower_const_arg ( end, FeedConstTy :: WithTy ( ty ) ) ;
2998+ let start = self . lower_const_arg ( start, ty ) ;
2999+ let end = self . lower_const_arg ( end, ty ) ;
30433000 Ok ( ty:: PatternKind :: Range { start, end } )
30443001 }
30453002 _ => Err ( self
0 commit comments