remove TypingMode::from_param_env in clippy

This commit is contained in:
lcnr 2024-11-19 19:31:02 +01:00
parent 4813fda2e6
commit d667dd5677
7 changed files with 77 additions and 68 deletions

View File

@ -15,6 +15,7 @@ use rustc_hir::{
self as hir, BindingMode, Body, BodyId, BorrowKind, Expr, ExprKind, HirId, MatchSource, Mutability, Node, Pat,
PatKind, Path, QPath, TyKind, UnOp,
};
use rustc_hir::def_id::DefId;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypeckResults};
@ -753,11 +754,10 @@ impl TyCoercionStability {
fn for_defined_ty<'tcx>(cx: &LateContext<'tcx>, ty: DefinedTy<'tcx>, for_return: bool) -> Self {
match ty {
DefinedTy::Hir(ty) => Self::for_hir_ty(ty),
DefinedTy::Mir(ty) => Self::for_mir_ty(
DefinedTy::Mir { def_site_def_id, ty } => Self::for_mir_ty(
cx.tcx,
// FIXME(#132279): convert `DefinedTy` to use `TypingEnv` instead.
ty::TypingEnv::from_param_env(ty.param_env),
cx.tcx.instantiate_bound_regions_with_erased(ty.value),
def_site_def_id,
cx.tcx.instantiate_bound_regions_with_erased(ty),
for_return,
),
}
@ -824,12 +824,15 @@ impl TyCoercionStability {
}
}
fn for_mir_ty<'tcx>(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, ty: Ty<'tcx>, for_return: bool) -> Self {
fn for_mir_ty<'tcx>(tcx: TyCtxt<'tcx>, def_site_def_id: Option<DefId>, ty: Ty<'tcx>, for_return: bool) -> Self {
let ty::Ref(_, mut ty, _) = *ty.kind() else {
return Self::None;
};
ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty);
if let Some(def_id) = def_site_def_id {
let typing_env = ty::TypingEnv::non_body_analysis(tcx, def_id);
ty = tcx.try_normalize_erasing_regions(typing_env, ty).unwrap_or(ty);
}
loop {
break match *ty.kind() {
ty::Ref(_, ref_ty, _) => {

View File

@ -454,13 +454,13 @@ fn check_partial_eq_without_eq<'tcx>(cx: &LateContext<'tcx>, span: Span, trait_r
&& cx.tcx.is_diagnostic_item(sym::PartialEq, def_id)
&& !has_non_exhaustive_attr(cx.tcx, *adt)
&& !ty_implements_eq_trait(cx.tcx, ty, eq_trait_def_id)
&& let param_env = param_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
&& let typing_env = typing_env_env_for_derived_eq(cx.tcx, adt.did(), eq_trait_def_id)
&& let Some(local_def_id) = adt.did().as_local()
// If all of our fields implement `Eq`, we can implement `Eq` too
&& adt
.all_fields()
.map(|f| f.ty(cx.tcx, args))
.all(|ty| implements_trait_with_env(cx.tcx, param_env, ty, eq_trait_def_id, None, &[]))
.all(|ty| implements_trait_with_env(cx.tcx, typing_env, ty, eq_trait_def_id, None, &[]))
{
span_lint_hir_and_then(
cx,
@ -485,7 +485,7 @@ fn ty_implements_eq_trait<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, eq_trait_id: De
}
/// Creates the `ParamEnv` used for the give type's derived `Eq` impl.
fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> ParamEnv<'_> {
fn typing_env_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) -> ty::TypingEnv<'_> {
// Initial map from generic index to param def.
// Vec<(param_def, needs_eq)>
let mut params = tcx
@ -506,7 +506,7 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) ->
}
}
ParamEnv::new(
let param_env = ParamEnv::new(
tcx.mk_clauses_from_iter(ty_predicates.iter().map(|&(p, _)| p).chain(
params.iter().filter(|&&(_, needs_eq)| needs_eq).map(|&(param, _)| {
ClauseKind::Trait(TraitPredicate {
@ -517,5 +517,9 @@ fn param_env_for_derived_eq(tcx: TyCtxt<'_>, did: DefId, eq_trait_id: DefId) ->
}),
)),
Reveal::UserFacing,
)
);
ty::TypingEnv {
typing_mode: ty::TypingMode::non_body_analysis(),
param_env,
}
}

View File

@ -115,11 +115,11 @@ fn is_ref_iterable<'tcx>(
.tcx
.liberate_late_bound_regions(fn_id, cx.tcx.fn_sig(fn_id).skip_binder())
&& let &[req_self_ty, req_res_ty] = &**sig.inputs_and_output
&& let param_env = cx.tcx.param_env(fn_id)
&& implements_trait_with_env(cx.tcx, param_env, req_self_ty, trait_id, Some(fn_id), &[])
&& let typing_env = ty::TypingEnv::non_body_analysis(cx.tcx, fn_id)
&& implements_trait_with_env(cx.tcx, typing_env, req_self_ty, trait_id, Some(fn_id), &[])
&& let Some(into_iter_ty) =
make_normalized_projection_with_regions(cx.tcx, param_env, trait_id, sym!(IntoIter), [req_self_ty])
&& let req_res_ty = normalize_with_regions(cx.tcx, param_env, req_res_ty)
make_normalized_projection_with_regions(cx.tcx, typing_env, trait_id, sym!(IntoIter), [req_self_ty])
&& let req_res_ty = normalize_with_regions(cx.tcx, typing_env, req_res_ty)
&& into_iter_ty == req_res_ty
{
let adjustments = typeck.expr_adjustments(self_arg);

View File

@ -85,8 +85,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowsForGenericArgs<'tcx> {
&& use_cx.same_ctxt
&& !use_cx.is_ty_unified
&& let use_node = use_cx.use_node(cx)
&& let Some(DefinedTy::Mir(ty)) = use_node.defined_ty(cx)
&& let ty::Param(ty) = *ty.value.skip_binder().kind()
&& let Some(DefinedTy::Mir { def_site_def_id: _, ty }) = use_node.defined_ty(cx)
&& let ty::Param(param_ty) = *ty.skip_binder().kind()
&& let Some((hir_id, fn_id, i)) = match use_node {
ExprUseNode::MethodArg(_, _, 0) => None,
ExprUseNode::MethodArg(hir_id, None, i) => cx
@ -112,7 +112,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowsForGenericArgs<'tcx> {
fn_id,
cx.typeck_results().node_args(hir_id),
i,
ty,
param_ty,
expr,
&self.msrv,
)

View File

@ -182,9 +182,14 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
&& !is_copy(cx, ty)
&& ty.is_sized(cx.tcx, cx.typing_env())
&& !allowed_traits.iter().any(|&t| {
implements_trait_with_env_from_iter(cx.tcx, cx.param_env, ty, t, None, [Option::<
ty::GenericArg<'tcx>,
>::None])
implements_trait_with_env_from_iter(
cx.tcx,
cx.typing_env(),
ty,
t,
None,
[None::<ty::GenericArg<'tcx>>]
)
})
&& !implements_borrow_trait
&& !all_borrowable_trait

View File

@ -116,8 +116,8 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::layout::IntegerExt;
use rustc_middle::ty::{
self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, FloatTy, GenericArgsRef, IntTy, ParamEnv,
ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt, UintTy, UpvarCapture,
self as rustc_ty, Binder, BorrowKind, ClosureKind, EarlyBinder, FloatTy, GenericArgsRef, IntTy,
Ty, TyCtxt, TypeVisitableExt, UintTy, UpvarCapture,
};
use rustc_span::hygiene::{ExpnKind, MacroKind};
use rustc_span::source_map::SourceMap;
@ -2712,9 +2712,17 @@ pub fn walk_to_expr_usage<'tcx, T>(
pub enum DefinedTy<'tcx> {
// Used for locals and closures defined within the function.
Hir(&'tcx hir::Ty<'tcx>),
/// Used for function signatures, and constant and static values. This includes the `ParamEnv`
/// from the definition site.
Mir(ParamEnvAnd<'tcx, Binder<'tcx, Ty<'tcx>>>),
/// Used for function signatures, and constant and static values. The type is
/// in the context of its definition site. We also track the `def_id` of its
/// definition site.
///
/// WARNING: As the `ty` in in the scope of the definition, not of the function
/// using it, you must be very careful with how you use it. Using it in the wrong
/// scope easily results in ICEs.
Mir {
def_site_def_id: Option<DefId>,
ty: Binder<'tcx, Ty<'tcx>>,
},
}
/// The context an expressions value is used in.
@ -2833,10 +2841,10 @@ impl<'tcx> ExprUseNode<'tcx> {
pub fn defined_ty(&self, cx: &LateContext<'tcx>) -> Option<DefinedTy<'tcx>> {
match *self {
Self::LetStmt(LetStmt { ty: Some(ty), .. }) => Some(DefinedTy::Hir(ty)),
Self::ConstStatic(id) => Some(DefinedTy::Mir(
cx.param_env
.and(Binder::dummy(cx.tcx.type_of(id).instantiate_identity())),
)),
Self::ConstStatic(id) => Some(DefinedTy::Mir {
def_site_def_id: Some(id.def_id.to_def_id()),
ty: Binder::dummy(cx.tcx.type_of(id).instantiate_identity()),
}),
Self::Return(id) => {
if let Node::Expr(Expr {
kind: ExprKind::Closure(c),
@ -2848,9 +2856,8 @@ impl<'tcx> ExprUseNode<'tcx> {
FnRetTy::Return(ty) => Some(DefinedTy::Hir(ty)),
}
} else {
Some(DefinedTy::Mir(
cx.param_env.and(cx.tcx.fn_sig(id).instantiate_identity().output()),
))
let ty = cx.tcx.fn_sig(id).instantiate_identity().output();
Some(DefinedTy::Mir { def_site_def_id: Some(id.def_id.to_def_id()), ty })
}
},
Self::Field(field) => match get_parent_expr_for_hir(cx, field.hir_id) {
@ -2866,12 +2873,9 @@ impl<'tcx> ExprUseNode<'tcx> {
.find(|f| f.name == field.ident.name)
.map(|f| (adt, f))
})
.map(|(adt, field_def)| {
DefinedTy::Mir(
cx.tcx
.param_env(adt.did())
.and(Binder::dummy(cx.tcx.type_of(field_def.did).instantiate_identity())),
)
.map(|(adt, field_def)| DefinedTy::Mir {
def_site_def_id: Some(adt.did()),
ty: Binder::dummy(cx.tcx.type_of(field_def.did).instantiate_identity()),
}),
_ => None,
},
@ -2880,17 +2884,19 @@ impl<'tcx> ExprUseNode<'tcx> {
let (hir_ty, ty) = sig.input_with_hir(i)?;
Some(match hir_ty {
Some(hir_ty) => DefinedTy::Hir(hir_ty),
None => DefinedTy::Mir(
sig.predicates_id()
.map_or(ParamEnv::empty(), |id| cx.tcx.param_env(id))
.and(ty),
),
None => DefinedTy::Mir {
def_site_def_id: sig.predicates_id(),
ty,
}
})
},
Self::MethodArg(id, _, i) => {
let id = cx.typeck_results().type_dependent_def_id(id)?;
let sig = cx.tcx.fn_sig(id).skip_binder();
Some(DefinedTy::Mir(cx.tcx.param_env(id).and(sig.input(i))))
Some(DefinedTy::Mir {
def_site_def_id: Some(id),
ty: sig.input(i),
})
},
Self::LetStmt(_) | Self::FieldAccess(..) | Self::Callee | Self::Other | Self::AddrOf(..) => None,
}

View File

@ -19,7 +19,7 @@ use rustc_middle::ty::layout::ValidityRequirement;
use rustc_middle::ty::{
self, AdtDef, AliasTy, AssocItem, AssocKind, Binder, BoundRegion, FnSig, GenericArg, GenericArgKind,
GenericArgsRef, GenericParamDefKind, IntTy, ParamEnv, Region, RegionKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
TypeVisitable, TypeVisitableExt, TypeVisitor, TypingMode, UintTy, Upcast, VariantDef, VariantDiscr,
TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
};
use rustc_span::symbol::Ident;
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
@ -226,7 +226,7 @@ pub fn implements_trait<'tcx>(
trait_id: DefId,
args: &[GenericArg<'tcx>],
) -> bool {
implements_trait_with_env_from_iter(cx.tcx, cx.param_env, ty, trait_id, None, args.iter().map(|&x| Some(x)))
implements_trait_with_env_from_iter(cx.tcx, cx.typing_env(), ty, trait_id, None, args.iter().map(|&x| Some(x)))
}
/// Same as `implements_trait` but allows using a `ParamEnv` different from the lint context.
@ -235,19 +235,19 @@ pub fn implements_trait<'tcx>(
/// environment, used for checking const traits.
pub fn implements_trait_with_env<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
ty: Ty<'tcx>,
trait_id: DefId,
callee_id: Option<DefId>,
args: &[GenericArg<'tcx>],
) -> bool {
implements_trait_with_env_from_iter(tcx, param_env, ty, trait_id, callee_id, args.iter().map(|&x| Some(x)))
implements_trait_with_env_from_iter(tcx, typing_env, ty, trait_id, callee_id, args.iter().map(|&x| Some(x)))
}
/// Same as `implements_trait_from_env` but takes the arguments as an iterator.
pub fn implements_trait_with_env_from_iter<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
ty: Ty<'tcx>,
trait_id: DefId,
callee_id: Option<DefId>,
@ -268,7 +268,7 @@ pub fn implements_trait_with_env_from_iter<'tcx>(
return false;
}
let infcx = tcx.infer_ctxt().build(TypingMode::from_param_env(param_env));
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
let args = args
.into_iter()
.map(|arg| arg.into().unwrap_or_else(|| infcx.next_ty_var(DUMMY_SP).into()))
@ -1239,12 +1239,12 @@ impl<'tcx> InteriorMut<'tcx> {
pub fn make_normalized_projection_with_regions<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
container_id: DefId,
assoc_ty: Symbol,
args: impl IntoIterator<Item = impl Into<GenericArg<'tcx>>>,
) -> Option<Ty<'tcx>> {
fn helper<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: AliasTy<'tcx>) -> Option<Ty<'tcx>> {
fn helper<'tcx>(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, ty: AliasTy<'tcx>) -> Option<Ty<'tcx>> {
#[cfg(debug_assertions)]
if let Some((i, arg)) = ty
.args
@ -1261,10 +1261,8 @@ pub fn make_normalized_projection_with_regions<'tcx>(
return None;
}
let cause = ObligationCause::dummy();
match tcx
.infer_ctxt()
.build(TypingMode::from_param_env(param_env))
.at(&cause, param_env)
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
match infcx.at(&cause, param_env)
.query_normalize(Ty::new_projection_from_args(tcx, ty.def_id, ty.args))
{
Ok(ty) => Some(ty.value),
@ -1274,20 +1272,13 @@ pub fn make_normalized_projection_with_regions<'tcx>(
},
}
}
helper(tcx, param_env, make_projection(tcx, container_id, assoc_ty, args)?)
helper(tcx, typing_env, make_projection(tcx, container_id, assoc_ty, args)?)
}
pub fn normalize_with_regions<'tcx>(tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
pub fn normalize_with_regions<'tcx>(tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
let cause = ObligationCause::dummy();
match tcx
.infer_ctxt()
.build(TypingMode::from_param_env(param_env))
.at(&cause, param_env)
.query_normalize(ty)
{
Ok(ty) => ty.value,
Err(_) => ty,
}
let (infcx, param_env) = tcx.infer_ctxt().build_with_typing_env(typing_env);
infcx.at(&cause, param_env).query_normalize(ty).map_or(ty, |ty| ty.value)
}
/// Checks if the type is `core::mem::ManuallyDrop<_>`