1
Fork 0

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?
This commit is contained in:
Michael Goulet 2023-01-11 22:25:50 -08:00 committed by GitHub
commit 9ec36f5668
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 660 additions and 696 deletions

View file

@ -1,6 +1,6 @@
use super::IsMethodCall; use super::IsMethodCall;
use crate::astconv::{ use crate::astconv::{
AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch,
GenericArgCountResult, GenericArgPosition, GenericArgCountResult, GenericArgPosition,
}; };
use crate::errors::AssocTypeBindingNotAllowed; use crate::errors::AssocTypeBindingNotAllowed;
@ -18,7 +18,6 @@ use rustc_session::lint::builtin::LATE_BOUND_LIFETIME_ARGUMENTS;
use rustc_span::{symbol::kw, Span}; use rustc_span::{symbol::kw, Span};
use smallvec::SmallVec; use smallvec::SmallVec;
impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
/// Report an error that a generic argument did not match the generic parameter that was /// Report an error that a generic argument did not match the generic parameter that was
/// expected. /// expected.
fn generic_arg_mismatch_err( fn generic_arg_mismatch_err(
@ -42,9 +41,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if matches!(arg, GenericArg::Type(hir::Ty { kind: hir::TyKind::Infer, .. })) { if matches!(arg, GenericArg::Type(hir::Ty { kind: hir::TyKind::Infer, .. })) {
err.help("const arguments cannot yet be inferred with `_`"); err.help("const arguments cannot yet be inferred with `_`");
if sess.is_nightly_build() { if sess.is_nightly_build() {
err.help( err.help("add `#![feature(generic_arg_infer)]` to the crate attributes to enable");
"add `#![feature(generic_arg_infer)]` to the crate attributes to enable",
);
} }
} }
} }
@ -73,9 +70,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
) => match path.res { ) => match path.res {
Res::Err => { Res::Err => {
add_braces_suggestion(arg, &mut err); add_braces_suggestion(arg, &mut err);
err.set_primary_message( err.set_primary_message("unresolved item provided when a constant was expected")
"unresolved item provided when a constant was expected",
)
.emit(); .emit();
return; return;
} }
@ -115,14 +110,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} }
(GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => { (GenericArg::Const(cnst), GenericParamDefKind::Type { .. }) => {
let body = tcx.hir().body(cnst.value.body); let body = tcx.hir().body(cnst.value.body);
if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = if let rustc_hir::ExprKind::Path(rustc_hir::QPath::Resolved(_, path)) = body.value.kind
body.value.kind
{ {
if let Res::Def(DefKind::Fn { .. }, id) = path.res { if let Res::Def(DefKind::Fn { .. }, id) = path.res {
err.help(&format!( err.help(&format!("`{}` is a function item, not a type", tcx.item_name(id)));
"`{}` is a function item, not a type",
tcx.item_name(id)
));
err.help("function item types cannot be named directly"); err.help("function item types cannot be named directly");
} }
} }
@ -178,7 +169,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
/// instantiate a `GenericArg`. /// instantiate a `GenericArg`.
/// - `inferred_kind`: if no parameter was provided, and inference is enabled, then /// - `inferred_kind`: if no parameter was provided, and inference is enabled, then
/// creates a suitable inference variable. /// creates a suitable inference variable.
pub fn create_substs_for_generic_args<'a>( pub fn create_substs_for_generic_args<'tcx, 'a>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
def_id: DefId, def_id: DefId,
parent_substs: &[subst::GenericArg<'tcx>], parent_substs: &[subst::GenericArg<'tcx>],
@ -305,7 +296,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
) = param_types_present.into_iter().unzip(); ) = param_types_present.into_iter().unzip();
param_types_present.dedup(); param_types_present.dedup();
Self::generic_arg_mismatch_err( generic_arg_mismatch_err(
tcx, tcx,
arg, arg,
param, param,
@ -361,7 +352,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
assert_eq!(kind, "lifetime"); assert_eq!(kind, "lifetime");
let (provided_arg, param) = let (provided_arg, param) =
force_infer_lt.expect("lifetimes ought to have been inferred"); force_infer_lt.expect("lifetimes ought to have been inferred");
Self::generic_arg_mismatch_err(tcx, provided_arg, param, false, None); generic_arg_mismatch_err(tcx, provided_arg, param, false, None);
} }
break; break;
@ -401,7 +392,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}; };
let has_self = generics.parent.is_none() && generics.has_self; let has_self = generics.parent.is_none() && generics.has_self;
Self::check_generic_arg_count( check_generic_arg_count(
tcx, tcx,
span, span,
def_id, def_id,
@ -436,26 +427,22 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let synth_type_param_count = gen_params let synth_type_param_count = gen_params
.params .params
.iter() .iter()
.filter(|param| { .filter(|param| matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. }))
matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. })
})
.count(); .count();
let named_type_param_count = let named_type_param_count = param_counts.types - has_self as usize - synth_type_param_count;
param_counts.types - has_self as usize - synth_type_param_count;
let infer_lifetimes = let infer_lifetimes =
(gen_pos != GenericArgPosition::Type || infer_args) && !gen_args.has_lifetime_params(); (gen_pos != GenericArgPosition::Type || infer_args) && !gen_args.has_lifetime_params();
if gen_pos != GenericArgPosition::Type && let Some(b) = gen_args.bindings.first() { if gen_pos != GenericArgPosition::Type && let Some(b) = gen_args.bindings.first() {
Self::prohibit_assoc_ty_binding(tcx, b.span); prohibit_assoc_ty_binding(tcx, b.span);
} }
let explicit_late_bound = let explicit_late_bound =
Self::prohibit_explicit_late_bound_lifetimes(tcx, gen_params, gen_args, gen_pos); prohibit_explicit_late_bound_lifetimes(tcx, gen_params, gen_args, gen_pos);
let mut invalid_args = vec![]; let mut invalid_args = vec![];
let mut check_lifetime_args = let mut check_lifetime_args = |min_expected_args: usize,
|min_expected_args: usize,
max_expected_args: usize, max_expected_args: usize,
provided_args: usize, provided_args: usize,
late_bounds_ignore: bool| { late_bounds_ignore: bool| {
@ -469,17 +456,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if provided_args > max_expected_args { if provided_args > max_expected_args {
invalid_args.extend( invalid_args.extend(
gen_args.args[max_expected_args..provided_args] gen_args.args[max_expected_args..provided_args].iter().map(|arg| arg.span()),
.iter()
.map(|arg| arg.span()),
); );
}; };
let gen_args_info = if provided_args > min_expected_args { let gen_args_info = if provided_args > min_expected_args {
invalid_args.extend( invalid_args.extend(
gen_args.args[min_expected_args..provided_args] gen_args.args[min_expected_args..provided_args].iter().map(|arg| arg.span()),
.iter()
.map(|arg| arg.span()),
); );
let num_redundant_args = provided_args - min_expected_args; let num_redundant_args = provided_args - min_expected_args;
GenericArgsInfo::ExcessLifetimes { num_redundant_args } GenericArgsInfo::ExcessLifetimes { num_redundant_args }
@ -601,9 +584,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
GenericArgCountResult { GenericArgCountResult {
explicit_late_bound, explicit_late_bound,
correct: lifetimes_correct.and(args_correct).map_err(|reported| { correct: lifetimes_correct
GenericArgCountMismatch { reported: Some(reported), invalid_args } .and(args_correct)
}), .map_err(|reported| GenericArgCountMismatch { reported: Some(reported), invalid_args }),
} }
} }
@ -656,4 +639,3 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
ExplicitLateBound::No ExplicitLateBound::No
} }
} }
}

View file

@ -3,8 +3,11 @@
//! instance of `AstConv`. //! instance of `AstConv`.
mod errors; mod errors;
mod generics; pub mod generics;
use crate::astconv::generics::{
check_generic_arg_count, create_substs_for_generic_args, prohibit_assoc_ty_binding,
};
use crate::bounds::Bounds; use crate::bounds::Bounds;
use crate::collect::HirPlaceholderCollector; use crate::collect::HirPlaceholderCollector;
use crate::errors::{ use crate::errors::{
@ -120,6 +123,13 @@ pub trait AstConv<'tcx> {
fn set_tainted_by_errors(&self, e: ErrorGuaranteed); fn set_tainted_by_errors(&self, e: ErrorGuaranteed);
fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span); fn record_ty(&self, hir_id: hir::HirId, ty: Ty<'tcx>, span: Span);
fn astconv(&self) -> &dyn AstConv<'tcx>
where
Self: Sized,
{
self
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -279,7 +289,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
ty::BoundConstness::NotConst, ty::BoundConstness::NotConst,
); );
if let Some(b) = item_segment.args().bindings.first() { if let Some(b) = item_segment.args().bindings.first() {
Self::prohibit_assoc_ty_binding(self.tcx(), b.span); prohibit_assoc_ty_binding(self.tcx(), b.span);
} }
substs substs
@ -349,7 +359,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
assert!(self_ty.is_none()); assert!(self_ty.is_none());
} }
let arg_count = Self::check_generic_arg_count( let arg_count = check_generic_arg_count(
tcx, tcx,
span, span,
def_id, def_id,
@ -524,7 +534,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
inferred_params: vec![], inferred_params: vec![],
infer_args, infer_args,
}; };
let substs = Self::create_substs_for_generic_args( let substs = create_substs_for_generic_args(
tcx, tcx,
def_id, def_id,
parent_substs, parent_substs,
@ -610,7 +620,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
); );
if let Some(b) = item_segment.args().bindings.first() { if let Some(b) = item_segment.args().bindings.first() {
Self::prohibit_assoc_ty_binding(self.tcx(), b.span); prohibit_assoc_ty_binding(self.tcx(), b.span);
} }
args args
@ -804,7 +814,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
constness, constness,
); );
if let Some(b) = trait_segment.args().bindings.first() { if let Some(b) = trait_segment.args().bindings.first() {
Self::prohibit_assoc_ty_binding(self.tcx(), b.span); prohibit_assoc_ty_binding(self.tcx(), b.span);
} }
self.tcx().mk_trait_ref(trait_def_id, substs) self.tcx().mk_trait_ref(trait_def_id, substs)
} }
@ -2301,7 +2311,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
for segment in segments { for segment in segments {
// Only emit the first error to avoid overloading the user with error messages. // Only emit the first error to avoid overloading the user with error messages.
if let Some(b) = segment.args().bindings.first() { if let Some(b) = segment.args().bindings.first() {
Self::prohibit_assoc_ty_binding(self.tcx(), b.span); prohibit_assoc_ty_binding(self.tcx(), b.span);
return true; return true;
} }
} }

View file

@ -351,7 +351,7 @@ impl<'tcx> ItemCtxt<'tcx> {
} }
pub fn to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> { pub fn to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
<dyn AstConv<'_>>::ast_ty_to_ty(self, ast_ty) self.astconv().ast_ty_to_ty(ast_ty)
} }
pub fn hir_id(&self) -> hir::HirId { pub fn hir_id(&self) -> hir::HirId {
@ -413,8 +413,7 @@ impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
poly_trait_ref: ty::PolyTraitRef<'tcx>, poly_trait_ref: ty::PolyTraitRef<'tcx>,
) -> Ty<'tcx> { ) -> Ty<'tcx> {
if let Some(trait_ref) = poly_trait_ref.no_bound_vars() { if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item( let item_substs = self.astconv().create_substs_for_associated_item(
self,
span, span,
item_def_id, item_def_id,
item_segment, item_segment,
@ -1112,8 +1111,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
tcx.hir().get_parent(hir_id) tcx.hir().get_parent(hir_id)
&& i.of_trait.is_some() && i.of_trait.is_some()
{ {
<dyn AstConv<'_>>::ty_of_fn( icx.astconv().ty_of_fn(
&icx,
hir_id, hir_id,
sig.header.unsafety, sig.header.unsafety,
sig.header.abi, sig.header.abi,
@ -1130,15 +1128,9 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _), kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
generics, generics,
.. ..
}) => <dyn AstConv<'_>>::ty_of_fn( }) => {
&icx, icx.astconv().ty_of_fn(hir_id, header.unsafety, header.abi, decl, Some(generics), None)
hir_id, }
header.unsafety,
header.abi,
decl,
Some(generics),
None,
),
ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => { ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
let abi = tcx.hir().get_foreign_abi(hir_id); let abi = tcx.hir().get_foreign_abi(hir_id);
@ -1244,8 +1236,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
ty::Binder::dummy(fn_sig) ty::Binder::dummy(fn_sig)
} }
None => <dyn AstConv<'_>>::ty_of_fn( None => icx.astconv().ty_of_fn(
icx,
hir_id, hir_id,
sig.header.unsafety, sig.header.unsafety,
sig.header.abi, sig.header.abi,
@ -1354,8 +1345,7 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
match item.kind { match item.kind {
hir::ItemKind::Impl(ref impl_) => impl_.of_trait.as_ref().map(|ast_trait_ref| { hir::ItemKind::Impl(ref impl_) => impl_.of_trait.as_ref().map(|ast_trait_ref| {
let selfty = tcx.type_of(def_id); let selfty = tcx.type_of(def_id);
<dyn AstConv<'_>>::instantiate_mono_trait_ref( icx.astconv().instantiate_mono_trait_ref(
&icx,
ast_trait_ref, ast_trait_ref,
selfty, selfty,
check_impl_constness(tcx, impl_.constness, ast_trait_ref), check_impl_constness(tcx, impl_.constness, ast_trait_ref),
@ -1485,15 +1475,8 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
hir::Unsafety::Unsafe hir::Unsafety::Unsafe
}; };
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let fty = <dyn AstConv<'_>>::ty_of_fn( let fty =
&ItemCtxt::new(tcx, def_id), ItemCtxt::new(tcx, def_id).astconv().ty_of_fn(hir_id, unsafety, abi, decl, None, None);
hir_id,
unsafety,
abi,
decl,
None,
None,
);
// Feature gate SIMD types in FFI, since I am not sure that the // Feature gate SIMD types in FFI, since I am not sure that the
// ABIs are handled at all correctly. -huonw // ABIs are handled at all correctly. -huonw

View file

@ -26,9 +26,9 @@ fn associated_type_bounds<'tcx>(
); );
let icx = ItemCtxt::new(tcx, assoc_item_def_id); let icx = ItemCtxt::new(tcx, assoc_item_def_id);
let mut bounds = <dyn AstConv<'_>>::compute_bounds(&icx, item_ty, ast_bounds); let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
// Associated types are implicitly sized unless a `?Sized` bound is found // Associated types are implicitly sized unless a `?Sized` bound is found
<dyn AstConv<'_>>::add_implicitly_sized(&icx, &mut bounds, item_ty, ast_bounds, None, span); icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
let trait_def_id = tcx.parent(assoc_item_def_id); let trait_def_id = tcx.parent(assoc_item_def_id);
let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id.expect_local()); let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id.expect_local());
@ -70,9 +70,9 @@ fn opaque_type_bounds<'tcx>(
}; };
let icx = ItemCtxt::new(tcx, opaque_def_id); let icx = ItemCtxt::new(tcx, opaque_def_id);
let mut bounds = <dyn AstConv<'_>>::compute_bounds(&icx, item_ty, ast_bounds); let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds);
// Opaque types are implicitly sized unless a `?Sized` bound is found // Opaque types are implicitly sized unless a `?Sized` bound is found
<dyn AstConv<'_>>::add_implicitly_sized(&icx, &mut bounds, item_ty, ast_bounds, None, span); icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
debug!(?bounds); debug!(?bounds);
tcx.arena.alloc_from_iter(bounds.predicates()) tcx.arena.alloc_from_iter(bounds.predicates())

View file

@ -162,8 +162,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
let mut bounds = Bounds::default(); let mut bounds = Bounds::default();
// Params are implicitly sized unless a `?Sized` bound is found // Params are implicitly sized unless a `?Sized` bound is found
<dyn AstConv<'_>>::add_implicitly_sized( icx.astconv().add_implicitly_sized(
&icx,
&mut bounds, &mut bounds,
param_ty, param_ty,
&[], &[],
@ -211,22 +210,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
} }
let mut bounds = Bounds::default(); let mut bounds = Bounds::default();
<dyn AstConv<'_>>::add_bounds( icx.astconv().add_bounds(ty, bound_pred.bounds.iter(), &mut bounds, bound_vars);
&icx,
ty,
bound_pred.bounds.iter(),
&mut bounds,
bound_vars,
);
predicates.extend(bounds.predicates()); predicates.extend(bounds.predicates());
} }
hir::WherePredicate::RegionPredicate(region_pred) => { hir::WherePredicate::RegionPredicate(region_pred) => {
let r1 = <dyn AstConv<'_>>::ast_region_to_region(&icx, &region_pred.lifetime, None); let r1 = icx.astconv().ast_region_to_region(&region_pred.lifetime, None);
predicates.extend(region_pred.bounds.iter().map(|bound| { predicates.extend(region_pred.bounds.iter().map(|bound| {
let (r2, span) = match bound { let (r2, span) = match bound {
hir::GenericBound::Outlives(lt) => { hir::GenericBound::Outlives(lt) => {
(<dyn AstConv<'_>>::ast_region_to_region(&icx, lt, None), lt.ident.span) (icx.astconv().ast_region_to_region(lt, None), lt.ident.span)
} }
_ => bug!(), _ => bug!(),
}; };
@ -279,7 +272,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
debug!(?lifetimes); debug!(?lifetimes);
for (arg, duplicate) in std::iter::zip(lifetimes, ast_generics.params) { for (arg, duplicate) in std::iter::zip(lifetimes, ast_generics.params) {
let hir::GenericArg::Lifetime(arg) = arg else { bug!() }; let hir::GenericArg::Lifetime(arg) = arg else { bug!() };
let orig_region = <dyn AstConv<'_>>::ast_region_to_region(&icx, &arg, None); let orig_region = icx.astconv().ast_region_to_region(&arg, None);
if !matches!(orig_region.kind(), ty::ReEarlyBound(..)) { if !matches!(orig_region.kind(), ty::ReEarlyBound(..)) {
// Only early-bound regions can point to the original generic parameter. // Only early-bound regions can point to the original generic parameter.
continue; continue;
@ -527,14 +520,9 @@ pub(super) fn super_predicates_that_define_assoc_type(
// Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`. // Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
let self_param_ty = tcx.types.self_param; let self_param_ty = tcx.types.self_param;
let superbounds1 = if let Some(assoc_name) = assoc_name { let superbounds1 = if let Some(assoc_name) = assoc_name {
<dyn AstConv<'_>>::compute_bounds_that_match_assoc_type( icx.astconv().compute_bounds_that_match_assoc_type(self_param_ty, bounds, assoc_name)
&icx,
self_param_ty,
bounds,
assoc_name,
)
} else { } else {
<dyn AstConv<'_>>::compute_bounds(&icx, self_param_ty, bounds) icx.astconv().compute_bounds(self_param_ty, bounds)
}; };
let superbounds1 = superbounds1.predicates(); let superbounds1 = superbounds1.predicates();

View file

@ -545,7 +545,7 @@ pub fn hir_ty_to_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
// scope. This is derived from the enclosing item-like thing. // scope. This is derived from the enclosing item-like thing.
let env_def_id = tcx.hir().get_parent_item(hir_ty.hir_id); let env_def_id = tcx.hir().get_parent_item(hir_ty.hir_id);
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id()); let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id());
<dyn AstConv<'_>>::ast_ty_to_ty(&item_cx, hir_ty) item_cx.astconv().ast_ty_to_ty(hir_ty)
} }
pub fn hir_trait_to_predicates<'tcx>( pub fn hir_trait_to_predicates<'tcx>(
@ -559,8 +559,7 @@ pub fn hir_trait_to_predicates<'tcx>(
let env_def_id = tcx.hir().get_parent_item(hir_trait.hir_ref_id); let env_def_id = tcx.hir().get_parent_item(hir_trait.hir_ref_id);
let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id()); let item_cx = self::collect::ItemCtxt::new(tcx, env_def_id.to_def_id());
let mut bounds = Bounds::default(); let mut bounds = Bounds::default();
let _ = <dyn AstConv<'_>>::instantiate_poly_trait_ref( let _ = &item_cx.astconv().instantiate_poly_trait_ref(
&item_cx,
hir_trait, hir_trait,
DUMMY_SP, DUMMY_SP,
ty::BoundConstness::NotConst, ty::BoundConstness::NotConst,

View file

@ -1807,7 +1807,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
// Get the return type. // Get the return type.
&& let hir::TyKind::OpaqueDef(..) = ty.kind && let hir::TyKind::OpaqueDef(..) = ty.kind
{ {
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty); let ty = fcx.astconv().ast_ty_to_ty( ty);
// Get the `impl Trait`'s `DefId`. // Get the `impl Trait`'s `DefId`.
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = ty.kind() if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = ty.kind()
// Get the `impl Trait`'s `Item` so that we can get its trait bounds and // 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> {
fn is_return_ty_unsized<'a>(&self, fcx: &FnCtxt<'a, 'tcx>, blk_id: hir::HirId) -> bool { fn is_return_ty_unsized<'a>(&self, fcx: &FnCtxt<'a, 'tcx>, blk_id: hir::HirId) -> bool {
if let Some((fn_decl, _)) = fcx.get_fn_decl(blk_id) if let Some((fn_decl, _)) = fcx.get_fn_decl(blk_id)
&& let hir::FnRetTy::Return(ty) = fn_decl.output && let hir::FnRetTy::Return(ty) = fn_decl.output
&& let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty) && let ty = fcx.astconv().ast_ty_to_ty( ty)
&& let ty::Dynamic(..) = ty.kind() && let ty::Dynamic(..) = ty.kind()
{ {
return true; return true;

View file

@ -10,6 +10,9 @@ use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem; use rustc_hir::lang_items::LangItem;
use rustc_hir::{ExprKind, GenericArg, Node, QPath}; use rustc_hir::{ExprKind, GenericArg, Node, QPath};
use rustc_hir_analysis::astconv::generics::{
check_generic_arg_count_for_call, create_substs_for_generic_args,
};
use rustc_hir_analysis::astconv::{ use rustc_hir_analysis::astconv::{
AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch, AstConv, CreateSubstsForGenericArgsCtxt, ExplicitLateBound, GenericArgCountMismatch,
GenericArgCountResult, IsMethodCall, PathSeg, GenericArgCountResult, IsMethodCall, PathSeg,
@ -374,7 +377,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
pub fn to_ty(&self, ast_t: &hir::Ty<'_>) -> RawTy<'tcx> { pub fn to_ty(&self, ast_t: &hir::Ty<'_>) -> RawTy<'tcx> {
let t = <dyn AstConv<'_>>::ast_ty_to_ty(self, ast_t); let t = self.astconv().ast_ty_to_ty(ast_t);
self.register_wf_obligation(t.into(), ast_t.span, traits::WellFormed(None)); self.register_wf_obligation(t.into(), ast_t.span, traits::WellFormed(None));
self.handle_raw_ty(ast_t.span, t) self.handle_raw_ty(ast_t.span, t)
} }
@ -777,7 +780,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// to be object-safe. // to be object-safe.
// We manually call `register_wf_obligation` in the success path // We manually call `register_wf_obligation` in the success path
// below. // below.
let ty = <dyn AstConv<'_>>::ast_ty_to_ty_in_path(self, qself); let ty = self.astconv().ast_ty_to_ty_in_path(qself);
(self.handle_raw_ty(span, ty), qself, segment) (self.handle_raw_ty(span, ty), qself, segment)
} }
QPath::LangItem(..) => { QPath::LangItem(..) => {
@ -975,8 +978,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let path_segs = match res { let path_segs = match res {
Res::Local(_) | Res::SelfCtor(_) => vec![], Res::Local(_) | Res::SelfCtor(_) => vec![],
Res::Def(kind, def_id) => <dyn AstConv<'_>>::def_ids_for_value_path_segments( Res::Def(kind, def_id) => self.astconv().def_ids_for_value_path_segments(
self,
segments, segments,
self_ty.map(|ty| ty.raw), self_ty.map(|ty| ty.raw),
kind, kind,
@ -1027,8 +1029,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// errors if type parameters are provided in an inappropriate place. // errors if type parameters are provided in an inappropriate place.
let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect(); let generic_segs: FxHashSet<_> = path_segs.iter().map(|PathSeg(_, index)| index).collect();
let generics_has_err = <dyn AstConv<'_>>::prohibit_generics( let generics_has_err = self.astconv().prohibit_generics(
self,
segments.iter().enumerate().filter_map(|(index, seg)| { segments.iter().enumerate().filter_map(|(index, seg)| {
if !generic_segs.contains(&index) || is_alias_variant_ctor { if !generic_segs.contains(&index) || is_alias_variant_ctor {
Some(seg) Some(seg)
@ -1069,7 +1070,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// parameter internally, but we don't allow users to specify the // parameter internally, but we don't allow users to specify the
// parameter's value explicitly, so we have to do some error- // parameter's value explicitly, so we have to do some error-
// checking here. // checking here.
let arg_count = <dyn AstConv<'_>>::check_generic_arg_count_for_call( let arg_count = check_generic_arg_count_for_call(
tcx, tcx,
span, span,
def_id, def_id,
@ -1177,7 +1178,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> ty::GenericArg<'tcx> { ) -> ty::GenericArg<'tcx> {
match (&param.kind, arg) { match (&param.kind, arg) {
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => { (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
<dyn AstConv<'_>>::ast_region_to_region(self.fcx, lt, Some(param)).into() self.fcx.astconv().ast_region_to_region(lt, Some(param)).into()
} }
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => { (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
self.fcx.to_ty(ty).raw.into() self.fcx.to_ty(ty).raw.into()
@ -1235,7 +1236,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
let substs_raw = self_ctor_substs.unwrap_or_else(|| { let substs_raw = self_ctor_substs.unwrap_or_else(|| {
<dyn AstConv<'_>>::create_substs_for_generic_args( create_substs_for_generic_args(
tcx, tcx,
def_id, def_id,
&[], &[],

View file

@ -1664,15 +1664,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match *qpath { match *qpath {
QPath::Resolved(ref maybe_qself, ref path) => { QPath::Resolved(ref maybe_qself, ref path) => {
let self_ty = maybe_qself.as_ref().map(|qself| self.to_ty(qself).raw); let self_ty = maybe_qself.as_ref().map(|qself| self.to_ty(qself).raw);
let ty = <dyn AstConv<'_>>::res_to_ty(self, self_ty, path, true); let ty = self.astconv().res_to_ty(self_ty, path, true);
(path.res, self.handle_raw_ty(path_span, ty)) (path.res, self.handle_raw_ty(path_span, ty))
} }
QPath::TypeRelative(ref qself, ref segment) => { QPath::TypeRelative(ref qself, ref segment) => {
let ty = self.to_ty(qself); let ty = self.to_ty(qself);
let result = <dyn AstConv<'_>>::associated_path_to_ty( let result = self
self, hir_id, path_span, ty.raw, qself, segment, true, .astconv()
); .associated_path_to_ty(hir_id, path_span, ty.raw, qself, segment, true);
let ty = result.map(|(ty, _, _)| ty).unwrap_or_else(|_| self.tcx().ty_error()); let ty = result.map(|(ty, _, _)| ty).unwrap_or_else(|_| self.tcx().ty_error());
let ty = self.handle_raw_ty(path_span, ty); let ty = self.handle_raw_ty(path_span, ty);
let result = result.map(|(_, kind, def_id)| (kind, def_id)); let result = result.map(|(_, kind, def_id)| (kind, def_id));

View file

@ -294,8 +294,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
poly_trait_ref, poly_trait_ref,
); );
let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item( let item_substs = self.astconv().create_substs_for_associated_item(
self,
span, span,
item_def_id, item_def_id,
item_segment, item_segment,

View file

@ -793,7 +793,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// are not, the expectation must have been caused by something else. // are not, the expectation must have been caused by something else.
debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.kind); debug!("suggest_missing_return_type: return type {:?} node {:?}", ty, ty.kind);
let span = ty.span; let span = ty.span;
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty); let ty = self.astconv().ast_ty_to_ty(ty);
debug!("suggest_missing_return_type: return type {:?}", ty); debug!("suggest_missing_return_type: return type {:?}", ty);
debug!("suggest_missing_return_type: expected type {:?}", ty); debug!("suggest_missing_return_type: expected type {:?}", ty);
let bound_vars = self.tcx.late_bound_vars(fn_id); let bound_vars = self.tcx.late_bound_vars(fn_id);
@ -864,7 +864,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.. ..
}) => { }) => {
// FIXME: Maybe these calls to `ast_ty_to_ty` can be removed (and the ones below) // FIXME: Maybe these calls to `ast_ty_to_ty` can be removed (and the ones below)
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, bounded_ty); let ty = self.astconv().ast_ty_to_ty(bounded_ty);
Some((ty, bounds)) Some((ty, bounds))
} }
_ => None, _ => None,
@ -902,7 +902,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let all_bounds_str = all_matching_bounds_strs.join(" + "); let all_bounds_str = all_matching_bounds_strs.join(" + ");
let ty_param_used_in_fn_params = fn_parameters.iter().any(|param| { let ty_param_used_in_fn_params = fn_parameters.iter().any(|param| {
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, param); let ty = self.astconv().ast_ty_to_ty( param);
matches!(ty.kind(), ty::Param(fn_param_ty_param) if expected_ty_as_param == fn_param_ty_param) matches!(ty.kind(), ty::Param(fn_param_ty_param) if expected_ty_as_param == fn_param_ty_param)
}); });
@ -956,7 +956,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
if let hir::FnRetTy::Return(ty) = fn_decl.output { if let hir::FnRetTy::Return(ty) = fn_decl.output {
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty); let ty = self.astconv().ast_ty_to_ty(ty);
let bound_vars = self.tcx.late_bound_vars(fn_id); let bound_vars = self.tcx.late_bound_vars(fn_id);
let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars)); let ty = self.tcx.erase_late_bound_regions(Binder::bind_with_vars(ty, bound_vars));
let ty = match self.tcx.asyncness(fn_id.owner) { let ty = match self.tcx.asyncness(fn_id.owner) {
@ -1349,7 +1349,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::Path { segments: [segment], .. }, hir::Path { segments: [segment], .. },
)) ))
| hir::ExprKind::Path(QPath::TypeRelative(ty, segment)) => { | hir::ExprKind::Path(QPath::TypeRelative(ty, segment)) => {
let self_ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, ty); let self_ty = self.astconv().ast_ty_to_ty(ty);
if let Ok(pick) = self.probe_for_name( if let Ok(pick) = self.probe_for_name(
Mode::Path, Mode::Path,
Ident::new(capitalized_name, segment.ident.span), Ident::new(capitalized_name, segment.ident.span),

View file

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

View file

@ -4,6 +4,9 @@ use crate::{callee, FnCtxt};
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::GenericArg; use rustc_hir::GenericArg;
use rustc_hir_analysis::astconv::generics::{
check_generic_arg_count_for_call, create_substs_for_generic_args,
};
use rustc_hir_analysis::astconv::{AstConv, CreateSubstsForGenericArgsCtxt, IsMethodCall}; use rustc_hir_analysis::astconv::{AstConv, CreateSubstsForGenericArgsCtxt, IsMethodCall};
use rustc_infer::infer::{self, InferOk}; use rustc_infer::infer::{self, InferOk};
use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext}; use rustc_middle::traits::{ObligationCauseCode, UnifyReceiverContext};
@ -331,7 +334,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
// variables. // variables.
let generics = self.tcx.generics_of(pick.item.def_id); let generics = self.tcx.generics_of(pick.item.def_id);
let arg_count_correct = <dyn AstConv<'_>>::check_generic_arg_count_for_call( let arg_count_correct = check_generic_arg_count_for_call(
self.tcx, self.tcx,
self.span, self.span,
pick.item.def_id, pick.item.def_id,
@ -369,8 +372,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
) -> subst::GenericArg<'tcx> { ) -> subst::GenericArg<'tcx> {
match (&param.kind, arg) { match (&param.kind, arg) {
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => { (GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
<dyn AstConv<'_>>::ast_region_to_region(self.cfcx.fcx, lt, Some(param)) self.cfcx.fcx.astconv().ast_region_to_region(lt, Some(param)).into()
.into()
} }
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => { (GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
self.cfcx.to_ty(ty).raw.into() self.cfcx.to_ty(ty).raw.into()
@ -399,7 +401,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
} }
} }
let substs = <dyn AstConv<'_>>::create_substs_for_generic_args( let substs = create_substs_for_generic_args(
self.tcx, self.tcx,
pick.item.def_id, pick.item.def_id,
parent_substs, parent_substs,