Skip to content

Commit 9f480fa

Browse files
committed
Factor out GenericBound::LangItemTrait
1 parent 58b5e22 commit 9f480fa

File tree

12 files changed

+95
-216
lines changed

12 files changed

+95
-216
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
800800
fn lower_ident(&self, ident: Ident) -> Ident {
801801
Ident::new(ident.name, self.lower_span(ident.span))
802802
}
803-
804-
fn lang_item_qpath(&mut self, lang_item: hir::LangItem, span: Span) -> hir::QPath<'hir> {
803+
fn lang_item_path(
804+
&mut self,
805+
lang_item: hir::LangItem,
806+
args: Option<&'hir hir::GenericArgs<'hir>>,
807+
span: Span,
808+
) -> &'hir hir::Path<'hir> {
805809
let def_id = self.tcx.require_lang_item(lang_item, Some(span));
806810
let res = Res::Def(lang_item.target().to_def_kind(), def_id);
807811
let name = self.tcx.item_name(def_id);
@@ -812,10 +816,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
812816
// associated items require a self segment - this works well enough
813817
hir::PathSegment::new(Ident::empty(), self.next_id(), Res::Err)
814818
});
815-
let segment = hir::PathSegment::new(Ident::new(name, span), self.next_id(), res);
819+
let segment = hir::PathSegment {
820+
ident: Ident::new(name, span),
821+
hir_id: self.next_id(),
822+
res,
823+
infer_args: args.is_none(),
824+
args,
825+
};
816826
let segments =
817827
self.arena.alloc_from_iter(self_segment.into_iter().chain(iter::once(segment)));
818-
let path = self.arena.alloc(hir::Path { span, res, segments });
828+
self.arena.alloc(hir::Path { span, res, segments })
829+
}
830+
831+
fn lang_item_qpath(&mut self, lang_item: hir::LangItem, span: Span) -> hir::QPath<'hir> {
832+
let path = self.lang_item_path(lang_item, None, span);
819833
hir::QPath::Resolved(None, path)
820834
}
821835

@@ -2088,21 +2102,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20882102
}
20892103
FnRetTy::Default(ret_ty_span) => self.arena.alloc(self.ty_tup(*ret_ty_span, &[])),
20902104
};
2091-
2105+
let span = self.lower_span(span);
20922106
// "<Output = T>"
20932107
let future_args = self.arena.alloc(hir::GenericArgs {
20942108
args: &[],
20952109
bindings: arena_vec![self; self.output_ty_binding(span, output_ty)],
20962110
parenthesized: false,
20972111
span_ext: DUMMY_SP,
20982112
});
2099-
2100-
hir::GenericBound::LangItemTrait(
2101-
// ::std::future::Future<future_params>
2102-
hir::LangItem::Future,
2103-
self.lower_span(span),
2104-
self.next_id(),
2105-
future_args,
2113+
let path = self.lang_item_path(hir::LangItem::Future, Some(future_args), span);
2114+
hir::GenericBound::Trait(
2115+
hir::PolyTraitRef {
2116+
bound_generic_params: &[],
2117+
trait_ref: hir::TraitRef { path, hir_ref_id: self.next_id() },
2118+
span,
2119+
},
2120+
hir::TraitBoundModifier::None,
21062121
)
21072122
}
21082123

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -781,22 +781,34 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
781781
if let hir::ItemKind::OpaqueTy(hir::OpaqueTy {
782782
bounds:
783783
[
784-
hir::GenericBound::LangItemTrait(
785-
hir::LangItem::Future,
786-
_,
787-
_,
788-
hir::GenericArgs {
789-
bindings:
790-
[
791-
hir::TypeBinding {
792-
ident: Ident { name: sym::Output, .. },
793-
kind:
794-
hir::TypeBindingKind::Equality { term: hir::Term::Ty(ty) },
795-
..
796-
},
797-
],
784+
hir::GenericBound::Trait(
785+
hir::PolyTraitRef {
786+
trait_ref: hir::TraitRef {
787+
path: hir::Path {
788+
segments: [
789+
..,
790+
hir::PathSegment {
791+
args: Some(hir::GenericArgs {
792+
args: [],
793+
bindings: [hir::TypeBinding {
794+
ident: Ident { name: sym::Output, .. },
795+
kind: hir::TypeBindingKind::Equality {
796+
term: hir::Term::Ty(ty)
797+
},
798+
..
799+
}],
800+
..
801+
}),
802+
..
803+
}
804+
],
805+
..
806+
},
807+
..
808+
},
798809
..
799810
},
811+
_,
800812
),
801813
],
802814
..

compiler/rustc_hir/src/hir.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::def::{CtorKind, DefKind, Res};
22
use crate::def_id::DefId;
33
pub(crate) use crate::hir_id::{HirId, ItemLocalId, OwnerId};
44
use crate::intravisit::FnKind;
5-
use crate::LangItem;
65

76
use rustc_ast as ast;
87
use rustc_ast::util::parser::ExprPrecedence;
@@ -427,8 +426,6 @@ pub enum TraitBoundModifier {
427426
#[derive(Clone, Debug, HashStable_Generic)]
428427
pub enum GenericBound<'hir> {
429428
Trait(PolyTraitRef<'hir>, TraitBoundModifier),
430-
// FIXME(davidtwco): Introduce `PolyTraitRef::LangItem`
431-
LangItemTrait(LangItem, Span, HirId, &'hir GenericArgs<'hir>),
432429
Outlives(&'hir Lifetime),
433430
}
434431

@@ -443,7 +440,6 @@ impl GenericBound<'_> {
443440
pub fn span(&self) -> Span {
444441
match self {
445442
GenericBound::Trait(t, ..) => t.span,
446-
GenericBound::LangItemTrait(_, span, ..) => *span,
447443
GenericBound::Outlives(l) => l.span,
448444
}
449445
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,10 +809,6 @@ pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericB
809809
GenericBound::Trait(ref typ, _modifier) => {
810810
visitor.visit_poly_trait_ref(typ);
811811
}
812-
GenericBound::LangItemTrait(_, _span, hir_id, args) => {
813-
visitor.visit_id(hir_id);
814-
visitor.visit_generic_args(args);
815-
}
816812
GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
817813
}
818814
}

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 36 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -642,37 +642,57 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
642642
)
643643
}
644644

645-
fn instantiate_poly_trait_ref_inner(
645+
/// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct
646+
/// a full trait reference. The resulting trait reference is returned. This may also generate
647+
/// auxiliary bounds, which are added to `bounds`.
648+
///
649+
/// Example:
650+
///
651+
/// ```ignore (illustrative)
652+
/// poly_trait_ref = Iterator<Item = u32>
653+
/// self_ty = Foo
654+
/// ```
655+
///
656+
/// this would return `Foo: Iterator` and add `<Foo as Iterator>::Item = u32` into `bounds`.
657+
///
658+
/// **A note on binders:** against our usual convention, there is an implied bounder around
659+
/// the `self_ty` and `poly_trait_ref` parameters here. So they may reference bound regions.
660+
/// If for example you had `for<'a> Foo<'a>: Bar<'a>`, then the `self_ty` would be `Foo<'a>`
661+
/// where `'a` is a bound region at depth 0. Similarly, the `poly_trait_ref` would be
662+
/// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly,
663+
/// however.
664+
#[instrument(level = "debug", skip(self, span, constness, bounds, speculative))]
665+
pub(crate) fn instantiate_poly_trait_ref(
646666
&self,
647-
hir_id: hir::HirId,
667+
trait_ref: &hir::TraitRef<'_>,
648668
span: Span,
649-
binding_span: Option<Span>,
650669
constness: ty::BoundConstness,
670+
self_ty: Ty<'tcx>,
651671
bounds: &mut Bounds<'tcx>,
652672
speculative: bool,
653-
trait_ref_span: Span,
654-
trait_def_id: DefId,
655-
trait_segment: &hir::PathSegment<'_>,
656-
args: &GenericArgs<'_>,
657-
infer_args: bool,
658-
self_ty: Ty<'tcx>,
659673
) -> GenericArgCountResult {
674+
let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
675+
let trait_segment = trait_ref.path.segments.last().unwrap();
676+
677+
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1.iter(), |_| {});
678+
self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment, false);
679+
660680
let (substs, arg_count) = self.create_substs_for_ast_path(
661-
trait_ref_span,
681+
trait_ref.path.span,
662682
trait_def_id,
663683
&[],
664684
trait_segment,
665-
args,
666-
infer_args,
685+
trait_segment.args(),
686+
trait_segment.infer_args,
667687
Some(self_ty),
668688
Some(constness),
669689
);
670690

671691
let tcx = self.tcx();
672-
let bound_vars = tcx.late_bound_vars(hir_id);
692+
let bound_vars = tcx.late_bound_vars(trait_ref.hir_ref_id);
673693
debug!(?bound_vars);
674694

675-
let assoc_bindings = self.create_assoc_bindings_for_generic_args(args);
695+
let assoc_bindings = self.create_assoc_bindings_for_generic_args(trait_segment.args());
676696

677697
let poly_trait_ref =
678698
ty::Binder::bind_with_vars(ty::TraitRef::new(trait_def_id, substs), bound_vars);
@@ -684,13 +704,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
684704
for binding in &assoc_bindings {
685705
// Specify type to assert that error was already reported in `Err` case.
686706
let _: Result<_, ErrorGuaranteed> = self.add_predicates_for_ast_type_binding(
687-
hir_id,
707+
trait_ref.hir_ref_id,
688708
poly_trait_ref,
689709
binding,
690710
bounds,
691711
speculative,
692712
&mut dup_bindings,
693-
binding_span.unwrap_or(binding.span),
713+
binding.span,
694714
constness,
695715
);
696716
// Okay to ignore `Err` because of `ErrorGuaranteed` (see above).
@@ -699,95 +719,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
699719
arg_count
700720
}
701721

702-
/// Given a trait bound like `Debug`, applies that trait bound the given self-type to construct
703-
/// a full trait reference. The resulting trait reference is returned. This may also generate
704-
/// auxiliary bounds, which are added to `bounds`.
705-
///
706-
/// Example:
707-
///
708-
/// ```ignore (illustrative)
709-
/// poly_trait_ref = Iterator<Item = u32>
710-
/// self_ty = Foo
711-
/// ```
712-
///
713-
/// this would return `Foo: Iterator` and add `<Foo as Iterator>::Item = u32` into `bounds`.
714-
///
715-
/// **A note on binders:** against our usual convention, there is an implied bounder around
716-
/// the `self_ty` and `poly_trait_ref` parameters here. So they may reference bound regions.
717-
/// If for example you had `for<'a> Foo<'a>: Bar<'a>`, then the `self_ty` would be `Foo<'a>`
718-
/// where `'a` is a bound region at depth 0. Similarly, the `poly_trait_ref` would be
719-
/// `Bar<'a>`. The returned poly-trait-ref will have this binder instantiated explicitly,
720-
/// however.
721-
#[instrument(level = "debug", skip(self, span, constness, bounds, speculative))]
722-
pub(crate) fn instantiate_poly_trait_ref(
723-
&self,
724-
trait_ref: &hir::TraitRef<'_>,
725-
span: Span,
726-
constness: ty::BoundConstness,
727-
self_ty: Ty<'tcx>,
728-
bounds: &mut Bounds<'tcx>,
729-
speculative: bool,
730-
) -> GenericArgCountResult {
731-
let hir_id = trait_ref.hir_ref_id;
732-
let binding_span = None;
733-
let trait_ref_span = trait_ref.path.span;
734-
let trait_def_id = trait_ref.trait_def_id().unwrap_or_else(|| FatalError.raise());
735-
let trait_segment = trait_ref.path.segments.last().unwrap();
736-
let args = trait_segment.args();
737-
let infer_args = trait_segment.infer_args;
738-
739-
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1.iter(), |_| {});
740-
self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment, false);
741-
742-
self.instantiate_poly_trait_ref_inner(
743-
hir_id,
744-
span,
745-
binding_span,
746-
constness,
747-
bounds,
748-
speculative,
749-
trait_ref_span,
750-
trait_def_id,
751-
trait_segment,
752-
args,
753-
infer_args,
754-
self_ty,
755-
)
756-
}
757-
758-
pub(crate) fn instantiate_lang_item_trait_ref(
759-
&self,
760-
lang_item: hir::LangItem,
761-
span: Span,
762-
hir_id: hir::HirId,
763-
args: &GenericArgs<'_>,
764-
self_ty: Ty<'tcx>,
765-
bounds: &mut Bounds<'tcx>,
766-
) {
767-
let binding_span = Some(span);
768-
let constness = ty::BoundConstness::NotConst;
769-
let speculative = false;
770-
let trait_ref_span = span;
771-
let trait_def_id = self.tcx().require_lang_item(lang_item, Some(span));
772-
let trait_segment = &hir::PathSegment::invalid();
773-
let infer_args = false;
774-
775-
self.instantiate_poly_trait_ref_inner(
776-
hir_id,
777-
span,
778-
binding_span,
779-
constness,
780-
bounds,
781-
speculative,
782-
trait_ref_span,
783-
trait_def_id,
784-
trait_segment,
785-
args,
786-
infer_args,
787-
self_ty,
788-
);
789-
}
790-
791722
fn ast_path_to_mono_trait_ref(
792723
&self,
793724
span: Span,
@@ -953,11 +884,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
953884
false,
954885
);
955886
}
956-
&hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => {
957-
self.instantiate_lang_item_trait_ref(
958-
lang_item, span, hir_id, args, param_ty, bounds,
959-
);
960-
}
961887
hir::GenericBound::Outlives(lifetime) => {
962888
let region = self.ast_region_to_region(lifetime, None);
963889
bounds

compiler/rustc_hir_analysis/src/collect/lifetimes.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,32 +1074,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
10741074
})
10751075
}
10761076

1077-
fn visit_param_bound(&mut self, bound: &'tcx hir::GenericBound<'tcx>) {
1078-
match bound {
1079-
hir::GenericBound::LangItemTrait(_, _, hir_id, _) => {
1080-
// FIXME(jackh726): This is pretty weird. `LangItemTrait` doesn't go
1081-
// through the regular poly trait ref code, so we don't get another
1082-
// chance to introduce a binder. For now, I'm keeping the existing logic
1083-
// of "if there isn't a Binder scope above us, add one", but I
1084-
// imagine there's a better way to go about this.
1085-
let (binders, scope_type) = self.poly_trait_ref_binder_info();
1086-
1087-
self.record_late_bound_vars(*hir_id, binders);
1088-
let scope = Scope::Binder {
1089-
hir_id: *hir_id,
1090-
lifetimes: FxIndexMap::default(),
1091-
s: self.scope,
1092-
scope_type,
1093-
where_bound_origin: None,
1094-
};
1095-
self.with(scope, |this| {
1096-
intravisit::walk_param_bound(this, bound);
1097-
});
1098-
}
1099-
_ => intravisit::walk_param_bound(self, bound),
1100-
}
1101-
}
1102-
11031077
fn visit_poly_trait_ref(&mut self, trait_ref: &'tcx hir::PolyTraitRef<'tcx>) {
11041078
debug!("visit_poly_trait_ref(trait_ref={:?})", trait_ref);
11051079

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,11 +2102,6 @@ impl<'a> State<'a> {
21022102
}
21032103
self.print_poly_trait_ref(tref);
21042104
}
2105-
GenericBound::LangItemTrait(lang_item, span, ..) => {
2106-
self.word("#[lang = \"");
2107-
self.print_ident(Ident::new(lang_item.name(), *span));
2108-
self.word("\"]");
2109-
}
21102105
GenericBound::Outlives(lt) => {
21112106
self.print_lifetime(lt);
21122107
}

0 commit comments

Comments
 (0)