Skip to content

Commit f1ea6cf

Browse files
Simplify instantiate_poly_trait_ref
1 parent 653546c commit f1ea6cf

File tree

1 file changed

+35
-75
lines changed
  • compiler/rustc_hir_analysis/src/astconv

1 file changed

+35
-75
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+35-75
Original file line numberDiff line numberDiff line change
@@ -672,36 +672,57 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
672672
)
673673
}
674674

675-
fn instantiate_poly_trait_ref_inner(
675+
/// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct
676+
/// a full trait reference. The resulting trait reference is returned. This may also generate
677+
/// auxiliary bounds, which are added to `bounds`.
678+
///
679+
/// Example:
680+
///
681+
/// ```ignore (illustrative)
682+
/// poly_trait_ref = Iterator<Item = u32>
683+
/// self_ty = Foo
684+
/// ```
685+
///
686+
/// this would return `Foo: Iterator` and add `<Foo as Iterator>::Item = u32` into `bounds`.
687+
///
688+
/// **A note on binders:** against our usual convention, there is an implied binder around
689+
/// the `self_ty` and `poly_trait_ref` parameters here. So they may reference bound regions.
690+
/// If for example you had `for<'a> Foo<'a>: Bar<'a>`, then the `self_ty` would be `Foo<'a>`
691+
/// where `'a` is a bound region at depth 0. Similarly, the `poly_trait_ref` would be
692+
/// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly,
693+
/// however.
694+
#[instrument(level = "debug", skip(self, span, constness, bounds, speculative))]
695+
pub(crate) fn instantiate_poly_trait_ref(
676696
&self,
677-
hir_id: hir::HirId,
697+
trait_ref: &hir::TraitRef<'_>,
678698
span: Span,
679-
binding_span: Option<Span>,
680699
constness: ty::BoundConstness,
681700
polarity: ty::ImplPolarity,
701+
self_ty: Ty<'tcx>,
682702
bounds: &mut Bounds<'tcx>,
683703
speculative: bool,
684-
trait_ref_span: Span,
685-
trait_def_id: DefId,
686-
trait_segment: &hir::PathSegment<'_>,
687-
args: &GenericArgs<'_>,
688-
infer_args: bool,
689-
self_ty: Ty<'tcx>,
690704
only_self_bounds: OnlySelfBounds,
691705
) -> GenericArgCountResult {
706+
let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
707+
let trait_segment = trait_ref.path.segments.last().unwrap();
708+
let args = trait_segment.args();
709+
710+
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1.iter(), |_| {});
711+
self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment, false);
712+
692713
let (generic_args, arg_count) = self.create_args_for_ast_path(
693-
trait_ref_span,
714+
trait_ref.path.span,
694715
trait_def_id,
695716
&[],
696717
trait_segment,
697718
args,
698-
infer_args,
719+
trait_segment.infer_args,
699720
Some(self_ty),
700721
constness,
701722
);
702723

703724
let tcx = self.tcx();
704-
let bound_vars = tcx.late_bound_vars(hir_id);
725+
let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
705726
debug!(?bound_vars);
706727

707728
let assoc_bindings = self.create_assoc_bindings_for_generic_args(args);
@@ -729,13 +750,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
729750

730751
// Specify type to assert that error was already reported in `Err` case.
731752
let _: Result<_, ErrorGuaranteed> = self.add_predicates_for_ast_type_binding(
732-
hir_id,
753+
trait_ref.hir_ref_id,
733754
poly_trait_ref,
734755
binding,
735756
bounds,
736757
speculative,
737758
&mut dup_bindings,
738-
binding_span.unwrap_or(binding.span),
759+
binding.span,
739760
constness,
740761
only_self_bounds,
741762
polarity,
@@ -746,67 +767,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
746767
arg_count
747768
}
748769

749-
/// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct
750-
/// a full trait reference. The resulting trait reference is returned. This may also generate
751-
/// auxiliary bounds, which are added to `bounds`.
752-
///
753-
/// Example:
754-
///
755-
/// ```ignore (illustrative)
756-
/// poly_trait_ref = Iterator<Item = u32>
757-
/// self_ty = Foo
758-
/// ```
759-
///
760-
/// this would return `Foo: Iterator` and add `<Foo as Iterator>::Item = u32` into `bounds`.
761-
///
762-
/// **A note on binders:** against our usual convention, there is an implied bounder around
763-
/// the `self_ty` and `poly_trait_ref` parameters here. So they may reference bound regions.
764-
/// If for example you had `for<'a> Foo<'a>: Bar<'a>`, then the `self_ty` would be `Foo<'a>`
765-
/// where `'a` is a bound region at depth 0. Similarly, the `poly_trait_ref` would be
766-
/// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly,
767-
/// however.
768-
#[instrument(level = "debug", skip(self, span, constness, bounds, speculative))]
769-
pub(crate) fn instantiate_poly_trait_ref(
770-
&self,
771-
trait_ref: &hir::TraitRef<'_>,
772-
span: Span,
773-
constness: ty::BoundConstness,
774-
polarity: ty::ImplPolarity,
775-
self_ty: Ty<'tcx>,
776-
bounds: &mut Bounds<'tcx>,
777-
speculative: bool,
778-
only_self_bounds: OnlySelfBounds,
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;
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;
787-
788-
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1.iter(), |_| {});
789-
self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment, false);
790-
791-
// TODO: inline
792-
self.instantiate_poly_trait_ref_inner(
793-
hir_id,
794-
span,
795-
binding_span,
796-
constness,
797-
polarity,
798-
bounds,
799-
speculative,
800-
trait_ref_span,
801-
trait_def_id,
802-
trait_segment,
803-
args,
804-
infer_args,
805-
self_ty,
806-
only_self_bounds,
807-
)
808-
}
809-
810770
fn ast_path_to_mono_trait_ref(
811771
&self,
812772
span: Span,

0 commit comments

Comments
 (0)