Skip to content

Commit 9ec36f5

Browse files
Rollup merge of #106739 - WaffleLapkin:astconv, r=estebank
Remove `<dyn AstConv<'tcx>>::fun(c, ...)` calls in favour of `c.astconv().fun(...)` This removes the need for <>><><><<>> dances and makes the code a bit nicer. Not sure if `astconv` is the best name though, maybe someone has a better idea?
2 parents d711394 + d642781 commit 9ec36f5

File tree

13 files changed

+628
-664
lines changed

13 files changed

+628
-664
lines changed

compiler/rustc_hir_analysis/src/astconv/generics.rs

+558-576
Large diffs are not rendered by default.

compiler/rustc_hir_analysis/src/astconv/mod.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
//! instance of `AstConv`.
44
55
mod errors;
6-
mod generics;
6+
pub mod generics;
77

8+
use crate::astconv::generics::{
9+
check_generic_arg_count, create_substs_for_generic_args, prohibit_assoc_ty_binding,
10+
};
811
use crate::bounds::Bounds;
912
use crate::collect::HirPlaceholderCollector;
1013
use crate::errors::{
@@ -120,6 +123,13 @@ pub trait AstConv<'tcx> {
120123
fn set_tainted_by_errors(&self, e: ErrorGuaranteed);
121124

122125
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
126+
127+
fn astconv(&self) -> &dyn AstConv<'tcx>
128+
where
129+
Self: Sized,
130+
{
131+
self
132+
}
123133
}
124134

125135
#[derive(Debug)]
@@ -279,7 +289,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
279289
ty::BoundConstness::NotConst,
280290
);
281291
if let Some(b) = item_segment.args().bindings.first() {
282-
Self::prohibit_assoc_ty_binding(self.tcx(), b.span);
292+
prohibit_assoc_ty_binding(self.tcx(), b.span);
283293
}
284294

285295
substs
@@ -349,7 +359,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
349359
assert!(self_ty.is_none());
350360
}
351361

352-
let arg_count = Self::check_generic_arg_count(
362+
let arg_count = check_generic_arg_count(
353363
tcx,
354364
span,
355365
def_id,
@@ -524,7 +534,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
524534
inferred_params: vec![],
525535
infer_args,
526536
};
527-
let substs = Self::create_substs_for_generic_args(
537+
let substs = create_substs_for_generic_args(
528538
tcx,
529539
def_id,
530540
parent_substs,
@@ -610,7 +620,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
610620
);
611621

612622
if let Some(b) = item_segment.args().bindings.first() {
613-
Self::prohibit_assoc_ty_binding(self.tcx(), b.span);
623+
prohibit_assoc_ty_binding(self.tcx(), b.span);
614624
}
615625

616626
args
@@ -804,7 +814,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
804814
constness,
805815
);
806816
if let Some(b) = trait_segment.args().bindings.first() {
807-
Self::prohibit_assoc_ty_binding(self.tcx(), b.span);
817+
prohibit_assoc_ty_binding(self.tcx(), b.span);
808818
}
809819
self.tcx().mk_trait_ref(trait_def_id, substs)
810820
}
@@ -2301,7 +2311,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23012311
for segment in segments {
23022312
// Only emit the first error to avoid overloading the user with error messages.
23032313
if let Some(b) = segment.args().bindings.first() {
2304-
Self::prohibit_assoc_ty_binding(self.tcx(), b.span);
2314+
prohibit_assoc_ty_binding(self.tcx(), b.span);
23052315
return true;
23062316
}
23072317
}

compiler/rustc_hir_analysis/src/collect.rs

+10-27
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl<'tcx> ItemCtxt<'tcx> {
351351
}
352352

353353
pub fn to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
354-
<dyn AstConv<'_>>::ast_ty_to_ty(self, ast_ty)
354+
self.astconv().ast_ty_to_ty(ast_ty)
355355
}
356356

357357
pub fn hir_id(&self) -> hir::HirId {
@@ -413,8 +413,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
413413
poly_trait_ref: ty::PolyTraitRef<'tcx>,
414414
) -> Ty<'tcx> {
415415
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
416-
let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
417-
self,
416+
let item_substs = self.astconv().create_substs_for_associated_item(
418417
span,
419418
item_def_id,
420419
item_segment,
@@ -1112,8 +1111,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
11121111
tcx.hir().get_parent(hir_id)
11131112
&& i.of_trait.is_some()
11141113
{
1115-
<dyn AstConv<'_>>::ty_of_fn(
1116-
&icx,
1114+
icx.astconv().ty_of_fn(
11171115
hir_id,
11181116
sig.header.unsafety,
11191117
sig.header.abi,
@@ -1130,15 +1128,9 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
11301128
kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
11311129
generics,
11321130
..
1133-
}) => <dyn AstConv<'_>>::ty_of_fn(
1134-
&icx,
1135-
hir_id,
1136-
header.unsafety,
1137-
header.abi,
1138-
decl,
1139-
Some(generics),
1140-
None,
1141-
),
1131+
}) => {
1132+
icx.astconv().ty_of_fn(hir_id, header.unsafety, header.abi, decl, Some(generics), None)
1133+
}
11421134

11431135
ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
11441136
let abi = tcx.hir().get_foreign_abi(hir_id);
@@ -1244,8 +1236,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
12441236

12451237
ty::Binder::dummy(fn_sig)
12461238
}
1247-
None => <dyn AstConv<'_>>::ty_of_fn(
1248-
icx,
1239+
None => icx.astconv().ty_of_fn(
12491240
hir_id,
12501241
sig.header.unsafety,
12511242
sig.header.abi,
@@ -1354,8 +1345,7 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
13541345
match item.kind {
13551346
hir::ItemKind::Impl(ref impl_) => impl_.of_trait.as_ref().map(|ast_trait_ref| {
13561347
let selfty = tcx.type_of(def_id);
1357-
<dyn AstConv<'_>>::instantiate_mono_trait_ref(
1358-
&icx,
1348+
icx.astconv().instantiate_mono_trait_ref(
13591349
ast_trait_ref,
13601350
selfty,
13611351
check_impl_constness(tcx, impl_.constness, ast_trait_ref),
@@ -1485,15 +1475,8 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
14851475
hir::Unsafety::Unsafe
14861476
};
14871477
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1488-
let fty = <dyn AstConv<'_>>::ty_of_fn(
1489-
&ItemCtxt::new(tcx, def_id),
1490-
hir_id,
1491-
unsafety,
1492-
abi,
1493-
decl,
1494-
None,
1495-
None,
1496-
);
1478+
let fty =
1479+
ItemCtxt::new(tcx, def_id).astconv().ty_of_fn(hir_id, unsafety, abi, decl, None, None);
14971480

14981481
// Feature gate SIMD types in FFI, since I am not sure that the
14991482
// ABIs are handled at all correctly. -huonw

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ fn associated_type_bounds<'tcx>(
2626
);
2727

2828
let icx = ItemCtxt::new(tcx, assoc_item_def_id);
29-
let mut bounds = <dyn AstConv<'_>>::compute_bounds(&icx, item_ty, ast_bounds);
29+
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
3030
// Associated types are implicitly sized unless a `?Sized` bound is found
31-
<dyn AstConv<'_>>::add_implicitly_sized(&icx, &mut bounds, item_ty, ast_bounds, None, span);
31+
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
3232

3333
let trait_def_id = tcx.parent(assoc_item_def_id);
3434
let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id.expect_local());
@@ -70,9 +70,9 @@ fn opaque_type_bounds<'tcx>(
7070
};
7171

7272
let icx = ItemCtxt::new(tcx, opaque_def_id);
73-
let mut bounds = <dyn AstConv<'_>>::compute_bounds(&icx, item_ty, ast_bounds);
73+
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
7474
// Opaque types are implicitly sized unless a `?Sized` bound is found
75-
<dyn AstConv<'_>>::add_implicitly_sized(&icx, &mut bounds, item_ty, ast_bounds, None, span);
75+
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
7676
debug!(?bounds);
7777

7878
tcx.arena.alloc_from_iter(bounds.predicates())

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+7-19
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
162162

163163
let mut bounds = Bounds::default();
164164
// Params are implicitly sized unless a `?Sized` bound is found
165-
<dyn AstConv<'_>>::add_implicitly_sized(
166-
&icx,
165+
icx.astconv().add_implicitly_sized(
167166
&mut bounds,
168167
param_ty,
169168
&[],
@@ -211,22 +210,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
211210
}
212211

213212
let mut bounds = Bounds::default();
214-
<dyn AstConv<'_>>::add_bounds(
215-
&icx,
216-
ty,
217-
bound_pred.bounds.iter(),
218-
&mut bounds,
219-
bound_vars,
220-
);
213+
icx.astconv().add_bounds(ty, bound_pred.bounds.iter(), &mut bounds, bound_vars);
221214
predicates.extend(bounds.predicates());
222215
}
223216

224217
hir::WherePredicate::RegionPredicate(region_pred) => {
225-
let r1 = <dyn AstConv<'_>>::ast_region_to_region(&icx, &region_pred.lifetime, None);
218+
let r1 = icx.astconv().ast_region_to_region(&region_pred.lifetime, None);
226219
predicates.extend(region_pred.bounds.iter().map(|bound| {
227220
let (r2, span) = match bound {
228221
hir::GenericBound::Outlives(lt) => {
229-
(<dyn AstConv<'_>>::ast_region_to_region(&icx, lt, None), lt.ident.span)
222+
(icx.astconv().ast_region_to_region(lt, None), lt.ident.span)
230223
}
231224
_ => bug!(),
232225
};
@@ -279,7 +272,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
279272
debug!(?lifetimes);
280273
for (arg, duplicate) in std::iter::zip(lifetimes, ast_generics.params) {
281274
let hir::GenericArg::Lifetime(arg) = arg else { bug!() };
282-
let orig_region = <dyn AstConv<'_>>::ast_region_to_region(&icx, &arg, None);
275+
let orig_region = icx.astconv().ast_region_to_region(&arg, None);
283276
if !matches!(orig_region.kind(), ty::ReEarlyBound(..)) {
284277
// Only early-bound regions can point to the original generic parameter.
285278
continue;
@@ -527,14 +520,9 @@ pub(super) fn super_predicates_that_define_assoc_type(
527520
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
528521
let self_param_ty = tcx.types.self_param;
529522
let superbounds1 = if let Some(assoc_name) = assoc_name {
530-
<dyn AstConv<'_>>::compute_bounds_that_match_assoc_type(
531-
&icx,
532-
self_param_ty,
533-
bounds,
534-
assoc_name,
535-
)
523+
icx.astconv().compute_bounds_that_match_assoc_type(self_param_ty, bounds, assoc_name)
536524
} else {
537-
<dyn AstConv<'_>>::compute_bounds(&icx, self_param_ty, bounds)
525+
icx.astconv().compute_bounds(self_param_ty, bounds)
538526
};
539527

540528
let superbounds1 = superbounds1.predicates();

compiler/rustc_hir_analysis/src/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
545545
// scope. This is derived from the enclosing item-like thing.
546546
let env_def_id = tcx.hir().get_parent_item(hir_ty.hir_id);
547547
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id());
548-
<dyn AstConv<'_>>::ast_ty_to_ty(&item_cx, hir_ty)
548+
item_cx.astconv().ast_ty_to_ty(hir_ty)
549549
}
550550

551551
pub fn hir_trait_to_predicates<'tcx>(
@@ -559,8 +559,7 @@ pub fn hir_trait_to_predicates<'tcx>(
559559
let env_def_id = tcx.hir().get_parent_item(hir_trait.hir_ref_id);
560560
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id());
561561
let mut bounds = Bounds::default();
562-
let _ = <dyn AstConv<'_>>::instantiate_poly_trait_ref(
563-
&item_cx,
562+
let _ = &item_cx.astconv().instantiate_poly_trait_ref(
564563
hir_trait,
565564
DUMMY_SP,
566565
ty::BoundConstness::NotConst,

compiler/rustc_hir_typeck/src/coercion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18071807
// Get the return type.
18081808
&& let hir::TyKind::OpaqueDef(..) = ty.kind
18091809
{
1810-
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty);
1810+
let ty = fcx.astconv().ast_ty_to_ty( ty);
18111811
// Get the `impl Trait`'s `DefId`.
18121812
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = ty.kind()
18131813
// Get the `impl Trait`'s `Item` so that we can get its trait bounds and
@@ -1866,7 +1866,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18661866
fn is_return_ty_unsized<'a>(&self, fcx: &FnCtxt<'a, 'tcx>, blk_id: hir::HirId) -> bool {
18671867
if let Some((fn_decl, _)) = fcx.get_fn_decl(blk_id)
18681868
&& let hir::FnRetTy::Return(ty) = fn_decl.output
1869-
&& let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty)
1869+
&& let ty = fcx.astconv().ast_ty_to_ty( ty)
18701870
&& let ty::Dynamic(..) = ty.kind()
18711871
{
18721872
return true;

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
1010
use rustc_hir::def_id::DefId;
1111
use rustc_hir::lang_items::LangItem;
1212
use rustc_hir::{ExprKind, GenericArg, Node, QPath};
13+
use rustc_hir_analysis::astconv::generics::{
14+
check_generic_arg_count_for_call, create_substs_for_generic_args,
15+
};
1316
use rustc_hir_analysis::astconv::{
1417
AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch,
1518
GenericArgCountResult, IsMethodCall, PathSeg,
@@ -374,7 +377,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
374377
}
375378

376379
pub fn to_ty(&self, ast_t: &hir::Ty<'_>) -> RawTy<'tcx> {
377-
let t = <dyn AstConv<'_>>::ast_ty_to_ty(self, ast_t);
380+
let t = self.astconv().ast_ty_to_ty(ast_t);
378381
self.register_wf_obligation(t.into(), ast_t.span, traits::WellFormed(None));
379382
self.handle_raw_ty(ast_t.span, t)
380383
}
@@ -777,7 +780,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
777780
// to be object-safe.
778781
// We manually call `register_wf_obligation` in the success path
779782
// below.
780-
let ty = <dyn AstConv<'_>>::ast_ty_to_ty_in_path(self, qself);
783+
let ty = self.astconv().ast_ty_to_ty_in_path(qself);
781784
(self.handle_raw_ty(span, ty), qself, segment)
782785
}
783786
QPath::LangItem(..) => {
@@ -975,8 +978,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
975978

976979
let path_segs = match res {
977980
Res::Local(_) | Res::SelfCtor(_) => vec![],
978-
Res::Def(kind, def_id) => <dyn AstConv<'_>>::def_ids_for_value_path_segments(
979-
self,
981+
Res::Def(kind, def_id) => self.astconv().def_ids_for_value_path_segments(
980982
segments,
981983
self_ty.map(|ty| ty.raw),
982984
kind,
@@ -1027,8 +1029,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10271029
// errors if type parameters are provided in an inappropriate place.
10281030

10291031
let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect();
1030-
let generics_has_err = <dyn AstConv<'_>>::prohibit_generics(
1031-
self,
1032+
let generics_has_err = self.astconv().prohibit_generics(
10321033
segments.iter().enumerate().filter_map(|(index, seg)| {
10331034
if !generic_segs.contains(&index) || is_alias_variant_ctor {
10341035
Some(seg)
@@ -1069,7 +1070,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10691070
// parameter internally, but we don't allow users to specify the
10701071
// parameter's value explicitly, so we have to do some error-
10711072
// checking here.
1072-
let arg_count = <dyn AstConv<'_>>::check_generic_arg_count_for_call(
1073+
let arg_count = check_generic_arg_count_for_call(
10731074
tcx,
10741075
span,
10751076
def_id,
@@ -1177,7 +1178,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11771178
) -> ty::GenericArg<'tcx> {
11781179
match (&param.kind, arg) {
11791180
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
1180-
<dyn AstConv<'_>>::ast_region_to_region(self.fcx, lt, Some(param)).into()
1181+
self.fcx.astconv().ast_region_to_region(lt, Some(param)).into()
11811182
}
11821183
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
11831184
self.fcx.to_ty(ty).raw.into()
@@ -1235,7 +1236,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12351236
}
12361237

12371238
let substs_raw = self_ctor_substs.unwrap_or_else(|| {
1238-
<dyn AstConv<'_>>::create_substs_for_generic_args(
1239+
create_substs_for_generic_args(
12391240
tcx,
12401241
def_id,
12411242
&[],

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1664,15 +1664,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16641664
match *qpath {
16651665
QPath::Resolved(ref maybe_qself, ref path) => {
16661666
let self_ty = maybe_qself.as_ref().map(|qself| self.to_ty(qself).raw);
1667-
let ty = <dyn AstConv<'_>>::res_to_ty(self, self_ty, path, true);
1667+
let ty = self.astconv().res_to_ty(self_ty, path, true);
16681668
(path.res, self.handle_raw_ty(path_span, ty))
16691669
}
16701670
QPath::TypeRelative(ref qself, ref segment) => {
16711671
let ty = self.to_ty(qself);
16721672

1673-
let result = <dyn AstConv<'_>>::associated_path_to_ty(
1674-
self, hir_id, path_span, ty.raw, qself, segment, true,
1675-
);
1673+
let result = self
1674+
.astconv()
1675+
.associated_path_to_ty(hir_id, path_span, ty.raw, qself, segment, true);
16761676
let ty = result.map(|(ty, _, _)| ty).unwrap_or_else(|_| self.tcx().ty_error());
16771677
let ty = self.handle_raw_ty(path_span, ty);
16781678
let result = result.map(|(_, kind, def_id)| (kind, def_id));

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
294294
poly_trait_ref,
295295
);
296296

297-
let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
298-
self,
297+
let item_substs = self.astconv().create_substs_for_associated_item(
299298
span,
300299
item_def_id,
301300
item_segment,

0 commit comments

Comments
 (0)