Skip to content

Commit 43994c2

Browse files
banish hir::GenericBound::LangItemTrait
1 parent e1e2517 commit 43994c2

File tree

12 files changed

+64
-135
lines changed

12 files changed

+64
-135
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,28 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
763763
self.resolver.get_import_res(id).present_items()
764764
}
765765

766+
fn make_lang_item_path(
767+
&mut self,
768+
lang_item: hir::LangItem,
769+
span: Span,
770+
args: Option<&'hir hir::GenericArgs<'hir>>,
771+
) -> &'hir hir::Path<'hir> {
772+
let def_id = self.tcx.require_lang_item(lang_item, Some(span));
773+
let def_kind = self.tcx.def_kind(def_id);
774+
let res = Res::Def(def_kind, def_id);
775+
self.arena.alloc(hir::Path {
776+
span,
777+
res,
778+
segments: self.arena.alloc_from_iter([hir::PathSegment {
779+
ident: Ident::new(lang_item.name(), span),
780+
hir_id: self.next_id(),
781+
res,
782+
args,
783+
infer_args: false,
784+
}]),
785+
})
786+
}
787+
766788
/// Reuses the span but adds information like the kind of the desugaring and features that are
767789
/// allowed inside this span.
768790
fn mark_span_with_reason(
@@ -1957,12 +1979,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19571979
span_ext: DUMMY_SP,
19581980
});
19591981

1960-
hir::GenericBound::LangItemTrait(
1961-
// ::std::future::Future<future_params>
1962-
hir::LangItem::Future,
1963-
self.lower_span(span),
1964-
self.next_id(),
1965-
future_args,
1982+
let span = self.lower_span(span);
1983+
hir::GenericBound::Trait(
1984+
hir::PolyTraitRef {
1985+
bound_generic_params: &[],
1986+
trait_ref: hir::TraitRef {
1987+
path: self.make_lang_item_path(hir::LangItem::Future, span, Some(future_args)),
1988+
hir_ref_id: self.next_id(),
1989+
},
1990+
span,
1991+
},
1992+
hir::TraitBoundModifier::None,
19661993
)
19671994
}
19681995

compiler/rustc_borrowck/src/diagnostics/region_name.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -773,28 +773,18 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
773773
};
774774
let opaque_ty = hir.item(id);
775775
if let hir::ItemKind::OpaqueTy(hir::OpaqueTy {
776-
bounds:
777-
[
778-
hir::GenericBound::LangItemTrait(
779-
hir::LangItem::Future,
780-
_,
781-
_,
782-
hir::GenericArgs {
783-
bindings:
784-
[
785-
hir::TypeBinding {
786-
ident: Ident { name: sym::Output, .. },
787-
kind:
788-
hir::TypeBindingKind::Equality { term: hir::Term::Ty(ty) },
789-
..
790-
},
791-
],
792-
..
793-
},
794-
),
795-
],
776+
bounds: [hir::GenericBound::Trait(trait_ref, _)],
796777
..
797778
}) = opaque_ty.kind
779+
&& let Some(segment) = trait_ref.trait_ref.path.segments.last()
780+
&& let Some(args) = segment.args
781+
&& let [
782+
hir::TypeBinding {
783+
ident: Ident { name: sym::Output, .. },
784+
kind: hir::TypeBindingKind::Equality { term: hir::Term::Ty(ty) },
785+
..
786+
},
787+
] = args.bindings
798788
{
799789
ty
800790
} 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
@@ -135,17 +135,6 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
135135
only_self_bounds,
136136
);
137137
}
138-
&hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => {
139-
self.instantiate_lang_item_trait_ref(
140-
lang_item,
141-
span,
142-
hir_id,
143-
args,
144-
param_ty,
145-
bounds,
146-
only_self_bounds,
147-
);
148-
}
149138
hir::GenericBound::Outlives(lifetime) => {
150139
let region = self.ast_region_to_region(lifetime, None);
151140
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
@@ -788,6 +788,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
788788
self.prohibit_generics(trait_ref.path.segments.split_last().unwrap().1.iter(), |_| {});
789789
self.complain_about_internal_fn_trait(span, trait_def_id, trait_segment, false);
790790

791+
// TODO: inline
791792
self.instantiate_poly_trait_ref_inner(
792793
hir_id,
793794
span,
@@ -806,42 +807,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
806807
)
807808
}
808809

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

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
@@ -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

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

47604760
let future = tcx.hir().get_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
4761-
let Some(hir::GenericBound::LangItemTrait(_, _, _, generics)) = future.bounds.get(0) else {
4762-
// `async fn` should always lower to a lang item bound... but don't ICE.
4761+
let [hir::GenericBound::Trait(trait_ref, _)] = future.bounds else {
4762+
// `async fn` should always lower to a single bound... but don't ICE.
4763+
return None;
4764+
};
4765+
let Some(hir::PathSegment { args: Some(generics), .. }) =
4766+
trait_ref.trait_ref.path.segments.last()
4767+
else {
4768+
// desugaring to a single path segment for `Future<...>`.
47634769
return None;
47644770
};
47654771
let Some(hir::TypeBindingKind::Equality { term: hir::Term::Ty(future_output_ty) }) =

0 commit comments

Comments
 (0)