Skip to content

Commit 1bbddd9

Browse files
banish hir::GenericBound::LangItemTrait
1 parent 74cd754 commit 1bbddd9

File tree

12 files changed

+70
-135
lines changed

12 files changed

+70
-135
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+39-6
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,29 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
730730
self.resolver.get_import_res(id).present_items()
731731
}
732732

733+
fn make_lang_item_path(
734+
&mut self,
735+
lang_item: hir::LangItem,
736+
def_kind: DefKind,
737+
span: Span,
738+
args: Option<&'hir hir::GenericArgs<'hir>>,
739+
) -> &'hir hir::Path<'hir> {
740+
let def_id = self.tcx.require_lang_item(lang_item, Some(span));
741+
// FIXME: def_kind could be deduced from the LangItem's Target.
742+
let res = Res::Def(def_kind, def_id);
743+
self.arena.alloc(hir::Path {
744+
span,
745+
res,
746+
segments: self.arena.alloc_from_iter([hir::PathSegment {
747+
ident: Ident::new(lang_item.name(), span),
748+
hir_id: self.next_id(),
749+
res,
750+
args,
751+
infer_args: false,
752+
}]),
753+
})
754+
}
755+
733756
fn diagnostic(&self) -> &Handler {
734757
self.tcx.sess.diagnostic()
735758
}
@@ -1918,12 +1941,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19181941
span_ext: DUMMY_SP,
19191942
});
19201943

1921-
hir::GenericBound::LangItemTrait(
1922-
// ::std::future::Future<future_params>
1923-
hir::LangItem::Future,
1924-
self.lower_span(span),
1925-
self.next_id(),
1926-
future_args,
1944+
let span = self.lower_span(span);
1945+
hir::GenericBound::Trait(
1946+
hir::PolyTraitRef {
1947+
bound_generic_params: &[],
1948+
trait_ref: hir::TraitRef {
1949+
path: self.make_lang_item_path(
1950+
hir::LangItem::Future,
1951+
DefKind::Trait,
1952+
span,
1953+
Some(future_args),
1954+
),
1955+
hir_ref_id: self.next_id(),
1956+
},
1957+
span,
1958+
},
1959+
hir::TraitBoundModifier::None,
19271960
)
19281961
}
19291962

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+10-20
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

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

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

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

808-
pub(crate) fn instantiate_lang_item_trait_ref(
809-
&self,
810-
lang_item: hir::LangItem,
811-
span: Span,
812-
hir_id: hir::HirId,
813-
args: &GenericArgs<'_>,
814-
self_ty: Ty<'tcx>,
815-
bounds: &mut Bounds<'tcx>,
816-
only_self_bounds: OnlySelfBounds,
817-
) {
818-
let binding_span = Some(span);
819-
let constness = ty::BoundConstness::NotConst;
820-
let speculative = false;
821-
let trait_ref_span = span;
822-
let trait_def_id = self.tcx().require_lang_item(lang_item, Some(span));
823-
let trait_segment = &hir::PathSegment::invalid();
824-
let infer_args = false;
825-
826-
self.instantiate_poly_trait_ref_inner(
827-
hir_id,
828-
span,
829-
binding_span,
830-
constness,
831-
ty::ImplPolarity::Positive,
832-
bounds,
833-
speculative,
834-
trait_ref_span,
835-
trait_def_id,
836-
trait_segment,
837-
args,
838-
infer_args,
839-
self_ty,
840-
only_self_bounds,
841-
);
842-
}
843-
844809
fn ast_path_to_mono_trait_ref(
845810
&self,
846811
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
@@ -2087,11 +2087,6 @@ impl<'a> State<'a> {
20872087
}
20882088
self.print_poly_trait_ref(tref);
20892089
}
2090-
GenericBound::LangItemTrait(lang_item, span, ..) => {
2091-
self.word("#[lang = \"");
2092-
self.print_ident(Ident::new(lang_item.name(), *span));
2093-
self.word("\"]");
2094-
}
20952090
GenericBound::Outlives(lt) => {
20962091
self.print_lifetime(lt);
20972092
}

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -805,9 +805,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
805805
&& let hir::Node::Item(hir::Item {
806806
kind: hir::ItemKind::OpaqueTy(op_ty), ..
807807
}) = self.tcx.hir().get(item_id.hir_id())
808-
&& let [
809-
hir::GenericBound::LangItemTrait(hir::LangItem::Future, _, _, generic_args),
810-
] = op_ty.bounds
808+
&& let [hir::GenericBound::Trait(trait_ref, _)] = op_ty.bounds
809+
&& let Some(hir::PathSegment { args: Some(generic_args), .. }) =
810+
trait_ref.trait_ref.path.segments.last()
811811
&& let hir::GenericArgs { bindings: [ty_binding], .. } = generic_args
812812
&& let hir::TypeBindingKind::Equality { term: hir::Term::Ty(term) } =
813813
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
@@ -411,7 +411,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
411411
fn visit_param_bound(&mut self, b: &'v hir::GenericBound<'v>) {
412412
record_variants!(
413413
(self, b, b, Id::None, hir, GenericBound, GenericBound),
414-
[Trait, LangItemTrait, Outlives]
414+
[Trait, Outlives]
415415
);
416416
hir_visit::walk_param_bound(self, b)
417417
}

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -4607,8 +4607,14 @@ pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
46074607
};
46084608

46094609
let future = tcx.hir().get_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
4610-
let Some(hir::GenericBound::LangItemTrait(_, _, _, generics)) = future.bounds.get(0) else {
4611-
// `async fn` should always lower to a lang item bound... but don't ICE.
4610+
let [hir::GenericBound::Trait(trait_ref, _)] = future.bounds else {
4611+
// `async fn` should always lower to a single bound... but don't ICE.
4612+
return None;
4613+
};
4614+
let Some(hir::PathSegment { args: Some(generics), .. }) =
4615+
trait_ref.trait_ref.path.segments.last()
4616+
else {
4617+
// desugaring to a single path segment for `Future<...>`.
46124618
return None;
46134619
};
46144620
let Some(hir::TypeBindingKind::Equality { term: hir::Term::Ty(future_output_ty) }) =

0 commit comments

Comments
 (0)