Skip to content

Commit 037f6e7

Browse files
committed
A bit of cleanup to astconv
1 parent d9ec06f commit 037f6e7

File tree

5 files changed

+182
-236
lines changed

5 files changed

+182
-236
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+117-86
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,61 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
695695
)
696696
}
697697

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+
698753
/// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct
699754
/// a full trait reference. The resulting trait reference is returned. This may also generate
700755
/// auxiliary bounds, which are added to `bounds`.
@@ -715,7 +770,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
715770
/// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly,
716771
/// however.
717772
#[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(
719774
&self,
720775
trait_ref: &hir::TraitRef<'_>,
721776
span: Span,
@@ -724,48 +779,34 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
724779
bounds: &mut Bounds<'tcx>,
725780
speculative: bool,
726781
) -> GenericArgCountResult {
782+
let hir_id = trait_ref.hir_ref_id;
783+
let binding_span = None;
784+
let trait_ref_span = trait_ref.path.span;
727785
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;
728789

729790
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1);
791+
self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment);
730792

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,
737801
trait_def_id,
802+
trait_segment,
803+
args,
804+
infer_args,
738805
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+
)
766807
}
767808

768-
pub fn instantiate_lang_item_trait_ref(
809+
pub(crate) fn instantiate_lang_item_trait_ref(
769810
&self,
770811
lang_item: hir::LangItem,
771812
span: Span,
@@ -774,36 +815,28 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
774815
self_ty: Ty<'tcx>,
775816
bounds: &mut Bounds<'tcx>,
776817
) {
818+
let binding_span = Some(span);
819+
let constness = Constness::NotConst;
820+
let speculative = false;
821+
let trait_ref_span = span;
777822
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;
778825

779-
let (substs, _) = self.create_substs_for_ast_path(
826+
self.instantiate_poly_trait_ref_inner(
827+
hir_id,
780828
span,
829+
binding_span,
830+
constness,
831+
bounds,
832+
speculative,
833+
trait_ref_span,
781834
trait_def_id,
782-
&[],
783-
&hir::PathSegment::invalid(),
835+
trait_segment,
784836
args,
785-
false,
786-
Some(self_ty),
837+
infer_args,
838+
self_ty,
787839
);
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-
}
807840
}
808841

809842
fn ast_path_to_mono_trait_ref(
@@ -937,46 +970,44 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
937970
/// **A note on binders:** there is an implied binder around
938971
/// `param_ty` and `ast_bounds`. See `instantiate_poly_trait_ref`
939972
/// 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>>>(
942975
&self,
943976
param_ty: Ty<'tcx>,
944-
ast_bounds: &[hir::GenericBound<'_>],
977+
ast_bounds: I,
945978
bounds: &mut Bounds<'tcx>,
946979
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
947980
) {
948981
let constness = self.default_constness_for_trait_bounds();
949982
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,
955994
constness,
956995
param_ty,
957996
bounds,
958997
false,
959998
);
960999
}
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,
9691003
);
9701004
}
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+
}
9801011
}
9811012
}
9821013
}
@@ -1035,7 +1066,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
10351066
) -> Bounds<'tcx> {
10361067
let mut bounds = Bounds::default();
10371068

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

10401071
bounds
10411072
}
@@ -1227,7 +1258,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
12271258
// Calling `skip_binder` is okay, because `add_bounds` expects the `param_ty`
12281259
// parameter to have a skipped binder.
12291260
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());
12311262
}
12321263
}
12331264
Ok(())

0 commit comments

Comments
 (0)