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