Skip to content

Commit 22ef04e

Browse files
committed
A bit of cleanup to astconv
1 parent 216906f commit 22ef04e

File tree

5 files changed

+181
-234
lines changed

5 files changed

+181
-234
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+118-87
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,61 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
693693
)
694694
}
695695

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+
696751
/// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct
697752
/// a full trait reference. The resulting trait reference is returned. This may also generate
698753
/// auxiliary bounds, which are added to `bounds`.
@@ -713,7 +768,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
713768
/// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly,
714769
/// however.
715770
#[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(
717772
&self,
718773
trait_ref: &hir::TraitRef<'_>,
719774
span: Span,
@@ -722,48 +777,34 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
722777
bounds: &mut Bounds<'tcx>,
723778
speculative: bool,
724779
) -> GenericArgCountResult {
780+
let hir_id = trait_ref.hir_ref_id;
781+
let binding_span = None;
782+
let trait_ref_span = trait_ref.path.span;
725783
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;
726787

727788
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1);
789+
self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment);
728790

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,
735799
trait_def_id,
800+
trait_segment,
801+
args,
802+
infer_args,
736803
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+
)
764805
}
765806

766-
pub fn instantiate_lang_item_trait_ref(
807+
pub(crate) fn instantiate_lang_item_trait_ref(
767808
&self,
768809
lang_item: hir::LangItem,
769810
span: Span,
@@ -772,36 +813,28 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
772813
self_ty: Ty<'tcx>,
773814
bounds: &mut Bounds<'tcx>,
774815
) {
816+
let binding_span = Some(span);
817+
let constness = ty::BoundConstness::NotConst;
818+
let speculative = false;
819+
let trait_ref_span = span;
775820
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;
776823

777-
let (substs, _) = self.create_substs_for_ast_path(
824+
self.instantiate_poly_trait_ref_inner(
825+
hir_id,
778826
span,
827+
binding_span,
828+
constness,
829+
bounds,
830+
speculative,
831+
trait_ref_span,
779832
trait_def_id,
780-
&[],
781-
&hir::PathSegment::invalid(),
833+
trait_segment,
782834
args,
783-
false,
784-
Some(self_ty),
835+
infer_args,
836+
self_ty,
785837
);
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-
}
805838
}
806839

807840
fn ast_path_to_mono_trait_ref(
@@ -935,45 +968,43 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
935968
/// **A note on binders:** there is an implied binder around
936969
/// `param_ty` and `ast_bounds`. See `instantiate_poly_trait_ref`
937970
/// 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>>>(
940973
&self,
941974
param_ty: Ty<'tcx>,
942-
ast_bounds: &[hir::GenericBound<'_>],
975+
ast_bounds: I,
943976
bounds: &mut Bounds<'tcx>,
944977
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
945978
) {
946979
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,
953992
param_ty,
954993
bounds,
955994
false,
956995
);
957996
}
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,
9661000
);
9671001
}
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+
}
9771008
}
9781009
}
9791010
}
@@ -1032,7 +1063,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10321063
) -> Bounds<'tcx> {
10331064
let mut bounds = Bounds::default();
10341065

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());
10361067

10371068
bounds
10381069
}
@@ -1224,7 +1255,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12241255
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
12251256
// parameter to have a skipped binder.
12261257
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());
12281259
}
12291260
}
12301261
Ok(())

0 commit comments

Comments
 (0)