@@ -693,6 +693,61 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
693
693
)
694
694
}
695
695
696
+ fn instantiate_poly_trait_ref_inner (
697
+ & self ,
698
+ hir_id : hir:: HirId ,
699
+ span : Span ,
700
+ binding_span : Option < Span > ,
701
+ constness : ty:: BoundConstness ,
702
+ bounds : & mut Bounds < ' tcx > ,
703
+ speculative : bool ,
704
+ trait_ref_span : Span ,
705
+ trait_def_id : DefId ,
706
+ trait_segment : & hir:: PathSegment < ' _ > ,
707
+ args : & GenericArgs < ' _ > ,
708
+ infer_args : bool ,
709
+ self_ty : Ty < ' tcx > ,
710
+ ) -> GenericArgCountResult {
711
+ let ( substs, arg_count) = self . create_substs_for_ast_path (
712
+ trait_ref_span,
713
+ trait_def_id,
714
+ & [ ] ,
715
+ trait_segment,
716
+ args,
717
+ infer_args,
718
+ Some ( self_ty) ,
719
+ ) ;
720
+
721
+ let tcx = self . tcx ( ) ;
722
+ let bound_vars = tcx. late_bound_vars ( hir_id) ;
723
+ debug ! ( ?bound_vars) ;
724
+
725
+ let assoc_bindings = self . create_assoc_bindings_for_generic_args ( args) ;
726
+
727
+ let poly_trait_ref =
728
+ ty:: Binder :: bind_with_vars ( ty:: TraitRef :: new ( trait_def_id, substs) , bound_vars) ;
729
+
730
+ debug ! ( ?poly_trait_ref, ?assoc_bindings) ;
731
+ bounds. trait_bounds . push ( ( poly_trait_ref, span, constness) ) ;
732
+
733
+ let mut dup_bindings = FxHashMap :: default ( ) ;
734
+ for binding in & assoc_bindings {
735
+ // Specify type to assert that error was already reported in `Err` case.
736
+ let _: Result < _ , ErrorReported > = self . add_predicates_for_ast_type_binding (
737
+ hir_id,
738
+ poly_trait_ref,
739
+ binding,
740
+ bounds,
741
+ speculative,
742
+ & mut dup_bindings,
743
+ binding_span. unwrap_or ( binding. span ) ,
744
+ ) ;
745
+ // Okay to ignore `Err` because of `ErrorReported` (see above).
746
+ }
747
+
748
+ arg_count
749
+ }
750
+
696
751
/// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct
697
752
/// a full trait reference. The resulting trait reference is returned. This may also generate
698
753
/// auxiliary bounds, which are added to `bounds`.
@@ -713,7 +768,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
713
768
/// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly,
714
769
/// however.
715
770
#[ tracing:: instrument( level = "debug" , skip( self , span, constness, bounds, speculative) ) ]
716
- pub fn instantiate_poly_trait_ref (
771
+ pub ( crate ) fn instantiate_poly_trait_ref (
717
772
& self ,
718
773
trait_ref : & hir:: TraitRef < ' _ > ,
719
774
span : Span ,
@@ -722,48 +777,34 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
722
777
bounds : & mut Bounds < ' tcx > ,
723
778
speculative : bool ,
724
779
) -> GenericArgCountResult {
780
+ let hir_id = trait_ref. hir_ref_id ;
781
+ let binding_span = None ;
782
+ let trait_ref_span = trait_ref. path . span ;
725
783
let trait_def_id = trait_ref. trait_def_id ( ) . unwrap_or_else ( || FatalError . raise ( ) ) ;
784
+ let trait_segment = trait_ref. path . segments . last ( ) . unwrap ( ) ;
785
+ let args = trait_segment. args ( ) ;
786
+ let infer_args = trait_segment. infer_args ;
726
787
727
788
self . prohibit_generics ( trait_ref. path . segments . split_last ( ) . unwrap ( ) . 1 ) ;
789
+ self . complain_about_internal_fn_trait ( span, trait_def_id, trait_segment) ;
728
790
729
- let tcx = self . tcx ( ) ;
730
- let bound_vars = tcx. late_bound_vars ( trait_ref. hir_ref_id ) ;
731
- debug ! ( ?bound_vars) ;
732
-
733
- let ( substs, arg_count) = self . create_substs_for_ast_trait_ref (
734
- trait_ref. path . span ,
791
+ self . instantiate_poly_trait_ref_inner (
792
+ hir_id,
793
+ span,
794
+ binding_span,
795
+ constness,
796
+ bounds,
797
+ speculative,
798
+ trait_ref_span,
735
799
trait_def_id,
800
+ trait_segment,
801
+ args,
802
+ infer_args,
736
803
self_ty,
737
- trait_ref. path . segments . last ( ) . unwrap ( ) ,
738
- ) ;
739
- let assoc_bindings = self
740
- . create_assoc_bindings_for_generic_args ( trait_ref. path . segments . last ( ) . unwrap ( ) . args ( ) ) ;
741
-
742
- let poly_trait_ref =
743
- ty:: Binder :: bind_with_vars ( ty:: TraitRef :: new ( trait_def_id, substs) , bound_vars) ;
744
-
745
- debug ! ( ?poly_trait_ref, ?assoc_bindings) ;
746
- bounds. trait_bounds . push ( ( poly_trait_ref, span, constness) ) ;
747
-
748
- let mut dup_bindings = FxHashMap :: default ( ) ;
749
- for binding in & assoc_bindings {
750
- // Specify type to assert that error was already reported in `Err` case.
751
- let _: Result < _ , ErrorReported > = self . add_predicates_for_ast_type_binding (
752
- trait_ref. hir_ref_id ,
753
- poly_trait_ref,
754
- binding,
755
- bounds,
756
- speculative,
757
- & mut dup_bindings,
758
- binding. span ,
759
- ) ;
760
- // Okay to ignore `Err` because of `ErrorReported` (see above).
761
- }
762
-
763
- arg_count
804
+ )
764
805
}
765
806
766
- pub fn instantiate_lang_item_trait_ref (
807
+ pub ( crate ) fn instantiate_lang_item_trait_ref (
767
808
& self ,
768
809
lang_item : hir:: LangItem ,
769
810
span : Span ,
@@ -772,36 +813,28 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
772
813
self_ty : Ty < ' tcx > ,
773
814
bounds : & mut Bounds < ' tcx > ,
774
815
) {
816
+ let binding_span = Some ( span) ;
817
+ let constness = ty:: BoundConstness :: NotConst ;
818
+ let speculative = false ;
819
+ let trait_ref_span = span;
775
820
let trait_def_id = self . tcx ( ) . require_lang_item ( lang_item, Some ( span) ) ;
821
+ let trait_segment = & hir:: PathSegment :: invalid ( ) ;
822
+ let infer_args = false ;
776
823
777
- let ( substs, _) = self . create_substs_for_ast_path (
824
+ self . instantiate_poly_trait_ref_inner (
825
+ hir_id,
778
826
span,
827
+ binding_span,
828
+ constness,
829
+ bounds,
830
+ speculative,
831
+ trait_ref_span,
779
832
trait_def_id,
780
- & [ ] ,
781
- & hir:: PathSegment :: invalid ( ) ,
833
+ trait_segment,
782
834
args,
783
- false ,
784
- Some ( self_ty) ,
835
+ infer_args ,
836
+ self_ty,
785
837
) ;
786
- let assoc_bindings = self . create_assoc_bindings_for_generic_args ( args) ;
787
- let tcx = self . tcx ( ) ;
788
- let bound_vars = tcx. late_bound_vars ( hir_id) ;
789
- let poly_trait_ref =
790
- ty:: Binder :: bind_with_vars ( ty:: TraitRef :: new ( trait_def_id, substs) , bound_vars) ;
791
- bounds. trait_bounds . push ( ( poly_trait_ref, span, ty:: BoundConstness :: NotConst ) ) ;
792
-
793
- let mut dup_bindings = FxHashMap :: default ( ) ;
794
- for binding in assoc_bindings {
795
- let _: Result < _ , ErrorReported > = self . add_predicates_for_ast_type_binding (
796
- hir_id,
797
- poly_trait_ref,
798
- & binding,
799
- bounds,
800
- false ,
801
- & mut dup_bindings,
802
- span,
803
- ) ;
804
- }
805
838
}
806
839
807
840
fn ast_path_to_mono_trait_ref (
@@ -935,45 +968,43 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
935
968
/// **A note on binders:** there is an implied binder around
936
969
/// `param_ty` and `ast_bounds`. See `instantiate_poly_trait_ref`
937
970
/// for more details.
938
- #[ tracing:: instrument( level = "debug" , skip( self , bounds) ) ]
939
- fn add_bounds (
971
+ #[ tracing:: instrument( level = "debug" , skip( self , ast_bounds , bounds) ) ]
972
+ pub ( crate ) fn add_bounds < ' hir , I : Iterator < Item = & ' hir hir :: GenericBound < ' hir > > > (
940
973
& self ,
941
974
param_ty : Ty < ' tcx > ,
942
- ast_bounds : & [ hir :: GenericBound < ' _ > ] ,
975
+ ast_bounds : I ,
943
976
bounds : & mut Bounds < ' tcx > ,
944
977
bound_vars : & ' tcx ty:: List < ty:: BoundVariableKind > ,
945
978
) {
946
979
for ast_bound in ast_bounds {
947
- match * ast_bound {
948
- hir:: GenericBound :: Trait ( ref b, hir:: TraitBoundModifier :: None ) => {
949
- self . instantiate_poly_trait_ref (
950
- & b. trait_ref ,
951
- b. span ,
952
- ty:: BoundConstness :: NotConst ,
980
+ match ast_bound {
981
+ hir:: GenericBound :: Trait ( poly_trait_ref, modifier) => {
982
+ let constness = match modifier {
983
+ hir:: TraitBoundModifier :: MaybeConst => ty:: BoundConstness :: ConstIfConst ,
984
+ hir:: TraitBoundModifier :: None => ty:: BoundConstness :: NotConst ,
985
+ hir:: TraitBoundModifier :: Maybe => continue ,
986
+ } ;
987
+
988
+ let _ = self . instantiate_poly_trait_ref (
989
+ & poly_trait_ref. trait_ref ,
990
+ poly_trait_ref. span ,
991
+ constness,
953
992
param_ty,
954
993
bounds,
955
994
false ,
956
995
) ;
957
996
}
958
- hir:: GenericBound :: Trait ( ref b, hir:: TraitBoundModifier :: MaybeConst ) => {
959
- self . instantiate_poly_trait_ref (
960
- & b. trait_ref ,
961
- b. span ,
962
- ty:: BoundConstness :: ConstIfConst ,
963
- param_ty,
964
- bounds,
965
- false ,
997
+ & hir:: GenericBound :: LangItemTrait ( lang_item, span, hir_id, args) => {
998
+ self . instantiate_lang_item_trait_ref (
999
+ lang_item, span, hir_id, args, param_ty, bounds,
966
1000
) ;
967
1001
}
968
- hir:: GenericBound :: Trait ( _, hir:: TraitBoundModifier :: Maybe ) => { }
969
- hir:: GenericBound :: LangItemTrait ( lang_item, span, hir_id, args) => self
970
- . instantiate_lang_item_trait_ref (
971
- lang_item, span, hir_id, args, param_ty, bounds,
972
- ) ,
973
- hir:: GenericBound :: Outlives ( ref l) => bounds. region_bounds . push ( (
974
- ty:: Binder :: bind_with_vars ( self . ast_region_to_region ( l, None ) , bound_vars) ,
975
- l. span ,
976
- ) ) ,
1002
+ hir:: GenericBound :: Outlives ( lifetime) => {
1003
+ let region = self . ast_region_to_region ( lifetime, None ) ;
1004
+ bounds
1005
+ . region_bounds
1006
+ . push ( ( ty:: Binder :: bind_with_vars ( region, bound_vars) , lifetime. span ) ) ;
1007
+ }
977
1008
}
978
1009
}
979
1010
}
@@ -1032,7 +1063,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1032
1063
) -> Bounds < ' tcx > {
1033
1064
let mut bounds = Bounds :: default ( ) ;
1034
1065
1035
- self . add_bounds ( param_ty, ast_bounds, & mut bounds, ty:: List :: empty ( ) ) ;
1066
+ self . add_bounds ( param_ty, ast_bounds. iter ( ) , & mut bounds, ty:: List :: empty ( ) ) ;
1036
1067
1037
1068
bounds
1038
1069
}
@@ -1224,7 +1255,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
1224
1255
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
1225
1256
// parameter to have a skipped binder.
1226
1257
let param_ty = tcx. mk_ty ( ty:: Projection ( projection_ty. skip_binder ( ) ) ) ;
1227
- self . add_bounds ( param_ty, ast_bounds, bounds, candidate. bound_vars ( ) ) ;
1258
+ self . add_bounds ( param_ty, ast_bounds. iter ( ) , bounds, candidate. bound_vars ( ) ) ;
1228
1259
}
1229
1260
}
1230
1261
Ok ( ( ) )
0 commit comments