Skip to content

Commit f5968c3

Browse files
banish hir::GenericBound::LangItemTrait
1 parent e78cfed commit f5968c3

File tree

12 files changed

+68
-135
lines changed

12 files changed

+68
-135
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+37-6
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,28 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
767767
self.resolver.get_import_res(id).present_items()
768768
}
769769

770+
fn make_lang_item_path(
771+
&mut self,
772+
lang_item: hir::LangItem,
773+
span: Span,
774+
args: Option<&'hir hir::GenericArgs<'hir>>,
775+
) -> &'hir hir::Path<'hir> {
776+
let def_id = self.tcx.require_lang_item(lang_item, Some(span));
777+
let def_kind = self.tcx.def_kind(def_id);
778+
let res = Res::Def(def_kind, def_id);
779+
self.arena.alloc(hir::Path {
780+
span,
781+
res,
782+
segments: self.arena.alloc_from_iter([hir::PathSegment {
783+
ident: Ident::new(lang_item.name(), span),
784+
hir_id: self.next_id(),
785+
res,
786+
args,
787+
infer_args: false,
788+
}]),
789+
})
790+
}
791+
770792
/// Reuses the span but adds information like the kind of the desugaring and features that are
771793
/// allowed inside this span.
772794
fn mark_span_with_reason(
@@ -1975,18 +1997,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19751997
CoroutineKind::AsyncGen { .. } => (sym::Item, hir::LangItem::AsyncIterator),
19761998
};
19771999

1978-
let future_args = self.arena.alloc(hir::GenericArgs {
2000+
let bound_args = self.arena.alloc(hir::GenericArgs {
19792001
args: &[],
19802002
bindings: arena_vec![self; self.assoc_ty_binding(assoc_ty_name, opaque_ty_span, output_ty)],
19812003
parenthesized: hir::GenericArgsParentheses::No,
19822004
span_ext: DUMMY_SP,
19832005
});
19842006

1985-
hir::GenericBound::LangItemTrait(
1986-
trait_lang_item,
1987-
opaque_ty_span,
1988-
self.next_id(),
1989-
future_args,
2007+
hir::GenericBound::Trait(
2008+
hir::PolyTraitRef {
2009+
bound_generic_params: &[],
2010+
trait_ref: hir::TraitRef {
2011+
path: self.make_lang_item_path(
2012+
trait_lang_item,
2013+
opaque_ty_span,
2014+
Some(bound_args),
2015+
),
2016+
hir_ref_id: self.next_id(),
2017+
},
2018+
span: opaque_ty_span,
2019+
},
2020+
hir::TraitBoundModifier::None,
19902021
)
19912022
}
19922023

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+10-20
Original file line numberDiff line numberDiff line change
@@ -788,28 +788,18 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
788788
};
789789
let opaque_ty = hir.item(id);
790790
if let hir::ItemKind::OpaqueTy(hir::OpaqueTy {
791-
bounds:
792-
[
793-
hir::GenericBound::LangItemTrait(
794-
hir::LangItem::Future,
795-
_,
796-
_,
797-
hir::GenericArgs {
798-
bindings:
799-
[
800-
hir::TypeBinding {
801-
ident: Ident { name: sym::Output, .. },
802-
kind:
803-
hir::TypeBindingKind::Equality { term: hir::Term::Ty(ty) },
804-
..
805-
},
806-
],
807-
..
808-
},
809-
),
810-
],
791+
bounds: [hir::GenericBound::Trait(trait_ref, _)],
811792
..
812793
}) = opaque_ty.kind
794+
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
795+
&& let Some(args) = segment.args
796+
&& let [
797+
hir::TypeBinding {
798+
ident: Ident { name: sym::Output, .. },
799+
kind: hir::TypeBindingKind::Equality { term: hir::Term::Ty(ty) },
800+
..
801+
},
802+
] = args.bindings
813803
{
814804
ty
815805
} else {

compiler/rustc_hir/src/hir.rs

-3
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,6 @@ pub enum TraitBoundModifier {
435435
#[derive(Clone, Copy, Debug, HashStable_Generic)]
436436
pub enum GenericBound<'hir> {
437437
Trait(PolyTraitRef<'hir>, TraitBoundModifier),
438-
// FIXME(davidtwco): Introduce `PolyTraitRef::LangItem`
439-
LangItemTrait(LangItem, Span, HirId, &'hir GenericArgs<'hir>),
440438
Outlives(&'hir Lifetime),
441439
}
442440

@@ -451,7 +449,6 @@ impl GenericBound<'_> {
451449
pub fn span(&self) -> Span {
452450
match self {
453451
GenericBound::Trait(t, ..) => t.span,
454-
GenericBound::LangItemTrait(_, span, ..) => *span,
455452
GenericBound::Outlives(l) => l.ident.span,
456453
}
457454
}

compiler/rustc_hir/src/intravisit.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1075,10 +1075,6 @@ pub fn walk_param_bound<'v, V: Visitor<'v>>(visitor: &mut V, bound: &'v GenericB
10751075
GenericBound::Trait(ref typ, _modifier) => {
10761076
visitor.visit_poly_trait_ref(typ);
10771077
}
1078-
GenericBound::LangItemTrait(_, _span, hir_id, args) => {
1079-
visitor.visit_id(hir_id);
1080-
visitor.visit_generic_args(args);
1081-
}
10821078
GenericBound::Outlives(ref lifetime) => visitor.visit_lifetime(lifetime),
10831079
}
10841080
}

compiler/rustc_hir_analysis/src/astconv/bounds.rs

-11
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,6 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
134134
only_self_bounds,
135135
);
136136
}
137-
&hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => {
138-
self.instantiate_lang_item_trait_ref(
139-
lang_item,
140-
span,
141-
hir_id,
142-
args,
143-
param_ty,
144-
bounds,
145-
only_self_bounds,
146-
);
147-
}
148137
hir::GenericBound::Outlives(lifetime) => {
149138
let region = self.ast_region_to_region(lifetime, None);
150139
bounds.push_region_bound(

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-36
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
791791
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1.iter(), |_| {});
792792
self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment, false);
793793

794+
// TODO: inline
794795
self.instantiate_poly_trait_ref_inner(
795796
hir_id,
796797
span,
@@ -809,42 +810,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
809810
)
810811
}
811812

812-
pub(crate) fn instantiate_lang_item_trait_ref(
813-
&self,
814-
lang_item: hir::LangItem,
815-
span: Span,
816-
hir_id: hir::HirId,
817-
args: &GenericArgs<'_>,
818-
self_ty: Ty<'tcx>,
819-
bounds: &mut Bounds<'tcx>,
820-
only_self_bounds: OnlySelfBounds,
821-
) {
822-
let binding_span = Some(span);
823-
let constness = ty::BoundConstness::NotConst;
824-
let speculative = false;
825-
let trait_ref_span = span;
826-
let trait_def_id = self.tcx().require_lang_item(lang_item, Some(span));
827-
let trait_segment = &hir::PathSegment::invalid();
828-
let infer_args = false;
829-
830-
self.instantiate_poly_trait_ref_inner(
831-
hir_id,
832-
span,
833-
binding_span,
834-
constness,
835-
ty::ImplPolarity::Positive,
836-
bounds,
837-
speculative,
838-
trait_ref_span,
839-
trait_def_id,
840-
trait_segment,
841-
args,
842-
infer_args,
843-
self_ty,
844-
only_self_bounds,
845-
);
846-
}
847-
848813
fn ast_path_to_mono_trait_ref(
849814
&self,
850815
span: Span,

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

-26
Original file line numberDiff line numberDiff line change
@@ -938,32 +938,6 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
938938
}
939939
}
940940

941-
fn visit_param_bound(&mut self, bound: &'tcx hir::GenericBound<'tcx>) {
942-
match bound {
943-
hir::GenericBound::LangItemTrait(_, _, hir_id, _) => {
944-
// FIXME(jackh726): This is pretty weird. `LangItemTrait` doesn't go
945-
// through the regular poly trait ref code, so we don't get another
946-
// chance to introduce a binder. For now, I'm keeping the existing logic
947-
// of "if there isn't a Binder scope above us, add one", but I
948-
// imagine there's a better way to go about this.
949-
let (binders, scope_type) = self.poly_trait_ref_binder_info();
950-
951-
self.record_late_bound_vars(*hir_id, binders);
952-
let scope = Scope::Binder {
953-
hir_id: *hir_id,
954-
bound_vars: FxIndexMap::default(),
955-
s: self.scope,
956-
scope_type,
957-
where_bound_origin: None,
958-
};
959-
self.with(scope, |this| {
960-
intravisit::walk_param_bound(this, bound);
961-
});
962-
}
963-
_ => intravisit::walk_param_bound(self, bound),
964-
}
965-
}
966-
967941
fn visit_poly_trait_ref(&mut self, trait_ref: &'tcx hir::PolyTraitRef<'tcx>) {
968942
self.visit_poly_trait_ref_inner(trait_ref, NonLifetimeBinderAllowed::Allow);
969943
}

compiler/rustc_hir_pretty/src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -2088,11 +2088,6 @@ impl<'a> State<'a> {
20882088
}
20892089
self.print_poly_trait_ref(tref);
20902090
}
2091-
GenericBound::LangItemTrait(lang_item, span, ..) => {
2092-
self.word("#[lang = \"");
2093-
self.print_ident(Ident::new(lang_item.name(), *span));
2094-
self.word("\"]");
2095-
}
20962091
GenericBound::Outlives(lt) => {
20972092
self.print_lifetime(lt);
20982093
}

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
825825
&& let hir::Node::Item(hir::Item {
826826
kind: hir::ItemKind::OpaqueTy(op_ty), ..
827827
}) = self.tcx.hir().get(item_id.hir_id())
828-
&& let [
829-
hir::GenericBound::LangItemTrait(hir::LangItem::Future, _, _, generic_args),
830-
] = op_ty.bounds
828+
&& let [hir::GenericBound::Trait(trait_ref, _)] = op_ty.bounds
829+
&& let Some(hir::PathSegment { args: Some(generic_args), .. }) =
830+
trait_ref.trait_ref.path.segments.last()
831831
&& let hir::GenericArgs { bindings: [ty_binding], .. } = generic_args
832832
&& let hir::TypeBindingKind::Equality { term: hir::Term::Ty(term) } =
833833
ty_binding.kind

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

+8-18
Original file line numberDiff line numberDiff line change
@@ -668,26 +668,16 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
668668
(
669669
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: last_bounds, .. }),
670670
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: exp_bounds, .. }),
671-
) if std::iter::zip(*last_bounds, *exp_bounds).all(|(left, right)| {
672-
match (left, right) {
673-
(
674-
hir::GenericBound::Trait(tl, ml),
675-
hir::GenericBound::Trait(tr, mr),
676-
) if tl.trait_ref.trait_def_id() == tr.trait_ref.trait_def_id()
671+
) if std::iter::zip(*last_bounds, *exp_bounds).all(|(left, right)| match (
672+
left, right,
673+
) {
674+
(hir::GenericBound::Trait(tl, ml), hir::GenericBound::Trait(tr, mr))
675+
if tl.trait_ref.trait_def_id() == tr.trait_ref.trait_def_id()
677676
&& ml == mr =>
678-
{
679-
true
680-
}
681-
(
682-
hir::GenericBound::LangItemTrait(langl, _, _, argsl),
683-
hir::GenericBound::LangItemTrait(langr, _, _, argsr),
684-
) if langl == langr => {
685-
// FIXME: consider the bounds!
686-
debug!("{:?} {:?}", argsl, argsr);
687-
true
688-
}
689-
_ => false,
677+
{
678+
true
690679
}
680+
_ => false,
691681
}) =>
692682
{
693683
StatementAsExpression::NeedsBoxing

compiler/rustc_passes/src/hir_stats.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
425425
fn visit_param_bound(&mut self, b: &'v hir::GenericBound<'v>) {
426426
record_variants!(
427427
(self, b, b, Id::None, hir, GenericBound, GenericBound),
428-
[Trait, LangItemTrait, Outlives]
428+
[Trait, Outlives]
429429
);
430430
hir_visit::walk_param_bound(self, b)
431431
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -4776,8 +4776,14 @@ pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
47764776
};
47774777

47784778
let future = tcx.hir().get_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
4779-
let Some(hir::GenericBound::LangItemTrait(_, _, _, generics)) = future.bounds.get(0) else {
4780-
// `async fn` should always lower to a lang item bound... but don't ICE.
4779+
let [hir::GenericBound::Trait(trait_ref, _)] = future.bounds else {
4780+
// `async fn` should always lower to a single bound... but don't ICE.
4781+
return None;
4782+
};
4783+
let Some(hir::PathSegment { args: Some(generics), .. }) =
4784+
trait_ref.trait_ref.path.segments.last()
4785+
else {
4786+
// desugaring to a single path segment for `Future<...>`.
47814787
return None;
47824788
};
47834789
let Some(hir::TypeBindingKind::Equality { term: hir::Term::Ty(future_output_ty) }) =

0 commit comments

Comments
 (0)