Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions compiler/rustc_hir_analysis/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,26 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_

let icx = ItemCtxt::new(tcx, def_id);

let new_bound_fn_def = |hir: HirId, did| {
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
Ty::new_fn_def_raw(
tcx,
did,
match &tcx
.late_bound_vars_map(hir.owner)
.get(&hir.local_id)
.cloned()
.map(|x| tcx.mk_bound_variable_kinds(&x))
{
Some(late_bound) => ty::Binder::bind_with_vars(args, late_bound),
None => ty::Binder::dummy(args),
},
)
};

let output = match tcx.hir_node(hir_id) {
Node::TraitItem(item) => match item.kind {
TraitItemKind::Fn(..) => {
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
Ty::new_fn_def(tcx, def_id.to_def_id(), ty::Binder::dummy(args))
}
TraitItemKind::Fn(_, _) => new_bound_fn_def(item.hir_id(), def_id.to_def_id()),
TraitItemKind::Const(ty, rhs) => rhs
.and_then(|rhs| {
ty.is_suggestable_infer_ty().then(|| {
Expand All @@ -92,11 +105,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
},

Node::ImplItem(item) => match item.kind {
ImplItemKind::Fn(..) => {
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
Ty::new_fn_def(tcx, def_id.to_def_id(), ty::Binder::dummy(args))
}
ImplItemKind::Fn(_, _) => new_bound_fn_def(item.hir_id(), def_id.to_def_id()),
ImplItemKind::Const(ty, rhs) => {
if ty.is_suggestable_infer_ty() {
infer_placeholder_type(
Expand Down Expand Up @@ -171,11 +180,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
}
_ => icx.lower_ty(self_ty),
},
ItemKind::Fn { .. } => {
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
Ty::new_fn_def(tcx, def_id.to_def_id(), ty::Binder::dummy(args))
}
ItemKind::Fn { .. } => new_bound_fn_def(item.hir_id(), def_id.to_def_id()),
ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
let def = tcx.adt_def(def_id);
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
Expand All @@ -196,10 +201,8 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
Node::OpaqueTy(..) => tcx.type_of_opaque(def_id).instantiate_identity().skip_norm_wip(),

Node::ForeignItem(foreign_item) => match foreign_item.kind {
ForeignItemKind::Fn(..) => {
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
Ty::new_fn_def(tcx, def_id.to_def_id(), ty::Binder::dummy(args))
ForeignItemKind::Fn(_, _, _generics) => {
new_bound_fn_def(foreign_item.hir_id(), def_id.to_def_id())
}
ForeignItemKind::Static(ty, _, _) => {
let ty = icx.lower_ty(ty);
Expand All @@ -219,11 +222,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
VariantData::Unit(..) | VariantData::Struct { .. } => {
tcx.type_of(tcx.hir_get_parent_item(hir_id)).instantiate_identity().skip_norm_wip()
}
VariantData::Tuple(_, _, ctor) => {
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
Ty::new_fn_def(tcx, ctor.to_def_id(), ty::Binder::dummy(args))
}
VariantData::Tuple(_, hir_id, ctor) => new_bound_fn_def(*hir_id, ctor.to_def_id()),
},

Node::Field(field) => icx.lower_ty(field.ty),
Expand Down
25 changes: 12 additions & 13 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,13 +1483,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
Ok(ct)
}
TypeRelativePath::Ctor { ctor_def_id, args } => match tcx.def_kind(ctor_def_id) {
DefKind::Ctor(_, CtorKind::Fn) => {
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
Ok(ty::Const::zero_sized(
tcx,
Ty::new_fn_def(tcx, ctor_def_id, ty::Binder::dummy(args)),
))
}
DefKind::Ctor(_, CtorKind::Fn) => Ok(ty::Const::zero_sized(
tcx,
tcx.type_of(ctor_def_id).instantiate(tcx, args).skip_norm_wip(),
)),
DefKind::Ctor(ctor_of, CtorKind::Const) => {
Ok(self.construct_const_ctor_value(ctor_def_id, ctor_of, args))
}
Expand Down Expand Up @@ -2913,8 +2910,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
&path.segments[generic_segments[0].1],
);

// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, ty::Binder::dummy(args)))
ty::Const::zero_sized(tcx, tcx.type_of(did).instantiate(tcx, args).skip_norm_wip())
}
Res::Def(DefKind::AssocConst { .. }, did) => {
let trait_segment = if let [modules @ .., trait_, _item] = path.segments {
Expand Down Expand Up @@ -2945,8 +2941,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);

if self.tcx().generics_of(did).own_synthetic_params_count() == 0 {
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, ty::Binder::dummy(args)))
ty::Const::zero_sized(
tcx,
tcx.type_of(did).instantiate(tcx, args).skip_norm_wip(),
)
} else {
let tcx = self.tcx();
let generics = tcx.generics_of(did);
Expand All @@ -2963,10 +2961,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
}
});

// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
ty::Const::zero_sized(
tcx,
Ty::new_fn_def(tcx, did, ty::Binder::dummy(args.collect::<Box<_>>())),
tcx.type_of(did)
.instantiate(tcx, &*args.collect::<Box<_>>())
.skip_norm_wip(),
)
}
}
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_middle/src/mir/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,13 @@ impl<'tcx> Operand<'tcx> {
pub fn function_handle(
tcx: TyCtxt<'tcx>,
def_id: DefId,
args: ty::Binder<'tcx, impl IntoIterator<Item = GenericArg<'tcx>>>,
args: &[GenericArg<'tcx>],
span: Span,
) -> Self {
let ty = Ty::new_fn_def(tcx, def_id, args);
Operand::zero_sized_constant(ty, span)
Operand::zero_sized_constant(
tcx.type_of(def_id).instantiate(tcx, args).skip_norm_wip(),
span,
)
}

/// Convenience helper to make a constant that refers to the given `DefId` and args. Since this
Expand Down
31 changes: 30 additions & 1 deletion compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,35 @@ impl<'tcx> Ty<'tcx> {
T::collect_and_apply(iter, |ts| Ty::new_tup(tcx, ts))
}

/// Prefer using the [TyCtxt::type_of] query over this, that makes it easier to get all the pieces correct
#[inline]
pub fn new_fn_def_raw(
tcx: TyCtxt<'tcx>,
def_id: DefId,
args: ty::Binder<'tcx, impl IntoIterator<Item: Into<GenericArg<'tcx>>>>,
) -> Ty<'tcx> {
debug_assert_matches!(
tcx.def_kind(def_id),
DefKind::AssocFn | DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn)
);

let args = args.map_bound(|args| tcx.check_and_mk_args(def_id, args));

// let hir = tcx.local_def_id_to_hir_id(def_id.as_local().unwrap());

// let args = match &tcx
// .late_bound_vars_map(hir.owner)
// .get(&hir.local_id)
// .cloned()
// .map(|x| tcx.mk_bound_variable_kinds(&x))
// {
// Some(late_bound) => ty::Binder::bind_with_vars(args, late_bound),
// None => ty::Binder::dummy(args),
// };

Ty::new(tcx, FnDef(def_id, args))
}

#[inline]
pub fn new_fn_def(
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -1113,7 +1142,7 @@ impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
def_id: DefId,
args: ty::Binder<'tcx, ty::GenericArgsRef<'tcx>>,
) -> Self {
Ty::new_fn_def(interner, def_id, args)
Ty::new_fn_def_raw(interner, def_id, args)
}

fn new_fn_ptr(interner: TyCtxt<'tcx>, sig: ty::Binder<'tcx, ty::FnSig<'tcx>>) -> Self {
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_mir_build/src/builder/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let success = this.cfg.start_new_block();
let clone_trait = this.tcx.require_lang_item(LangItem::Clone, span);
let clone_fn = this.tcx.associated_item_def_ids(clone_trait)[0];
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
let func = Operand::function_handle(
this.tcx,
clone_fn,
ty::Binder::dummy([ty.into()]),
expr_span,
);
let func =
Operand::function_handle(this.tcx, clone_fn, &[ty.into()], expr_span);
let ref_ty = Ty::new_imm_ref(this.tcx, this.tcx.lifetimes.re_erased, ty);
let ref_place = this.temp(ref_ty, span);
this.cfg.push_assign(
Expand Down
13 changes: 4 additions & 9 deletions compiler/rustc_mir_build/src/builder/matches/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let source_info = self.source_info(span);
let re_erased = self.tcx.lifetimes.re_erased;
let trait_item = self.tcx.require_lang_item(trait_item, span);
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
let method = trait_method(self.tcx, trait_item, method, ty::Binder::dummy([ty]));
let method = trait_method(self.tcx, trait_item, method, &[ty.into()]);
let ref_src = self.temp(Ty::new_ref(self.tcx, re_erased, ty, mutability), span);
// `let ref_src = &src_place;`
// or `let ref_src = &mut src_place;`
Expand Down Expand Up @@ -422,9 +421,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
) {
let str_ty = self.tcx.types.str_;
let eq_def_id = self.tcx.require_lang_item(LangItem::PartialEq, source_info.span);
let method =
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
trait_method(self.tcx, eq_def_id, sym::eq, ty::Binder::dummy([str_ty, str_ty]));
let method = trait_method(self.tcx, eq_def_id, sym::eq, &[str_ty.into(), str_ty.into()]);

let bool_ty = self.tcx.types.bool;
let eq_result = self.temp(bool_ty, source_info.span);
Expand Down Expand Up @@ -471,7 +468,7 @@ fn trait_method<'tcx>(
tcx: TyCtxt<'tcx>,
trait_def_id: DefId,
method_name: Symbol,
args: ty::Binder<'tcx, impl IntoIterator<Item: Into<GenericArg<'tcx>>>>,
args: &[GenericArg<'tcx>],
) -> Const<'tcx> {
// The unhygienic comparison here is acceptable because this is only
// used on known traits.
Expand All @@ -481,7 +478,5 @@ fn trait_method<'tcx>(
.find(|item| item.is_fn())
.expect("trait method not found");

let method_ty = Ty::new_fn_def(tcx, item.def_id, args);

Const::zero_sized(method_ty)
Const::zero_sized(tcx.type_of(item.def_id).instantiate(tcx, args).skip_norm_wip())
}
42 changes: 18 additions & 24 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,11 @@ impl<'tcx> ThirBuildCx<'tcx> {
// We don't need to do call adjust_span here since
// deref coercions always start with a built-in deref.
let call_def_id = deref.method_call(self.tcx);
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
let overloaded_callee = Ty::new_fn_def(
self.tcx,
call_def_id,
ty::Binder::dummy(self.tcx.mk_args(&[expr.ty.into()])),
);
let overloaded_callee = self
.tcx
.type_of(call_def_id)
.instantiate(self.tcx, self.tcx.mk_args(&[expr.ty.into()]))
.skip_norm_wip();

expr = Expr {
temp_scope_id,
Expand Down Expand Up @@ -855,12 +854,10 @@ impl<'tcx> ThirBuildCx<'tcx> {
};
let mk_call =
|thir: &mut Thir<'tcx>, ty: Ty<'tcx>, variant: VariantIdx, field: FieldIdx| {
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
let fun_ty = Ty::new_fn_def(
tcx,
offset_of_intrinsic,
ty::Binder::dummy([ty::GenericArg::from(ty)]),
);
let fun_ty = tcx
.type_of(offset_of_intrinsic)
.instantiate(tcx, &[ty.into()])
.skip_norm_wip();
let fun = thir
.exprs
.push(mk_expr(ExprKind::ZstLiteral { user_ty: None }, fun_ty));
Expand Down Expand Up @@ -1211,12 +1208,10 @@ impl<'tcx> ThirBuildCx<'tcx> {
let user_ty = self.user_args_applied_to_res(expr.hir_id, Res::Def(kind, def_id));
debug!("method_callee: user_ty={:?}", user_ty);
(
// FIXME: instantiate binder correctly (turbofish/fndex)
Ty::new_fn_def(
self.tcx,
def_id,
ty::Binder::dummy(self.typeck_results.node_args(expr.hir_id)),
),
self.tcx
.type_of(def_id)
.instantiate(self.tcx, self.typeck_results.node_args(expr.hir_id))
.skip_norm_wip(),
user_ty,
)
}
Expand Down Expand Up @@ -1250,12 +1245,11 @@ impl<'tcx> ThirBuildCx<'tcx> {

Expr {
temp_scope_id: expr.hir_id.local_id,
// FIXME(156581): actually instantiate the binder correctly (turbofishing/fndef changes)
ty: Ty::new_fn_def(
self.tcx,
def_id,
ty::Binder::dummy(self.typeck_results.node_args(expr.hir_id)),
),
ty: self
.tcx
.type_of(def_id)
.instantiate(self.tcx, self.typeck_results.node_args(expr.hir_id))
.skip_norm_wip(),
span,
kind: ExprKind::ZstLiteral { user_ty },
}
Expand Down
Loading
Loading