Skip to content

Commit 1711486

Browse files
Simplify instantiate_poly_trait_ref
1 parent f230c8a commit 1711486

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 bounder 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);
@@ -728,13 +749,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
728749

729750
// Specify type to assert that error was already reported in `Err` case.
730751
let _: Result<_, ErrorGuaranteed> = self.add_predicates_for_ast_type_binding(
731-
hir_id,
752+
trait_ref.hir_ref_id,
732753
poly_trait_ref,
733754
binding,
734755
bounds,
735756
speculative,
736757
&mut dup_bindings,
737-
binding_span.unwrap_or(binding.span),
758+
binding.span,
738759
constness,
739760
only_self_bounds,
740761
polarity,
@@ -745,67 +766,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
745766
arg_count
746767
}
747768

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

0 commit comments

Comments
 (0)