Skip to content

Commit fc010de

Browse files
banish hir::GenericBound::LangItemTrait
1 parent ad00641 commit fc010de

File tree

12 files changed

+68
-135
lines changed

12 files changed

+68
-135
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,28 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
766766
self.resolver.get_import_res(id).present_items()
767767
}
768768

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

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

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

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 10 additions & 20 deletions
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

Lines changed: 0 additions & 3 deletions
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 11 deletions
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

Lines changed: 1 addition & 36 deletions
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

Lines changed: 0 additions & 26 deletions
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

Lines changed: 0 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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_node(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

Lines changed: 8 additions & 18 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
429429
fn visit_param_bound(&mut self, b: &'v hir::GenericBound<'v>) {
430430
record_variants!(
431431
(self, b, b, Id::None, hir, GenericBound, GenericBound),
432-
[Trait, LangItemTrait, Outlives]
432+
[Trait, Outlives]
433433
);
434434
hir_visit::walk_param_bound(self, b)
435435
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4783,8 +4783,14 @@ pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
47834783
};
47844784

47854785
let future = tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
4786-
let Some(hir::GenericBound::LangItemTrait(_, _, _, generics)) = future.bounds.get(0) else {
4787-
// `async fn` should always lower to a lang item bound... but don't ICE.
4786+
let [hir::GenericBound::Trait(trait_ref, _)] = future.bounds else {
4787+
// `async fn` should always lower to a single bound... but don't ICE.
4788+
return None;
4789+
};
4790+
let Some(hir::PathSegment { args: Some(generics), .. }) =
4791+
trait_ref.trait_ref.path.segments.last()
4792+
else {
4793+
// desugaring to a single path segment for `Future<...>`.
47884794
return None;
47894795
};
47904796
let Some(hir::TypeBindingKind::Equality { term: hir::Term::Ty(future_output_ty) }) =

0 commit comments

Comments
 (0)