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
12 changes: 4 additions & 8 deletions compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -964,8 +964,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
let ty = tcx.type_of(def_id).instantiate_identity();
let ty_span = tcx.ty_span(def_id);
let ty = wfcx.deeply_normalize(ty_span, Some(WellFormedLoc::Ty(def_id)), ty);
wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(def_id)), ty.into());
let ty = wfcx.deeply_normalize(ty_span, WellFormedLoc::Ty(def_id), ty);
wfcx.register_wf_obligation(ty_span, WellFormedLoc::Ty(def_id), ty.into());
wfcx.register_bound(
traits::ObligationCause::new(
ty_span,
Expand Down Expand Up @@ -993,12 +993,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
let span = tcx.def_span(def_id);
if tcx.type_alias_is_checked(def_id) {
res = res.and(enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty);
wfcx.register_wf_obligation(
span,
Some(WellFormedLoc::Ty(def_id)),
item_ty.into(),
);
let item_ty = wfcx.deeply_normalize(span, WellFormedLoc::Ty(def_id), ty);
wfcx.register_wf_obligation(span, WellFormedLoc::Ty(def_id), item_ty.into());
check_where_clauses(wfcx, def_id);
Ok(())
}));
Expand Down
73 changes: 40 additions & 33 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {

// Convenience function to normalize during wfcheck. This performs
// `ObligationCtxt::normalize`, but provides a nice `ObligationCauseCode`.
fn normalize<T>(
&self,
span: Span,
loc: Option<WellFormedLoc>,
value: Unnormalized<'tcx, T>,
) -> T
fn normalize<T>(&self, span: Span, loc: WellFormedLoc, value: Unnormalized<'tcx, T>) -> T
where
T: TypeFoldable<TyCtxt<'tcx>>,
{
Expand All @@ -96,7 +91,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
pub(super) fn deeply_normalize<T>(
&self,
span: Span,
loc: Option<WellFormedLoc>,
loc: WellFormedLoc,
value: Unnormalized<'tcx, T>,
) -> T
where
Expand All @@ -122,7 +117,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
pub(super) fn register_wf_obligation(
&self,
span: Span,
loc: Option<WellFormedLoc>,
loc: WellFormedLoc,
term: ty::Term<'tcx>,
) {
let cause = traits::ObligationCause::new(
Expand Down Expand Up @@ -802,7 +797,7 @@ fn check_param_wf(tcx: TyCtxt<'_>, param: &ty::GenericParamDef) -> Result<(), Er

if tcx.features().const_param_ty_unchecked() {
enter_wf_checking_ctxt(tcx, tcx.local_parent(def_id), |wfcx| {
wfcx.register_wf_obligation(span, None, ty.into());
wfcx.register_wf_obligation(span, WellFormedLoc::None, ty.into());
Ok(())
})
} else if tcx.features().adt_const_params() || tcx.features().min_adt_const_params() {
Expand Down Expand Up @@ -916,7 +911,7 @@ pub(crate) fn check_associated_item(
tcx: TyCtxt<'_>,
def_id: LocalDefId,
) -> Result<(), ErrorGuaranteed> {
let loc = Some(WellFormedLoc::Ty(def_id));
let loc = WellFormedLoc::Ty(def_id);
enter_wf_checking_ctxt(tcx, def_id, |wfcx| {
let item = tcx.associated_item(def_id);

Expand All @@ -936,7 +931,7 @@ pub(crate) fn check_associated_item(
match item.kind {
ty::AssocKind::Const { .. } => {
let ty = tcx.type_of(def_id).instantiate_identity();
let ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty);
let ty = wfcx.deeply_normalize(span, WellFormedLoc::Ty(def_id), ty);
wfcx.register_wf_obligation(span, loc, ty.into());

if item.defaultness(tcx).has_value() {
Expand Down Expand Up @@ -964,7 +959,7 @@ pub(crate) fn check_associated_item(
}
if item.defaultness(tcx).has_value() {
let ty = tcx.type_of(def_id).instantiate_identity();
let ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), ty);
let ty = wfcx.deeply_normalize(span, WellFormedLoc::Ty(def_id), ty);
wfcx.register_wf_obligation(span, loc, ty.into());
}
Ok(())
Expand Down Expand Up @@ -1013,10 +1008,10 @@ pub(crate) fn check_type_defn<'tcx>(
let span = tcx.ty_span(field_id);
let ty = wfcx.deeply_normalize(
span,
None,
WellFormedLoc::None,
tcx.type_of(field.did).instantiate_identity(),
);
wfcx.register_wf_obligation(span, Some(WellFormedLoc::Ty(field_id)), ty.into());
wfcx.register_wf_obligation(span, WellFormedLoc::Ty(field_id), ty.into());

if matches!(ty.kind(), ty::Adt(def, _) if def.repr().scalable())
&& !matches!(adt_def.repr().scalable, Some(ScalableElt::Container))
Expand Down Expand Up @@ -1051,7 +1046,11 @@ pub(crate) fn check_type_defn<'tcx>(
{
let last = idx == variant.fields.len() - 1;
let span = tcx.ty_span(field.did.expect_local());
let ty = wfcx.normalize(span, None, tcx.type_of(field.did).instantiate_identity());
let ty = wfcx.normalize(
span,
WellFormedLoc::None,
tcx.type_of(field.did).instantiate_identity(),
);
wfcx.register_bound(
traits::ObligationCause::new(
span,
Expand Down Expand Up @@ -1215,7 +1214,7 @@ pub(crate) fn check_static_item<'tcx>(
}

let span = tcx.ty_span(item_id);
let loc = Some(WellFormedLoc::Ty(item_id));
let loc = WellFormedLoc::Ty(item_id);
let item_ty = wfcx.deeply_normalize(span, loc, Unnormalized::new_wip(ty));

let is_foreign_item = tcx.is_foreign_item(item_id);
Expand All @@ -1231,7 +1230,7 @@ pub(crate) fn check_static_item<'tcx>(
};
let forbid_unsized = !(is_foreign_item && is_structurally_foreign_item());

wfcx.register_wf_obligation(span, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
wfcx.register_wf_obligation(span, WellFormedLoc::Ty(item_id), item_ty.into());
if forbid_unsized {
let span = tcx.def_span(item_id);
wfcx.register_bound(
Expand Down Expand Up @@ -1300,12 +1299,16 @@ pub(super) fn check_const_item<'tcx>(

if let Some(direct_rhs) = tcx.const_of_item(def_id) {
let raw_ct = direct_rhs.instantiate_identity();
let norm_ct = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(def_id)), raw_ct);
wfcx.register_wf_obligation(span, Some(WellFormedLoc::Ty(def_id)), norm_ct.into());
let norm_ct = wfcx.deeply_normalize(span, WellFormedLoc::Ty(def_id), raw_ct);
wfcx.register_wf_obligation(span, WellFormedLoc::Ty(def_id), norm_ct.into());

wfcx.register_obligation(Obligation::new(
tcx,
ObligationCause::new(span, def_id, ObligationCauseCode::WellFormed(None)),
ObligationCause::new(
span,
def_id,
ObligationCauseCode::WellFormed(WellFormedLoc::Ty(def_id)),
),
wfcx.param_env,
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(norm_ct, item_ty)),
));
Expand All @@ -1330,7 +1333,7 @@ fn check_impl<'tcx>(
let trait_span = of_trait.trait_ref.path.span;
let trait_ref = wfcx.deeply_normalize(
trait_span,
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
WellFormedLoc::Ty(item.hir_id().expect_owner().def_id),
trait_ref,
);
let trait_pred =
Expand Down Expand Up @@ -1367,15 +1370,17 @@ fn check_impl<'tcx>(
{
let bound = wfcx.normalize(
item.span,
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
WellFormedLoc::Ty(item.hir_id().expect_owner().def_id),
bound,
);
wfcx.register_obligation(Obligation::new(
tcx,
ObligationCause::new(
impl_.self_ty.span,
wfcx.body_def_id,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::WellFormed(WellFormedLoc::HirId(
of_trait.trait_ref.hir_ref_id,
)),
),
wfcx.param_env,
bound.to_host_effect_clause(tcx, ty::BoundConstness::Maybe),
Expand All @@ -1390,12 +1395,12 @@ fn check_impl<'tcx>(
let self_ty = tcx.type_of(item.owner_id).instantiate_identity().skip_norm_wip();
let self_ty = wfcx.deeply_normalize(
item.span,
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
WellFormedLoc::Ty(item.hir_id().expect_owner().def_id),
Unnormalized::new_wip(self_ty),
);
wfcx.register_wf_obligation(
impl_.self_ty.span,
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
WellFormedLoc::Ty(item.hir_id().expect_owner().def_id),
self_ty.into(),
);
}
Expand Down Expand Up @@ -1437,7 +1442,8 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id:
wfcx.register_wf_obligation(
tcx.def_span(param.def_id),
matches!(param.kind, GenericParamDefKind::Type { .. })
.then(|| WellFormedLoc::Ty(param.def_id.expect_local())),
.then(|| WellFormedLoc::Ty(param.def_id.expect_local()))
.unwrap_or(WellFormedLoc::None),
default.as_term().unwrap(),
);
} else {
Expand Down Expand Up @@ -1466,7 +1472,7 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id:
let cause = traits::ObligationCause::new(
tcx.def_span(param.def_id),
wfcx.body_def_id,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::WellFormed(WellFormedLoc::Ty(def_id)),
);
wfcx.register_obligation(Obligation::new(
tcx,
Expand Down Expand Up @@ -1560,7 +1566,7 @@ pub(super) fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, def_id:
// Note the subtle difference from how we handle `gen_clauses`
// below: there, we are not trying to prove those clauses
// to be *true* but merely *well-formed*.
let clause = wfcx.normalize(sp, None, clause);
let clause = wfcx.normalize(sp, WellFormedLoc::None, clause);
let cause = traits::ObligationCause::new(
sp,
wfcx.body_def_id,
Expand Down Expand Up @@ -1636,20 +1642,20 @@ fn check_fn_or_method<'tcx>(
tcx.mk_type_list_from_iter(sig.inputs_and_output.iter().enumerate().map(|(idx, ty)| {
wfcx.deeply_normalize(
arg_span(idx),
Some(WellFormedLoc::Param {
WellFormedLoc::Param {
function: def_id,
// Note that the `param_idx` of the output type is
// one greater than the index of the last input type.
param_idx: idx,
}),
},
Unnormalized::new_wip(ty),
)
}));

for (idx, ty) in sig.inputs_and_output.iter().enumerate() {
wfcx.register_wf_obligation(
arg_span(idx),
Some(WellFormedLoc::Param { function: def_id, param_idx: idx }),
WellFormedLoc::Param { function: def_id, param_idx: idx },
ty.into(),
);
}
Expand Down Expand Up @@ -1726,7 +1732,7 @@ fn check_method_receiver<'tcx>(
}

let span = fn_sig.decl.inputs[0].span;
let loc = Some(WellFormedLoc::Param { function: method.def_id.expect_local(), param_idx: 0 });
let loc = WellFormedLoc::Param { function: method.def_id.expect_local(), param_idx: 0 };

let sig = tcx.fn_sig(method.def_id).instantiate_identity().skip_norm_wip();
let sig = tcx.liberate_late_bound_regions(method.def_id, sig);
Expand Down Expand Up @@ -2319,7 +2325,8 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {

// Match the existing behavior.
if clause.is_global() && !clause.has_type_flags(TypeFlags::HAS_BINDER_VARS) {
let clause = self.normalize(span, None, Unnormalized::new_wip(clause));
let clause =
self.normalize(span, WellFormedLoc::None, Unnormalized::new_wip(clause));

// only use the span of the predicate clause (#90869)
let hir_node = tcx.hir_node_by_def_id(self.body_def_id);
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_hir_analysis/src/hir_wf_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>(
) -> Option<ObligationCause<'tcx>> {
let def_id = match loc {
WellFormedLoc::Ty(def_id) => def_id,
WellFormedLoc::Param { function, param_idx: _ } => function,
WellFormedLoc::Param { function, .. } => function,
WellFormedLoc::HirId(_) | WellFormedLoc::None => return None,
};
let hir_id = tcx.local_def_id_to_hir_id(def_id);

Expand Down Expand Up @@ -85,7 +86,7 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>(
let cause = traits::ObligationCause::new(
ty.span,
self.def_id,
traits::ObligationCauseCode::WellFormed(None),
traits::ObligationCauseCode::WellFormed(WellFormedLoc::HirId(ty.hir_id)),
);

ocx.register_obligation(traits::Obligation::new(
Expand Down Expand Up @@ -191,7 +192,7 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>(
}
ref node => bug!("Unexpected node {:?}", node),
},
WellFormedLoc::Param { function: _, param_idx } => {
WellFormedLoc::Param { param_idx, .. } => {
let fn_decl = tcx.hir_fn_decl_by_hir_id(hir_id).unwrap();
// Get return type
if param_idx as usize == fn_decl.inputs.len() {
Expand All @@ -204,6 +205,7 @@ pub(super) fn diagnostic_hir_wf_check<'tcx>(
vec![&fn_decl.inputs[param_idx as usize]]
}
}
WellFormedLoc::HirId(_) | WellFormedLoc::None => return None,
};
for ty in tys {
visitor.visit_ty_unambig(ty);
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, HirId};
use rustc_hir_analysis::autoderef::Autoderef;
use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes};
use rustc_infer::traits::{Obligation, ObligationCause, ObligationCauseCode};
use rustc_infer::traits::{Obligation, ObligationCause, ObligationCauseCode, WellFormedLoc};
use rustc_middle::ty::adjustment::{
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability,
};
Expand Down Expand Up @@ -177,7 +177,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.register_wf_obligation(
output.into(),
call_expr.span,
ObligationCauseCode::WellFormed(None),
ObligationCauseCode::WellFormed(WellFormedLoc::HirId(call_expr.hir_id)),
);

output
Expand Down Expand Up @@ -728,7 +728,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// now we have to unify it with signature input in order to resolve
// all inference variables. After that we update input signature for
// adjustments search for mapped arguments.
let cause = self.cause(call_expr.span, ObligationCauseCode::Misc);
let cause = self.cause(
call_expr.span,
ObligationCauseCode::WellFormed(WellFormedLoc::HirId(call_expr.hir_id)),
);
if self
.at(&cause, self.param_env)
.sup(DefineOpaqueTypes::Yes, formal_input_tys[0], adjusted_arg_type)
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ pub(super) fn check_fn<'a, 'tcx>(
fcx.register_wf_obligation(
param_ty.into(),
param.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param {
ObligationCauseCode::WellFormed(WellFormedLoc::Param {
function: fn_def_id,
param_idx: idx,
})),
}),
);
}

Expand Down
Loading
Loading