1- //! Conversion from AST representation of types to the `ty.rs` representation.
2- //! The main routine here is `ast_ty_to_ty()`; each use is parameterized by an
3- //! instance of `AstConv`.
1+ //! HIR/ty lowering: Lowers type-system entities from HIR to `rustc_middle::ty` representation.
2+ //!
3+ //! Not to be confused with *AST lowering* which lowers AST constructs to HIR ones.
4+ //!
5+ //! The main routine here is `<dyn HirTyLowerer>::lower_ty()`. The other routines
6+ //! are defined on `dyn HirTyLowerer` (see [`HirTyLowerer`]).
47
58mod bounds;
69mod errors;
@@ -69,6 +72,9 @@ pub enum PredicateFilter {
6972 SelfAndAssociatedTypeBounds ,
7073}
7174
75+ /// A context which can lower type-system entities from [HIR][hir] to [`rustc_middle::ty`] representation.
76+ ///
77+ /// This used to be called `AstConv`.
7278pub trait HirTyLowerer < ' tcx > {
7379 fn tcx ( & self ) -> TyCtxt < ' tcx > ;
7480
@@ -127,6 +133,7 @@ pub trait HirTyLowerer<'tcx> {
127133 ) -> Ty < ' tcx > ;
128134
129135 /// Returns `AdtDef` if `ty` is an ADT.
136+ ///
130137 /// Note that `ty` might be a projection type that needs normalization.
131138 /// This used to get the enum variants in scope of the type.
132139 /// For example, `Self::A` could refer to an associated type
@@ -214,6 +221,7 @@ pub struct GenericArgCountResult {
214221 pub correct : Result < ( ) , GenericArgCountMismatch > ,
215222}
216223
224+ /// A context which can lower HIR's [`GenericArg`] to `rustc_middle`'s [`ty::GenericArg`].
217225pub trait GenericArgsLowerer < ' a , ' tcx > {
218226 fn args_for_def_id ( & mut self , def_id : DefId ) -> ( Option < & ' a GenericArgs < ' tcx > > , bool ) ;
219227
@@ -309,7 +317,6 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
309317 if let Some ( b) = item_segment. args ( ) . bindings . first ( ) {
310318 prohibit_assoc_ty_binding ( self . tcx ( ) , b. span , Some ( ( item_segment, span) ) ) ;
311319 }
312-
313320 args
314321 }
315322
@@ -593,7 +600,7 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
593600 & self ,
594601 generic_args : & ' a hir:: GenericArgs < ' tcx > ,
595602 ) -> Vec < LoweredBinding < ' a , ' tcx > > {
596- // Convert associated-type bindings or constraints into a separate vector.
603+ // Lower associated-type bindings or constraints into a separate vector.
597604 // Example: Given this:
598605 //
599606 // T: Iterator<Item = u32>
@@ -655,11 +662,9 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
655662 None ,
656663 ty:: BoundConstness :: NotConst ,
657664 ) ;
658-
659665 if let Some ( b) = item_segment. args ( ) . bindings . first ( ) {
660666 prohibit_assoc_ty_binding ( self . tcx ( ) , b. span , Some ( ( item_segment, span) ) ) ;
661667 }
662-
663668 args
664669 }
665670
@@ -786,7 +791,7 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
786791 self_ty : Ty < ' tcx > ,
787792 trait_segment : & hir:: PathSegment < ' tcx > ,
788793 is_impl : bool ,
789- // FIXME(effects) move all host param things in astconv to hir lowering
794+ // FIXME(effects): Move all host param things in HIR/ty lowering to AST lowering.
790795 constness : ty:: BoundConstness ,
791796 ) -> ty:: TraitRef < ' tcx > {
792797 let ( generic_args, _) = self . lower_args_for_trait_ref (
@@ -973,10 +978,11 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
973978 reported
974979 }
975980
976- // Search for a bound on a type parameter which includes the associated item
977- // given by `assoc_name`. `ty_param_def_id` is the `DefId` of the type parameter
978- // This function will fail if there are no suitable bounds or there is
979- // any ambiguity.
981+ /// Search for a bound on a type parameter which includes the associated item given by `assoc_name`.
982+ ///
983+ /// `ty_param_def_id` is the `DefId` of the type parameter.
984+ /// This function will fail if there are no suitable bounds or there is any ambiguity.
985+ #[ instrument( level = "debug" , skip( self ) , ret) ]
980986 fn find_bound_for_assoc_item (
981987 & self ,
982988 ty_param_def_id : LocalDefId ,
@@ -1015,8 +1021,8 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
10151021 )
10161022 }
10171023
1018- // Checks that `bounds` contains exactly one element and reports appropriate
1019- // errors otherwise.
1024+ // FIXME(fmease): This also *resolves* the bound. Update docs!
1025+ /// Checks that `bounds` contains exactly one element and reports appropriate errors otherwise.
10201026 #[ instrument( level = "debug" , skip( self , all_candidates, ty_param_name, binding) , ret) ]
10211027 fn one_bound_for_assoc_item < I > (
10221028 & self ,
@@ -1133,12 +1139,14 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
11331139 Ok ( bound)
11341140 }
11351141
1136- // Create a type from a path to an associated type or to an enum variant.
1137- // For a path `A::B::C::D`, `qself_ty` and `qself_def` are the type and def for `A::B::C`
1138- // and item_segment is the path segment for `D`. We return a type and a def for
1139- // the whole path.
1140- // Will fail except for `T::A` and `Self::A`; i.e., if `qself_ty`/`qself_def` are not a type
1141- // parameter or `Self`.
1142+ /// Create a type from a path to an associated type or to an enum variant.
1143+ ///
1144+ /// For a path `A::B::C::D`, `qself_ty` and `qself` are the type and def for `A::B::C`
1145+ /// and item_segment is the path segment for `D`. We return a type and a def for
1146+ /// the whole path.
1147+ ///
1148+ /// Will fail except for `T::A` and `Self::A`; i.e., if `qself_ty`/`qself` are not a type
1149+ /// parameter or `Self`.
11421150 // NOTE: When this function starts resolving `Trait::AssocTy` successfully
11431151 // it should also start reporting the `BARE_TRAIT_OBJECTS` lint.
11441152 #[ instrument( level = "debug" , skip( self , hir_ref_id, span, qself, assoc_segment) , fields( assoc_ident=?assoc_segment. ident) , ret) ]
@@ -1436,8 +1444,9 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
14361444 // Don't attempt to look up inherent associated types when the feature is not enabled.
14371445 // Theoretically it'd be fine to do so since we feature-gate their definition site.
14381446 // However, due to current limitations of the implementation (caused by us performing
1439- // selection in AstConv), IATs can lead to cycle errors (#108491, #110106) which mask the
1440- // feature-gate error, needlessly confusing users that use IATs by accident (#113265).
1447+ // selection during HIR/ty lowering instead of in the trait solver), IATs can lead to cycle
1448+ // errors (#108491, #110106) which mask the feature-gate error, needlessly confusing users
1449+ // who use IATs by accident (#113265).
14411450 if !tcx. features ( ) . inherent_associated_types {
14421451 return Ok ( None ) ;
14431452 }
@@ -2014,7 +2023,7 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
20142023 path_segs
20152024 }
20162025
2017- /// Check a type `Path` and convert it to a `Ty`.
2026+ /// Check a type `Path` and lower it to a `Ty`.
20182027 pub fn lower_res_to_ty (
20192028 & self ,
20202029 opt_self_ty : Option < Ty < ' tcx > > ,
@@ -2054,7 +2063,7 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
20542063 self . lower_path_to_ty ( span, did, path. segments . last ( ) . unwrap ( ) )
20552064 }
20562065 Res :: Def ( kind @ DefKind :: Variant , def_id) if permit_variants => {
2057- // Convert "variant type" as if it were a real type.
2066+ // Lower "variant type" as if it were a real type.
20582067 // The resulting `Ty` is type of the variant's enum for now.
20592068 assert_eq ! ( opt_self_ty, None ) ;
20602069
@@ -2255,8 +2264,8 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
22552264 }
22562265 }
22572266
2258- // Converts a hir id corresponding to a type parameter to
2259- // a early-bound `ty::Param` or late-bound `ty::Bound`.
2267+ /// Lower a `HirId` corresponding to a type parameter to an early-bound
2268+ /// [ `ty::Param`] or late-bound [ `ty::Bound`] .
22602269 pub ( crate ) fn lower_ty_param ( & self , hir_id : hir:: HirId ) -> Ty < ' tcx > {
22612270 let tcx = self . tcx ( ) ;
22622271 match tcx. named_bound_var ( hir_id) {
@@ -2280,8 +2289,8 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
22802289 }
22812290 }
22822291
2283- // Converts a hir id corresponding to a const parameter to
2284- // a early-bound ` ConstKind::Param` or late-bound ` ConstKind::Bound`.
2292+ /// Lower a `HirId` corresponding to a const parameter to an early-bound
2293+ /// [`ty:: ConstKind::Param`] or late-bound [`ty:: ConstKind::Bound`] .
22852294 pub ( crate ) fn lower_const_param ( & self , hir_id : hir:: HirId , param_ty : Ty < ' tcx > ) -> Const < ' tcx > {
22862295 let tcx = self . tcx ( ) ;
22872296 match tcx. named_bound_var ( hir_id) {
@@ -2422,8 +2431,10 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
24222431 }
24232432 }
24242433
2425- /// Turns a `hir::Ty` into a `Ty`. For diagnostics' purposes we keep track of whether trait
2426- /// objects are borrowed like `&dyn Trait` to avoid emitting redundant errors.
2434+ /// Lowers a [`hir::Ty`] to a [`Ty`].
2435+ ///
2436+ /// For diagnostics' purposes we keep track of whether trait objects are
2437+ /// borrowed like `&dyn Trait` to avoid emitting redundant errors.
24272438 #[ instrument( level = "debug" , skip( self ) , ret) ]
24282439 fn lower_ty_inner ( & self , ast_ty : & hir:: Ty < ' tcx > , borrowed : bool , in_path : bool ) -> Ty < ' tcx > {
24292440 let tcx = self . tcx ( ) ;
@@ -2812,7 +2823,9 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
28122823 }
28132824
28142825 /// Given the bounds on an object, determines what single region bound (if any) we can
2815- /// use to summarize this type. The basic idea is that we will use the bound the user
2826+ /// use to summarize this type.
2827+ ///
2828+ /// The basic idea is that we will use the bound the user
28162829 /// provided, if they provided one, and otherwise search the supertypes of trait bounds
28172830 /// for region bounds. It may be that we can derive no bound at all, in which case
28182831 /// we return `None`.
0 commit comments