Skip to content

Commit 89f1555

Browse files
committed
Add AstConv::astconv method to remove <dyn AstConv>:: calls
1 parent b22c152 commit 89f1555

File tree

12 files changed

+50
-77
lines changed

12 files changed

+50
-77
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ pub trait AstConv<'tcx> {
120120
fn set_tainted_by_errors(&self, e: ErrorGuaranteed);
121121

122122
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
123+
124+
fn astconv(&self) -> &dyn AstConv<'tcx>
125+
where
126+
Self: Sized,
127+
{
128+
self
129+
}
123130
}
124131

125132
#[derive(Debug)]

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
@@ -544,7 +544,7 @@ pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
544544
// scope. This is derived from the enclosing item-like thing.
545545
let env_def_id = tcx.hir().get_parent_item(hir_ty.hir_id);
546546
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id());
547-
<dyn AstConv<'_>>::ast_ty_to_ty(&item_cx, hir_ty)
547+
item_cx.astconv().ast_ty_to_ty(hir_ty)
548548
}
549549

550550
pub fn hir_trait_to_predicates<'tcx>(
@@ -558,8 +558,7 @@ pub fn hir_trait_to_predicates<'tcx>(
558558
let env_def_id = tcx.hir().get_parent_item(hir_trait.hir_ref_id);
559559
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id());
560560
let mut bounds = Bounds::default();
561-
let _ = <dyn AstConv<'_>>::instantiate_poly_trait_ref(
562-
&item_cx,
561+
let _ = &item_cx.astconv().instantiate_poly_trait_ref(
563562
hir_trait,
564563
DUMMY_SP,
565564
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

+5-7
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
374374
}
375375

376376
pub fn to_ty(&self, ast_t: &hir::Ty<'_>) -> RawTy<'tcx> {
377-
let t = <dyn AstConv<'_>>::ast_ty_to_ty(self, ast_t);
377+
let t = self.astconv().ast_ty_to_ty(ast_t);
378378
self.register_wf_obligation(t.into(), ast_t.span, traits::WellFormed(None));
379379
self.handle_raw_ty(ast_t.span, t)
380380
}
@@ -777,7 +777,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
777777
// to be object-safe.
778778
// We manually call `register_wf_obligation` in the success path
779779
// below.
780-
let ty = <dyn AstConv<'_>>::ast_ty_to_ty_in_path(self, qself);
780+
let ty = self.astconv().ast_ty_to_ty_in_path(qself);
781781
(self.handle_raw_ty(span, ty), qself, segment)
782782
}
783783
QPath::LangItem(..) => {
@@ -975,8 +975,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
975975

976976
let path_segs = match res {
977977
Res::Local(_) | Res::SelfCtor(_) => vec![],
978-
Res::Def(kind, def_id) => <dyn AstConv<'_>>::def_ids_for_value_path_segments(
979-
self,
978+
Res::Def(kind, def_id) => self.astconv().def_ids_for_value_path_segments(
980979
segments,
981980
self_ty.map(|ty| ty.raw),
982981
kind,
@@ -1027,8 +1026,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10271026
// errors if type parameters are provided in an inappropriate place.
10281027

10291028
let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect();
1030-
let generics_has_err = <dyn AstConv<'_>>::prohibit_generics(
1031-
self,
1029+
let generics_has_err = self.astconv().prohibit_generics(
10321030
segments.iter().enumerate().filter_map(|(index, seg)| {
10331031
if !generic_segs.contains(&index) || is_alias_variant_ctor {
10341032
Some(seg)
@@ -1177,7 +1175,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11771175
) -> ty::GenericArg<'tcx> {
11781176
match (&param.kind, arg) {
11791177
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
1180-
<dyn AstConv<'_>>::ast_region_to_region(self.fcx, lt, Some(param)).into()
1178+
self.fcx.astconv().ast_region_to_region(lt, Some(param)).into()
11811179
}
11821180
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
11831181
self.fcx.to_ty(ty).raw.into()

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
@@ -286,8 +286,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
286286
poly_trait_ref,
287287
);
288288

289-
let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
290-
self,
289+
let item_substs = self.astconv().create_substs_for_associated_item(
291290
span,
292291
item_def_id,
293292
item_segment,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
783783
// are not, the expectation must have been caused by something else.
784784
debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.kind);
785785
let span = ty.span;
786-
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty);
786+
let ty = self.astconv().ast_ty_to_ty(ty);
787787
debug!("suggest_missing_return_type: return type {:?}", ty);
788788
debug!("suggest_missing_return_type: expected type {:?}", ty);
789789
let bound_vars = self.tcx.late_bound_vars(fn_id);
@@ -854,7 +854,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
854854
..
855855
}) => {
856856
// FIXME: Maybe these calls to `ast_ty_to_ty` can be removed (and the ones below)
857-
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, bounded_ty);
857+
let ty = self.astconv().ast_ty_to_ty(bounded_ty);
858858
Some((ty, bounds))
859859
}
860860
_ => None,
@@ -892,7 +892,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
892892
let all_bounds_str = all_matching_bounds_strs.join(" + ");
893893

894894
let ty_param_used_in_fn_params = fn_parameters.iter().any(|param| {
895-
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, param);
895+
let ty = self.astconv().ast_ty_to_ty( param);
896896
matches!(ty.kind(), ty::Param(fn_param_ty_param) if expected_ty_as_param == fn_param_ty_param)
897897
});
898898

@@ -946,7 +946,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
946946
}
947947

948948
if let hir::FnRetTy::Return(ty) = fn_decl.output {
949-
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty);
949+
let ty = self.astconv().ast_ty_to_ty(ty);
950950
let bound_vars = self.tcx.late_bound_vars(fn_id);
951951
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
952952
let ty = match self.tcx.asyncness(fn_id.owner) {
@@ -1339,7 +1339,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13391339
hir::Path { segments: [segment], .. },
13401340
))
13411341
| hir::ExprKind::Path(QPath::TypeRelative(ty, segment)) => {
1342-
let self_ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty);
1342+
let self_ty = self.astconv().ast_ty_to_ty(ty);
13431343
if let Ok(pick) = self.probe_for_name(
13441344
Mode::Path,
13451345
Ident::new(capitalized_name, segment.ident.span),

compiler/rustc_hir_typeck/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ fn typeck_with_fallback<'tcx>(
205205

206206
if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
207207
let fn_sig = if rustc_hir_analysis::collect::get_infer_ret_ty(&decl.output).is_some() {
208-
<dyn AstConv<'_>>::ty_of_fn(&fcx, id, header.unsafety, header.abi, decl, None, None)
208+
fcx.astconv().ty_of_fn(id, header.unsafety, header.abi, decl, None, None)
209209
} else {
210210
tcx.fn_sig(def_id)
211211
};
@@ -220,7 +220,7 @@ fn typeck_with_fallback<'tcx>(
220220
} else {
221221
let expected_type = body_ty
222222
.and_then(|ty| match ty.kind {
223-
hir::TyKind::Infer => Some(<dyn AstConv<'_>>::ast_ty_to_ty(&fcx, ty)),
223+
hir::TyKind::Infer => Some(fcx.astconv().ast_ty_to_ty(ty)),
224224
_ => None,
225225
})
226226
.unwrap_or_else(|| match tcx.hir().get(id) {

compiler/rustc_hir_typeck/src/method/confirm.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
369369
) -> subst::GenericArg<'tcx> {
370370
match (&param.kind, arg) {
371371
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
372-
<dyn AstConv<'_>>::ast_region_to_region(self.cfcx.fcx, lt, Some(param))
373-
.into()
372+
self.cfcx.fcx.astconv().ast_region_to_region(lt, Some(param)).into()
374373
}
375374
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
376375
self.cfcx.to_ty(ty).raw.into()

0 commit comments

Comments
 (0)