Remove some glob imports from the type system

This commit is contained in:
Michael Goulet 2025-06-17 17:55:06 +00:00
parent 2801f9aaf9
commit 44254c8cd7
32 changed files with 243 additions and 185 deletions

View file

@ -909,11 +909,11 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
}
}
(GenericArgKind::Const(inner_ct), TermKind::Const(target_ct)) => {
use ty::InferConst::*;
match (inner_ct.kind(), target_ct.kind()) {
(ty::ConstKind::Infer(Var(a_vid)), ty::ConstKind::Infer(Var(b_vid))) => {
self.tecx.root_const_var(a_vid) == self.tecx.root_const_var(b_vid)
}
(
ty::ConstKind::Infer(ty::InferConst::Var(a_vid)),
ty::ConstKind::Infer(ty::InferConst::Var(b_vid)),
) => self.tecx.root_const_var(a_vid) == self.tecx.root_const_var(b_vid),
_ => false,
}
}

View file

@ -5,7 +5,6 @@ use rustc_span::Span;
use crate::error_reporting::TypeErrCtxt;
use crate::infer::RegionResolutionError;
use crate::infer::RegionResolutionError::*;
mod different_lifetimes;
pub mod find_anon_type;
@ -83,8 +82,10 @@ impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
pub(super) fn regions(&self) -> Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)> {
match (&self.error, self.regions) {
(Some(ConcreteFailure(origin, sub, sup)), None) => Some((origin.span(), *sub, *sup)),
(Some(SubSupConflict(_, _, origin, sub, _, sup, _)), None) => {
(Some(RegionResolutionError::ConcreteFailure(origin, sub, sup)), None) => {
Some((origin.span(), *sub, *sup))
}
(Some(RegionResolutionError::SubSupConflict(_, _, origin, sub, _, sup, _)), None) => {
Some((origin.span(), *sub, *sup))
}
(None, Some((span, sub, sup))) => Some((span, sub, sup)),

View file

@ -5,6 +5,7 @@ use rustc_hir::def::{Namespace, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{Visitor, walk_ty};
use rustc_hir::{self as hir, AmbigArg};
use rustc_infer::infer::SubregionOrigin;
use rustc_middle::hir::nested_filter;
use rustc_middle::traits::ObligationCauseCode;
use rustc_middle::ty::error::ExpectedFound;
@ -16,7 +17,7 @@ use tracing::debug;
use crate::error_reporting::infer::nice_region_error::NiceRegionError;
use crate::error_reporting::infer::nice_region_error::placeholder_error::Highlighted;
use crate::errors::{ConsiderBorrowingParamHelp, RelationshipHelp, TraitImplDiff};
use crate::infer::{RegionResolutionError, Subtype, ValuePairs};
use crate::infer::{RegionResolutionError, ValuePairs};
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
/// Print the error message for lifetime errors when the `impl` doesn't conform to the `trait`.
@ -32,7 +33,8 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
_sup,
_,
) = error.clone()
&& let (Subtype(sup_trace), Subtype(sub_trace)) = (&sup_origin, &sub_origin)
&& let (SubregionOrigin::Subtype(sup_trace), SubregionOrigin::Subtype(sub_trace)) =
(&sup_origin, &sub_origin)
&& let &ObligationCauseCode::CompareImplItem { trait_item_def_id, .. } =
sub_trace.cause.code()
&& sub_trace.values == sup_trace.values

View file

@ -27,7 +27,10 @@ use crate::errors::{
};
use crate::fluent_generated as fluent;
use crate::infer::region_constraints::GenericKind;
use crate::infer::{self, InferCtxt, RegionResolutionError, RegionVariableOrigin, SubregionOrigin};
use crate::infer::{
BoundRegionConversionTime, InferCtxt, RegionResolutionError, RegionVariableOrigin,
SubregionOrigin,
};
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
pub fn report_region_errors(
@ -219,21 +222,21 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
pub(super) fn note_region_origin(&self, err: &mut Diag<'_>, origin: &SubregionOrigin<'tcx>) {
match *origin {
infer::Subtype(ref trace) => RegionOriginNote::WithRequirement {
SubregionOrigin::Subtype(ref trace) => RegionOriginNote::WithRequirement {
span: trace.cause.span,
requirement: ObligationCauseAsDiagArg(trace.cause.clone()),
expected_found: self.values_str(trace.values, &trace.cause, err.long_ty_path()),
}
.add_to_diag(err),
infer::Reborrow(span) => {
SubregionOrigin::Reborrow(span) => {
RegionOriginNote::Plain { span, msg: fluent::trait_selection_reborrow }
.add_to_diag(err)
}
infer::RelateObjectBound(span) => {
SubregionOrigin::RelateObjectBound(span) => {
RegionOriginNote::Plain { span, msg: fluent::trait_selection_relate_object_bound }
.add_to_diag(err);
}
infer::ReferenceOutlivesReferent(ty, span) => {
SubregionOrigin::ReferenceOutlivesReferent(ty, span) => {
RegionOriginNote::WithName {
span,
msg: fluent::trait_selection_reference_outlives_referent,
@ -242,7 +245,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
.add_to_diag(err);
}
infer::RelateParamBound(span, ty, opt_span) => {
SubregionOrigin::RelateParamBound(span, ty, opt_span) => {
RegionOriginNote::WithName {
span,
msg: fluent::trait_selection_relate_param_bound,
@ -258,24 +261,24 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
.add_to_diag(err);
}
}
infer::RelateRegionParamBound(span, _) => {
SubregionOrigin::RelateRegionParamBound(span, _) => {
RegionOriginNote::Plain {
span,
msg: fluent::trait_selection_relate_region_param_bound,
}
.add_to_diag(err);
}
infer::CompareImplItemObligation { span, .. } => {
SubregionOrigin::CompareImplItemObligation { span, .. } => {
RegionOriginNote::Plain {
span,
msg: fluent::trait_selection_compare_impl_item_obligation,
}
.add_to_diag(err);
}
infer::CheckAssociatedTypeBounds { ref parent, .. } => {
SubregionOrigin::CheckAssociatedTypeBounds { ref parent, .. } => {
self.note_region_origin(err, parent);
}
infer::AscribeUserTypeProvePredicate(span) => {
SubregionOrigin::AscribeUserTypeProvePredicate(span) => {
RegionOriginNote::Plain {
span,
msg: fluent::trait_selection_ascribe_user_type_prove_predicate,
@ -293,7 +296,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
sup: Region<'tcx>,
) -> Diag<'a> {
let mut err = match origin {
infer::Subtype(box trace) => {
SubregionOrigin::Subtype(box trace) => {
let terr = TypeError::RegionsDoesNotOutlive(sup, sub);
let mut err = self.report_and_explain_type_error(
trace,
@ -347,7 +350,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
err
}
infer::Reborrow(span) => {
SubregionOrigin::Reborrow(span) => {
let reference_valid = note_and_explain::RegionExplanation::new(
self.tcx,
generic_param_scope,
@ -369,7 +372,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
notes: reference_valid.into_iter().chain(content_valid).collect(),
})
}
infer::RelateObjectBound(span) => {
SubregionOrigin::RelateObjectBound(span) => {
let object_valid = note_and_explain::RegionExplanation::new(
self.tcx,
generic_param_scope,
@ -391,7 +394,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
notes: object_valid.into_iter().chain(pointer_valid).collect(),
})
}
infer::RelateParamBound(span, ty, opt_span) => {
SubregionOrigin::RelateParamBound(span, ty, opt_span) => {
let prefix = match sub.kind() {
ty::ReStatic => note_and_explain::PrefixKind::TypeSatisfy,
_ => note_and_explain::PrefixKind::TypeOutlive,
@ -415,7 +418,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
note,
})
}
infer::RelateRegionParamBound(span, ty) => {
SubregionOrigin::RelateRegionParamBound(span, ty) => {
let param_instantiated = note_and_explain::RegionExplanation::new(
self.tcx,
generic_param_scope,
@ -457,7 +460,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
notes: param_instantiated.into_iter().chain(param_must_outlive).collect(),
})
}
infer::ReferenceOutlivesReferent(ty, span) => {
SubregionOrigin::ReferenceOutlivesReferent(ty, span) => {
let pointer_valid = note_and_explain::RegionExplanation::new(
self.tcx,
generic_param_scope,
@ -480,7 +483,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
notes: pointer_valid.into_iter().chain(data_valid).collect(),
})
}
infer::CompareImplItemObligation { span, impl_item_def_id, trait_item_def_id } => {
SubregionOrigin::CompareImplItemObligation {
span,
impl_item_def_id,
trait_item_def_id,
} => {
let mut err = self.report_extra_impl_obligation(
span,
impl_item_def_id,
@ -499,7 +506,11 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
err
}
infer::CheckAssociatedTypeBounds { impl_item_def_id, trait_item_def_id, parent } => {
SubregionOrigin::CheckAssociatedTypeBounds {
impl_item_def_id,
trait_item_def_id,
parent,
} => {
let mut err = self.report_concrete_failure(generic_param_scope, *parent, sub, sup);
// Don't mention the item name if it's an RPITIT, since that'll just confuse
@ -520,7 +531,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
);
err
}
infer::AscribeUserTypeProvePredicate(span) => {
SubregionOrigin::AscribeUserTypeProvePredicate(span) => {
let instantiated = note_and_explain::RegionExplanation::new(
self.tcx,
generic_param_scope,
@ -618,7 +629,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// I can't think how to do better than this right now. -nikomatsakis
debug!(?placeholder_origin, ?sub, ?sup, "report_placeholder_failure");
match placeholder_origin {
infer::Subtype(box ref trace)
SubregionOrigin::Subtype(box ref trace)
if matches!(
&trace.cause.code().peel_derives(),
ObligationCauseCode::WhereClause(..)
@ -648,7 +659,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
)
}
}
infer::Subtype(box trace) => {
SubregionOrigin::Subtype(box trace) => {
let terr = TypeError::RegionsPlaceholderMismatch;
return self.report_and_explain_type_error(
trace,
@ -945,8 +956,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
debug!("report_sub_sup_conflict: sup_region={:?}", sup_region);
debug!("report_sub_sup_conflict: sup_origin={:?}", sup_origin);
if let infer::Subtype(ref sup_trace) = sup_origin
&& let infer::Subtype(ref sub_trace) = sub_origin
if let SubregionOrigin::Subtype(ref sup_trace) = sup_origin
&& let SubregionOrigin::Subtype(ref sub_trace) = sub_origin
&& let Some((sup_expected, sup_found)) =
self.values_str(sup_trace.values, &sup_trace.cause, err.long_ty_path())
&& let Some((sub_expected, sub_found)) =
@ -1004,30 +1015,38 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
s
};
let var_description = match var_origin {
infer::MiscVariable(_) => String::new(),
infer::PatternRegion(_) => " for pattern".to_string(),
infer::BorrowRegion(_) => " for borrow expression".to_string(),
infer::Autoref(_) => " for autoref".to_string(),
infer::Coercion(_) => " for automatic coercion".to_string(),
infer::BoundRegion(_, br, infer::FnCall) => {
RegionVariableOrigin::MiscVariable(_) => String::new(),
RegionVariableOrigin::PatternRegion(_) => " for pattern".to_string(),
RegionVariableOrigin::BorrowRegion(_) => " for borrow expression".to_string(),
RegionVariableOrigin::Autoref(_) => " for autoref".to_string(),
RegionVariableOrigin::Coercion(_) => " for automatic coercion".to_string(),
RegionVariableOrigin::BoundRegion(_, br, BoundRegionConversionTime::FnCall) => {
format!(" for lifetime parameter {}in function call", br_string(br))
}
infer::BoundRegion(_, br, infer::HigherRankedType) => {
RegionVariableOrigin::BoundRegion(
_,
br,
BoundRegionConversionTime::HigherRankedType,
) => {
format!(" for lifetime parameter {}in generic type", br_string(br))
}
infer::BoundRegion(_, br, infer::AssocTypeProjection(def_id)) => format!(
RegionVariableOrigin::BoundRegion(
_,
br,
BoundRegionConversionTime::AssocTypeProjection(def_id),
) => format!(
" for lifetime parameter {}in trait containing associated type `{}`",
br_string(br),
self.tcx.associated_item(def_id).name()
),
infer::RegionParameterDefinition(_, name) => {
RegionVariableOrigin::RegionParameterDefinition(_, name) => {
format!(" for lifetime parameter `{name}`")
}
infer::UpvarRegion(ref upvar_id, _) => {
RegionVariableOrigin::UpvarRegion(ref upvar_id, _) => {
let var_name = self.tcx.hir_name(upvar_id.var_path.hir_id);
format!(" for capture of `{var_name}` by closure")
}
infer::Nll(..) => bug!("NLL variable found in lexical phase"),
RegionVariableOrigin::Nll(..) => bug!("NLL variable found in lexical phase"),
};
struct_span_code_err!(

View file

@ -47,8 +47,8 @@ use crate::infer::{self, InferCtxt, InferCtxtExt as _};
use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
use crate::traits::{
MismatchedProjectionTypes, NormalizeExt, Obligation, ObligationCause, ObligationCauseCode,
ObligationCtxt, Overflow, PredicateObligation, SelectionContext, SelectionError,
SignatureMismatch, TraitDynIncompatible, elaborate, specialization_graph,
ObligationCtxt, PredicateObligation, SelectionContext, SelectionError, elaborate,
specialization_graph,
};
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
@ -659,7 +659,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
}
SignatureMismatch(box SignatureMismatchData {
SelectionError::SignatureMismatch(box SignatureMismatchData {
found_trait_ref,
expected_trait_ref,
terr: terr @ TypeError::CyclicTy(_),
@ -669,7 +669,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
expected_trait_ref,
terr,
),
SignatureMismatch(box SignatureMismatchData {
SelectionError::SignatureMismatch(box SignatureMismatchData {
found_trait_ref,
expected_trait_ref,
terr: _,
@ -690,7 +690,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
def_id,
),
TraitDynIncompatible(did) => {
SelectionError::TraitDynIncompatible(did) => {
let violations = self.tcx.dyn_compatibility_violations(did);
report_dyn_incompatibility(self.tcx, span, None, did, violations)
}
@ -710,12 +710,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
// Already reported in the query.
SelectionError::NotConstEvaluatable(NotConstEvaluatable::Error(guar)) |
// Already reported.
Overflow(OverflowError::Error(guar)) => {
SelectionError::Overflow(OverflowError::Error(guar)) => {
self.set_tainted_by_errors(guar);
return guar
},
Overflow(_) => {
SelectionError::Overflow(_) => {
bug!("overflow should be handled before the `report_selection_error` path");
}