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`]).
7
+ //!
8
+ //! This module used to be called `astconv`.
4
9
5
10
mod bounds;
6
11
mod errors;
@@ -69,6 +74,9 @@ pub enum PredicateFilter {
69
74
SelfAndAssociatedTypeBounds ,
70
75
}
71
76
77
+ /// A context which can lower type-system entities from [HIR][hir] to [`rustc_middle::ty`] representation.
78
+ ///
79
+ /// This trait used to be called `AstConv`.
72
80
pub trait HirTyLowerer < ' tcx > {
73
81
fn tcx ( & self ) -> TyCtxt < ' tcx > ;
74
82
@@ -127,6 +135,7 @@ pub trait HirTyLowerer<'tcx> {
127
135
) -> Ty < ' tcx > ;
128
136
129
137
/// Returns `AdtDef` if `ty` is an ADT.
138
+ ///
130
139
/// Note that `ty` might be a projection type that needs normalization.
131
140
/// This used to get the enum variants in scope of the type.
132
141
/// For example, `Self::A` could refer to an associated type
@@ -214,6 +223,7 @@ pub struct GenericArgCountResult {
214
223
pub correct : Result < ( ) , GenericArgCountMismatch > ,
215
224
}
216
225
226
+ /// A context which can lower HIR's [`GenericArg`] to `rustc_middle`'s [`ty::GenericArg`].
217
227
pub trait GenericArgsLowerer < ' a , ' tcx > {
218
228
fn args_for_def_id ( & mut self , def_id : DefId ) -> ( Option < & ' a GenericArgs < ' tcx > > , bool ) ;
219
229
@@ -309,7 +319,6 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
309
319
if let Some ( b) = item_segment. args ( ) . bindings . first ( ) {
310
320
prohibit_assoc_ty_binding ( self . tcx ( ) , b. span , Some ( ( item_segment, span) ) ) ;
311
321
}
312
-
313
322
args
314
323
}
315
324
@@ -593,7 +602,7 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
593
602
& self ,
594
603
generic_args : & ' a hir:: GenericArgs < ' tcx > ,
595
604
) -> Vec < LoweredBinding < ' a , ' tcx > > {
596
- // Convert associated-type bindings or constraints into a separate vector.
605
+ // Lower associated-type bindings or constraints into a separate vector.
597
606
// Example: Given this:
598
607
//
599
608
// T: Iterator<Item = u32>
@@ -655,11 +664,9 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
655
664
None ,
656
665
ty:: BoundConstness :: NotConst ,
657
666
) ;
658
-
659
667
if let Some ( b) = item_segment. args ( ) . bindings . first ( ) {
660
668
prohibit_assoc_ty_binding ( self . tcx ( ) , b. span , Some ( ( item_segment, span) ) ) ;
661
669
}
662
-
663
670
args
664
671
}
665
672
@@ -786,7 +793,7 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
786
793
self_ty : Ty < ' tcx > ,
787
794
trait_segment : & hir:: PathSegment < ' tcx > ,
788
795
is_impl : bool ,
789
- // FIXME(effects) move all host param things in astconv to hir lowering
796
+ // FIXME(effects): Move all host param things in HIR/ty lowering to AST lowering.
790
797
constness : ty:: BoundConstness ,
791
798
) -> ty:: TraitRef < ' tcx > {
792
799
let ( generic_args, _) = self . lower_args_for_trait_ref (
@@ -973,10 +980,11 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
973
980
reported
974
981
}
975
982
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.
983
+ /// Search for a bound on a type parameter which includes the associated item given by `assoc_name`.
984
+ ///
985
+ /// `ty_param_def_id` is the `DefId` of the type parameter.
986
+ /// This function will fail if there are no suitable bounds or there is any ambiguity.
987
+ #[ instrument( level = "debug" , skip( self ) , ret) ]
980
988
fn find_bound_for_assoc_item (
981
989
& self ,
982
990
ty_param_def_id : LocalDefId ,
@@ -1015,8 +1023,8 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
1015
1023
)
1016
1024
}
1017
1025
1018
- // Checks that `bounds` contains exactly one element and reports appropriate
1019
- // errors otherwise.
1026
+ // FIXME(fmease): This also *resolves* the bound. Update docs!
1027
+ /// Checks that `bounds` contains exactly one element and reports appropriate errors otherwise.
1020
1028
#[ instrument( level = "debug" , skip( self , all_candidates, ty_param_name, binding) , ret) ]
1021
1029
fn one_bound_for_assoc_item < I > (
1022
1030
& self ,
@@ -1133,12 +1141,14 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
1133
1141
Ok ( bound)
1134
1142
}
1135
1143
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`.
1144
+ /// Create a type from a path to an associated type or to an enum variant.
1145
+ ///
1146
+ /// For a path `A::B::C::D`, `qself_ty` and `qself` are the type and def for `A::B::C`
1147
+ /// and item_segment is the path segment for `D`. We return a type and a def for
1148
+ /// the whole path.
1149
+ ///
1150
+ /// Will fail except for `T::A` and `Self::A`; i.e., if `qself_ty`/`qself` are not a type
1151
+ /// parameter or `Self`.
1142
1152
// NOTE: When this function starts resolving `Trait::AssocTy` successfully
1143
1153
// it should also start reporting the `BARE_TRAIT_OBJECTS` lint.
1144
1154
#[ instrument( level = "debug" , skip( self , hir_ref_id, span, qself, assoc_segment) , fields( assoc_ident=?assoc_segment. ident) , ret) ]
@@ -1436,8 +1446,9 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
1436
1446
// Don't attempt to look up inherent associated types when the feature is not enabled.
1437
1447
// Theoretically it'd be fine to do so since we feature-gate their definition site.
1438
1448
// 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).
1449
+ // selection during HIR/ty lowering instead of in the trait solver), IATs can lead to cycle
1450
+ // errors (#108491, #110106) which mask the feature-gate error, needlessly confusing users
1451
+ // who use IATs by accident (#113265).
1441
1452
if !tcx. features ( ) . inherent_associated_types {
1442
1453
return Ok ( None ) ;
1443
1454
}
@@ -2014,7 +2025,7 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
2014
2025
path_segs
2015
2026
}
2016
2027
2017
- /// Check a type `Path` and convert it to a `Ty`.
2028
+ /// Check a type `Path` and lower it to a `Ty`.
2018
2029
pub fn lower_res_to_ty (
2019
2030
& self ,
2020
2031
opt_self_ty : Option < Ty < ' tcx > > ,
@@ -2054,7 +2065,7 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
2054
2065
self . lower_path_to_ty ( span, did, path. segments . last ( ) . unwrap ( ) )
2055
2066
}
2056
2067
Res :: Def ( kind @ DefKind :: Variant , def_id) if permit_variants => {
2057
- // Convert "variant type" as if it were a real type.
2068
+ // Lower "variant type" as if it were a real type.
2058
2069
// The resulting `Ty` is type of the variant's enum for now.
2059
2070
assert_eq ! ( opt_self_ty, None ) ;
2060
2071
@@ -2255,8 +2266,8 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
2255
2266
}
2256
2267
}
2257
2268
2258
- // Converts a hir id corresponding to a type parameter to
2259
- // a early-bound `ty::Param` or late-bound `ty::Bound`.
2269
+ /// Lower a `HirId` corresponding to a type parameter to an early-bound
2270
+ /// [ `ty::Param`] or late-bound [ `ty::Bound`] .
2260
2271
pub ( crate ) fn lower_type_param ( & self , hir_id : hir:: HirId ) -> Ty < ' tcx > {
2261
2272
let tcx = self . tcx ( ) ;
2262
2273
match tcx. named_bound_var ( hir_id) {
@@ -2280,8 +2291,8 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
2280
2291
}
2281
2292
}
2282
2293
2283
- // Converts a hir id corresponding to a const parameter to
2284
- // a early-bound ` ConstKind::Param` or late-bound ` ConstKind::Bound`.
2294
+ /// Lower a `HirId` corresponding to a const parameter to an early-bound
2295
+ /// [`ty:: ConstKind::Param`] or late-bound [`ty:: ConstKind::Bound`] .
2285
2296
pub ( crate ) fn lower_const_param ( & self , hir_id : hir:: HirId , param_ty : Ty < ' tcx > ) -> Const < ' tcx > {
2286
2297
let tcx = self . tcx ( ) ;
2287
2298
match tcx. named_bound_var ( hir_id) {
@@ -2422,8 +2433,10 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
2422
2433
}
2423
2434
}
2424
2435
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.
2436
+ /// Lowers a [`hir::Ty`] to a [`Ty`].
2437
+ ///
2438
+ /// For diagnostics' purposes we keep track of whether trait objects are
2439
+ /// borrowed like `&dyn Trait` to avoid emitting redundant errors.
2427
2440
#[ instrument( level = "debug" , skip( self ) , ret) ]
2428
2441
fn lower_ty_inner ( & self , ast_ty : & hir:: Ty < ' tcx > , borrowed : bool , in_path : bool ) -> Ty < ' tcx > {
2429
2442
let tcx = self . tcx ( ) ;
@@ -2812,7 +2825,9 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
2812
2825
}
2813
2826
2814
2827
/// 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
2828
+ /// use to summarize this type.
2829
+ ///
2830
+ /// The basic idea is that we will use the bound the user
2816
2831
/// provided, if they provided one, and otherwise search the supertypes of trait bounds
2817
2832
/// for region bounds. It may be that we can derive no bound at all, in which case
2818
2833
/// we return `None`.
0 commit comments