Skip to content

Commit

Permalink
Auto merge of #124952 - compiler-errors:no-error, r=lcnr
Browse files Browse the repository at this point in the history
Rename some `FulfillmentErrorCode`/`ObligationCauseCode` variants to be less redundant

1. Rename some `FulfillmentErrorCode` variants.
2. Always use `ObligationCauseCode::` to prefix a code, rather than using a glob import and naming them through `traits::`.
3. Rename some `ObligationCauseCode` variants -- I wasn't particularly thorough with thinking of a new names for these, so could workshop them if necessary.
4. Misc stuff from renaming.

r? lcnr
  • Loading branch information
bors committed May 10, 2024
2 parents 6a19a87 + 6f77bfe commit 2cce088
Show file tree
Hide file tree
Showing 48 changed files with 432 additions and 393 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
Applicability::MaybeIncorrect,
);
for error in errors {
if let FulfillmentErrorCode::SelectionError(
if let FulfillmentErrorCode::Select(
SelectionError::Unimplemented,
) = error.code
&& let ty::PredicateKind::Clause(ty::ClauseKind::Trait(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}
// The type doesn't implement Clone because of unmet obligations.
for error in errors {
if let traits::FulfillmentErrorCode::SelectionError(
if let traits::FulfillmentErrorCode::Select(
traits::SelectionError::Unimplemented,
) = error.code
&& let ty::PredicateKind::Clause(ty::ClauseKind::Trait(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,15 +2059,15 @@ impl<'tcx> RegionInferenceContext<'tcx> {
// We currently do not store the `DefId` in the `ConstraintCategory`
// for performances reasons. The error reporting code used by NLL only
// uses the span, so this doesn't cause any problems at the moment.
Some(ObligationCauseCode::BindingObligation(
Some(ObligationCauseCode::SpannedWhereClause(
CRATE_DEF_ID.to_def_id(),
predicate_span,
))
} else {
None
}
})
.unwrap_or_else(|| ObligationCauseCode::MiscObligation);
.unwrap_or_else(|| ObligationCauseCode::Misc);

// Classify each of the constraints along the path.
let mut categorized_path: Vec<BlameConstraint<'tcx>> = path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
let cause = ObligationCause::new(
terminator.source_info.span,
self.body.source.def_id().expect_local(),
ObligationCauseCode::ItemObligation(callee),
ObligationCauseCode::WhereClause(callee),
);
let normalized_predicates = ocx.normalize(&cause, param_env, predicates);
ocx.register_obligations(traits::predicates_for_generics(
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn compare_method_predicate_entailment<'tcx>(
let cause = ObligationCause::new(
impl_m_span,
impl_m_def_id,
ObligationCauseCode::CompareImplItemObligation {
ObligationCauseCode::CompareImplItem {
impl_item_def_id: impl_m_def_id,
trait_item_def_id: trait_m.def_id,
kind: impl_m.kind,
Expand Down Expand Up @@ -236,7 +236,7 @@ fn compare_method_predicate_entailment<'tcx>(
let cause = ObligationCause::new(
span,
impl_m_def_id,
ObligationCauseCode::CompareImplItemObligation {
ObligationCauseCode::CompareImplItem {
impl_item_def_id: impl_m_def_id,
trait_item_def_id: trait_m.def_id,
kind: impl_m.kind,
Expand Down Expand Up @@ -464,7 +464,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
let cause = ObligationCause::new(
return_span,
impl_m_def_id,
ObligationCauseCode::CompareImplItemObligation {
ObligationCauseCode::CompareImplItem {
impl_item_def_id: impl_m_def_id,
trait_item_def_id: trait_m.def_id,
kind: impl_m.kind,
Expand Down Expand Up @@ -819,7 +819,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ImplTraitInTraitCollector<'_, 'tcx> {
ObligationCause::new(
self.span,
self.body_id,
ObligationCauseCode::BindingObligation(proj.def_id, pred_span),
ObligationCauseCode::SpannedWhereClause(proj.def_id, pred_span),
),
self.param_env,
pred,
Expand Down Expand Up @@ -1752,7 +1752,7 @@ fn compare_const_predicate_entailment<'tcx>(
let impl_ty = tcx.type_of(impl_ct_def_id).instantiate_identity();

let trait_ty = tcx.type_of(trait_ct.def_id).instantiate(tcx, trait_to_impl_args);
let code = ObligationCauseCode::CompareImplItemObligation {
let code = ObligationCauseCode::CompareImplItem {
impl_item_def_id: impl_ct_def_id,
trait_item_def_id: trait_ct.def_id,
kind: impl_ct.kind,
Expand Down Expand Up @@ -1924,7 +1924,7 @@ fn compare_type_predicate_entailment<'tcx>(
let cause = ObligationCause::new(
span,
impl_ty_def_id,
ObligationCauseCode::CompareImplItemObligation {
ObligationCauseCode::CompareImplItem {
impl_item_def_id: impl_ty.def_id.expect_local(),
trait_item_def_id: trait_ty.def_id,
kind: impl_ty.kind,
Expand Down Expand Up @@ -2012,9 +2012,9 @@ pub(super) fn check_type_bounds<'tcx>(
);
let mk_cause = |span: Span| {
let code = if span.is_dummy() {
traits::ItemObligation(trait_ty.def_id)
ObligationCauseCode::WhereClause(trait_ty.def_id)
} else {
traits::BindingObligation(trait_ty.def_id, span)
ObligationCauseCode::SpannedWhereClause(trait_ty.def_id, span)
};
ObligationCause::new(impl_ty_span, impl_ty_def_id, code)
};
Expand Down Expand Up @@ -2251,7 +2251,8 @@ fn try_report_async_mismatch<'tcx>(
};

for error in errors {
if let traits::BindingObligation(def_id, _) = *error.root_obligation.cause.code()
if let ObligationCauseCode::SpannedWhereClause(def_id, _) =
*error.root_obligation.cause.code()
&& def_id == async_future_def_id
&& let Some(proj) = error.root_obligation.predicate.to_opt_poly_projection_pred()
&& let Some(proj) = proj.no_bound_vars()
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_analysis/src/check/dropck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{codes::*, struct_span_code_err, ErrorGuaranteed};
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{RegionResolutionError, TyCtxtInferExt};
use rustc_infer::traits::ObligationCauseCode;
use rustc_middle::ty::util::CheckRegions;
use rustc_middle::ty::GenericArgsRef;
use rustc_middle::ty::{self, TyCtxt};
Expand Down Expand Up @@ -139,7 +140,7 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
for (pred, span) in tcx.predicates_of(drop_impl_def_id).instantiate_identity(tcx) {
let normalize_cause = traits::ObligationCause::misc(span, adt_def_id);
let pred = ocx.normalize(&normalize_cause, param_env, pred);
let cause = traits::ObligationCause::new(span, adt_def_id, traits::DropImpl);
let cause = traits::ObligationCause::new(span, adt_def_id, ObligationCauseCode::DropImpl);
ocx.register_obligation(traits::Obligation::new(tcx, cause, param_env, pred));
}

Expand Down
24 changes: 18 additions & 6 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ fn check_type_defn<'tcx>(
traits::ObligationCause::new(
hir_ty.span,
wfcx.body_def_id,
traits::FieldSized {
ObligationCauseCode::FieldSized {
adt_kind: match &item.kind {
ItemKind::Struct(..) => AdtKind::Struct,
ItemKind::Union(..) => AdtKind::Union,
Expand All @@ -1161,7 +1161,7 @@ fn check_type_defn<'tcx>(
let cause = traits::ObligationCause::new(
tcx.def_span(discr_def_id),
wfcx.body_def_id,
traits::MiscObligation,
ObligationCauseCode::Misc,
);
wfcx.register_obligation(traits::Obligation::new(
tcx,
Expand Down Expand Up @@ -1278,7 +1278,11 @@ fn check_item_type(
wfcx.register_wf_obligation(ty_span, Some(WellFormedLoc::Ty(item_id)), item_ty.into());
if forbid_unsized {
wfcx.register_bound(
traits::ObligationCause::new(ty_span, wfcx.body_def_id, traits::WellFormed(None)),
traits::ObligationCause::new(
ty_span,
wfcx.body_def_id,
ObligationCauseCode::WellFormed(None),
),
wfcx.param_env,
item_ty,
tcx.require_lang_item(LangItem::Sized, None),
Expand All @@ -1293,7 +1297,11 @@ fn check_item_type(

if should_check_for_sync {
wfcx.register_bound(
traits::ObligationCause::new(ty_span, wfcx.body_def_id, traits::SharedStatic),
traits::ObligationCause::new(
ty_span,
wfcx.body_def_id,
ObligationCauseCode::SharedStatic,
),
wfcx.param_env,
item_ty,
tcx.require_lang_item(LangItem::Sync, Some(ty_span)),
Expand Down Expand Up @@ -1542,7 +1550,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
let cause = traits::ObligationCause::new(
sp,
wfcx.body_def_id,
traits::ItemObligation(def_id.to_def_id()),
ObligationCauseCode::WhereClause(def_id.to_def_id()),
);
traits::Obligation::new(tcx, cause, wfcx.param_env, pred)
});
Expand Down Expand Up @@ -1982,7 +1990,11 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {

let obligation = traits::Obligation::new(
tcx,
traits::ObligationCause::new(span, self.body_def_id, traits::TrivialBound),
traits::ObligationCause::new(
span,
self.body_def_id,
ObligationCauseCode::TrivialBound,
),
empty_env,
pred,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn get_impl_args(
traits::ObligationCause::new(
impl1_span,
impl1_def_id,
traits::ObligationCauseCode::BindingObligation(impl2_node.def_id(), span),
traits::ObligationCauseCode::SpannedWhereClause(impl2_node.def_id(), span),
)
},
);
Expand Down
11 changes: 8 additions & 3 deletions compiler/rustc_hir_typeck/src/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_hir as hir;
use rustc_hir::def::{self, CtorKind, Namespace, Res};
use rustc_hir::def_id::DefId;
use rustc_hir_analysis::autoderef::Autoderef;
use rustc_infer::traits::ObligationCauseCode;
use rustc_infer::{
infer,
traits::{self, Obligation, ObligationCause},
Expand Down Expand Up @@ -117,7 +118,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
};

// we must check that return type of called functions is WF:
self.register_wf_obligation(output.into(), call_expr.span, traits::WellFormed(None));
self.register_wf_obligation(
output.into(),
call_expr.span,
ObligationCauseCode::WellFormed(None),
);

output
}
Expand Down Expand Up @@ -524,9 +529,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.register_bound(
ty,
self.tcx.require_lang_item(hir::LangItem::Tuple, Some(sp)),
traits::ObligationCause::new(sp, self.body_id, traits::RustCall),
traits::ObligationCause::new(sp, self.body_id, ObligationCauseCode::RustCall),
);
self.require_type_is_sized(ty, sp, traits::RustCall);
self.require_type_is_sized(ty, sp, ObligationCauseCode::RustCall);
} else {
self.dcx().emit_err(errors::RustCallIncorrectArgs { span: sp });
}
Expand Down
17 changes: 12 additions & 5 deletions compiler/rustc_hir_typeck/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use rustc_middle::ty::{self, Binder, Ty, TyCtxt};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::sym;
use rustc_target::spec::abi::Abi;
use rustc_trait_selection::traits;
use rustc_trait_selection::traits::{ObligationCause, ObligationCauseCode};

/// Helper used for fns and closures. Does the grungy work of checking a function
Expand Down Expand Up @@ -76,7 +75,7 @@ pub(super) fn check_fn<'a, 'tcx>(
fcx.register_wf_obligation(
param_ty.into(),
param.span,
traits::WellFormed(Some(WellFormedLoc::Param {
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Param {
function: fn_def_id,
param_idx: idx,
})),
Expand All @@ -101,7 +100,7 @@ pub(super) fn check_fn<'a, 'tcx>(
param.pat.span,
// ty.span == binding_span iff this is a closure parameter with no type ascription,
// or if it's an implicit `self` parameter
traits::SizedArgumentType(
ObligationCauseCode::SizedArgumentType(
if ty_span == Some(param.span) && tcx.is_closure_like(fn_def_id.into()) {
None
} else {
Expand All @@ -121,10 +120,18 @@ pub(super) fn check_fn<'a, 'tcx>(
hir::FnRetTy::Return(ty) => ty.span,
};

fcx.require_type_is_sized(declared_ret_ty, return_or_body_span, traits::SizedReturnType);
fcx.require_type_is_sized(
declared_ret_ty,
return_or_body_span,
ObligationCauseCode::SizedReturnType,
);
// We checked the root's signature during wfcheck, but not the child.
if fcx.tcx.is_typeck_child(fn_def_id.to_def_id()) {
fcx.require_type_is_sized(declared_ret_ty, return_or_body_span, traits::WellFormed(None));
fcx.require_type_is_sized(
declared_ret_ty,
return_or_body_span,
ObligationCauseCode::WellFormed(None),
);
}

fcx.is_whole_body.set(true);
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_hir::lang_items::LangItem;
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes};
use rustc_infer::infer::{InferOk, InferResult};
use rustc_infer::traits::ObligationCauseCode;
use rustc_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
use rustc_middle::ty::GenericArgs;
Expand Down Expand Up @@ -119,7 +120,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)
| hir::CoroutineKind::Coroutine(_) => {
let yield_ty = self.next_ty_var(expr_span);
self.require_type_is_sized(yield_ty, expr_span, traits::SizedYieldType);
self.require_type_is_sized(
yield_ty,
expr_span,
ObligationCauseCode::SizedYieldType,
);
yield_ty
}
// HACK(-Ztrait-solver=next): In the *old* trait solver, we must eagerly
Expand All @@ -128,7 +133,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// not a problem.
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _) => {
let yield_ty = self.next_ty_var(expr_span);
self.require_type_is_sized(yield_ty, expr_span, traits::SizedYieldType);
self.require_type_is_sized(
yield_ty,
expr_span,
ObligationCauseCode::SizedYieldType,
);

Ty::new_adt(
tcx,
Expand Down
Loading

0 comments on commit 2cce088

Please sign in to comment.